StoreCartServices.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\BaseServices;
  14. use app\dao\order\StoreCartDao;
  15. use app\services\system\SystemUserLevelServices;
  16. use app\services\user\UserServices;
  17. use app\jobs\ProductLogJob;
  18. use crmeb\services\CacheService;
  19. use think\exception\ValidateException;
  20. use app\services\user\UserLevelServices;
  21. use app\services\activity\StoreSeckillServices;
  22. use app\services\activity\StoreBargainServices;
  23. use app\services\activity\StoreBargainUserServices;
  24. use app\services\activity\StoreCombinationServices;
  25. use app\services\product\product\StoreProductServices;
  26. use app\services\product\sku\StoreProductAttrValueServices;
  27. /**
  28. *
  29. * Class StoreCartServices
  30. * @package app\services\order
  31. * @method updateCartStatus($cartIds) 修改购物车状态
  32. * @method getUserCartNum(int $uid, string $type, int $numType) 购物车数量
  33. * @method deleteCartStatus(array $cartIds) 修改购物车状态
  34. * @method array productIdByCartNum(array $ids, int $uid) 根据商品id获取购物车数量
  35. * @method getCartList(array $where, ?int $page = 0, ?int $limit = 0, ?array $with = []) 获取用户购物车
  36. * @method getSum($where, $field) 求和
  37. * @method getProductTrend($time, $timeType, $str) 购物车趋势
  38. */
  39. class StoreCartServices extends BaseServices
  40. {
  41. /**
  42. * StoreCartServices constructor.
  43. * @param StoreCartDao $dao
  44. */
  45. public function __construct(StoreCartDao $dao)
  46. {
  47. $this->dao = $dao;
  48. }
  49. /**
  50. * 获取某个用户下的购物车数量
  51. * @param array $unique
  52. * @param int $productId
  53. * @param int $uid
  54. * @return array
  55. */
  56. public function getUserCartNums(array $unique, int $productId, int $uid)
  57. {
  58. $where['is_pay'] = 0;
  59. $where['is_del'] = 0;
  60. $where['is_new'] = 0;
  61. $where['type'] = 'product';
  62. $where['product_id'] = $productId;
  63. $where['uid'] = $uid;
  64. return $this->dao->getUserCartNums($where, $unique);
  65. }
  66. /**
  67. * 获取用户下的购物车列表
  68. * @param $uid
  69. * @param string $cartIds
  70. * @param bool $new
  71. * @param int $status
  72. * @return array
  73. * @throws \Psr\SimpleCache\InvalidArgumentException
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function getUserProductCartListV1($uid, $cartIds = '', bool $new, $status = 0)
  79. {
  80. if ($new) {
  81. $cartIds = explode(',', $cartIds);
  82. $cartInfo = [];
  83. foreach ($cartIds as $key) {
  84. $info = CacheService::redisHandler()->get($key);
  85. if ($info) {
  86. $cartInfo[] = $info;
  87. }
  88. }
  89. if (!$cartInfo) {
  90. throw new ValidateException('获取购物车信息失败');
  91. }
  92. /** @var StoreProductServices $productServices */
  93. $productServices = app()->make(StoreProductServices::class);
  94. foreach ($cartInfo as &$item) {
  95. $productInfo = $item['productInfo'];
  96. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  97. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  98. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  99. if ($item['seckill_id'] == 0 && $item['bargain_id'] == 0 && $item['combination_id'] == 0) {
  100. $item['truePrice'] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, true, 0, $item['productInfo']['attrInfo']['vip_price'], $productInfo['is_vip'] ?? 0, true);
  101. $item['vip_truePrice'] = (float)$productServices->setLevelPrice($productInfo['attrInfo']['price'], $uid, false, 0, $item['productInfo']['attrInfo']['vip_price'], $productInfo['is_vip'] ?? 0, true);
  102. } else {
  103. $item['truePrice'] = $productInfo['attrInfo']['price'];
  104. $item['vip_truePrice'] = 0;
  105. }
  106. } else {
  107. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  108. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  109. if ($item['seckill_id'] == 0 && $item['bargain_id'] == 0 && $item['combination_id'] == 0) {
  110. $item['truePrice'] = $productServices->setLevelPrice($item['productInfo']['price'] ?? 0, $uid, true, 0, $item['productInfo']['attrInfo']['vip_price'] ?? 0, $item['productInfo']['is_vip'] ?? 0, true);
  111. $item['vip_truePrice'] = (float)$productServices->setLevelPrice($item['productInfo']['price'], $uid, false, 0, $item['productInfo']['attrInfo']['vip_price'] ?? 0, $item['productInfo']['is_vip'] ?? 0, true);
  112. } else {
  113. $item['truePrice'] = $productInfo['productInfo']['price'];
  114. $item['vip_truePrice'] = 0;
  115. }
  116. }
  117. }
  118. $seckillIds = array_unique(array_column($cartInfo, 'seckill_id'));
  119. $bargainIds = array_unique(array_column($cartInfo, 'bargain_id'));
  120. $combinationId = array_unique(array_column($cartInfo, 'combination_id'));
  121. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0];
  122. return ['valid' => $cartInfo, 'invalid' => [], 'deduction' => $deduction];
  123. } else {
  124. return $this->getUserCartList($uid, $status, $cartIds);
  125. }
  126. }
  127. /**
  128. * 使用雪花算法生成订单ID
  129. * @return string
  130. * @throws \Exception
  131. */
  132. public function getCartId($prefix)
  133. {
  134. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  135. //32位
  136. if (PHP_INT_SIZE == 4) {
  137. $id = abs($snowflake->id());
  138. } else {
  139. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->id();
  140. }
  141. return $prefix . $id;
  142. }
  143. /**
  144. * 验证库存
  145. * @param int $uid
  146. * @param int $cartNum
  147. * @param string $unique
  148. * @param $productId
  149. * @param int $seckillId
  150. * @param int $bargainId
  151. * @param int $combinationId
  152. * @return array
  153. * @throws \Psr\SimpleCache\InvalidArgumentException
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\DbException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. */
  158. public function checkProductStock(int $uid, int $cartNum, string $unique, $productId, int $seckillId, int $bargainId, int $combinationId)
  159. {
  160. if ($cartNum < 1) $cartNum = 1;
  161. /** @var StoreProductAttrValueServices $attrValueServices */
  162. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  163. if ($seckillId) {
  164. if ($unique == '') {
  165. $unique = $attrValueServices->value(['product_id' => $seckillId, 'type' => 1], 'unique');
  166. }
  167. /** @var StoreSeckillServices $seckillService */
  168. $seckillService = app()->make(StoreSeckillServices::class);
  169. if (!$seckillService->getSeckillCount($seckillId)) {
  170. throw new ValidateException('活动已结束');
  171. }
  172. $StoreSeckillinfo = $seckillService->getValidProduct($seckillId);
  173. if (!$StoreSeckillinfo) {
  174. throw new ValidateException('该商品已下架或删除');
  175. }
  176. /** @var StoreOrderServices $orderServices */
  177. $orderServices = app()->make(StoreOrderServices::class);
  178. $userBuyCount = $orderServices->getBuyCount($uid, 'seckill_id', $seckillId);
  179. if ($StoreSeckillinfo['once_num'] < $cartNum) {
  180. throw new ValidateException('每个订单限购' . $StoreSeckillinfo['once_num'] . '件');
  181. }
  182. if ($StoreSeckillinfo['num'] < ($userBuyCount + $cartNum)) {
  183. throw new ValidateException('每人总共限购' . $StoreSeckillinfo['num'] . '件');
  184. }
  185. $unapid = $orderServices->count(['uid' => $uid, 'status' => 0, 'paid' => 0, 'refund_status' => 0, 'is_del' => 0, 'seckill_id' => $seckillId]);
  186. if ($unapid > 0) {
  187. throw new ValidateException('该商品已经下单成功,请去完成支付');
  188. }
  189. if ($StoreSeckillinfo['num'] < $cartNum) {
  190. throw new ValidateException('每人限购' . $StoreSeckillinfo['num'] . '件');
  191. }
  192. $res = $attrValueServices->getOne(['product_id' => $seckillId, 'unique' => $unique, 'type' => 1]);
  193. if ($cartNum > $res['quota']) {
  194. throw new ValidateException('该商品库存不足' . $cartNum);
  195. }
  196. $product_stock = $attrValueServices->value(['product_id' => $StoreSeckillinfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  197. if ($product_stock < $cartNum) {
  198. throw new ValidateException('该商品库存不足' . $cartNum);
  199. }
  200. if (!$seckillService->isSeckillStock($unique, 1, (int)$cartNum)) {
  201. throw new ValidateException('商品库存不足' . $cartNum . ',无法购买请选择其他商品!');
  202. }
  203. } elseif ($bargainId) {
  204. /** @var StoreBargainServices $bargainService */
  205. $bargainService = app()->make(StoreBargainServices::class);
  206. if (!$bargainService->validBargain($bargainId)) {
  207. throw new ValidateException('该商品已下架或删除');
  208. }
  209. /** @var StoreBargainUserServices $bargainUserService */
  210. $bargainUserService = app()->make(StoreBargainUserServices::class);
  211. $bargainUserInfo = $bargainUserService->getOne(['uid' => $uid, 'bargain_id' => $bargainId, 'status' => 1, 'is_del' => 0]);
  212. if ($bargainUserInfo['bargain_price_min'] < bcsub((string)$bargainUserInfo['bargain_price'], (string)$bargainUserInfo['price'], 2)) {
  213. throw new ValidateException('砍价未成功');
  214. }
  215. $StoreBargainInfo = $bargainService->get($bargainId);
  216. $res = $attrValueServices->getOne(['product_id' => $bargainId, 'type' => 2]);
  217. $unique = $res['unique'];
  218. if ($cartNum > $res['quota']) {
  219. throw new ValidateException('该商品库存不足' . $cartNum);
  220. }
  221. $product_stock = $attrValueServices->value(['product_id' => $StoreBargainInfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  222. if ($product_stock < $cartNum) {
  223. throw new ValidateException('该商品库存不足' . $cartNum);
  224. }
  225. } elseif ($combinationId) {//拼团
  226. if ($unique == '') {
  227. $unique = $attrValueServices->value(['product_id' => $combinationId, 'type' => 3], 'unique');
  228. }
  229. /** @var StoreCombinationServices $combinationService */
  230. $combinationService = app()->make(StoreCombinationServices::class);
  231. $StoreCombinationInfo = $combinationService->getCombinationOne($combinationId);
  232. if (!$StoreCombinationInfo) {
  233. throw new ValidateException('该商品已下架或删除');
  234. }
  235. /** @var StoreOrderServices $orderServices */
  236. $orderServices = app()->make(StoreOrderServices::class);
  237. $userBuyCount = $orderServices->getBuyCount($uid, 'combination_id', $combinationId);
  238. if ($StoreCombinationInfo['once_num'] < $cartNum) {
  239. throw new ValidateException('每个订单限购' . $StoreCombinationInfo['once_num'] . '件');
  240. }
  241. if ($StoreCombinationInfo['num'] < ($userBuyCount + $cartNum)) {
  242. throw new ValidateException('每人总共限购' . $StoreCombinationInfo['num'] . '件');
  243. }
  244. $res = $attrValueServices->getOne(['product_id' => $combinationId, 'unique' => $unique, 'type' => 3]);
  245. if ($cartNum > $res['quota']) {
  246. throw new ValidateException('该商品库存不足' . $cartNum);
  247. }
  248. $product_stock = $attrValueServices->value(['product_id' => $StoreCombinationInfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  249. if ($product_stock < $cartNum) {
  250. throw new ValidateException('该商品库存不足' . $cartNum);
  251. }
  252. } else {
  253. if ($unique == '') {
  254. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  255. }
  256. /** @var StoreProductServices $productServices */
  257. $productServices = app()->make(StoreProductServices::class);
  258. $res = $attrValueServices->getOne(['unique' => $unique, 'type' => 0]);
  259. if (!$productServices->isValidProduct($productId)) {
  260. throw new ValidateException('该商品已下架或删除');
  261. }
  262. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  263. throw new ValidateException('请选择有效的商品属性');
  264. }
  265. $stockNum = $this->dao->value(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], 'cart_num') ?: 0;
  266. $nowStock = $productServices->getProductStock($productId, $unique);//现有库存
  267. if ($nowStock < ($cartNum + $stockNum)) {
  268. $surplusStock = $nowStock - $cartNum;//剩余库存
  269. if ($surplusStock < 0) {
  270. throw new ValidateException('该商品库存不足' . $cartNum);
  271. } else {
  272. if ($surplusStock < $stockNum) {
  273. if ($surplusStock > 0) {
  274. $this->dao->update(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], ['cart_num' => $surplusStock]);
  275. } else {
  276. if (!$productServices->value(['product_id' => $productId], 'spec_type')) $this->dao->update(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid], ['status' => 0]);
  277. }
  278. }
  279. }
  280. }
  281. }
  282. return [$res, $unique, $bargainUserInfo['bargain_price_min'] ?? 0, $cartNum];
  283. }
  284. /**
  285. * 添加购物车
  286. * @param int $uid 用户UID
  287. * @param int $product_id 商品ID
  288. * @param int $cart_num 商品数量
  289. * @param string $product_attr_unique 商品SKU
  290. * @param string $type 添加购物车类型
  291. * @param bool $new true = 立即购买,false = 加入购物车
  292. * @param int $combination_id 拼团商品ID
  293. * @param int $seckill_id 秒杀商品ID
  294. * @param int $bargain_id 砍价商品ID
  295. * @return mixed|string
  296. * @throws \Psr\SimpleCache\InvalidArgumentException
  297. * @throws \think\db\exception\DataNotFoundException
  298. * @throws \think\db\exception\DbException
  299. * @throws \think\db\exception\ModelNotFoundException
  300. */
  301. public function setCart(int $uid, int $product_id, int $cart_num = 1, string $product_attr_unique = '', string $type = 'product', bool $new = false, int $combination_id = 0, int $seckill_id = 0, int $bargain_id = 0)
  302. {
  303. /** @var StoreProductServices $productServices */
  304. $productServices = app()->make(StoreProductServices::class);
  305. /** @var StoreSeckillServices $seckillService */
  306. $seckillService = app()->make(StoreSeckillServices::class);
  307. /** @var StoreBargainServices $bargainService */
  308. $bargainService = app()->make(StoreBargainServices::class);
  309. /** @var StoreCombinationServices $combinationService */
  310. $combinationService = app()->make(StoreCombinationServices::class);
  311. [$res, $product_attr_unique, $bargainPriceMin, $cart_num] = $this->checkProductStock($uid, $cart_num, $product_attr_unique, (int)$product_id, $seckill_id, $bargain_id, $combination_id);
  312. if ($new) {
  313. $key = $this->getCartId($uid);
  314. $info['id'] = $key;
  315. $info['type'] = $type;
  316. $info['seckill_id'] = $seckill_id;
  317. $info['bargain_id'] = $bargain_id;
  318. $info['combination_id'] = $combination_id;
  319. $info['product_id'] = $product_id;
  320. $info['product_attr_unique'] = $product_attr_unique;
  321. $info['cart_num'] = $cart_num;
  322. $productInfoField = ['id', 'image', 'cate_id', 'price', 'ot_price', 'vip_price', 'postage', 'give_integral', 'sales', 'stock', 'store_name', 'unit_name', 'is_show', 'is_del', 'is_postage', 'cost', 'is_sub', 'temp_id', 'is_vip'];
  323. $seckillInfoField = ['id', 'image', 'price', 'ot_price', 'postage', 'give_integral', 'sales', 'stock', 'title as store_name', 'unit_name', 'is_show', 'is_del', 'is_postage', 'cost', 'temp_id', 'weight', 'volume', 'start_time', 'stop_time', 'time_id'];
  324. $bargainInfoField = ['id', 'image', 'min_price as price', 'price as ot_price', 'postage', 'give_integral', 'sales', 'stock', 'title as store_name', 'unit_name', 'status as is_show', 'is_del', 'is_postage', 'cost', 'temp_id', 'weight', 'volume'];
  325. $combinationInfoField = ['id', 'image', 'price', 'postage', 'sales', 'stock', 'title as store_name', 'is_show', 'is_del', 'is_postage', 'cost', 'temp_id', 'weight', 'volume'];
  326. if ($seckill_id) {
  327. $info['productInfo'] = $seckillService->get($seckill_id, $seckillInfoField)->toArray();
  328. } elseif ($bargain_id) {
  329. $info['productInfo'] = $bargainService->get($bargain_id, $bargainInfoField)->toArray();
  330. } elseif ($combination_id) {
  331. $info['productInfo'] = $combinationService->get($combination_id, $combinationInfoField)->toArray();
  332. } else {
  333. $info['productInfo'] = $productServices->get($product_id, $productInfoField)->toArray();
  334. }
  335. $info['productInfo']['attrInfo'] = $res->toArray();
  336. //砍价
  337. if ($bargain_id) {
  338. $info['truePrice'] = $bargainPriceMin;
  339. $info['productInfo']['attrInfo']['price'] = $bargainPriceMin;
  340. } else {
  341. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? $info['productInfo']['price'] ?? 0;
  342. }
  343. //拼团砍价秒杀不参与会员价
  344. if (!$bargain_id || !$combination_id || !$seckill_id) {
  345. $info['truePrice'] = $productServices->setLevelPrice($info['productInfo']['attrInfo']['price'], $uid, true, 0, $info['productInfo']['attrInfo']['vip_price'] ?? 0, $info['productInfo']['is_vip'] ?? 0, true);
  346. $info['vip_truePrice'] = (float)$productServices->setLevelPrice($info['productInfo']['attrInfo']['price'], $uid, false, 0, $info['productInfo']['attrInfo']['vip_price'] ?? 0, $info['productInfo']['is_vip'] ?? 0, true);
  347. } else {
  348. $info['vip_truePrice'] = 0;
  349. }
  350. $info['trueStock'] = $info['productInfo']['attrInfo']['stock'];
  351. $info['costPrice'] = $info['productInfo']['attrInfo']['cost'];
  352. try {
  353. CacheService::redisHandler()->set($key, $info, 3600);
  354. } catch (\Throwable $e) {
  355. throw new ValidateException($e->getMessage());
  356. }
  357. return $key;
  358. } else {
  359. //加入购物车记录
  360. ProductLogJob::dispatch(['cart', ['uid' => $uid, 'product_id' => $product_id, 'cart_num' => $cart_num]]);
  361. $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]);
  362. if ($cart) {
  363. $cart->cart_num = $cart_num + $cart->cart_num;
  364. $cart->add_time = time();
  365. $cart->save();
  366. return $cart->id;
  367. } else {
  368. $add_time = time();
  369. return $this->dao->save(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'type', 'add_time'))->id;
  370. }
  371. }
  372. }
  373. /**移除购物车商品
  374. * @param int $uid
  375. * @param array $ids
  376. * @return StoreCartDao|bool
  377. */
  378. public function removeUserCart(int $uid, array $ids)
  379. {
  380. if (!$uid || !$ids) return false;
  381. return $this->dao->removeUserCart($uid, $ids);
  382. }
  383. /**购物车 修改商品数量
  384. * @param $id
  385. * @param $number
  386. * @param $uid
  387. * @return bool|\crmeb\basic\BaseModel
  388. * @throws \think\db\exception\DataNotFoundException
  389. * @throws \think\db\exception\DbException
  390. * @throws \think\db\exception\ModelNotFoundException
  391. */
  392. public function changeUserCartNum($id, $number, $uid)
  393. {
  394. if (!$id || !$number || !$uid) return false;
  395. $where = ['uid' => $uid, 'id' => $id];
  396. $carInfo = $this->dao->getOne($where, 'product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num');
  397. /** @var StoreProductServices $StoreProduct */
  398. $StoreProduct = app()->make(StoreProductServices::class);
  399. $stock = $StoreProduct->getProductStock($carInfo->product_id, $carInfo->product_attr_unique);
  400. if (!$stock) throw new ValidateException('暂无库存');
  401. if (!$number) throw new ValidateException('库存错误');
  402. if ($stock < $number) throw new ValidateException('库存不足' . $number);
  403. if ($carInfo->cart_num == $number) return true;
  404. return $this->dao->changeUserCartNum(['uid' => $uid, 'id' => $id], $number);
  405. }
  406. /**
  407. * 修改购物车状态
  408. * @param int $productId
  409. * @param int $type 1 商品下架
  410. */
  411. public function changeStatus(int $productId)
  412. {
  413. $this->dao->update($productId, ['status' => 0], 'product_id');
  414. }
  415. /**
  416. * 获取购物车列表
  417. * @param int $uid
  418. * @param int $status
  419. * @return array
  420. * @throws \think\db\exception\DataNotFoundException
  421. * @throws \think\db\exception\DbException
  422. * @throws \think\db\exception\ModelNotFoundException
  423. */
  424. public function getUserCartList(int $uid, int $status, string $cartIds = '')
  425. {
  426. [$page, $limit] = $this->getPageValue();
  427. $list = $this->dao->getCartList(['uid' => $uid, 'status' => $status, 'id' => $cartIds], $page, $limit, ['productInfo', 'attrInfo']);
  428. /** @var StoreProductServices $productServices */
  429. $productServices = app()->make(StoreProductServices::class);
  430. foreach ($list as &$item) {
  431. $item['attrStatus'] = $item['attrInfo']['stock'] ? true : false;
  432. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  433. $item['productInfo']['attrInfo']['image'] = $item['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  434. $item['productInfo']['attrInfo']['suk'] = $item['attrInfo']['suk'] ?? '已失效';
  435. $productInfo = $item['productInfo'];
  436. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  437. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  438. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  439. $item['truePrice'] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, true, 0, $item['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  440. $item['vip_truePrice'] = (float)$productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, false, 0, $item['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  441. } else {
  442. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  443. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  444. $item['truePrice'] = $productServices->setLevelPrice($item['productInfo']['price'] ?? 0, $uid, true, 0, $item['attrInfo']['vip_price'] ?? 0, $item['productInfo']['is_vip'] ?? 0);
  445. $item['vip_truePrice'] = (float)$productServices->setLevelPrice($item['productInfo']['price'] ?? 0, $uid, false, 0, $item['attrInfo']['vip_price'] ?? 0, $item['productInfo']['is_vip'] ?? 0);
  446. }
  447. unset($item['attrInfo']);
  448. }
  449. $seckillIds = array_unique(array_column($list, 'seckill_id'));
  450. $bargainIds = array_unique(array_column($list, 'bargain_id'));
  451. $combinationId = array_unique(array_column($list, 'combination_id'));
  452. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0];
  453. if ($status == 1) {
  454. return ['valid' => $list, 'invalid' => [], 'deduction' => $deduction];
  455. } else {
  456. return ['valid' => [], 'invalid' => $list, 'deduction' => $deduction];
  457. }
  458. }
  459. /**
  460. * 购物车重选
  461. * @param int $cart_id
  462. * @param int $product_id
  463. * @param string $unique
  464. */
  465. public function modifyCart(int $cart_id, int $product_id, string $unique)
  466. {
  467. /** @var StoreProductAttrValueServices $attrService */
  468. $attrService = app()->make(StoreProductAttrValueServices::class);
  469. $stock = $attrService->value(['product_id' => $product_id, 'unique' => $unique, 'type' => 0], 'stock');
  470. if ($stock > 0) {
  471. $this->dao->update($cart_id, ['product_attr_unique' => $unique, 'cart_num' => 1]);
  472. } else {
  473. throw new ValidateException('选择的规格库存不足');
  474. }
  475. }
  476. /**
  477. * 重选购物车
  478. * @param $id
  479. * @param $uid
  480. * @param $productId
  481. * @param $unique
  482. * @param $num
  483. * @throws \think\db\exception\DataNotFoundException
  484. * @throws \think\db\exception\DbException
  485. * @throws \think\db\exception\ModelNotFoundException
  486. */
  487. public function resetCart($id, $uid, $productId, $unique, $num)
  488. {
  489. $res = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  490. if ($res) {
  491. $res->cart_num = $res->cart_num + $num;
  492. $res->save();
  493. $this->dao->delete($id);
  494. } else {
  495. $this->dao->update($id, ['product_attr_unique' => $unique, 'cart_num' => $num]);
  496. }
  497. }
  498. /**
  499. * 首页加入购物车
  500. * @param $uid
  501. * @param $productId
  502. * @param $num
  503. * @param $unique
  504. * @param $type
  505. * @return mixed
  506. * @throws \think\db\exception\DataNotFoundException
  507. * @throws \think\db\exception\DbException
  508. * @throws \think\db\exception\ModelNotFoundException
  509. */
  510. public function setCartNum($uid, $productId, $num, $unique, $type)
  511. {
  512. /** @var StoreProductAttrValueServices $attrValueServices */
  513. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  514. if ($unique == '') {
  515. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  516. }
  517. /** @var StoreProductServices $productServices */
  518. $productServices = app()->make(StoreProductServices::class);
  519. if (!$productServices->isValidProduct((int)$productId)) {
  520. throw new ValidateException('该商品已下架或删除');
  521. }
  522. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  523. throw new ValidateException('请选择有效的商品属性');
  524. }
  525. if ($productServices->getProductStock((int)$productId, $unique) < $num) {
  526. throw new ValidateException('该商品库存不足' . $num);
  527. }
  528. $cart = $this->dao->getOne(['type' => 'product', 'uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  529. if ($cart) {
  530. if ($type == -1) {
  531. $cart->cart_num = $num;
  532. } elseif ($type == 0) {
  533. $cart->cart_num = $cart->cart_num - $num;
  534. } elseif ($type == 1) {
  535. $cart->cart_num = $cart->cart_num + $num;
  536. }
  537. if ($cart->cart_num === 0) {
  538. return $this->dao->delete($cart->id);
  539. } else {
  540. $cart->add_time = time();
  541. $cart->save();
  542. return $cart->id;
  543. }
  544. } else {
  545. $data = [
  546. 'uid' => $uid,
  547. 'product_id' => $productId,
  548. 'cart_num' => $num,
  549. 'product_attr_unique' => $unique,
  550. 'type' => 'product',
  551. 'add_time' => time()
  552. ];
  553. return $this->dao->save($data)->id;
  554. }
  555. }
  556. }