StoreAdvanceServices.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\advance;
  13. use app\dao\activity\advance\StoreAdvanceDao;
  14. use app\jobs\ProductLogJob;
  15. use app\Request;
  16. use app\services\BaseServices;
  17. use app\services\order\StoreOrderServices;
  18. use app\services\other\QrcodeServices;
  19. use app\services\product\product\StoreDescriptionServices;
  20. use app\services\product\product\StoreProductRelationServices;
  21. use app\services\product\product\StoreProductReplyServices;
  22. use app\services\product\product\StoreProductServices;
  23. use app\services\product\sku\StoreProductAttrResultServices;
  24. use app\services\product\sku\StoreProductAttrServices;
  25. use app\services\product\sku\StoreProductAttrValueServices;
  26. use crmeb\exceptions\AdminException;
  27. use crmeb\exceptions\ApiException;
  28. use crmeb\services\CacheService;
  29. /**
  30. * 商品预售
  31. * Class StoreAdvanceServices
  32. * @package app\services\activity
  33. * @method get(int $id, array $field) 获取一条数据
  34. * @method getAdvanceStatus(array $ids) 获取预售商品是否开启
  35. */
  36. class StoreAdvanceServices extends BaseServices
  37. {
  38. /**
  39. * StoreAdvanceServices constructor.
  40. * @param StoreAdvanceDao $dao
  41. */
  42. public function __construct(StoreAdvanceDao $dao)
  43. {
  44. $this->dao = $dao;
  45. }
  46. /**
  47. * 后台获取预售列表
  48. * @param $where
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function getList($where)
  55. {
  56. [$page, $limit] = $this->getPageValue();
  57. $where['is_del'] = 0;
  58. $list = $this->dao->getList($where, $page, $limit);
  59. $count = $this->dao->getCount($where);
  60. return compact('list', 'count');
  61. }
  62. /**
  63. * 保存预售数据
  64. * @param $id
  65. * @param $data
  66. */
  67. public function saveData($id, $data)
  68. {
  69. $description = $data['description'];
  70. $detail = $data['attrs'];
  71. $items = $data['items'];
  72. $data['start_time'] = strtotime($data['section_time'][0]);
  73. $data['stop_time'] = strtotime($data['section_time'][1]);
  74. $data['images'] = json_encode($data['images']);
  75. $data['price'] = min(array_column($detail, 'price'));
  76. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  77. $data['cost'] = min(array_column($detail, 'cost'));
  78. $data['quota'] = $data['quota_show'] = array_sum(array_column($detail, 'quota'));
  79. $data['stock'] = array_sum(array_column($detail, 'stock'));
  80. if ($data['type']) {
  81. $data['pay_start_time'] = strtotime($data['pay_time'][0]);
  82. $data['pay_stop_time'] = strtotime($data['pay_time'][1]);
  83. }
  84. unset($data['section_time'], $data['description'], $data['attrs'], $data['items']);
  85. /** @var StoreDescriptionServices $storeDescriptionServices */
  86. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  87. /** @var StoreProductAttrServices $storeProductAttrServices */
  88. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  89. /** @var StoreProductServices $storeProductServices */
  90. $storeProductServices = app()->make(StoreProductServices::class);
  91. if ($data['quota'] > $storeProductServices->value(['id' => $data['product_id']], 'stock')) {
  92. throw new AdminException(400090);
  93. }
  94. $this->transaction(function () use ($id, $data, $description, $detail, $items, $storeDescriptionServices, $storeProductAttrServices, $storeProductServices) {
  95. if ($id) {
  96. $res = $this->dao->update($id, $data);
  97. $storeDescriptionServices->saveDescription((int)$id, $description, 6);
  98. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$id, 6);
  99. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$id, 6);
  100. if (!$res) throw new AdminException(100007);
  101. } else {
  102. if (!$storeProductServices->getOne(['is_show' => 1, 'is_del' => 0, 'id' => $data['product_id']])) {
  103. throw new AdminException(400091);
  104. }
  105. $data['add_time'] = time();
  106. $res = $this->dao->save($data);
  107. $storeProductServices->update($data['product_id'], ['is_show' => 0]);
  108. $storeDescriptionServices->saveDescription((int)$res->id, $description, 6);
  109. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$res->id, 6);
  110. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$res->id, 6);
  111. if (!$res) throw new AdminException(100022);
  112. }
  113. });
  114. }
  115. /**
  116. * 获取预售详情
  117. * @param int $id
  118. * @return array|\think\Model|null
  119. */
  120. public function getInfo(int $id)
  121. {
  122. $info = $this->dao->get($id);
  123. if ($info) {
  124. if ($info['start_time'] && $info['stop_time']) {
  125. $start_time = date('Y-m-d H:i', (int)$info['start_time']);
  126. $stop_time = date('Y-m-d H:i', (int)$info['stop_time']);
  127. }
  128. if (isset($start_time) && isset($stop_time)) {
  129. $info['section_time'] = [$start_time, $stop_time];
  130. } else {
  131. $info['section_time'] = [];
  132. }
  133. if ($info['pay_start_time'] && $info['pay_stop_time']) {
  134. $start_time = date('Y-m-d H:i', (int)$info['pay_start_time']);
  135. $stop_time = date('Y-m-d H:i', (int)$info['pay_stop_time']);
  136. }
  137. if (isset($start_time) && isset($stop_time)) {
  138. $info['pay_time'] = [$start_time, $stop_time];
  139. } else {
  140. $info['pay_time'] = [];
  141. }
  142. $info['price'] = floatval($info['price']);
  143. $info['ot_price'] = floatval($info['ot_price']);
  144. /** @var StoreDescriptionServices $storeDescriptionServices */
  145. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  146. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 6]);
  147. $info['attrs'] = $this->attrList($id, $info['product_id']);
  148. }
  149. return $info;
  150. }
  151. /**
  152. * 获取规格
  153. * @param int $id
  154. * @param int $pid
  155. * @return mixed
  156. */
  157. public function attrList(int $id, int $pid)
  158. {
  159. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  160. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  161. $advanceResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 6], 'result');
  162. $items = json_decode($advanceResult, true)['attr'];
  163. $productAttr = $this->getAttr($items, $pid, 0);
  164. $advanceAttr = $this->getAttr($items, $id, 6);
  165. foreach ($productAttr as $pk => $pv) {
  166. foreach ($advanceAttr as &$sv) {
  167. if ($pv['detail'] == $sv['detail']) {
  168. $productAttr[$pk] = $sv;
  169. }
  170. }
  171. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  172. }
  173. $attrs['items'] = $items;
  174. $attrs['value'] = $productAttr;
  175. foreach ($items as $key => $item) {
  176. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  177. }
  178. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  179. $header[] = ['title' => '预售价', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  180. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  181. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  182. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  183. $header[] = ['title' => '限量', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  184. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  185. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  186. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  187. $attrs['header'] = $header;
  188. return $attrs;
  189. }
  190. /**
  191. * 获取规格
  192. * @param $attr
  193. * @param $id
  194. * @param $type
  195. * @return array
  196. */
  197. public function getAttr($attr, $id, $type)
  198. {
  199. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  200. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  201. list($value, $head) = attr_format($attr);
  202. $valueNew = [];
  203. $count = 0;
  204. foreach ($value as $suk) {
  205. $detail = explode(',', $suk);
  206. $sukValue = $storeProductAttrValueServices->getColumn(['product_id' => $id, 'type' => $type, 'suk' => $suk], 'bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  207. if (count($sukValue)) {
  208. foreach ($detail as $k => $v) {
  209. $valueNew[$count]['value' . ($k + 1)] = $v;
  210. }
  211. $valueNew[$count]['detail'] = json_encode(array_combine($head, $detail));
  212. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  213. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  214. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  215. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  216. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  217. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  218. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  219. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  220. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  221. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  222. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  223. $valueNew[$count]['_checked'] = $type != 0;
  224. $count++;
  225. }
  226. }
  227. return $valueNew;
  228. }
  229. /**
  230. * 商品详情
  231. * @param Request $request
  232. * @param int $id
  233. * @return array
  234. * @throws \think\db\exception\DataNotFoundException
  235. * @throws \think\db\exception\DbException
  236. * @throws \think\db\exception\ModelNotFoundException
  237. */
  238. public function getAdvanceinfo(Request $request, int $id)
  239. {
  240. $uid = (int)$request->uid();
  241. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['description']);
  242. if (!$storeInfo) {
  243. throw new ApiException(410294);
  244. } else {
  245. $storeInfo = $storeInfo->toArray();
  246. }
  247. $siteUrl = sys_config('site_url');
  248. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  249. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  250. if (time() < $storeInfo['start_time']) {
  251. $data['pay_status'] = 1;
  252. } elseif (time() > $storeInfo['stop_time']) {
  253. $data['pay_status'] = 3;
  254. } else {
  255. $data['pay_status'] = 2;
  256. }
  257. $storeInfo['start_time'] = date('Y-m-d H:i:s', (int)$storeInfo['start_time']);
  258. $storeInfo['stop_time'] = date('Y-m-d H:i:s', (int)$storeInfo['stop_time']);
  259. /** @var StoreProductServices $storeProductService */
  260. $storeProductService = app()->make(StoreProductServices::class);
  261. $productInfo = $storeProductService->get($storeInfo['product_id']);
  262. $storeInfo['total'] = $productInfo['sales'] + $productInfo['ficti'];
  263. $storeInfo['store_name'] = $storeInfo['title'];
  264. $storeInfo['store_info'] = $storeInfo['info'];
  265. /** @var QrcodeServices $qrcodeService */
  266. $qrcodeService = app()->make(QrcodeServices::class);
  267. $storeInfo['code_base'] = $qrcodeService->getWechatQrcodePath($id . '_product_advance_detail_wap.jpg', 'pages/activity/presell_details/index?id=' . $id);
  268. /** @var StoreOrderServices $storeOrderServices */
  269. $storeOrderServices = app()->make(StoreOrderServices::class);
  270. $data['buy_num'] = $storeOrderServices->getBuyCount($uid, 'advance_id', $id);
  271. /** @var StoreProductRelationServices $storeProductRelationServices */
  272. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  273. $storeInfo['userCollect'] = $storeProductRelationServices->isProductRelation(['uid' => $uid, 'product_id' => $storeInfo['product_id'], 'type' => 'collect', 'category' => 'product']);
  274. $storeInfo['userLike'] = false;
  275. $storeInfo['uid'] = $uid;
  276. if ($storeInfo['quota'] > 0) {
  277. $percent = (int)(($storeInfo['quota_show'] - $storeInfo['quota']) / $storeInfo['quota_show'] * 100);
  278. $storeInfo['percent'] = $percent;
  279. $storeInfo['stock'] = $storeInfo['quota'];
  280. } else {
  281. $storeInfo['percent'] = 100;
  282. $storeInfo['stock'] = 0;
  283. }
  284. //商品详情
  285. $data['storeInfo'] = get_thumb_water($storeInfo, 'big', ['image', 'images']);
  286. /** @var StoreProductReplyServices $storeProductReplyService */
  287. $storeProductReplyService = app()->make(StoreProductReplyServices::class);
  288. $data['reply'] = get_thumb_water($storeProductReplyService->getRecProductReply($storeInfo['product_id']), 'small', ['pics']);
  289. [$replyCount, $goodReply, $replyChance] = $storeProductReplyService->getProductReplyData((int)$storeInfo['product_id']);
  290. $data['replyChance'] = $replyChance;
  291. $data['replyCount'] = $replyCount;
  292. /** @var StoreProductAttrServices $storeProductAttrServices */
  293. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  294. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 6, $storeInfo['product_id']);
  295. $data['productAttr'] = $productAttr;
  296. $data['productValue'] = $productValue;
  297. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  298. //用户访问事件
  299. event('UserVisitListener', [$uid, $id, 'advance', $storeInfo['product_id'], 'view']);
  300. //浏览记录
  301. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $storeInfo['product_id']]]);
  302. return $data;
  303. }
  304. /**
  305. * 修改预售库存
  306. * @param int $num
  307. * @param int $advanceId
  308. * @return bool
  309. */
  310. public function decAdvanceStock(int $num, int $advanceId, string $unique = '')
  311. {
  312. $product_id = $this->dao->value(['id' => $advanceId], 'product_id');
  313. if ($unique) {
  314. /** @var StoreProductAttrValueServices $skuValueServices */
  315. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  316. //减去预售商品的sku库存增加销量
  317. $res = false !== $skuValueServices->decProductAttrStock($advanceId, $unique, $num, 6);
  318. //减去预售库存
  319. $res = $res && $this->dao->decStockIncSales(['id' => $advanceId, 'type' => 6], $num);
  320. //获取预售的sku
  321. $sku = $skuValueServices->value(['product_id' => $advanceId, 'unique' => $unique, 'type' => 6], 'suk');
  322. //减去当前普通商品sku的库存增加销量
  323. $res = $res && $skuValueServices->decStockIncSales(['product_id' => $product_id, 'suk' => $sku, 'type' => 0], $num);
  324. } else {
  325. $res = false !== $this->dao->decStockIncSales(['id' => $advanceId, 'type' => 6], $num);
  326. }
  327. /** @var StoreProductServices $services */
  328. $services = app()->make(StoreProductServices::class);
  329. //减去普通商品库存
  330. $res = $res && $services->decProductStock($num, $product_id);
  331. return $res;
  332. }
  333. /**
  334. * 减销量加库存
  335. * @param int $num
  336. * @param int $advanceId
  337. * @param string $unique
  338. * @return bool
  339. */
  340. public function incAdvanceStock(int $num, int $advanceId, string $unique = '')
  341. {
  342. $product_id = $this->dao->value(['id' => $advanceId], 'product_id');
  343. if ($unique) {
  344. /** @var StoreProductAttrValueServices $skuValueServices */
  345. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  346. //减去砍价商品sku的销量,增加库存和限购数量
  347. $res = false !== $skuValueServices->incProductAttrStock($advanceId, $unique, $num, 6);
  348. //减去砍价商品的销量,增加库存
  349. $res = $res && $this->dao->incStockDecSales(['id' => $advanceId, 'type' => 6], $num);
  350. //减掉普通商品sku的销量,增加库存
  351. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $advanceId, 'type' => 6], 'suk');
  352. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  353. if ($productUnique) {
  354. $res = $res && $skuValueServices->incProductAttrStock($product_id, $productUnique, $num);
  355. }
  356. } else {
  357. //减去砍价商品的销量,增加库存
  358. $res = false !== $this->dao->incStockDecSales(['id' => $advanceId, 'type' => 6], $num);
  359. }
  360. /** @var StoreProductServices $services */
  361. $services = app()->make(StoreProductServices::class);
  362. //减掉普通商品的库存加销量
  363. $res = $res && $services->incProductStock($num, $product_id);
  364. return $res;
  365. }
  366. /**
  367. * 验证预售下单库存限量
  368. * @param int $uid
  369. * @param int $combinationId
  370. * @param int $cartNum
  371. * @param string $unique
  372. * @return array
  373. * @throws \think\db\exception\DataNotFoundException
  374. * @throws \think\db\exception\DbException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. */
  377. public function checkAdvanceStock(int $uid, int $advanceId, int $cartNum = 1, string $unique = '')
  378. {
  379. $productInfo = $this->dao->getOne(['id' => $advanceId, 'status' => 1, 'is_del' => 0], '*,title as store_name');
  380. if (!$productInfo) throw new ApiException(400093);
  381. /** @var StoreProductAttrValueServices $attrValueServices */
  382. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  383. if ($unique == '') {
  384. $unique = $attrValueServices->value(['product_id' => $advanceId, 'type' => 6], 'unique');
  385. }
  386. $attrInfo = $attrValueServices->getOne(['product_id' => $advanceId, 'unique' => $unique, 'type' => 6]);
  387. if (!$attrInfo || $attrInfo['product_id'] != $advanceId) {
  388. throw new ApiException(400094);
  389. }
  390. /** @var StoreOrderServices $orderServices */
  391. $orderServices = app()->make(StoreOrderServices::class);
  392. $userBuyCount = $orderServices->getBuyCount($uid, 'advance_id', $advanceId);
  393. if ($productInfo['num'] < ($userBuyCount + $cartNum)) {
  394. throw new ApiException(410298, ['num' => $productInfo['num']]);
  395. }
  396. if ($productInfo['start_time'] > time()) throw new ApiException(410321);
  397. if ($productInfo['stop_time'] < time()) throw new ApiException(410322);
  398. if ($cartNum > $attrInfo['quota']) {
  399. throw new ApiException(410297, ['num' => $cartNum]);
  400. }
  401. return [$attrInfo, $unique, $productInfo];
  402. }
  403. }