StoreAdvanceServices.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. $res = true;
  114. foreach ($valueGroup->toArray() as $item) {
  115. $res = $res && CacheService::setStock($item['unique'], (int)$item['quota_show'], 6);
  116. }
  117. if (!$res) {
  118. throw new AdminException(400092);
  119. }
  120. });
  121. }
  122. /**
  123. * 获取预售详情
  124. * @param int $id
  125. * @return array|\think\Model|null
  126. */
  127. public function getInfo(int $id)
  128. {
  129. $info = $this->dao->get($id);
  130. if ($info) {
  131. if ($info['start_time'] && $info['stop_time']) {
  132. $start_time = date('Y-m-d H:i', (int)$info['start_time']);
  133. $stop_time = date('Y-m-d H:i', (int)$info['stop_time']);
  134. }
  135. if (isset($start_time) && isset($stop_time)) {
  136. $info['section_time'] = [$start_time, $stop_time];
  137. } else {
  138. $info['section_time'] = [];
  139. }
  140. if ($info['pay_start_time'] && $info['pay_stop_time']) {
  141. $start_time = date('Y-m-d H:i', (int)$info['pay_start_time']);
  142. $stop_time = date('Y-m-d H:i', (int)$info['pay_stop_time']);
  143. }
  144. if (isset($start_time) && isset($stop_time)) {
  145. $info['pay_time'] = [$start_time, $stop_time];
  146. } else {
  147. $info['pay_time'] = [];
  148. }
  149. $info['price'] = floatval($info['price']);
  150. $info['ot_price'] = floatval($info['ot_price']);
  151. /** @var StoreDescriptionServices $storeDescriptionServices */
  152. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  153. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 6]);
  154. $info['attrs'] = $this->attrList($id, $info['product_id']);
  155. }
  156. return $info;
  157. }
  158. /**
  159. * 获取规格
  160. * @param int $id
  161. * @param int $pid
  162. * @return mixed
  163. */
  164. public function attrList(int $id, int $pid)
  165. {
  166. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  167. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  168. $advanceResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 6], 'result');
  169. $items = json_decode($advanceResult, true)['attr'];
  170. $productAttr = $this->getAttr($items, $pid, 0);
  171. $advanceAttr = $this->getAttr($items, $id, 6);
  172. foreach ($productAttr as $pk => $pv) {
  173. foreach ($advanceAttr as &$sv) {
  174. if ($pv['detail'] == $sv['detail']) {
  175. $productAttr[$pk] = $sv;
  176. }
  177. }
  178. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  179. }
  180. $attrs['items'] = $items;
  181. $attrs['value'] = $productAttr;
  182. foreach ($items as $key => $item) {
  183. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  184. }
  185. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  186. $header[] = ['title' => '预售价', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  187. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  188. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  189. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  190. $header[] = ['title' => '限量', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  191. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  192. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  193. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  194. $attrs['header'] = $header;
  195. return $attrs;
  196. }
  197. /**
  198. * 获取规格
  199. * @param $attr
  200. * @param $id
  201. * @param $type
  202. * @return array
  203. */
  204. public function getAttr($attr, $id, $type)
  205. {
  206. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  207. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  208. list($value, $head) = attr_format($attr);
  209. $valueNew = [];
  210. $count = 0;
  211. foreach ($value as $suk) {
  212. $detail = explode(',', $suk);
  213. $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');
  214. if (count($sukValue)) {
  215. foreach ($detail as $k => $v) {
  216. $valueNew[$count]['value' . ($k + 1)] = $v;
  217. }
  218. $valueNew[$count]['detail'] = json_encode(array_combine($head, $detail));
  219. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  220. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  221. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  222. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  223. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  224. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  225. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  226. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  227. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  228. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  229. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  230. $valueNew[$count]['_checked'] = $type != 0;
  231. $count++;
  232. }
  233. }
  234. return $valueNew;
  235. }
  236. /**
  237. * 商品详情
  238. * @param Request $request
  239. * @param int $id
  240. * @return array
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. */
  245. public function getAdvanceinfo(Request $request, int $id)
  246. {
  247. $uid = (int)$request->uid();
  248. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['description']);
  249. if (!$storeInfo) {
  250. throw new ApiException(410294);
  251. } else {
  252. $storeInfo = $storeInfo->toArray();
  253. }
  254. $siteUrl = sys_config('site_url');
  255. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  256. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  257. if (time() < $storeInfo['start_time']) {
  258. $data['pay_status'] = 1;
  259. } elseif (time() > $storeInfo['stop_time']) {
  260. $data['pay_status'] = 3;
  261. } else {
  262. $data['pay_status'] = 2;
  263. }
  264. $storeInfo['start_time'] = date('Y-m-d H:i:s', (int)$storeInfo['start_time']);
  265. $storeInfo['stop_time'] = date('Y-m-d H:i:s', (int)$storeInfo['stop_time']);
  266. /** @var StoreProductServices $storeProductService */
  267. $storeProductService = app()->make(StoreProductServices::class);
  268. $productInfo = $storeProductService->get($storeInfo['product_id']);
  269. $storeInfo['total'] = $productInfo['sales'] + $productInfo['ficti'];
  270. $storeInfo['store_name'] = $storeInfo['title'];
  271. $storeInfo['store_info'] = $storeInfo['info'];
  272. /** @var QrcodeServices $qrcodeService */
  273. $qrcodeService = app()->make(QrcodeServices::class);
  274. $storeInfo['code_base'] = $qrcodeService->getWechatQrcodePath($id . '_product_advance_detail_wap.jpg', 'pages/activity/presell_details/index?id=' . $id);
  275. /** @var StoreOrderServices $storeOrderServices */
  276. $storeOrderServices = app()->make(StoreOrderServices::class);
  277. $data['buy_num'] = $storeOrderServices->getBuyCount($uid, 'advance_id', $id);
  278. /** @var StoreProductRelationServices $storeProductRelationServices */
  279. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  280. $storeInfo['userCollect'] = $storeProductRelationServices->isProductRelation(['uid' => $uid, 'product_id' => $storeInfo['product_id'], 'type' => 'collect', 'category' => 'product']);
  281. $storeInfo['userLike'] = false;
  282. $storeInfo['uid'] = $uid;
  283. if ($storeInfo['quota'] > 0) {
  284. $percent = (int)(($storeInfo['quota_show'] - $storeInfo['quota']) / $storeInfo['quota_show'] * 100);
  285. $storeInfo['percent'] = $percent;
  286. $storeInfo['stock'] = $storeInfo['quota'];
  287. } else {
  288. $storeInfo['percent'] = 100;
  289. $storeInfo['stock'] = 0;
  290. }
  291. //商品详情
  292. $data['storeInfo'] = get_thumb_water($storeInfo, 'big', ['image', 'images']);
  293. /** @var StoreProductReplyServices $storeProductReplyService */
  294. $storeProductReplyService = app()->make(StoreProductReplyServices::class);
  295. $data['reply'] = get_thumb_water($storeProductReplyService->getRecProductReply($storeInfo['product_id']), 'small', ['pics']);
  296. [$replyCount, $goodReply, $replyChance] = $storeProductReplyService->getProductReplyData((int)$storeInfo['product_id']);
  297. $data['replyChance'] = $replyChance;
  298. $data['replyCount'] = $replyCount;
  299. /** @var StoreProductAttrServices $storeProductAttrServices */
  300. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  301. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 6, $storeInfo['product_id']);
  302. $data['productAttr'] = $productAttr;
  303. $data['productValue'] = $productValue;
  304. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  305. //用户访问事件
  306. event('user.userVisit', [$uid, $id, 'advance', $storeInfo['product_id'], 'view']);
  307. //浏览记录
  308. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $storeInfo['product_id']]]);
  309. return $data;
  310. }
  311. /**
  312. * 修改预售库存
  313. * @param int $num
  314. * @param int $advanceId
  315. * @return bool
  316. */
  317. public function decAdvanceStock(int $num, int $advanceId, string $unique = '')
  318. {
  319. $product_id = $this->dao->value(['id' => $advanceId], 'product_id');
  320. if ($unique) {
  321. /** @var StoreProductAttrValueServices $skuValueServices */
  322. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  323. //减去预售商品的sku库存增加销量
  324. $res = false !== $skuValueServices->decProductAttrStock($advanceId, $unique, $num, 6);
  325. //减去预售库存
  326. $res = $res && $this->dao->decStockIncSales(['id' => $advanceId, 'type' => 6], $num);
  327. //获取预售的sku
  328. $sku = $skuValueServices->value(['product_id' => $advanceId, 'unique' => $unique, 'type' => 6], 'suk');
  329. //减去当前普通商品sku的库存增加销量
  330. $res = $res && $skuValueServices->decStockIncSales(['product_id' => $product_id, 'suk' => $sku, 'type' => 0], $num);
  331. } else {
  332. $res = false !== $this->dao->decStockIncSales(['id' => $advanceId, 'type' => 6], $num);
  333. }
  334. /** @var StoreProductServices $services */
  335. $services = app()->make(StoreProductServices::class);
  336. //减去普通商品库存
  337. $res = $res && $services->decProductStock($num, $product_id);
  338. return $res;
  339. }
  340. /**
  341. * 减销量加库存
  342. * @param int $num
  343. * @param int $advanceId
  344. * @param string $unique
  345. * @return bool
  346. */
  347. public function incAdvanceStock(int $num, int $advanceId, string $unique = '')
  348. {
  349. $product_id = $this->dao->value(['id' => $advanceId], 'product_id');
  350. if ($unique) {
  351. /** @var StoreProductAttrValueServices $skuValueServices */
  352. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  353. //减去砍价商品sku的销量,增加库存和限购数量
  354. $res = false !== $skuValueServices->incProductAttrStock($advanceId, $unique, $num, 6);
  355. //减去砍价商品的销量,增加库存
  356. $res = $res && $this->dao->incStockDecSales(['id' => $advanceId, 'type' => 6], $num);
  357. //减掉普通商品sku的销量,增加库存
  358. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $advanceId, 'type' => 6], 'suk');
  359. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  360. if ($productUnique) {
  361. $res = $res && $skuValueServices->incProductAttrStock($product_id, $productUnique, $num);
  362. }
  363. } else {
  364. //减去砍价商品的销量,增加库存
  365. $res = false !== $this->dao->incStockDecSales(['id' => $advanceId, 'type' => 6], $num);
  366. }
  367. /** @var StoreProductServices $services */
  368. $services = app()->make(StoreProductServices::class);
  369. //减掉普通商品的库存加销量
  370. $res = $res && $services->incProductStock($num, $product_id);
  371. return $res;
  372. }
  373. /**
  374. * 验证预售下单库存限量
  375. * @param int $uid
  376. * @param int $combinationId
  377. * @param int $cartNum
  378. * @param string $unique
  379. * @return array
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\DbException
  382. * @throws \think\db\exception\ModelNotFoundException
  383. */
  384. public function checkAdvanceStock(int $uid, int $advanceId, int $cartNum = 1, string $unique = '')
  385. {
  386. $productInfo = $this->dao->getOne(['id' => $advanceId, 'status' => 1, 'is_del' => 0], '*,title as store_name');
  387. if (!$productInfo) throw new ApiException(400093);
  388. /** @var StoreProductAttrValueServices $attrValueServices */
  389. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  390. if ($unique == '') {
  391. $unique = $attrValueServices->value(['product_id' => $advanceId, 'type' => 6], 'unique');
  392. }
  393. $attrInfo = $attrValueServices->getOne(['product_id' => $advanceId, 'unique' => $unique, 'type' => 6]);
  394. if (!$attrInfo || $attrInfo['product_id'] != $advanceId) {
  395. throw new ApiException(400094);
  396. }
  397. /** @var StoreOrderServices $orderServices */
  398. $orderServices = app()->make(StoreOrderServices::class);
  399. $userBuyCount = $orderServices->getBuyCount($uid, 'advance_id', $advanceId);
  400. if ($productInfo['num'] < ($userBuyCount + $cartNum)) {
  401. throw new ApiException(410298, ['num' => $productInfo['num']]);
  402. }
  403. if ($productInfo['start_time'] > time()) throw new ApiException(410321);
  404. if ($productInfo['stop_time'] < time()) throw new ApiException(410322);
  405. if ($cartNum > $attrInfo['quota']) {
  406. throw new ApiException(410297, ['num' => $cartNum]);
  407. }
  408. return [$attrInfo, $unique, $productInfo];
  409. }
  410. }