StoreCartServices.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. //检查限购
  417. $this->checkLimit($uid, $productId, $num, 0);
  418. /** @var StoreProductAttrValueServices $attrValueServices */
  419. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  420. if ($unique == '') {
  421. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  422. }
  423. /** @var StoreProductServices $productServices */
  424. $productServices = app()->make(StoreProductServices::class);
  425. if (!$productServices->isValidProduct((int)$productId, 'id')) {
  426. throw new ApiException(410295);
  427. }
  428. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  429. throw new ApiException(410305);
  430. }
  431. if ($productServices->getProductStock((int)$productId, $unique) < $num) {
  432. throw new ApiException(410297, ['num' => $num]);
  433. }
  434. $cart = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  435. if ($cart) {
  436. if ($type == -1) {
  437. $cart->cart_num = $num;
  438. } elseif ($type == 0) {
  439. $cart->cart_num = $cart->cart_num - $num;
  440. } elseif ($type == 1) {
  441. $cart->cart_num = $cart->cart_num + $num;
  442. }
  443. if ($cart->cart_num === 0) {
  444. return $this->dao->delete($cart->id);
  445. } else {
  446. $cart->add_time = time();
  447. $cart->save();
  448. return $cart->id;
  449. }
  450. } else {
  451. $data = [
  452. 'uid' => $uid,
  453. 'product_id' => $productId,
  454. 'cart_num' => $num,
  455. 'product_attr_unique' => $unique,
  456. 'type' => 0,
  457. 'add_time' => time()
  458. ];
  459. return $this->dao->save($data)->id;
  460. }
  461. }
  462. /**
  463. * 获取用户购物车数量 ids 统计金额
  464. * @param int $uid
  465. * @param string $numType
  466. * @throws \think\db\exception\DataNotFoundException
  467. * @throws \think\db\exception\DbException
  468. * @throws \think\db\exception\ModelNotFoundException
  469. */
  470. public function getUserCartCount(int $uid, string $numType)
  471. {
  472. $count = 0;
  473. $ids = [];
  474. $sum_price = 0;
  475. $cartList = $this->dao->getUserCartList($uid, '*', ['productInfo', 'attrInfo']);
  476. if ($cartList) {
  477. /** @var StoreProductServices $productServices */
  478. $productServices = app()->make(StoreProductServices::class);
  479. /** @var MemberCardServices $memberCardService */
  480. $memberCardService = app()->make(MemberCardServices::class);
  481. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  482. /** @var UserServices $user */
  483. $user = app()->make(UserServices::class);
  484. $userInfo = $user->getUserInfo($uid);
  485. $discount = 100;
  486. if (sys_config('member_func_status', 1)) {
  487. /** @var SystemUserLevelServices $systemLevel */
  488. $systemLevel = app()->make(SystemUserLevelServices::class);
  489. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  490. }
  491. foreach ($cartList as &$item) {
  492. $productInfo = $item['productInfo'];
  493. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  494. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  495. $item['truePrice'] = $truePrice;
  496. $item['price_type'] = $type;
  497. } else {
  498. [$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);
  499. $item['truePrice'] = $truePrice;
  500. $item['price_type'] = $type;
  501. }
  502. $sum_price = bcadd((string)$sum_price, (string)bcmul((string)$item['cart_num'], (string)$item['truePrice'], 4), 2);
  503. }
  504. $ids = array_column($cartList, 'id');
  505. if ($numType) {
  506. $count = count($cartList);
  507. } else {
  508. $count = array_sum(array_column($cartList, 'cart_num'));
  509. }
  510. }
  511. return compact('count', 'ids', 'sum_price');
  512. }
  513. /**
  514. * 处理购物车数据
  515. * @param int $uid
  516. * @param array $cartList
  517. * @param array $addr
  518. * @param int $shipping_type
  519. * @return array
  520. * @throws \think\db\exception\DataNotFoundException
  521. * @throws \think\db\exception\DbException
  522. * @throws \think\db\exception\ModelNotFoundException
  523. * @author 吴汐
  524. * @email 442384644@qq.com
  525. * @date 2023/02/16
  526. */
  527. public function handleCartList(int $uid, array $cartList, array $addr = [], int $shipping_type = 1)
  528. {
  529. if (!$cartList) {
  530. return [$cartList, [], []];
  531. }
  532. /** @var StoreProductServices $productServices */
  533. $productServices = app()->make(StoreProductServices::class);
  534. /** @var MemberCardServices $memberCardService */
  535. $memberCardService = app()->make(MemberCardServices::class);
  536. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  537. $tempIds = [];
  538. $userInfo = [];
  539. $discount = 100;
  540. if ($uid) {
  541. /** @var UserServices $user */
  542. $user = app()->make(UserServices::class);
  543. $userInfo = $user->getUserInfo($uid);
  544. //用户等级是否开启
  545. if (sys_config('member_func_status', 1)) {
  546. /** @var SystemUserLevelServices $systemLevel */
  547. $systemLevel = app()->make(SystemUserLevelServices::class);
  548. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  549. }
  550. }
  551. //不送达运费模板
  552. if ($shipping_type == 1 && $addr) {
  553. $cityId = (int)($addr['city_id'] ?? 0);
  554. if ($cityId) {
  555. foreach ($cartList as $item) {
  556. $tempIds[] = $item['productInfo']['temp_id'];
  557. }
  558. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  559. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  560. $tempIds = $noDeliveryServices->isNoDelivery(array_unique($tempIds), $cityId);
  561. }
  562. }
  563. $valid = $invalid = [];
  564. foreach ($cartList as &$item) {
  565. $item['productInfo']['express_delivery'] = false;
  566. $item['productInfo']['store_mention'] = false;
  567. if (isset($item['productInfo']['logistics'])) {
  568. if (in_array(1, explode(',', $item['productInfo']['logistics']))) {
  569. $item['productInfo']['express_delivery'] = true;
  570. }
  571. if (in_array(2, explode(',', $item['productInfo']['logistics']))) {
  572. $item['productInfo']['store_mention'] = true;
  573. }
  574. }
  575. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  576. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  577. }
  578. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  579. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  580. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  581. if (isset($item['productInfo']['attrInfo'])) {
  582. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  583. }
  584. $item['productInfo'] = get_thumb_water($item['productInfo']);
  585. $productInfo = $item['productInfo'];
  586. $item['vip_truePrice'] = 0;
  587. $is_activity = $item['seckill_id'] || $item['bargain_id'] || $item['combination_id'] || $item['advance_id'];
  588. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  589. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  590. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  591. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  592. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  593. if (!$is_activity) {
  594. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  595. $item['truePrice'] = $truePrice;
  596. $item['vip_truePrice'] = $vip_truePrice;
  597. $item['price_type'] = $type;
  598. } else {
  599. $item['price_type'] = 'activity';
  600. }
  601. } else {
  602. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  603. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  604. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  605. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  606. if (!$is_activity) {
  607. [$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);
  608. $item['truePrice'] = $truePrice;
  609. $item['vip_truePrice'] = $vip_truePrice;
  610. $item['price_type'] = $type;
  611. } else {
  612. $item['price_type'] = 'activity';
  613. }
  614. }
  615. if (isset($item['status']) && $item['status'] == 0) {
  616. $item['is_valid'] = 0;
  617. $invalid[] = $item;
  618. } else {
  619. switch ($shipping_type) {
  620. case 1:
  621. //不送达
  622. if (in_array($item['productInfo']['temp_id'], $tempIds) || (isset($item['productInfo']['logistics']) && !in_array(1, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0)) {
  623. $item['is_valid'] = 0;
  624. $invalid[] = $item;
  625. } else {
  626. $item['is_valid'] = 1;
  627. $valid[] = $item;
  628. }
  629. break;
  630. case 2:
  631. //不支持到店自提
  632. if (isset($item['productInfo']['logistics']) && $item['productInfo']['logistics'] && !in_array(2, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0) {
  633. $item['is_valid'] = 0;
  634. $invalid[] = $item;
  635. } else {
  636. $item['is_valid'] = 1;
  637. $valid[] = $item;
  638. }
  639. break;
  640. }
  641. }
  642. unset($item['attrInfo']);
  643. }
  644. return [$cartList, $valid, $invalid];
  645. }
  646. /**
  647. * 检查限购
  648. * @param $uid
  649. * @param $product_id
  650. * @param $num
  651. * @param $new
  652. * @return bool
  653. */
  654. public function checkLimit($uid, $product_id, $num, $new)
  655. {
  656. /** @var StoreProductServices $productServices */
  657. $productServices = app()->make(StoreProductServices::class);
  658. /** @var StoreOrderCartInfoServices $orderCartServices */
  659. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  660. $limitInfo = $productServices->get($product_id, ['is_limit', 'limit_type', 'limit_num']);
  661. if (!$limitInfo) throw new ApiException(410294);
  662. $limitInfo = $limitInfo->toArray();
  663. if (!$limitInfo['is_limit']) return true;
  664. if ($limitInfo['limit_type'] == 1) {
  665. $cartNum = 0;
  666. if (!$new) {
  667. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  668. }
  669. if (($num + $cartNum) > $limitInfo['limit_num']) {
  670. throw new ApiException(410239, ['limit' => $limitInfo['limit_num']]);
  671. }
  672. } else if ($limitInfo['limit_type'] == 2) {
  673. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  674. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  675. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id], 'refund_num');
  676. $orderNum = $orderPayNum - $orderRefundNum;
  677. if (($num + $orderNum + $cartNum) > $limitInfo['limit_num']) {
  678. throw new ApiException(410240, ['limit' => $limitInfo['limit_num'], 'pay_num' => $orderNum]);
  679. }
  680. }
  681. return true;
  682. }
  683. }