StoreCartServices.php 29 KB

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