StoreProductServices.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\out;
  12. use app\dao\product\product\StoreProductDao;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\services\diy\DiyServices;
  16. use app\services\product\sku\StoreProductAttrServices;
  17. use app\services\product\sku\StoreProductAttrValueServices;
  18. use app\services\system\form\SystemFormServices;
  19. use think\exception\ValidateException;
  20. use app\services\product\product\StoreProductServices as StoreProductsServices;
  21. /**
  22. * Class StoreProductService
  23. * @package app\services\product\product
  24. * @mixin StoreProductDao
  25. */
  26. class StoreProductServices extends BaseServices
  27. {
  28. public function __construct(StoreProductDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 获取商品详情
  34. * @param Request $request
  35. * @param int $id
  36. * @param int $type
  37. * @return mixed
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function productDetail(Request $request, $spu)
  43. {
  44. $storeInfo = $this->dao->getOne(['spu' => $spu], 'id,store_name,is_show,cate_id,keyword,unit_name,store_info,image,video_link,system_form_id', ['descriptions']);
  45. if (!$storeInfo) {
  46. throw new ValidateException('商品不存在');
  47. } else {
  48. $storeInfo = $storeInfo->toArray();
  49. }
  50. $siteUrl = sys_config('site_url');
  51. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  52. /** @var StoreProductAttrServices $storeProductAttrServices */
  53. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  54. [$productAttr, $productValue] = $storeProductAttrServices->getProductAttrDetail($storeInfo['id'], 0, 0, 0, 0, $storeInfo);
  55. $storeInfo['productValue'] = $productValue;
  56. $storeInfo['small_image'] = get_thumb_water($storeInfo['image']);
  57. if (isset($storeInfo['system_form_id']) && $storeInfo['system_form_id']) {
  58. /** @var SystemFormServices $systemFormServices */
  59. $systemFormServices = app()->make(SystemFormServices::class);
  60. $formInfo = $systemFormServices->value(['id' => $storeInfo['system_form_id']], 'value');
  61. if ($formInfo) {
  62. $storeInfo['custom_form'] = is_string($formInfo) ? json_decode($formInfo, true) : $formInfo;
  63. }
  64. }
  65. //浏览记录
  66. // ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $id]]);
  67. return $storeInfo;
  68. }
  69. /**
  70. * @param Request $request
  71. * @param $spu
  72. * @param $is_show
  73. * @return bool
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function setShow(Request $request, $spu, $is_show)
  79. {
  80. $storeInfo = $this->dao->getOne(['spu' => $spu], 'id');
  81. if (!$storeInfo) {
  82. throw new ValidateException('商品不存在');
  83. } else {
  84. $storeInfo = $storeInfo->toArray();
  85. }
  86. /** @var StoreProductsServices $storeProductsService */
  87. $storeProductsService = app()->make(StoreProductsServices::class);
  88. $storeProductsService->setShow([$storeInfo['id']], $is_show);
  89. return true;
  90. }
  91. /**
  92. * 修改库存
  93. * @param $data
  94. * @param $spu
  95. * @return bool
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. public function setStock($data, $spu)
  101. {
  102. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  103. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  104. $storeInfo = $this->dao->getOne(['spu' => $spu], 'id');
  105. if (!$storeInfo)
  106. throw new ValidateException('商品不存在');
  107. foreach ($data as $item) {
  108. if (isset($item['unique']) && $item['stock']) {
  109. $attr = $storeProductAttrValueServices->getOne(['unique' => $item['unique'], 'type' => 0, 'product_id' => $storeInfo['id']]);
  110. if ($attr) {
  111. $attr->stock = $item['stock'];
  112. $attr->save();
  113. }
  114. }
  115. }
  116. $stock = $storeProductAttrValueServices->pidBuStock($storeInfo['id']);
  117. $storeInfo->stock = $stock ?? 0;
  118. $storeInfo->save();
  119. return true;
  120. }
  121. }