StoreCartServices.php 34 KB

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