StoreCartServices.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. if ($type == 0) {
  227. //检查限购
  228. $this->checkLimit($uid, $product_id, $cart_num, $new);
  229. }
  230. //检测库存限量
  231. [$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);
  232. if ($new) {
  233. /** @var StoreOrderCreateServices $storeOrderCreateService */
  234. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  235. $key = $storeOrderCreateService->getNewOrderId((string)$uid);
  236. $info['id'] = $key;
  237. $info['type'] = $type;
  238. $info['seckill_id'] = $seckill_id;
  239. $info['bargain_id'] = $bargain_id;
  240. $info['combination_id'] = $combination_id;
  241. $info['advance_id'] = $advance_id;
  242. $info['product_id'] = $product_id;
  243. $info['product_attr_unique'] = $product_attr_unique;
  244. $info['cart_num'] = $cart_num;
  245. $info['productInfo'] = $productInfo ? $productInfo->toArray() : [];
  246. $info['productInfo']['attrInfo'] = $attrInfo->toArray();
  247. $info['attrInfo'] = $attrInfo->toArray();
  248. $info['sum_price'] = $info['productInfo']['attrInfo']['price'];
  249. //砍价
  250. if ($bargain_id) {
  251. $info['truePrice'] = $bargainPriceMin;
  252. $info['productInfo']['attrInfo']['price'] = $bargainPriceMin;
  253. } else {
  254. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? $info['productInfo']['price'] ?? 0;
  255. }
  256. //拼团砍价秒杀不参与会员价
  257. if ($bargain_id || $combination_id || $seckill_id || $advance_id) {
  258. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? 0;
  259. $info['vip_truePrice'] = 0;
  260. }
  261. $info['trueStock'] = $info['productInfo']['attrInfo']['stock'];
  262. $info['costPrice'] = $info['productInfo']['attrInfo']['cost'];
  263. try {
  264. CacheService::set($key, $info, 3600);
  265. } catch (\Throwable $e) {
  266. throw new ApiException($e->getMessage());
  267. }
  268. return $key;
  269. } else {//加入购物车记录
  270. ProductLogJob::dispatch(['cart', ['uid' => $uid, 'product_id' => $product_id, 'cart_num' => $cart_num]]);
  271. $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]);
  272. if ($cart) {
  273. $cart->cart_num = $cart_num + $cart->cart_num;
  274. $cart->add_time = time();
  275. $cart->save();
  276. return $cart->id;
  277. } else {
  278. $add_time = time();
  279. return $this->dao->save(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'type', 'add_time'))->id;
  280. }
  281. }
  282. }
  283. /**移除购物车商品
  284. * @param int $uid
  285. * @param array $ids
  286. * @return StoreCartDao|bool
  287. */
  288. public function removeUserCart(int $uid, array $ids)
  289. {
  290. if (!$uid || !$ids) return false;
  291. return $this->dao->removeUserCart($uid, $ids);
  292. }
  293. /**购物车 修改商品数量
  294. * @param $id
  295. * @param $number
  296. * @param $uid
  297. * @return bool|\crmeb\basic\BaseModel
  298. * @throws \think\db\exception\DataNotFoundException
  299. * @throws \think\db\exception\DbException
  300. * @throws \think\db\exception\ModelNotFoundException
  301. */
  302. public function changeUserCartNum($id, $number, $uid)
  303. {
  304. if (!$id || !$number || !$uid) return false;
  305. $where = ['uid' => $uid, 'id' => $id];
  306. $carInfo = $this->dao->getOne($where, 'product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num');
  307. //购物车修改数量检查限购
  308. /** @var StoreProductServices $productServices */
  309. $productServices = app()->make(StoreProductServices::class);
  310. $limitInfo = $productServices->get($carInfo->product_id, ['is_limit', 'limit_type', 'limit_num']);
  311. if ($limitInfo['is_limit']) {
  312. if ($limitInfo['limit_type'] == 1 && $number > $limitInfo['limit_num']) {
  313. throw new ApiException(410239, ['limit' => $limitInfo['limit_num']]);
  314. } else if ($limitInfo['limit_type'] == 2) {
  315. /** @var StoreOrderCartInfoServices $orderCartServices */
  316. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  317. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $carInfo->product_id], 'cart_num');
  318. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $carInfo->product_id], 'refund_num');
  319. $orderNum = $orderPayNum - $orderRefundNum;
  320. if (($number + $orderNum) > $limitInfo['limit_num']) {
  321. throw new ApiException(410240, ['limit' => $limitInfo['limit_num'], 'pay_num' => $orderNum]);
  322. }
  323. }
  324. }
  325. $stock = $productServices->getProductStock($carInfo->product_id, $carInfo->product_attr_unique);
  326. if (!$stock) throw new ApiException(410237);
  327. if ($stock < $number) throw new ApiException(410297, ['num' => $number]);
  328. if ($carInfo->cart_num == $number) return true;
  329. return $this->dao->changeUserCartNum(['uid' => $uid, 'id' => $id], (int)$number);
  330. }
  331. /**
  332. * 修改购物车状态
  333. * @param int $productId
  334. * @param int $status 0 商品下架
  335. */
  336. public function changeStatus(int $productId, $status = 0)
  337. {
  338. $this->dao->update($productId, ['status' => $status], 'product_id');
  339. }
  340. /**
  341. * 获取购物车列表
  342. * @param int $uid
  343. * @param int $status
  344. * @return array
  345. * @throws \think\db\exception\DataNotFoundException
  346. * @throws \think\db\exception\DbException
  347. * @throws \think\db\exception\ModelNotFoundException
  348. */
  349. public function getUserCartList(int $uid, int $status, string $cartIds = '')
  350. {
  351. [$page, $limit] = $this->getPageValue();
  352. $list = $this->dao->getCartList(['uid' => $uid, 'status' => $status, 'id' => $cartIds], $page, $limit, ['productInfo', 'attrInfo']);
  353. [$list, $valid, $invalid] = $this->handleCartList($uid, $list);
  354. $seckillIds = array_unique(array_column($list, 'seckill_id'));
  355. $bargainIds = array_unique(array_column($list, 'bargain_id'));
  356. $combinationId = array_unique(array_column($list, 'combination_id'));
  357. $discountId = array_unique(array_column($list, 'discount_id'));
  358. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0, 'discount_id' => $discountId[0] ?? 0];
  359. if ($status == 1) {
  360. return ['valid' => $list, 'invalid' => [], 'deduction' => $deduction];
  361. } else {
  362. return ['valid' => [], 'invalid' => $list, 'deduction' => $deduction];
  363. }
  364. }
  365. /**
  366. * 购物车重选
  367. * @param int $cart_id
  368. * @param int $product_id
  369. * @param string $unique
  370. */
  371. public function modifyCart(int $cart_id, int $product_id, string $unique)
  372. {
  373. /** @var StoreProductAttrValueServices $attrService */
  374. $attrService = app()->make(StoreProductAttrValueServices::class);
  375. $stock = $attrService->value(['product_id' => $product_id, 'unique' => $unique, 'type' => 0], 'stock');
  376. if ($stock > 0) {
  377. $this->dao->update($cart_id, ['product_attr_unique' => $unique, 'cart_num' => 1]);
  378. } else {
  379. throw new ApiException(410238);
  380. }
  381. }
  382. /**
  383. * 重选购物车
  384. * @param $id
  385. * @param $uid
  386. * @param $productId
  387. * @param $unique
  388. * @param $num
  389. * @throws \think\db\exception\DataNotFoundException
  390. * @throws \think\db\exception\DbException
  391. * @throws \think\db\exception\ModelNotFoundException
  392. */
  393. public function resetCart($id, $uid, $productId, $unique, $num)
  394. {
  395. $res = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  396. if ($res) {
  397. $res->cart_num = $res->cart_num + $num;
  398. $res->save();
  399. $this->dao->delete($id);
  400. } else {
  401. $this->dao->update($id, ['product_attr_unique' => $unique, 'cart_num' => $num]);
  402. }
  403. }
  404. /**
  405. * 首页加入购物车
  406. * @param $uid
  407. * @param $productId
  408. * @param $num
  409. * @param $unique
  410. * @param $type
  411. * @return mixed
  412. * @throws \think\db\exception\DataNotFoundException
  413. * @throws \think\db\exception\DbException
  414. * @throws \think\db\exception\ModelNotFoundException
  415. */
  416. public function setCartNum($uid, $productId, $num, $unique, $type)
  417. {
  418. if ($type == 1) {
  419. //检查限购
  420. $this->checkLimit($uid, $productId, $num, 0);
  421. }
  422. /** @var StoreProductAttrValueServices $attrValueServices */
  423. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  424. if ($unique == '') {
  425. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  426. }
  427. /** @var StoreProductServices $productServices */
  428. $productServices = app()->make(StoreProductServices::class);
  429. if (!$productServices->isValidProduct((int)$productId, 'id')) {
  430. throw new ApiException(410295);
  431. }
  432. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  433. throw new ApiException(410305);
  434. }
  435. if ($productServices->getProductStock((int)$productId, $unique) < $num) {
  436. throw new ApiException(410297, ['num' => $num]);
  437. }
  438. $cart = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  439. if ($cart) {
  440. if ($type == -1) {
  441. $cart->cart_num = $num;
  442. } elseif ($type == 0) {
  443. $cart->cart_num = $cart->cart_num - $num;
  444. } elseif ($type == 1) {
  445. $cart->cart_num = $cart->cart_num + $num;
  446. }
  447. if ($cart->cart_num === 0) {
  448. return $this->dao->delete($cart->id);
  449. } else {
  450. $cart->add_time = time();
  451. $cart->save();
  452. return $cart->id;
  453. }
  454. } else {
  455. $data = [
  456. 'uid' => $uid,
  457. 'product_id' => $productId,
  458. 'cart_num' => $num,
  459. 'product_attr_unique' => $unique,
  460. 'type' => 0,
  461. 'add_time' => time()
  462. ];
  463. return $this->dao->save($data)->id;
  464. }
  465. }
  466. /**
  467. * 获取用户购物车数量 ids 统计金额
  468. * @param int $uid
  469. * @param string $numType
  470. * @throws \think\db\exception\DataNotFoundException
  471. * @throws \think\db\exception\DbException
  472. * @throws \think\db\exception\ModelNotFoundException
  473. */
  474. public function getUserCartCount(int $uid, string $numType)
  475. {
  476. $count = 0;
  477. $ids = [];
  478. $sum_price = 0;
  479. $cartList = $this->dao->getUserCartList($uid, '*', ['productInfo', 'attrInfo']);
  480. if ($cartList) {
  481. /** @var StoreProductServices $productServices */
  482. $productServices = app()->make(StoreProductServices::class);
  483. /** @var MemberCardServices $memberCardService */
  484. $memberCardService = app()->make(MemberCardServices::class);
  485. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  486. /** @var UserServices $user */
  487. $user = app()->make(UserServices::class);
  488. $userInfo = $user->getUserInfo($uid);
  489. $discount = 100;
  490. if (sys_config('member_func_status', 1)) {
  491. /** @var SystemUserLevelServices $systemLevel */
  492. $systemLevel = app()->make(SystemUserLevelServices::class);
  493. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  494. }
  495. foreach ($cartList as &$item) {
  496. $productInfo = $item['productInfo'];
  497. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  498. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  499. $item['truePrice'] = $truePrice;
  500. $item['price_type'] = $type;
  501. } else {
  502. [$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);
  503. $item['truePrice'] = $truePrice;
  504. $item['price_type'] = $type;
  505. }
  506. $sum_price = bcadd((string)$sum_price, (string)bcmul((string)$item['cart_num'], (string)$item['truePrice'], 4), 2);
  507. }
  508. $ids = array_column($cartList, 'id');
  509. if ($numType) {
  510. $count = count($cartList);
  511. } else {
  512. $count = array_sum(array_column($cartList, 'cart_num'));
  513. }
  514. }
  515. return compact('count', 'ids', 'sum_price');
  516. }
  517. /**
  518. * 处理购物车数据
  519. * @param int $uid
  520. * @param array $cartList
  521. * @param array $addr
  522. * @param int $shipping_type
  523. * @return array
  524. * @throws \think\db\exception\DataNotFoundException
  525. * @throws \think\db\exception\DbException
  526. * @throws \think\db\exception\ModelNotFoundException
  527. * @author 吴汐
  528. * @email 442384644@qq.com
  529. * @date 2023/02/16
  530. */
  531. public function handleCartList(int $uid, array $cartList, array $addr = [], int $shipping_type = 1)
  532. {
  533. if (!$cartList) {
  534. return [$cartList, [], []];
  535. }
  536. /** @var StoreProductServices $productServices */
  537. $productServices = app()->make(StoreProductServices::class);
  538. /** @var MemberCardServices $memberCardService */
  539. $memberCardService = app()->make(MemberCardServices::class);
  540. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  541. $tempIds = [];
  542. $userInfo = [];
  543. $discount = 100;
  544. if ($uid) {
  545. /** @var UserServices $user */
  546. $user = app()->make(UserServices::class);
  547. $userInfo = $user->getUserInfo($uid);
  548. //用户等级是否开启
  549. if (sys_config('member_func_status', 1)) {
  550. /** @var SystemUserLevelServices $systemLevel */
  551. $systemLevel = app()->make(SystemUserLevelServices::class);
  552. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  553. }
  554. }
  555. //不送达运费模板
  556. if ($shipping_type == 1 && $addr) {
  557. $cityId = (int)($addr['city_id'] ?? 0);
  558. if ($cityId) {
  559. foreach ($cartList as $item) {
  560. $tempIds[] = $item['productInfo']['temp_id'];
  561. }
  562. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  563. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  564. $tempIds = $noDeliveryServices->isNoDelivery(array_unique($tempIds), $cityId);
  565. }
  566. }
  567. $valid = $invalid = [];
  568. foreach ($cartList as &$item) {
  569. $item['productInfo']['express_delivery'] = false;
  570. $item['productInfo']['store_mention'] = false;
  571. if (isset($item['productInfo']['logistics'])) {
  572. if (in_array(1, explode(',', $item['productInfo']['logistics']))) {
  573. $item['productInfo']['express_delivery'] = true;
  574. }
  575. if (in_array(2, explode(',', $item['productInfo']['logistics']))) {
  576. $item['productInfo']['store_mention'] = true;
  577. }
  578. }
  579. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  580. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  581. }
  582. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  583. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  584. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  585. if (isset($item['productInfo']['attrInfo'])) {
  586. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  587. }
  588. $item['productInfo'] = get_thumb_water($item['productInfo']);
  589. $productInfo = $item['productInfo'];
  590. $item['vip_truePrice'] = 0;
  591. $is_activity = $item['seckill_id'] || $item['bargain_id'] || $item['combination_id'] || $item['advance_id'];
  592. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  593. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  594. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  595. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  596. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  597. if (!$is_activity) {
  598. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  599. $item['truePrice'] = $truePrice;
  600. $item['vip_truePrice'] = $vip_truePrice;
  601. $item['price_type'] = $type;
  602. } else {
  603. $item['price_type'] = 'activity';
  604. }
  605. } else {
  606. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  607. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  608. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  609. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  610. if (!$is_activity) {
  611. [$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);
  612. $item['truePrice'] = $truePrice;
  613. $item['vip_truePrice'] = $vip_truePrice;
  614. $item['price_type'] = $type;
  615. } else {
  616. $item['price_type'] = 'activity';
  617. }
  618. }
  619. if (isset($item['status']) && $item['status'] == 0) {
  620. $item['is_valid'] = 0;
  621. $invalid[] = $item;
  622. } else {
  623. switch ($shipping_type) {
  624. case 1:
  625. //不送达
  626. if (in_array($item['productInfo']['temp_id'], $tempIds) || (isset($item['productInfo']['logistics']) && !in_array(1, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0)) {
  627. $item['is_valid'] = 0;
  628. $invalid[] = $item;
  629. } else {
  630. $item['is_valid'] = 1;
  631. $valid[] = $item;
  632. }
  633. break;
  634. case 2:
  635. //不支持到店自提
  636. if (isset($item['productInfo']['logistics']) && $item['productInfo']['logistics'] && !in_array(2, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0) {
  637. $item['is_valid'] = 0;
  638. $invalid[] = $item;
  639. } else {
  640. $item['is_valid'] = 1;
  641. $valid[] = $item;
  642. }
  643. break;
  644. }
  645. }
  646. unset($item['attrInfo']);
  647. }
  648. return [$cartList, $valid, $invalid];
  649. }
  650. /**
  651. * 检查限购
  652. * @param $uid
  653. * @param $product_id
  654. * @param $num
  655. * @param $new
  656. * @return bool
  657. * @throws \ReflectionException
  658. */
  659. public function checkLimit($uid, $product_id, $num, $new)
  660. {
  661. /** @var StoreProductServices $productServices */
  662. $productServices = app()->make(StoreProductServices::class);
  663. /** @var StoreOrderCartInfoServices $orderCartServices */
  664. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  665. $limitInfo = $productServices->get($product_id, ['is_limit', 'limit_type', 'limit_num']);
  666. if (!$limitInfo) throw new ApiException(410294);
  667. $limitInfo = $limitInfo->toArray();
  668. if (!$limitInfo['is_limit']) return true;
  669. if ($limitInfo['limit_type'] == 1) {
  670. $cartNum = 0;
  671. if (!$new) {
  672. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  673. }
  674. if (($num + $cartNum) > $limitInfo['limit_num']) {
  675. throw new ApiException(410239, ['limit' => $limitInfo['limit_num']]);
  676. }
  677. } else if ($limitInfo['limit_type'] == 2) {
  678. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  679. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id, 'split_status' => 0], 'cart_num');
  680. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id, 'split_status' => 0], 'refund_num');
  681. $orderNum = $orderPayNum - $orderRefundNum;
  682. if (($num + $orderNum + $cartNum) > $limitInfo['limit_num']) {
  683. throw new ApiException(410240, ['limit' => $limitInfo['limit_num'], 'pay_num' => $orderNum]);
  684. }
  685. }
  686. return true;
  687. }
  688. }