StoreCartServices.php 33 KB

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