StoreCartServices.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. //购物车修改数量检查限购
  316. /** @var StoreProductServices $productServices */
  317. $productServices = app()->make(StoreProductServices::class);
  318. $limitInfo = $productServices->get($carInfo->product_id, ['is_limit', 'limit_type', 'limit_num']);
  319. if ($limitInfo['is_limit']) {
  320. if ($limitInfo['limit_type'] == 1 && $number > $limitInfo['limit_num']) {
  321. throw new ApiException(410239, ['limit' => $limitInfo['limit_num']]);
  322. } else if ($limitInfo['limit_type'] == 2) {
  323. /** @var StoreOrderCartInfoServices $orderCartServices */
  324. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  325. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $carInfo->product_id], 'cart_num');
  326. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $carInfo->product_id], 'refund_num');
  327. $orderNum = $orderPayNum - $orderRefundNum;
  328. if (($number + $orderNum) > $limitInfo['limit_num']) {
  329. throw new ApiException(410240, ['limit' => $limitInfo['limit_num'], 'pay_num' => $orderNum]);
  330. }
  331. }
  332. }
  333. $stock = $productServices->getProductStock($carInfo->product_id, $carInfo->product_attr_unique);
  334. if (!$stock) throw new ApiException(410237);
  335. if ($stock < $number) throw new ApiException(410297, ['num' => $number]);
  336. if ($carInfo->cart_num == $number) return true;
  337. return $this->dao->changeUserCartNum(['uid' => $uid, 'id' => $id], (int)$number);
  338. }
  339. /**
  340. * 修改购物车状态
  341. * @param int $productId
  342. * @param int $status 0 商品下架
  343. */
  344. public function changeStatus(int $productId, $status = 0)
  345. {
  346. $this->dao->update($productId, ['status' => $status], 'product_id');
  347. }
  348. /**
  349. * 获取购物车列表
  350. * @param int $uid
  351. * @param int $status
  352. * @return array
  353. * @throws \think\db\exception\DataNotFoundException
  354. * @throws \think\db\exception\DbException
  355. * @throws \think\db\exception\ModelNotFoundException
  356. */
  357. public function getUserCartList(int $uid, int $status, string $cartIds = '')
  358. {
  359. [$page, $limit] = $this->getPageValue();
  360. $list = $this->dao->getCartList(['uid' => $uid, 'status' => $status, 'id' => $cartIds], $page, $limit, ['productInfo', 'attrInfo']);
  361. [$list, $valid, $invalid] = $this->handleCartList($uid, $list);
  362. $seckillIds = array_unique(array_column($list, 'seckill_id'));
  363. $bargainIds = array_unique(array_column($list, 'bargain_id'));
  364. $combinationId = array_unique(array_column($list, 'combination_id'));
  365. $discountId = array_unique(array_column($list, 'discount_id'));
  366. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0, 'discount_id' => $discountId[0] ?? 0];
  367. if ($status == 1) {
  368. return ['valid' => $list, 'invalid' => [], 'deduction' => $deduction];
  369. } else {
  370. return ['valid' => [], 'invalid' => $list, 'deduction' => $deduction];
  371. }
  372. }
  373. /**
  374. * 购物车重选
  375. * @param int $cart_id
  376. * @param int $product_id
  377. * @param string $unique
  378. */
  379. public function modifyCart(int $cart_id, int $product_id, string $unique)
  380. {
  381. /** @var StoreProductAttrValueServices $attrService */
  382. $attrService = app()->make(StoreProductAttrValueServices::class);
  383. $stock = $attrService->value(['product_id' => $product_id, 'unique' => $unique, 'type' => 0], 'stock');
  384. if ($stock > 0) {
  385. $this->dao->update($cart_id, ['product_attr_unique' => $unique, 'cart_num' => 1]);
  386. } else {
  387. throw new ApiException(410238);
  388. }
  389. }
  390. /**
  391. * 重选购物车
  392. * @param $id
  393. * @param $uid
  394. * @param $productId
  395. * @param $unique
  396. * @param $num
  397. * @throws \think\db\exception\DataNotFoundException
  398. * @throws \think\db\exception\DbException
  399. * @throws \think\db\exception\ModelNotFoundException
  400. */
  401. public function resetCart($id, $uid, $productId, $unique, $num)
  402. {
  403. $res = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  404. if ($res) {
  405. $res->cart_num = $res->cart_num + $num;
  406. $res->save();
  407. $this->dao->delete($id);
  408. } else {
  409. $this->dao->update($id, ['product_attr_unique' => $unique, 'cart_num' => $num]);
  410. }
  411. }
  412. /**
  413. * 首页加入购物车
  414. * @param $uid
  415. * @param $productId
  416. * @param $num
  417. * @param $unique
  418. * @param $type
  419. * @return mixed
  420. * @throws \think\db\exception\DataNotFoundException
  421. * @throws \think\db\exception\DbException
  422. * @throws \think\db\exception\ModelNotFoundException
  423. */
  424. public function setCartNum($uid, $productId, $num, $unique, $type)
  425. {
  426. /** @var StoreProductAttrValueServices $attrValueServices */
  427. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  428. if ($unique == '') {
  429. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  430. }
  431. /** @var StoreProductServices $productServices */
  432. $productServices = app()->make(StoreProductServices::class);
  433. if (!$productServices->isValidProduct((int)$productId)) {
  434. throw new ApiException(410295);
  435. }
  436. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  437. throw new ApiException(410305);
  438. }
  439. if ($productServices->getProductStock((int)$productId, $unique) < $num) {
  440. throw new ApiException(410297, ['num' => $num]);
  441. }
  442. $cart = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  443. if ($cart) {
  444. if ($type == -1) {
  445. $cart->cart_num = $num;
  446. } elseif ($type == 0) {
  447. $cart->cart_num = $cart->cart_num - $num;
  448. } elseif ($type == 1) {
  449. $cart->cart_num = $cart->cart_num + $num;
  450. }
  451. if ($cart->cart_num === 0) {
  452. return $this->dao->delete($cart->id);
  453. } else {
  454. $cart->add_time = time();
  455. $cart->save();
  456. return $cart->id;
  457. }
  458. } else {
  459. $data = [
  460. 'uid' => $uid,
  461. 'product_id' => $productId,
  462. 'cart_num' => $num,
  463. 'product_attr_unique' => $unique,
  464. 'type' => 0,
  465. 'add_time' => time()
  466. ];
  467. return $this->dao->save($data)->id;
  468. }
  469. }
  470. /**
  471. * 获取用户购物车数量 ids 统计金额
  472. * @param int $uid
  473. * @param string $numType
  474. * @throws \think\db\exception\DataNotFoundException
  475. * @throws \think\db\exception\DbException
  476. * @throws \think\db\exception\ModelNotFoundException
  477. */
  478. public function getUserCartCount(int $uid, string $numType)
  479. {
  480. $count = 0;
  481. $ids = [];
  482. $sum_price = 0;
  483. $cartList = $this->dao->getUserCartList($uid, '*', ['productInfo', 'attrInfo']);
  484. if ($cartList) {
  485. /** @var StoreProductServices $productServices */
  486. $productServices = app()->make(StoreProductServices::class);
  487. /** @var MemberCardServices $memberCardService */
  488. $memberCardService = app()->make(MemberCardServices::class);
  489. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  490. /** @var UserServices $user */
  491. $user = app()->make(UserServices::class);
  492. $userInfo = $user->getUserInfo($uid);
  493. $discount = 100;
  494. if (sys_config('member_func_status', 1)) {
  495. /** @var SystemUserLevelServices $systemLevel */
  496. $systemLevel = app()->make(SystemUserLevelServices::class);
  497. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  498. }
  499. foreach ($cartList as &$item) {
  500. $productInfo = $item['productInfo'];
  501. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  502. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  503. $item['truePrice'] = $truePrice;
  504. $item['price_type'] = $type;
  505. } else {
  506. [$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);
  507. $item['truePrice'] = $truePrice;
  508. $item['price_type'] = $type;
  509. }
  510. $sum_price = bcadd((string)$sum_price, (string)bcmul((string)$item['cart_num'], (string)$item['truePrice'], 4), 2);
  511. }
  512. $ids = array_column($cartList, 'id');
  513. if ($numType) {
  514. $count = count($cartList);
  515. } else {
  516. $count = array_sum(array_column($cartList, 'cart_num'));
  517. }
  518. }
  519. return compact('count', 'ids', 'sum_price');
  520. }
  521. /**
  522. * 处理购物车数据
  523. * @param int $uid
  524. * @param array $cartList
  525. * @param array $addr
  526. * @return array
  527. */
  528. public function handleCartList(int $uid, array $cartList, array $addr = [], int $shipping_type = 1)
  529. {
  530. if (!$cartList) {
  531. return [$cartList, [], []];
  532. }
  533. /** @var StoreProductServices $productServices */
  534. $productServices = app()->make(StoreProductServices::class);
  535. /** @var MemberCardServices $memberCardService */
  536. $memberCardService = app()->make(MemberCardServices::class);
  537. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  538. $tempIds = [];
  539. $userInfo = [];
  540. $discount = 100;
  541. if ($uid) {
  542. /** @var UserServices $user */
  543. $user = app()->make(UserServices::class);
  544. $userInfo = $user->getUserInfo($uid);
  545. //用户等级是否开启
  546. if (sys_config('member_func_status', 1)) {
  547. /** @var SystemUserLevelServices $systemLevel */
  548. $systemLevel = app()->make(SystemUserLevelServices::class);
  549. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  550. }
  551. }
  552. //不送达运费模板
  553. if ($shipping_type == 1 && $addr) {
  554. $cityId = (int)($addr['city_id'] ?? 0);
  555. if ($cityId) {
  556. foreach ($cartList as $item) {
  557. $tempIds[] = $item['productInfo']['temp_id'];
  558. }
  559. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  560. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  561. $tempIds = $noDeliveryServices->isNoDelivery(array_unique($tempIds), $cityId);
  562. }
  563. }
  564. $valid = $invalid = [];
  565. foreach ($cartList as &$item) {
  566. $item['productInfo']['express_delivery'] = false;
  567. $item['productInfo']['store_mention'] = false;
  568. if (isset($item['productInfo']['logistics'])) {
  569. if (in_array(1, explode(',', $item['productInfo']['logistics']))) {
  570. $item['productInfo']['express_delivery'] = true;
  571. }
  572. if (in_array(2, explode(',', $item['productInfo']['logistics']))) {
  573. $item['productInfo']['store_mention'] = true;
  574. }
  575. }
  576. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  577. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  578. }
  579. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  580. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  581. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  582. if (isset($item['productInfo']['attrInfo'])) {
  583. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  584. }
  585. $item['productInfo'] = get_thumb_water($item['productInfo']);
  586. $productInfo = $item['productInfo'];
  587. $item['vip_truePrice'] = 0;
  588. $is_activity = $item['seckill_id'] || $item['bargain_id'] || $item['combination_id'] || $item['advance_id'];
  589. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  590. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  591. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  592. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  593. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  594. if (!$is_activity) {
  595. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  596. $item['truePrice'] = $truePrice;
  597. $item['vip_truePrice'] = $vip_truePrice;
  598. $item['price_type'] = $type;
  599. }
  600. } else {
  601. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  602. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  603. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  604. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  605. if (!$is_activity) {
  606. [$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);
  607. $item['truePrice'] = $truePrice;
  608. $item['vip_truePrice'] = $vip_truePrice;
  609. $item['price_type'] = $type;
  610. }
  611. }
  612. if (isset($item['status']) && $item['status'] == 0) {
  613. $item['is_valid'] = 0;
  614. $invalid[] = $item;
  615. } else {
  616. switch ($shipping_type) {
  617. case 1:
  618. //不送达
  619. if (in_array($item['productInfo']['temp_id'], $tempIds) || (isset($item['productInfo']['logistics']) && !in_array(1, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0)) {
  620. $item['is_valid'] = 0;
  621. $invalid[] = $item;
  622. } else {
  623. $item['is_valid'] = 1;
  624. $valid[] = $item;
  625. }
  626. break;
  627. case 2:
  628. //不支持到店自提
  629. if (isset($item['productInfo']['logistics']) && $item['productInfo']['logistics'] && !in_array(2, explode(',', $item['productInfo']['logistics'])) && $item['productInfo']['logistics'] != 0) {
  630. $item['is_valid'] = 0;
  631. $invalid[] = $item;
  632. } else {
  633. $item['is_valid'] = 1;
  634. $valid[] = $item;
  635. }
  636. break;
  637. }
  638. }
  639. unset($item['attrInfo']);
  640. }
  641. return [$cartList, $valid, $invalid];
  642. }
  643. /**
  644. * 检查限购
  645. * @param $uid
  646. * @param $product_id
  647. * @param $num
  648. * @param $new
  649. * @return bool
  650. */
  651. public function checkLimit($uid, $product_id, $num, $new)
  652. {
  653. /** @var StoreProductServices $productServices */
  654. $productServices = app()->make(StoreProductServices::class);
  655. /** @var StoreOrderCartInfoServices $orderCartServices */
  656. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  657. $limitInfo = $productServices->get($product_id, ['is_limit', 'limit_type', 'limit_num']);
  658. if (!$limitInfo) throw new ApiException(410294);
  659. $limitInfo = $limitInfo->toArray();
  660. if (!$limitInfo['is_limit']) return true;
  661. if ($limitInfo['limit_type'] == 1) {
  662. $cartNum = 0;
  663. if (!$new) {
  664. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  665. }
  666. if (($num + $cartNum) > $limitInfo['limit_num']) {
  667. throw new ApiException(410239, ['limit' => $limitInfo['limit_num']]);
  668. }
  669. } else if ($limitInfo['limit_type'] == 2) {
  670. $cartNum = $this->dao->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  671. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id], 'cart_num');
  672. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_id], 'refund_num');
  673. $orderNum = $orderPayNum - $orderRefundNum;
  674. if (($num + $orderNum + $cartNum) > $limitInfo['limit_num']) {
  675. throw new ApiException(410240, ['limit' => $limitInfo['limit_num'], 'pay_num' => $orderNum]);
  676. }
  677. }
  678. return true;
  679. }
  680. }