StoreCartServices.php 32 KB

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