StoreCartServices.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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\activity\coupon\StoreCouponIssueUserServices;
  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. $redis = CacheService::redisHandler();
  86. foreach ($cartIds as $key) {
  87. $info = $redis->get($key);
  88. if ($info) {
  89. $cartInfo[] = $info;
  90. }
  91. }
  92. } else {
  93. $cartInfo = $this->dao->getCartList(['uid' => $uid, 'status' => 1, 'id' => $cartIds], 0, 0, ['productInfo', 'attrInfo']);
  94. }
  95. if (!$cartInfo) {
  96. throw new ApiException(410233);
  97. }
  98. [$cartInfo, $valid, $invalid] = $this->handleCartList($uid, $cartInfo, $addr, $shipping_type);
  99. $seckillIds = array_unique(array_column($cartInfo, 'seckill_id'));
  100. $bargainIds = array_unique(array_column($cartInfo, 'bargain_id'));
  101. $combinationId = array_unique(array_column($cartInfo, 'combination_id'));
  102. $advanceId = array_unique(array_column($cartInfo, 'advance_id'));
  103. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0, 'advance_id' => $advanceId[0] ?? 0];
  104. return ['cartInfo' => $cartInfo, 'valid' => $valid, 'invalid' => $invalid, 'deduction' => $deduction];
  105. }
  106. /**
  107. * 使用雪花算法生成订单ID
  108. * @return string
  109. * @throws \Exception
  110. */
  111. public function getCartId($prefix)
  112. {
  113. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  114. //32位
  115. if (PHP_INT_SIZE == 4) {
  116. $id = abs((int)$snowflake->id());
  117. } else {
  118. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->id();
  119. }
  120. return $prefix . $id;
  121. }
  122. /**
  123. * 验证库存
  124. * @param int $uid
  125. * @param int $cartNum
  126. * @param string $unique
  127. * @param int $type
  128. * @param $productId
  129. * @param int $seckillId
  130. * @param int $bargainId
  131. * @param int $combinationId
  132. * @return array
  133. * @throws \Psr\SimpleCache\InvalidArgumentException
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function checkProductStock(int $uid, int $cartNum, string $unique, int $type = 0, $productId, int $seckillId, int $bargainId, int $combinationId, int $advanceId)
  139. {
  140. /** @var StoreProductAttrValueServices $attrValueServices */
  141. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  142. switch ($type) {
  143. case 0://普通
  144. if ($unique == '') {
  145. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  146. }
  147. /** @var StoreProductServices $productServices */
  148. $productServices = app()->make(StoreProductServices::class);
  149. $productInfo = $productServices->isValidProduct($productId);
  150. if (!$productInfo) {
  151. throw new ApiException(410295);
  152. }
  153. $attrInfo = $attrValueServices->getOne(['unique' => $unique, 'type' => 0]);
  154. if (!$unique || !$attrInfo || $attrInfo['product_id'] != $productId) {
  155. throw new ApiException(410305);
  156. }
  157. $nowStock = $attrInfo['stock'];//现有库存
  158. if ($cartNum > $nowStock) {
  159. throw new ApiException(410297, ['num' => $cartNum]);
  160. }
  161. if ($productInfo['is_virtual'] == 1 && $productInfo['virtual_type'] == 2 && $attrInfo['coupon_id']) {
  162. /** @var StoreCouponIssueServices $issueCoupon */
  163. $issueCoupon = app()->make(StoreCouponIssueServices::class);
  164. if (!$issueCoupon->getCount(['id' => $attrInfo['coupon_id'], 'status' => 1, 'is_del' => 0])) {
  165. throw new ApiException(410234);
  166. }
  167. /** @var StoreCouponIssueUserServices $issueUserCoupon */
  168. $issueUserCoupon = app()->make(StoreCouponIssueUserServices::class);
  169. if ($issueUserCoupon->getCount(['uid' => $uid, 'issue_coupon_id' => $attrInfo['coupon_id']])) {
  170. throw new ApiException(410235);
  171. }
  172. }
  173. $stockNum = $this->dao->value(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], 'cart_num') ?: 0;
  174. if ($nowStock < ($cartNum + $stockNum)) {
  175. $surplusStock = $nowStock - $cartNum;//剩余库存
  176. if ($surplusStock < $stockNum) {
  177. $this->dao->update(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], ['cart_num' => $surplusStock]);
  178. }
  179. }
  180. break;
  181. case 1://秒杀
  182. /** @var StoreSeckillServices $seckillService */
  183. $seckillService = app()->make(StoreSeckillServices::class);
  184. [$attrInfo, $unique, $productInfo] = $seckillService->checkSeckillStock($uid, $seckillId, $cartNum, $unique);
  185. break;
  186. case 2://砍价
  187. /** @var StoreBargainServices $bargainService */
  188. $bargainService = app()->make(StoreBargainServices::class);
  189. [$attrInfo, $unique, $productInfo, $bargainUserInfo] = $bargainService->checkBargainStock($uid, $bargainId, $cartNum, $unique);
  190. break;
  191. case 3://拼团
  192. /** @var StoreCombinationServices $combinationService */
  193. $combinationService = app()->make(StoreCombinationServices::class);
  194. [$attrInfo, $unique, $productInfo] = $combinationService->checkCombinationStock($uid, $combinationId, $cartNum, $unique);
  195. break;
  196. case 6://预售
  197. /** @var StoreAdvanceServices $advanceService */
  198. $advanceService = app()->make(StoreAdvanceServices::class);
  199. [$attrInfo, $unique, $productInfo] = $advanceService->checkAdvanceStock($uid, $advanceId, $cartNum, $unique);
  200. break;
  201. default:
  202. throw new ApiException(410236);
  203. }
  204. if ($type && $type != 6) {
  205. //根商品规格库存
  206. $product_stock = $attrValueServices->value(['product_id' => $productInfo['product_id'], 'suk' => $attrInfo['suk'], 'type' => 0], 'stock');
  207. if ($product_stock < $cartNum) {
  208. throw new ApiException(410297, ['num' => $cartNum]);
  209. }
  210. if ($type != 5 && !CacheService::checkStock($unique, (int)$cartNum, $type)) {
  211. throw new ApiException(410297, ['num' => $cartNum]);
  212. }
  213. }
  214. return [$attrInfo, $unique, $bargainUserInfo['bargain_price_min'] ?? 0, $cartNum, $productInfo];
  215. }
  216. /**
  217. * 添加购物车
  218. * @param int $uid 用户UID
  219. * @param int $product_id 商品ID
  220. * @param int $cart_num 商品数量
  221. * @param string $product_attr_unique 商品SKU
  222. * @param string $type 添加购物车类型
  223. * @param bool $new true = 立即购买,false = 加入购物车
  224. * @param int $combination_id 拼团商品ID
  225. * @param int $seckill_id 秒杀商品ID
  226. * @param int $bargain_id 砍价商品ID
  227. * @return mixed|string
  228. * @throws \Psr\SimpleCache\InvalidArgumentException
  229. * @throws \think\db\exception\DataNotFoundException
  230. * @throws \think\db\exception\DbException
  231. * @throws \think\db\exception\ModelNotFoundException
  232. */
  233. 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)
  234. {
  235. if ($cart_num < 1) $cart_num = 1;
  236. //检查限购
  237. $this->checkLimit($uid, $product_id, $cart_num, $new);
  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['attrInfo'] = $attrInfo->toArray();
  256. $info['sum_price'] = $info['productInfo']['attrInfo']['price'];
  257. //砍价
  258. if ($bargain_id) {
  259. $info['truePrice'] = $bargainPriceMin;
  260. $info['productInfo']['attrInfo']['price'] = $bargainPriceMin;
  261. } else {
  262. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? $info['productInfo']['price'] ?? 0;
  263. }
  264. //拼团砍价秒杀不参与会员价
  265. if ($bargain_id || $combination_id || $seckill_id || $advance_id) {
  266. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? 0;
  267. $info['vip_truePrice'] = 0;
  268. }
  269. $info['trueStock'] = $info['productInfo']['attrInfo']['stock'];
  270. $info['costPrice'] = $info['productInfo']['attrInfo']['cost'];
  271. try {
  272. CacheService::redisHandler()->set($key, $info, 3600);
  273. } catch (\Throwable $e) {
  274. throw new ApiException($e->getMessage());
  275. }
  276. return $key;
  277. } else {//加入购物车记录
  278. ProductLogJob::dispatch(['cart', ['uid' => $uid, 'product_id' => $product_id, 'cart_num' => $cart_num]]);
  279. $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]);
  280. if ($cart) {
  281. $cart->cart_num = $cart_num + $cart->cart_num;
  282. $cart->add_time = time();
  283. $cart->save();
  284. return $cart->id;
  285. } else {
  286. $add_time = time();
  287. return $this->dao->save(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'type', 'add_time'))->id;
  288. }
  289. }
  290. }
  291. /**移除购物车商品
  292. * @param int $uid
  293. * @param array $ids
  294. * @return StoreCartDao|bool
  295. */
  296. public function removeUserCart(int $uid, array $ids)
  297. {
  298. if (!$uid || !$ids) return false;
  299. return $this->dao->removeUserCart($uid, $ids);
  300. }
  301. /**购物车 修改商品数量
  302. * @param $id
  303. * @param $number
  304. * @param $uid
  305. * @return bool|\crmeb\basic\BaseModel
  306. * @throws \think\db\exception\DataNotFoundException
  307. * @throws \think\db\exception\DbException
  308. * @throws \think\db\exception\ModelNotFoundException
  309. */
  310. public function changeUserCartNum($id, $number, $uid)
  311. {
  312. if (!$id || !$number || !$uid) return false;
  313. $where = ['uid' => $uid, 'id' => $id];
  314. $carInfo = $this->dao->getOne($where, 'product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num');
  315. /** @var StoreProductServices $StoreProduct */
  316. $StoreProduct = app()->make(StoreProductServices::class);
  317. $stock = $StoreProduct->getProductStock($carInfo->product_id, $carInfo->product_attr_unique);
  318. if (!$stock) throw new ApiException(410237);
  319. if ($stock < $number) throw new ApiException(410297, ['num' => $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 ApiException(410238);
  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 ApiException(410295);
  419. }
  420. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  421. throw new ApiException(410305);
  422. }
  423. if ($productServices->getProductStock((int)$productId, $unique) < $num) {
  424. throw new ApiException(410297, ['num' => $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, array $addr = [], int $shipping_type = 1)
  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. //不送达运费模板
  537. if ($shipping_type == 1 && $addr) {
  538. $cityId = (int)($addr['city_id'] ?? 0);
  539. if ($cityId) {
  540. foreach ($cartList as $item) {
  541. $tempIds[] = $item['productInfo']['temp_id'];
  542. }
  543. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  544. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  545. $tempIds = $noDeliveryServices->isNoDelivery(array_unique($tempIds), $cityId);
  546. }
  547. }
  548. $valid = $invalid = [];
  549. foreach ($cartList as &$item) {
  550. $item['productInfo']['express_delivery'] = false;
  551. $item['productInfo']['store_mention'] = false;
  552. if (isset($item['productInfo']['logistics'])) {
  553. if (in_array(1, explode(',', $item['productInfo']['logistics']))) {
  554. $item['productInfo']['express_delivery'] = true;
  555. }
  556. if (in_array(2, explode(',', $item['productInfo']['logistics']))) {
  557. $item['productInfo']['store_mention'] = true;
  558. }
  559. }
  560. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  561. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  562. }
  563. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  564. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  565. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  566. if (isset($item['productInfo']['attrInfo'])) {
  567. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  568. }
  569. $item['productInfo'] = get_thumb_water($item['productInfo']);
  570. $productInfo = $item['productInfo'];
  571. $item['vip_truePrice'] = 0;
  572. $is_activity = $item['seckill_id'] || $item['bargain_id'] || $item['combination_id'] || $item['advance_id'];
  573. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  574. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  575. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  576. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  577. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  578. if (!$is_activity) {
  579. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  580. $item['truePrice'] = $truePrice;
  581. $item['vip_truePrice'] = $vip_truePrice;
  582. $item['price_type'] = $type;
  583. }
  584. } else {
  585. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  586. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  587. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  588. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  589. if (!$is_activity) {
  590. [$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);
  591. $item['truePrice'] = $truePrice;
  592. $item['vip_truePrice'] = $vip_truePrice;
  593. $item['price_type'] = $type;
  594. }
  595. }
  596. if (isset($item['status']) && $item['status'] == 0) {
  597. $item['is_valid'] = 0;
  598. $invalid[] = $item;
  599. } else {
  600. switch ($shipping_type) {
  601. case 1:
  602. //不送达
  603. if (in_array($item['productInfo']['temp_id'], $tempIds) || (isset($item['productInfo']['logistics']) && !in_array(1, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0)) {
  604. $item['is_valid'] = 0;
  605. $invalid[] = $item;
  606. } else {
  607. $item['is_valid'] = 1;
  608. $valid[] = $item;
  609. }
  610. break;
  611. case 2:
  612. //不支持到店自提
  613. if (isset($item['productInfo']['logistics']) && $item['productInfo']['logistics'] && !in_array(2, 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. }
  622. }
  623. unset($item['attrInfo']);
  624. }
  625. return [$cartList, $valid, $invalid];
  626. }
  627. /**
  628. * 检查限购
  629. * @param $uid
  630. * @param $product_id
  631. * @param $num
  632. * @param $new
  633. * @return bool
  634. */
  635. public function checkLimit($uid, $product_id, $num, $new)
  636. {
  637. /** @var StoreProductServices $productServices */
  638. $productServices = app()->make(StoreProductServices::class);
  639. /** @var StoreOrderCartInfoServices $orderCartServices */
  640. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  641. $limitInfo = $productServices->get($product_id, ['is_limit', 'limit_type', 'limit_num']);
  642. if (!$limitInfo) throw new ApiException(410294);
  643. $limitInfo = $limitInfo->toArray();
  644. if (!$limitInfo['is_limit']) return true;
  645. if ($limitInfo['limit_type'] == 1) {
  646. $cartNum = 0;
  647. if (!$new) {
  648. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  649. }
  650. if (($num + $cartNum) > $limitInfo['limit_num']) {
  651. throw new ApiException(410239, ['limit' => $limitInfo['limit_num']]);
  652. }
  653. } else if ($limitInfo['limit_type'] == 2) {
  654. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  655. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  656. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id], 'refund_num');
  657. $orderNum = $orderPayNum - $orderRefundNum;
  658. if (($num + $orderNum + $cartNum) > $limitInfo['limit_num']) {
  659. throw new ApiException(410240, ['limit' => $limitInfo['limit_num'], 'pay_num' => $orderNum]);
  660. }
  661. }
  662. return true;
  663. }
  664. }