StoreCartServices.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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\order;
  13. use app\services\activity\advance\StoreAdvanceServices;
  14. use app\services\BaseServices;
  15. use app\dao\order\StoreCartDao;
  16. use app\services\activity\coupon\StoreCouponIssueServices;
  17. use app\services\shipping\ShippingTemplatesNoDeliveryServices;
  18. use app\services\system\SystemUserLevelServices;
  19. use app\services\user\member\MemberCardServices;
  20. use app\services\user\UserServices;
  21. use app\jobs\ProductLogJob;
  22. use crmeb\exceptions\ApiException;
  23. use crmeb\services\CacheService;
  24. use app\services\activity\seckill\StoreSeckillServices;
  25. use app\services\activity\bargain\StoreBargainServices;
  26. use app\services\activity\combination\StoreCombinationServices;
  27. use app\services\product\product\StoreProductServices;
  28. use app\services\product\sku\StoreProductAttrValueServices;
  29. /**
  30. *
  31. * Class StoreCartServices
  32. * @package app\services\order
  33. * @method updateCartStatus($cartIds) 修改购物车状态
  34. * @method getUserCartNum(int $uid, string $type, int $numType) 购物车数量
  35. * @method deleteCartStatus(array $cartIds) 修改购物车状态
  36. * @method array productIdByCartNum(array $ids, int $uid) 根据商品id获取购物车数量
  37. * @method getCartList(array $where, ?int $page = 0, ?int $limit = 0, ?array $with = []) 获取用户购物车
  38. * @method getSum($where, $field) 求和
  39. * @method getProductTrend($time, $timeType, $str) 购物车趋势
  40. */
  41. class StoreCartServices extends BaseServices
  42. {
  43. /**
  44. * StoreCartServices constructor.
  45. * @param StoreCartDao $dao
  46. */
  47. public function __construct(StoreCartDao $dao)
  48. {
  49. $this->dao = $dao;
  50. }
  51. /**
  52. * 获取某个用户下的购物车数量
  53. * @param array $unique
  54. * @param int $productId
  55. * @param int $uid
  56. * @return array
  57. */
  58. public function getUserCartNums(array $unique, int $productId, int $uid)
  59. {
  60. $where['is_pay'] = 0;
  61. $where['is_del'] = 0;
  62. $where['is_new'] = 0;
  63. $where['product_id'] = $productId;
  64. $where['uid'] = $uid;
  65. return $this->dao->getUserCartNums($where, $unique);
  66. }
  67. /**
  68. * 获取用户下的购物车列表
  69. * @param $uid
  70. * @param string $cartIds
  71. * @param bool $new
  72. * @param array $addr
  73. * @return array
  74. * @throws \Psr\SimpleCache\InvalidArgumentException
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function getUserProductCartListV1($uid, $cartIds = '', bool $new, $addr = [], int $shipping_type = 1)
  80. {
  81. if ($new) {
  82. $cartIds = explode(',', $cartIds);
  83. $cartInfo = [];
  84. foreach ($cartIds as $key) {
  85. $info = CacheService::get($key);
  86. if ($info) {
  87. $cartInfo[] = $info;
  88. }
  89. }
  90. } else {
  91. $cartInfo = $this->dao->getCartList(['uid' => $uid, 'status' => 1, 'id' => $cartIds], 0, 0, ['productInfo', 'attrInfo']);
  92. }
  93. if (!$cartInfo) {
  94. throw new ApiException(410233);
  95. }
  96. [$cartInfo, $valid, $invalid] = $this->handleCartList($uid, $cartInfo, $addr, $shipping_type);
  97. $seckillIds = array_unique(array_column($cartInfo, 'seckill_id'));
  98. $bargainIds = array_unique(array_column($cartInfo, 'bargain_id'));
  99. $combinationId = array_unique(array_column($cartInfo, 'combination_id'));
  100. $advanceId = array_unique(array_column($cartInfo, 'advance_id'));
  101. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0, 'advance_id' => $advanceId[0] ?? 0];
  102. return ['cartInfo' => $cartInfo, 'valid' => $valid, 'invalid' => $invalid, 'deduction' => $deduction];
  103. }
  104. /**
  105. * 使用雪花算法生成订单ID
  106. * @return string
  107. * @throws \Exception
  108. */
  109. public function getCartId($prefix)
  110. {
  111. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  112. //32位
  113. if (PHP_INT_SIZE == 4) {
  114. $id = abs((int)$snowflake->id());
  115. } else {
  116. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->id();
  117. }
  118. return $prefix . $id;
  119. }
  120. /**
  121. * 验证库存
  122. * @param int $uid
  123. * @param int $cartNum
  124. * @param string $unique
  125. * @param int $type
  126. * @param $productId
  127. * @param int $seckillId
  128. * @param int $bargainId
  129. * @param int $combinationId
  130. * @return array
  131. * @throws \Psr\SimpleCache\InvalidArgumentException
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function checkProductStock(int $uid, int $cartNum, string $unique, int $type = 0, $productId, int $seckillId, int $bargainId, int $combinationId, int $advanceId)
  137. {
  138. /** @var StoreProductAttrValueServices $attrValueServices */
  139. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  140. switch ($type) {
  141. case 0://普通
  142. if ($unique == '') {
  143. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  144. }
  145. /** @var StoreProductServices $productServices */
  146. $productServices = app()->make(StoreProductServices::class);
  147. $productInfo = $productServices->isValidProduct($productId);
  148. if (!$productInfo) {
  149. throw new ApiException(410295);
  150. }
  151. $attrInfo = $attrValueServices->getOne(['unique' => $unique, 'type' => 0]);
  152. if (!$unique || !$attrInfo || $attrInfo['product_id'] != $productId) {
  153. throw new ApiException(410305);
  154. }
  155. $nowStock = $attrInfo['stock'];//现有库存
  156. if ($cartNum > $nowStock) {
  157. throw new ApiException(410297, ['num' => $cartNum]);
  158. }
  159. if ($productInfo['is_virtual'] == 1 && $productInfo['virtual_type'] == 2 && $attrInfo['coupon_id']) {
  160. /** @var StoreCouponIssueServices $issueCoupon */
  161. $issueCoupon = app()->make(StoreCouponIssueServices::class);
  162. if (!$issueCoupon->getCount(['id' => $attrInfo['coupon_id'], 'status' => 1, 'is_del' => 0])) {
  163. throw new ApiException(410234);
  164. }
  165. }
  166. $stockNum = $this->dao->value(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], 'cart_num') ?: 0;
  167. if ($nowStock < ($cartNum + $stockNum)) {
  168. $surplusStock = $nowStock - $cartNum;//剩余库存
  169. if ($surplusStock < $stockNum) {
  170. $this->dao->update(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], ['cart_num' => $surplusStock]);
  171. }
  172. }
  173. break;
  174. case 1://秒杀
  175. /** @var StoreSeckillServices $seckillService */
  176. $seckillService = app()->make(StoreSeckillServices::class);
  177. [$attrInfo, $unique, $productInfo] = $seckillService->checkSeckillStock($uid, $seckillId, $cartNum, $unique);
  178. break;
  179. case 2://砍价
  180. /** @var StoreBargainServices $bargainService */
  181. $bargainService = app()->make(StoreBargainServices::class);
  182. [$attrInfo, $unique, $productInfo, $bargainUserInfo] = $bargainService->checkBargainStock($uid, $bargainId, $cartNum, $unique);
  183. break;
  184. case 3://拼团
  185. /** @var StoreCombinationServices $combinationService */
  186. $combinationService = app()->make(StoreCombinationServices::class);
  187. [$attrInfo, $unique, $productInfo] = $combinationService->checkCombinationStock($uid, $combinationId, $cartNum, $unique);
  188. break;
  189. case 6://预售
  190. /** @var StoreAdvanceServices $advanceService */
  191. $advanceService = app()->make(StoreAdvanceServices::class);
  192. [$attrInfo, $unique, $productInfo] = $advanceService->checkAdvanceStock($uid, $advanceId, $cartNum, $unique);
  193. break;
  194. default:
  195. throw new ApiException(410236);
  196. }
  197. if ($type && $type != 6) {
  198. //根商品规格库存
  199. $product_stock = $attrValueServices->value(['product_id' => $productInfo['product_id'], 'suk' => $attrInfo['suk'], 'type' => 0], 'stock');
  200. if ($product_stock < $cartNum) {
  201. throw new ApiException(410297, ['num' => $cartNum]);
  202. }
  203. }
  204. return [$attrInfo, $unique, $bargainUserInfo['bargain_price_min'] ?? 0, $cartNum, $productInfo];
  205. }
  206. /**
  207. * 添加购物车
  208. * @param int $uid 用户UID
  209. * @param int $product_id 商品ID
  210. * @param int $cart_num 商品数量
  211. * @param string $product_attr_unique 商品SKU
  212. * @param string $type 添加购物车类型
  213. * @param bool $new true = 立即购买,false = 加入购物车
  214. * @param int $combination_id 拼团商品ID
  215. * @param int $seckill_id 秒杀商品ID
  216. * @param int $bargain_id 砍价商品ID
  217. * @return mixed|string
  218. * @throws \Psr\SimpleCache\InvalidArgumentException
  219. * @throws \think\db\exception\DataNotFoundException
  220. * @throws \think\db\exception\DbException
  221. * @throws \think\db\exception\ModelNotFoundException
  222. */
  223. public function setCart(int $uid, int $product_id, int $cart_num = 1, string $product_attr_unique = '', int $type = 0, bool $new = true, int $combination_id = 0, int $seckill_id = 0, int $bargain_id = 0, int $advance_id = 0)
  224. {
  225. if ($cart_num < 1) $cart_num = 1;
  226. //检查限购
  227. $this->checkLimit($uid, $product_id, $cart_num, $new);
  228. //检测库存限量
  229. [$attrInfo, $product_attr_unique, $bargainPriceMin, $cart_num, $productInfo] = $this->checkProductStock($uid, $cart_num, $product_attr_unique, $type, $product_id, $seckill_id, $bargain_id, $combination_id, $advance_id);
  230. if ($new) {
  231. /** @var StoreOrderCreateServices $storeOrderCreateService */
  232. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  233. $key = $storeOrderCreateService->getNewOrderId((string)$uid);
  234. $info['id'] = $key;
  235. $info['type'] = $type;
  236. $info['seckill_id'] = $seckill_id;
  237. $info['bargain_id'] = $bargain_id;
  238. $info['combination_id'] = $combination_id;
  239. $info['advance_id'] = $advance_id;
  240. $info['product_id'] = $product_id;
  241. $info['product_attr_unique'] = $product_attr_unique;
  242. $info['cart_num'] = $cart_num;
  243. $info['productInfo'] = $productInfo ? $productInfo->toArray() : [];
  244. $info['productInfo']['attrInfo'] = $attrInfo->toArray();
  245. $info['attrInfo'] = $attrInfo->toArray();
  246. $info['sum_price'] = $info['productInfo']['attrInfo']['price'];
  247. //砍价
  248. if ($bargain_id) {
  249. $info['truePrice'] = $bargainPriceMin;
  250. $info['productInfo']['attrInfo']['price'] = $bargainPriceMin;
  251. } else {
  252. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? $info['productInfo']['price'] ?? 0;
  253. }
  254. //拼团砍价秒杀不参与会员价
  255. if ($bargain_id || $combination_id || $seckill_id || $advance_id) {
  256. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? 0;
  257. $info['vip_truePrice'] = 0;
  258. }
  259. $info['trueStock'] = $info['productInfo']['attrInfo']['stock'];
  260. $info['costPrice'] = $info['productInfo']['attrInfo']['cost'];
  261. try {
  262. CacheService::set($key, $info, 3600);
  263. } catch (\Throwable $e) {
  264. throw new ApiException($e->getMessage());
  265. }
  266. return $key;
  267. } else {//加入购物车记录
  268. ProductLogJob::dispatch(['cart', ['uid' => $uid, 'product_id' => $product_id, 'cart_num' => $cart_num]]);
  269. $cart = $this->dao->getOne(['type' => $type, 'uid' => $uid, 'product_id' => $product_id, 'product_attr_unique' => $product_attr_unique, 'is_del' => 0, 'is_new' => 0, 'is_pay' => 0, 'status' => 1]);
  270. if ($cart) {
  271. $cart->cart_num = $cart_num + $cart->cart_num;
  272. $cart->add_time = time();
  273. $cart->save();
  274. return $cart->id;
  275. } else {
  276. $add_time = time();
  277. return $this->dao->save(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'type', 'add_time'))->id;
  278. }
  279. }
  280. }
  281. /**移除购物车商品
  282. * @param int $uid
  283. * @param array $ids
  284. * @return StoreCartDao|bool
  285. */
  286. public function removeUserCart(int $uid, array $ids)
  287. {
  288. if (!$uid || !$ids) return false;
  289. return $this->dao->removeUserCart($uid, $ids);
  290. }
  291. /**购物车 修改商品数量
  292. * @param $id
  293. * @param $number
  294. * @param $uid
  295. * @return bool|\crmeb\basic\BaseModel
  296. * @throws \think\db\exception\DataNotFoundException
  297. * @throws \think\db\exception\DbException
  298. * @throws \think\db\exception\ModelNotFoundException
  299. */
  300. public function changeUserCartNum($id, $number, $uid)
  301. {
  302. if (!$id || !$number || !$uid) return false;
  303. $where = ['uid' => $uid, 'id' => $id];
  304. $carInfo = $this->dao->getOne($where, 'product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num');
  305. //购物车修改数量检查限购
  306. /** @var StoreProductServices $productServices */
  307. $productServices = app()->make(StoreProductServices::class);
  308. $limitInfo = $productServices->get($carInfo->product_id, ['is_limit', 'limit_type', 'limit_num']);
  309. if ($limitInfo['is_limit']) {
  310. if ($limitInfo['limit_type'] == 1 && $number > $limitInfo['limit_num']) {
  311. throw new ApiException(410239, ['limit' => $limitInfo['limit_num']]);
  312. } else if ($limitInfo['limit_type'] == 2) {
  313. /** @var StoreOrderCartInfoServices $orderCartServices */
  314. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  315. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $carInfo->product_id], 'cart_num');
  316. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $carInfo->product_id], 'refund_num');
  317. $orderNum = $orderPayNum - $orderRefundNum;
  318. if (($number + $orderNum) > $limitInfo['limit_num']) {
  319. throw new ApiException(410240, ['limit' => $limitInfo['limit_num'], 'pay_num' => $orderNum]);
  320. }
  321. }
  322. }
  323. $stock = $productServices->getProductStock($carInfo->product_id, $carInfo->product_attr_unique);
  324. if (!$stock) throw new ApiException(410237);
  325. if ($stock < $number) throw new ApiException(410297, ['num' => $number]);
  326. if ($carInfo->cart_num == $number) return true;
  327. return $this->dao->changeUserCartNum(['uid' => $uid, 'id' => $id], (int)$number);
  328. }
  329. /**
  330. * 修改购物车状态
  331. * @param int $productId
  332. * @param int $status 0 商品下架
  333. */
  334. public function changeStatus(int $productId, $status = 0)
  335. {
  336. $this->dao->update($productId, ['status' => $status], 'product_id');
  337. }
  338. /**
  339. * 获取购物车列表
  340. * @param int $uid
  341. * @param int $status
  342. * @return array
  343. * @throws \think\db\exception\DataNotFoundException
  344. * @throws \think\db\exception\DbException
  345. * @throws \think\db\exception\ModelNotFoundException
  346. */
  347. public function getUserCartList(int $uid, int $status, string $cartIds = '')
  348. {
  349. [$page, $limit] = $this->getPageValue();
  350. $list = $this->dao->getCartList(['uid' => $uid, 'status' => $status, 'id' => $cartIds], $page, $limit, ['productInfo', 'attrInfo']);
  351. [$list, $valid, $invalid] = $this->handleCartList($uid, $list);
  352. $seckillIds = array_unique(array_column($list, 'seckill_id'));
  353. $bargainIds = array_unique(array_column($list, 'bargain_id'));
  354. $combinationId = array_unique(array_column($list, 'combination_id'));
  355. $discountId = array_unique(array_column($list, 'discount_id'));
  356. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0, 'discount_id' => $discountId[0] ?? 0];
  357. if ($status == 1) {
  358. return ['valid' => $list, 'invalid' => [], 'deduction' => $deduction];
  359. } else {
  360. return ['valid' => [], 'invalid' => $list, 'deduction' => $deduction];
  361. }
  362. }
  363. /**
  364. * 购物车重选
  365. * @param int $cart_id
  366. * @param int $product_id
  367. * @param string $unique
  368. */
  369. public function modifyCart(int $cart_id, int $product_id, string $unique)
  370. {
  371. /** @var StoreProductAttrValueServices $attrService */
  372. $attrService = app()->make(StoreProductAttrValueServices::class);
  373. $stock = $attrService->value(['product_id' => $product_id, 'unique' => $unique, 'type' => 0], 'stock');
  374. if ($stock > 0) {
  375. $this->dao->update($cart_id, ['product_attr_unique' => $unique, 'cart_num' => 1]);
  376. } else {
  377. throw new ApiException(410238);
  378. }
  379. }
  380. /**
  381. * 重选购物车
  382. * @param $id
  383. * @param $uid
  384. * @param $productId
  385. * @param $unique
  386. * @param $num
  387. * @throws \think\db\exception\DataNotFoundException
  388. * @throws \think\db\exception\DbException
  389. * @throws \think\db\exception\ModelNotFoundException
  390. */
  391. public function resetCart($id, $uid, $productId, $unique, $num)
  392. {
  393. $res = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  394. if ($res) {
  395. $res->cart_num = $res->cart_num + $num;
  396. $res->save();
  397. $this->dao->delete($id);
  398. } else {
  399. $this->dao->update($id, ['product_attr_unique' => $unique, 'cart_num' => $num]);
  400. }
  401. }
  402. /**
  403. * 首页加入购物车
  404. * @param $uid
  405. * @param $productId
  406. * @param $num
  407. * @param $unique
  408. * @param $type
  409. * @return mixed
  410. * @throws \think\db\exception\DataNotFoundException
  411. * @throws \think\db\exception\DbException
  412. * @throws \think\db\exception\ModelNotFoundException
  413. */
  414. public function setCartNum($uid, $productId, $num, $unique, $type)
  415. {
  416. if ($type == 1) {
  417. //检查限购
  418. $this->checkLimit($uid, $productId, $num, 0);
  419. }
  420. /** @var StoreProductAttrValueServices $attrValueServices */
  421. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  422. if ($unique == '') {
  423. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  424. }
  425. /** @var StoreProductServices $productServices */
  426. $productServices = app()->make(StoreProductServices::class);
  427. if (!$productServices->isValidProduct((int)$productId, 'id')) {
  428. throw new ApiException(410295);
  429. }
  430. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  431. throw new ApiException(410305);
  432. }
  433. if ($productServices->getProductStock((int)$productId, $unique) < $num) {
  434. throw new ApiException(410297, ['num' => $num]);
  435. }
  436. $cart = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  437. if ($cart) {
  438. if ($type == -1) {
  439. $cart->cart_num = $num;
  440. } elseif ($type == 0) {
  441. $cart->cart_num = $cart->cart_num - $num;
  442. } elseif ($type == 1) {
  443. $cart->cart_num = $cart->cart_num + $num;
  444. }
  445. if ($cart->cart_num === 0) {
  446. return $this->dao->delete($cart->id);
  447. } else {
  448. $cart->add_time = time();
  449. $cart->save();
  450. return $cart->id;
  451. }
  452. } else {
  453. $data = [
  454. 'uid' => $uid,
  455. 'product_id' => $productId,
  456. 'cart_num' => $num,
  457. 'product_attr_unique' => $unique,
  458. 'type' => 0,
  459. 'add_time' => time()
  460. ];
  461. return $this->dao->save($data)->id;
  462. }
  463. }
  464. /**
  465. * 获取用户购物车数量 ids 统计金额
  466. * @param int $uid
  467. * @param string $numType
  468. * @throws \think\db\exception\DataNotFoundException
  469. * @throws \think\db\exception\DbException
  470. * @throws \think\db\exception\ModelNotFoundException
  471. */
  472. public function getUserCartCount(int $uid, string $numType)
  473. {
  474. $count = 0;
  475. $ids = [];
  476. $sum_price = 0;
  477. $cartList = $this->dao->getUserCartList($uid, '*', ['productInfo', 'attrInfo']);
  478. if ($cartList) {
  479. /** @var StoreProductServices $productServices */
  480. $productServices = app()->make(StoreProductServices::class);
  481. /** @var MemberCardServices $memberCardService */
  482. $memberCardService = app()->make(MemberCardServices::class);
  483. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  484. /** @var UserServices $user */
  485. $user = app()->make(UserServices::class);
  486. $userInfo = $user->getUserInfo($uid);
  487. $discount = 100;
  488. if (sys_config('member_func_status', 1)) {
  489. /** @var SystemUserLevelServices $systemLevel */
  490. $systemLevel = app()->make(SystemUserLevelServices::class);
  491. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  492. }
  493. foreach ($cartList as &$item) {
  494. $productInfo = $item['productInfo'];
  495. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  496. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  497. $item['truePrice'] = $truePrice;
  498. $item['price_type'] = $type;
  499. } else {
  500. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($item['productInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $item['productInfo']['vip_price'] ?? 0, $item['productInfo']['is_vip'] ?? 0, true);
  501. $item['truePrice'] = $truePrice;
  502. $item['price_type'] = $type;
  503. }
  504. $sum_price = bcadd((string)$sum_price, (string)bcmul((string)$item['cart_num'], (string)$item['truePrice'], 4), 2);
  505. }
  506. $ids = array_column($cartList, 'id');
  507. if ($numType) {
  508. $count = count($cartList);
  509. } else {
  510. $count = array_sum(array_column($cartList, 'cart_num'));
  511. }
  512. }
  513. return compact('count', 'ids', 'sum_price');
  514. }
  515. /**
  516. * 处理购物车数据
  517. * @param int $uid
  518. * @param array $cartList
  519. * @param array $addr
  520. * @param int $shipping_type
  521. * @return array
  522. * @throws \think\db\exception\DataNotFoundException
  523. * @throws \think\db\exception\DbException
  524. * @throws \think\db\exception\ModelNotFoundException
  525. * @author 吴汐
  526. * @email 442384644@qq.com
  527. * @date 2023/02/16
  528. */
  529. public function handleCartList(int $uid, array $cartList, array $addr = [], int $shipping_type = 1)
  530. {
  531. if (!$cartList) {
  532. return [$cartList, [], []];
  533. }
  534. /** @var StoreProductServices $productServices */
  535. $productServices = app()->make(StoreProductServices::class);
  536. /** @var MemberCardServices $memberCardService */
  537. $memberCardService = app()->make(MemberCardServices::class);
  538. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  539. $tempIds = [];
  540. $userInfo = [];
  541. $discount = 100;
  542. if ($uid) {
  543. /** @var UserServices $user */
  544. $user = app()->make(UserServices::class);
  545. $userInfo = $user->getUserInfo($uid);
  546. //用户等级是否开启
  547. if (sys_config('member_func_status', 1)) {
  548. /** @var SystemUserLevelServices $systemLevel */
  549. $systemLevel = app()->make(SystemUserLevelServices::class);
  550. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  551. }
  552. }
  553. //不送达运费模板
  554. if ($shipping_type == 1 && $addr) {
  555. $cityId = (int)($addr['city_id'] ?? 0);
  556. if ($cityId) {
  557. foreach ($cartList as $item) {
  558. $tempIds[] = $item['productInfo']['temp_id'];
  559. }
  560. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  561. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  562. $tempIds = $noDeliveryServices->isNoDelivery(array_unique($tempIds), $cityId);
  563. }
  564. }
  565. $valid = $invalid = [];
  566. foreach ($cartList as &$item) {
  567. $item['productInfo']['express_delivery'] = false;
  568. $item['productInfo']['store_mention'] = false;
  569. if (isset($item['productInfo']['logistics'])) {
  570. if (in_array(1, explode(',', $item['productInfo']['logistics']))) {
  571. $item['productInfo']['express_delivery'] = true;
  572. }
  573. if (in_array(2, explode(',', $item['productInfo']['logistics']))) {
  574. $item['productInfo']['store_mention'] = true;
  575. }
  576. }
  577. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  578. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  579. }
  580. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  581. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  582. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  583. if (isset($item['productInfo']['attrInfo'])) {
  584. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  585. }
  586. $item['productInfo'] = get_thumb_water($item['productInfo']);
  587. $productInfo = $item['productInfo'];
  588. $item['vip_truePrice'] = 0;
  589. $is_activity = $item['seckill_id'] || $item['bargain_id'] || $item['combination_id'] || $item['advance_id'];
  590. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  591. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  592. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  593. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  594. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  595. if (!$is_activity) {
  596. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  597. $item['truePrice'] = $truePrice;
  598. $item['vip_truePrice'] = $vip_truePrice;
  599. $item['price_type'] = $type;
  600. } else {
  601. $item['price_type'] = 'activity';
  602. }
  603. } else {
  604. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  605. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  606. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  607. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  608. if (!$is_activity) {
  609. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($item['productInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $item['productInfo']['vip_price'] ?? 0, $item['productInfo']['is_vip'] ?? 0, true);
  610. $item['truePrice'] = $truePrice;
  611. $item['vip_truePrice'] = $vip_truePrice;
  612. $item['price_type'] = $type;
  613. } else {
  614. $item['price_type'] = 'activity';
  615. }
  616. }
  617. if (isset($item['status']) && $item['status'] == 0) {
  618. $item['is_valid'] = 0;
  619. $invalid[] = $item;
  620. } else {
  621. switch ($shipping_type) {
  622. case 1:
  623. //不送达
  624. if (in_array($item['productInfo']['temp_id'], $tempIds) || (isset($item['productInfo']['logistics']) && !in_array(1, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0)) {
  625. $item['is_valid'] = 0;
  626. $invalid[] = $item;
  627. } else {
  628. $item['is_valid'] = 1;
  629. $valid[] = $item;
  630. }
  631. break;
  632. case 2:
  633. //不支持到店自提
  634. if (isset($item['productInfo']['logistics']) && $item['productInfo']['logistics'] && !in_array(2, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0) {
  635. $item['is_valid'] = 0;
  636. $invalid[] = $item;
  637. } else {
  638. $item['is_valid'] = 1;
  639. $valid[] = $item;
  640. }
  641. break;
  642. }
  643. }
  644. unset($item['attrInfo']);
  645. }
  646. return [$cartList, $valid, $invalid];
  647. }
  648. /**
  649. * 检查限购
  650. * @param $uid
  651. * @param $product_id
  652. * @param $num
  653. * @param $new
  654. * @return bool
  655. * @throws \ReflectionException
  656. */
  657. public function checkLimit($uid, $product_id, $num, $new)
  658. {
  659. /** @var StoreProductServices $productServices */
  660. $productServices = app()->make(StoreProductServices::class);
  661. /** @var StoreOrderCartInfoServices $orderCartServices */
  662. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  663. $limitInfo = $productServices->get($product_id, ['is_limit', 'limit_type', 'limit_num']);
  664. if (!$limitInfo) throw new ApiException(410294);
  665. $limitInfo = $limitInfo->toArray();
  666. if (!$limitInfo['is_limit']) return true;
  667. if ($limitInfo['limit_type'] == 1) {
  668. $cartNum = 0;
  669. if (!$new) {
  670. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  671. }
  672. if (($num + $cartNum) > $limitInfo['limit_num']) {
  673. throw new ApiException(410239, ['limit' => $limitInfo['limit_num']]);
  674. }
  675. } else if ($limitInfo['limit_type'] == 2) {
  676. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  677. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id, 'pid' => 0], 'cart_num');
  678. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id, 'pid' => 0], 'refund_num');
  679. $orderNum = $orderPayNum - $orderRefundNum;
  680. if (($num + $orderNum + $cartNum) > $limitInfo['limit_num']) {
  681. throw new ApiException(410240, ['limit' => $limitInfo['limit_num'], 'pay_num' => $orderNum]);
  682. }
  683. }
  684. return true;
  685. }
  686. }