StoreCartServices.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. foreach ($cartIds as $key) {
  85. $info = CacheService::get($key);
  86. if ($info) {
  87. $cartInfo[] = $info;
  88. }
  89. }
  90. } else {
  91. $cartInfo = $this->dao->getCartList(['uid' => $uid, 'status' => 1, 'id' => $cartIds], 0, 0, ['productInfo', 'attrInfo']);
  92. }
  93. if (!$cartInfo) {
  94. throw new ApiException(410233);
  95. }
  96. [$cartInfo, $valid, $invalid] = $this->handleCartList($uid, $cartInfo, $addr, $shipping_type);
  97. $seckillIds = array_unique(array_column($cartInfo, 'seckill_id'));
  98. $bargainIds = array_unique(array_column($cartInfo, 'bargain_id'));
  99. $combinationId = array_unique(array_column($cartInfo, 'combination_id'));
  100. $advanceId = array_unique(array_column($cartInfo, 'advance_id'));
  101. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0, 'advance_id' => $advanceId[0] ?? 0];
  102. return ['cartInfo' => $cartInfo, 'valid' => $valid, 'invalid' => $invalid, 'deduction' => $deduction];
  103. }
  104. /**
  105. * 使用雪花算法生成订单ID
  106. * @return string
  107. * @throws \Exception
  108. */
  109. public function getCartId($prefix)
  110. {
  111. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  112. //32位
  113. if (PHP_INT_SIZE == 4) {
  114. $id = abs((int)$snowflake->id());
  115. } else {
  116. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->id();
  117. }
  118. return $prefix . $id;
  119. }
  120. /**
  121. * 验证库存
  122. * @param int $uid
  123. * @param int $cartNum
  124. * @param string $unique
  125. * @param int $type
  126. * @param $productId
  127. * @param int $seckillId
  128. * @param int $bargainId
  129. * @param int $combinationId
  130. * @return array
  131. * @throws \Psr\SimpleCache\InvalidArgumentException
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function checkProductStock(int $uid, int $cartNum, string $unique, int $type = 0, $productId, int $seckillId, int $bargainId, int $combinationId, int $advanceId)
  137. {
  138. /** @var StoreProductAttrValueServices $attrValueServices */
  139. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  140. switch ($type) {
  141. case 0://普通
  142. if ($unique == '') {
  143. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  144. }
  145. /** @var StoreProductServices $productServices */
  146. $productServices = app()->make(StoreProductServices::class);
  147. $productInfo = $productServices->isValidProduct($productId);
  148. if (!$productInfo) {
  149. throw new ApiException(410295);
  150. }
  151. $attrInfo = $attrValueServices->getOne(['unique' => $unique, 'type' => 0]);
  152. if (!$unique || !$attrInfo || $attrInfo['product_id'] != $productId) {
  153. throw new ApiException(410305);
  154. }
  155. $nowStock = $attrInfo['stock'];//现有库存
  156. if ($cartNum > $nowStock) {
  157. throw new ApiException(410297, ['num' => $cartNum]);
  158. }
  159. if ($productInfo['is_virtual'] == 1 && $productInfo['virtual_type'] == 2 && $attrInfo['coupon_id']) {
  160. /** @var StoreCouponIssueServices $issueCoupon */
  161. $issueCoupon = app()->make(StoreCouponIssueServices::class);
  162. if (!$issueCoupon->getCount(['id' => $attrInfo['coupon_id'], 'status' => 1, 'is_del' => 0])) {
  163. throw new ApiException(410234);
  164. }
  165. }
  166. $stockNum = $this->dao->value(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], 'cart_num') ?: 0;
  167. if ($nowStock < ($cartNum + $stockNum)) {
  168. $surplusStock = $nowStock - $cartNum;//剩余库存
  169. if ($surplusStock < $stockNum) {
  170. $this->dao->update(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], ['cart_num' => $surplusStock]);
  171. }
  172. }
  173. break;
  174. case 1://秒杀
  175. /** @var StoreSeckillServices $seckillService */
  176. $seckillService = app()->make(StoreSeckillServices::class);
  177. [$attrInfo, $unique, $productInfo] = $seckillService->checkSeckillStock($uid, $seckillId, $cartNum, $unique);
  178. break;
  179. case 2://砍价
  180. /** @var StoreBargainServices $bargainService */
  181. $bargainService = app()->make(StoreBargainServices::class);
  182. [$attrInfo, $unique, $productInfo, $bargainUserInfo] = $bargainService->checkBargainStock($uid, $bargainId, $cartNum, $unique);
  183. break;
  184. case 3://拼团
  185. /** @var StoreCombinationServices $combinationService */
  186. $combinationService = app()->make(StoreCombinationServices::class);
  187. [$attrInfo, $unique, $productInfo] = $combinationService->checkCombinationStock($uid, $combinationId, $cartNum, $unique);
  188. break;
  189. case 6://预售
  190. /** @var StoreAdvanceServices $advanceService */
  191. $advanceService = app()->make(StoreAdvanceServices::class);
  192. [$attrInfo, $unique, $productInfo] = $advanceService->checkAdvanceStock($uid, $advanceId, $cartNum, $unique);
  193. break;
  194. default:
  195. throw new ApiException(410236);
  196. }
  197. if ($type && $type != 6) {
  198. //根商品规格库存
  199. $product_stock = $attrValueServices->value(['product_id' => $productInfo['product_id'], 'suk' => $attrInfo['suk'], 'type' => 0], 'stock');
  200. if ($product_stock < $cartNum) {
  201. throw new ApiException(410297, ['num' => $cartNum]);
  202. }
  203. if ($type != 5 && !CacheService::checkStock($unique, (int)$cartNum, $type)) {
  204. throw new ApiException(410297, ['num' => $cartNum]);
  205. }
  206. }
  207. return [$attrInfo, $unique, $bargainUserInfo['bargain_price_min'] ?? 0, $cartNum, $productInfo];
  208. }
  209. /**
  210. * 添加购物车
  211. * @param int $uid 用户UID
  212. * @param int $product_id 商品ID
  213. * @param int $cart_num 商品数量
  214. * @param string $product_attr_unique 商品SKU
  215. * @param string $type 添加购物车类型
  216. * @param bool $new true = 立即购买,false = 加入购物车
  217. * @param int $combination_id 拼团商品ID
  218. * @param int $seckill_id 秒杀商品ID
  219. * @param int $bargain_id 砍价商品ID
  220. * @return mixed|string
  221. * @throws \Psr\SimpleCache\InvalidArgumentException
  222. * @throws \think\db\exception\DataNotFoundException
  223. * @throws \think\db\exception\DbException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. */
  226. 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)
  227. {
  228. if ($cart_num < 1) $cart_num = 1;
  229. //检查限购
  230. $this->checkLimit($uid, $product_id, $cart_num, $new);
  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. /** @var StoreProductAttrValueServices $attrValueServices */
  420. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  421. if ($unique == '') {
  422. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  423. }
  424. /** @var StoreProductServices $productServices */
  425. $productServices = app()->make(StoreProductServices::class);
  426. if (!$productServices->isValidProduct((int)$productId)) {
  427. throw new ApiException(410295);
  428. }
  429. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  430. throw new ApiException(410305);
  431. }
  432. if ($productServices->getProductStock((int)$productId, $unique) < $num) {
  433. throw new ApiException(410297, ['num' => $num]);
  434. }
  435. $cart = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  436. if ($cart) {
  437. if ($type == -1) {
  438. $cart->cart_num = $num;
  439. } elseif ($type == 0) {
  440. $cart->cart_num = $cart->cart_num - $num;
  441. } elseif ($type == 1) {
  442. $cart->cart_num = $cart->cart_num + $num;
  443. }
  444. if ($cart->cart_num === 0) {
  445. return $this->dao->delete($cart->id);
  446. } else {
  447. $cart->add_time = time();
  448. $cart->save();
  449. return $cart->id;
  450. }
  451. } else {
  452. $data = [
  453. 'uid' => $uid,
  454. 'product_id' => $productId,
  455. 'cart_num' => $num,
  456. 'product_attr_unique' => $unique,
  457. 'type' => 0,
  458. 'add_time' => time()
  459. ];
  460. return $this->dao->save($data)->id;
  461. }
  462. }
  463. /**
  464. * 获取用户购物车数量 ids 统计金额
  465. * @param int $uid
  466. * @param string $numType
  467. * @throws \think\db\exception\DataNotFoundException
  468. * @throws \think\db\exception\DbException
  469. * @throws \think\db\exception\ModelNotFoundException
  470. */
  471. public function getUserCartCount(int $uid, string $numType)
  472. {
  473. $count = 0;
  474. $ids = [];
  475. $sum_price = 0;
  476. $cartList = $this->dao->getUserCartList($uid, '*', ['productInfo', 'attrInfo']);
  477. if ($cartList) {
  478. /** @var StoreProductServices $productServices */
  479. $productServices = app()->make(StoreProductServices::class);
  480. /** @var MemberCardServices $memberCardService */
  481. $memberCardService = app()->make(MemberCardServices::class);
  482. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  483. /** @var UserServices $user */
  484. $user = app()->make(UserServices::class);
  485. $userInfo = $user->getUserInfo($uid);
  486. $discount = 100;
  487. if (sys_config('member_func_status', 1)) {
  488. /** @var SystemUserLevelServices $systemLevel */
  489. $systemLevel = app()->make(SystemUserLevelServices::class);
  490. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  491. }
  492. foreach ($cartList as &$item) {
  493. $productInfo = $item['productInfo'];
  494. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  495. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  496. $item['truePrice'] = $truePrice;
  497. $item['price_type'] = $type;
  498. } else {
  499. [$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);
  500. $item['truePrice'] = $truePrice;
  501. $item['price_type'] = $type;
  502. }
  503. $sum_price = bcadd((string)$sum_price, (string)bcmul((string)$item['cart_num'], (string)$item['truePrice'], 4), 2);
  504. }
  505. $ids = array_column($cartList, 'id');
  506. if ($numType) {
  507. $count = count($cartList);
  508. } else {
  509. $count = array_sum(array_column($cartList, 'cart_num'));
  510. }
  511. }
  512. return compact('count', 'ids', 'sum_price');
  513. }
  514. /**
  515. * 处理购物车数据
  516. * @param int $uid
  517. * @param array $cartList
  518. * @param array $addr
  519. * @return array
  520. */
  521. public function handleCartList(int $uid, array $cartList, array $addr = [], int $shipping_type = 1)
  522. {
  523. if (!$cartList) {
  524. return [$cartList, [], []];
  525. }
  526. /** @var StoreProductServices $productServices */
  527. $productServices = app()->make(StoreProductServices::class);
  528. /** @var MemberCardServices $memberCardService */
  529. $memberCardService = app()->make(MemberCardServices::class);
  530. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  531. $tempIds = [];
  532. $userInfo = [];
  533. $discount = 100;
  534. if ($uid) {
  535. /** @var UserServices $user */
  536. $user = app()->make(UserServices::class);
  537. $userInfo = $user->getUserInfo($uid);
  538. //用户等级是否开启
  539. if (sys_config('member_func_status', 1)) {
  540. /** @var SystemUserLevelServices $systemLevel */
  541. $systemLevel = app()->make(SystemUserLevelServices::class);
  542. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  543. }
  544. }
  545. //不送达运费模板
  546. if ($shipping_type == 1 && $addr) {
  547. $cityId = (int)($addr['city_id'] ?? 0);
  548. if ($cityId) {
  549. foreach ($cartList as $item) {
  550. $tempIds[] = $item['productInfo']['temp_id'];
  551. }
  552. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  553. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  554. $tempIds = $noDeliveryServices->isNoDelivery(array_unique($tempIds), $cityId);
  555. }
  556. }
  557. $valid = $invalid = [];
  558. foreach ($cartList as &$item) {
  559. $item['productInfo']['express_delivery'] = false;
  560. $item['productInfo']['store_mention'] = false;
  561. if (isset($item['productInfo']['logistics'])) {
  562. if (in_array(1, explode(',', $item['productInfo']['logistics']))) {
  563. $item['productInfo']['express_delivery'] = true;
  564. }
  565. if (in_array(2, explode(',', $item['productInfo']['logistics']))) {
  566. $item['productInfo']['store_mention'] = true;
  567. }
  568. }
  569. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  570. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  571. }
  572. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  573. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  574. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  575. if (isset($item['productInfo']['attrInfo'])) {
  576. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  577. }
  578. $item['productInfo'] = get_thumb_water($item['productInfo']);
  579. $productInfo = $item['productInfo'];
  580. $item['vip_truePrice'] = 0;
  581. $is_activity = $item['seckill_id'] || $item['bargain_id'] || $item['combination_id'] || $item['advance_id'];
  582. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  583. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  584. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  585. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  586. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  587. if (!$is_activity) {
  588. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  589. $item['truePrice'] = $truePrice;
  590. $item['vip_truePrice'] = $vip_truePrice;
  591. $item['price_type'] = $type;
  592. } else {
  593. $item['price_type'] = 'activity';
  594. }
  595. } else {
  596. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  597. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  598. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  599. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  600. if (!$is_activity) {
  601. [$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);
  602. $item['truePrice'] = $truePrice;
  603. $item['vip_truePrice'] = $vip_truePrice;
  604. $item['price_type'] = $type;
  605. } else {
  606. $item['price_type'] = 'activity';
  607. }
  608. }
  609. if (isset($item['status']) && $item['status'] == 0) {
  610. $item['is_valid'] = 0;
  611. $invalid[] = $item;
  612. } else {
  613. switch ($shipping_type) {
  614. case 1:
  615. //不送达
  616. if (in_array($item['productInfo']['temp_id'], $tempIds) || (isset($item['productInfo']['logistics']) && !in_array(1, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0)) {
  617. $item['is_valid'] = 0;
  618. $invalid[] = $item;
  619. } else {
  620. $item['is_valid'] = 1;
  621. $valid[] = $item;
  622. }
  623. break;
  624. case 2:
  625. //不支持到店自提
  626. if (isset($item['productInfo']['logistics']) && $item['productInfo']['logistics'] && !in_array(2, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0) {
  627. $item['is_valid'] = 0;
  628. $invalid[] = $item;
  629. } else {
  630. $item['is_valid'] = 1;
  631. $valid[] = $item;
  632. }
  633. break;
  634. }
  635. }
  636. unset($item['attrInfo']);
  637. }
  638. return [$cartList, $valid, $invalid];
  639. }
  640. /**
  641. * 检查限购
  642. * @param $uid
  643. * @param $product_id
  644. * @param $num
  645. * @param $new
  646. * @return bool
  647. */
  648. public function checkLimit($uid, $product_id, $num, $new)
  649. {
  650. /** @var StoreProductServices $productServices */
  651. $productServices = app()->make(StoreProductServices::class);
  652. /** @var StoreOrderCartInfoServices $orderCartServices */
  653. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  654. $limitInfo = $productServices->get($product_id, ['is_limit', 'limit_type', 'limit_num']);
  655. if (!$limitInfo) throw new ApiException(410294);
  656. $limitInfo = $limitInfo->toArray();
  657. if (!$limitInfo['is_limit']) return true;
  658. if ($limitInfo['limit_type'] == 1) {
  659. $cartNum = 0;
  660. if (!$new) {
  661. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  662. }
  663. if (($num + $cartNum) > $limitInfo['limit_num']) {
  664. throw new ApiException(410239, ['limit' => $limitInfo['limit_num']]);
  665. }
  666. } else if ($limitInfo['limit_type'] == 2) {
  667. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  668. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  669. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id], 'refund_num');
  670. $orderNum = $orderPayNum - $orderRefundNum;
  671. if (($num + $orderNum + $cartNum) > $limitInfo['limit_num']) {
  672. throw new ApiException(410240, ['limit' => $limitInfo['limit_num'], 'pay_num' => $orderNum]);
  673. }
  674. }
  675. return true;
  676. }
  677. }