StoreCartServices.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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\dao\order\StoreCartDao;
  14. use app\services\activity\discounts\StoreDiscountsProductsServices;
  15. use app\services\activity\bargain\StoreBargainServices;
  16. use app\services\activity\combination\StoreCombinationServices;
  17. use app\services\activity\integral\StoreIntegralServices;
  18. use app\services\activity\lottery\LuckLotteryRecordServices;
  19. use app\services\activity\newcomer\StoreNewcomerServices;
  20. use app\services\activity\promotions\StorePromotionsServices;
  21. use app\services\activity\seckill\StoreSeckillServices;
  22. use app\services\BaseServices;
  23. use app\services\other\CityAreaServices;
  24. use app\services\product\branch\StoreBranchProductServices;
  25. use app\services\product\product\StoreProductServices;
  26. use app\services\product\sku\StoreProductAttrValueServices;
  27. use app\services\product\shipping\ShippingTemplatesServices;
  28. use app\services\product\shipping\ShippingTemplatesNoDeliveryServices;
  29. use app\services\store\SystemStoreServices;
  30. use app\services\user\level\SystemUserLevelServices;
  31. use app\services\user\member\MemberCardServices;
  32. use app\services\user\UserAddressServices;
  33. use app\services\user\UserServices;
  34. use app\jobs\product\ProductLogJob;
  35. use crmeb\services\CacheService;
  36. use crmeb\traits\OptionTrait;
  37. use crmeb\traits\ServicesTrait;
  38. use think\exception\ValidateException;
  39. /**
  40. *
  41. * Class StoreCartServices
  42. * @package app\services\order
  43. * @mixin StoreCartDao
  44. */
  45. class StoreCartServices extends BaseServices
  46. {
  47. use OptionTrait, ServicesTrait;
  48. //库存字段比对
  49. const STOCK_FIELD = 'sum_stock';
  50. //购物车最大数量
  51. protected $maxCartNum = 100;
  52. /**
  53. * StoreCartServices constructor.
  54. * @param StoreCartDao $dao
  55. */
  56. public function __construct(StoreCartDao $dao)
  57. {
  58. $this->dao = $dao;
  59. }
  60. /**
  61. * 获取某个用户下的购物车数量
  62. * @param array $unique
  63. * @param int $productId
  64. * @param int $uid
  65. * @param string $userKey
  66. * @return array
  67. */
  68. public function getUserCartNums(array $unique, int $productId, int $uid, string $userKey = 'uid')
  69. {
  70. $where['is_pay'] = 0;
  71. $where['is_del'] = 0;
  72. $where['is_new'] = 0;
  73. $where['product_id'] = $productId;
  74. $where[$userKey] = $uid;
  75. return $this->dao->getUserCartNums($where, $unique);
  76. }
  77. /**
  78. * 计算首单优惠
  79. * @param int $uid
  80. * @param array $cartInfo
  81. * @param array $newcomerArr
  82. * @return array
  83. */
  84. public function computedFirstDiscount(int $uid, array $cartInfo, array $newcomerArr = [])
  85. {
  86. $first_order_price = $first_discount = $first_discount_limit = 0;
  87. if ($uid && $cartInfo) {
  88. if (!$newcomerArr) {
  89. /** @var StoreNewcomerServices $newcomerServices */
  90. $newcomerServices = app()->make(StoreNewcomerServices::class);
  91. $newcomerArr = $newcomerServices->checkUserFirstDiscount($uid);
  92. }
  93. if ($newcomerArr) {//首单优惠
  94. [$first_discount, $first_discount_limit] = $newcomerArr;
  95. /** @var StoreOrderComputedServices $orderServices */
  96. $orderServices = app()->make(StoreOrderComputedServices::class);
  97. $totalPrice = $orderServices->getOrderSumPrice($cartInfo, 'truePrice');//获取订单svip、用户等级优惠之后总金额
  98. $first_discount = bcsub('1', (string)bcdiv($first_discount, '100', 2), 2);
  99. $first_order_price = (float)bcmul((string)$totalPrice, (string)$first_discount, 2);
  100. $first_order_price = min($first_order_price, $first_discount_limit, $totalPrice);
  101. }
  102. }
  103. return [$cartInfo, $first_order_price, $first_discount, $first_discount_limit];
  104. }
  105. /**
  106. * 获取用户下的购物车列表
  107. * @param int $uid
  108. * @param $cartIds
  109. * @param bool $new
  110. * @param array $addr
  111. * @param int $shipping_type
  112. * @param int $store_id
  113. * @param int $coupon_id
  114. * @param bool $isCart
  115. * @return array
  116. * @throws \Psr\SimpleCache\InvalidArgumentException
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getUserProductCartListV1(int $uid, $cartIds, bool $new, array $addr = [], int $shipping_type = 1, int $store_id = 0, int $coupon_id = 0, bool $isCart = false)
  122. {
  123. if ($new) {
  124. $cartIds = $cartIds && is_string($cartIds) ? explode(',', $cartIds) : (is_array($cartIds) ? $cartIds : []);
  125. $cartInfo = [];
  126. if ($cartIds) {
  127. foreach ($cartIds as $key) {
  128. $info = CacheService::redisHandler()->get($key);
  129. if ($info) {
  130. $cartInfo[] = $info;
  131. }
  132. }
  133. }
  134. } else {
  135. $cartInfo = $this->dao->getCartList(['uid' => $uid, 'status' => 1, 'id' => $cartIds], 0, 0, ['productInfo', 'attrInfo']);
  136. }
  137. if (!$cartInfo) {
  138. throw new ValidateException('获取购物车信息失败');
  139. }
  140. foreach ($cartInfo as $cart) {
  141. //检查限购
  142. if (isset($cart['type']) && $cart['type'] != 8) {
  143. $this->checkLimit($uid, $cart['product_id'] ?? 0, $cart['cart_num'] ?? 1, true, $store_id);
  144. }
  145. }
  146. [$cartInfo, $valid, $invalid] = $this->handleCartList($uid, $cartInfo, $addr, $shipping_type, $store_id);
  147. $type = array_unique(array_column($cartInfo, 'type'));
  148. $product_type = array_unique(array_column($cartInfo, 'product_type'));
  149. $activity_id = array_unique(array_column($cartInfo, 'activity_id'));
  150. $collate_code_id = array_unique(array_column($cartInfo, 'collate_code_id'));
  151. $deduction = ['product_type' => $product_type[0] ?? 0, 'type' => $type[0] ?? 0, 'activity_id' => $activity_id[0] ?? 0, 'collate_code_id' => $collate_code_id[0] ?? 0];
  152. $promotions = $giveCoupon = $giveCartList = $useCoupon = $giveProduct = [];
  153. $giveIntegral = $couponPrice = $firstOrderPrice = 0;
  154. if (!$deduction['activity_id'] && $deduction['type'] != 6) {
  155. /** @var StoreNewcomerServices $newcomerServices */
  156. $newcomerServices = app()->make(StoreNewcomerServices::class);
  157. $newcomerArr = $newcomerServices->checkUserFirstDiscount($uid);
  158. if ($newcomerArr) {//首单优惠
  159. //计算首单优惠
  160. [$valid, $firstOrderPrice, $first_discount, $first_discount_limit] = $this->computedFirstDiscount($uid, $valid, $newcomerArr);
  161. } else {
  162. /** @var StorePromotionsServices $storePromotionsServices */
  163. $storePromotionsServices = app()->make(StorePromotionsServices::class);
  164. //计算相关优惠活动
  165. [$valid, $couponPrice, $useCoupon, $promotions, $giveIntegral, $giveCoupon, $giveCartList] = $storePromotionsServices->computedPromotions($uid, $valid, $store_id, $coupon_id, $isCart);
  166. if ($giveCartList) {
  167. foreach ($giveCartList as $key => $give) {
  168. $giveProduct[] = [
  169. 'promotions_id' => $give['promotions_id'][0] ?? 0,
  170. 'product_id' => $give['product_id'] ?? 0,
  171. 'unique' => $give['product_attr_unique'] ?? '',
  172. 'cart_num' => $give['cart_num'] ?? 1,
  173. ];
  174. }
  175. }
  176. }
  177. }
  178. return compact('cartInfo', 'valid', 'invalid', 'deduction', 'couponPrice', 'useCoupon', 'promotions', 'giveCartList', 'giveIntegral', 'giveCoupon', 'giveProduct', 'firstOrderPrice');
  179. }
  180. /**
  181. * 验证库存
  182. * @param int $uid
  183. * @param int $productId
  184. * @param int $cartNum
  185. * @param int $store_id
  186. * @param string $unique
  187. * @param bool $new
  188. * @param int $type
  189. * @param int $activity_id
  190. * @param int $discount_product_id
  191. * @return array
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\DbException
  194. * @throws \think\db\exception\ModelNotFoundException
  195. */
  196. public function checkProductStock(int $uid, int $productId, int $cartNum = 1, int $store_id = 0, string $unique = '', bool $new = false, int $type = 0, int $activity_id = 0, $discount_product_id = 0, $sum_cart_num = 0)
  197. {
  198. //验证限量
  199. $this->checkLimit($uid, $productId, $cartNum, $new, $store_id);
  200. /** @var StoreProductAttrValueServices $attrValueServices */
  201. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  202. $isSet = $this->getItem('is_set', 0);
  203. switch ($type) {
  204. case 0://普通
  205. if ($unique == '') {
  206. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  207. }
  208. /** @var StoreProductServices $productServices */
  209. $productServices = app()->make(StoreProductServices::class);
  210. $productInfo = $productServices->isValidProduct($productId);
  211. if (!$productInfo) {
  212. throw new ValidateException('该商品已下架或删除');
  213. }
  214. if ($productInfo['is_vip_product']) {
  215. /** @var UserServices $userServices */
  216. $userServices = app()->make(UserServices::class);
  217. $is_vip = $userServices->value(['uid' => $uid], 'is_money_level');
  218. if (!$is_vip) {
  219. throw new ValidateException('该商品为付费会员专享商品');
  220. }
  221. }
  222. //预售商品
  223. if ($productInfo['is_presale_product']) {
  224. if ($productInfo['presale_start_time'] > time()) throw new ValidateException('预售活动未开始');
  225. if ($productInfo['presale_end_time'] < time()) throw new ValidateException('预售活动已结束');
  226. }
  227. $attrInfo = $attrValueServices->getOne(['unique' => $unique, 'type' => 0]);
  228. if (!$unique || !$attrInfo || $attrInfo['product_id'] != $productId) {
  229. throw new ValidateException('请选择有效的商品属性');
  230. }
  231. $nowStock = $attrInfo['stock'];//现有平台库存
  232. if ($cartNum > $nowStock) {
  233. throw new ValidateException('该商品库存不足' . $cartNum);
  234. }
  235. //直接设置购物车商品数量
  236. if ($isSet) {
  237. $stockNum = 0;
  238. } else {
  239. $stockNum = $this->dao->value(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1, 'store_id' => $store_id], 'cart_num') ?: 0;
  240. }
  241. if ($nowStock < ($cartNum + $stockNum)) {
  242. if ($store_id) {
  243. throw new ValidateException('该商品库存不足');
  244. }
  245. $surplusStock = $nowStock - $cartNum;//剩余库存
  246. if ($surplusStock < $stockNum) {
  247. $this->dao->update(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1, 'store_id' => $store_id], ['cart_num' => $surplusStock]);
  248. }
  249. }
  250. break;
  251. case 1://秒杀
  252. /** @var StoreSeckillServices $seckillService */
  253. $seckillService = app()->make(StoreSeckillServices::class);
  254. [$attrInfo, $unique, $productInfo] = $seckillService->checkSeckillStock($uid, $activity_id, $cartNum, $store_id, $unique);
  255. break;
  256. case 2://砍价
  257. /** @var StoreBargainServices $bargainService */
  258. $bargainService = app()->make(StoreBargainServices::class);
  259. [$attrInfo, $unique, $productInfo, $bargainUserInfo] = $bargainService->checkBargainStock($uid, $activity_id, $cartNum, $unique);
  260. break;
  261. case 3://拼团
  262. /** @var StoreCombinationServices $combinationService */
  263. $combinationService = app()->make(StoreCombinationServices::class);
  264. [$attrInfo, $unique, $productInfo] = $combinationService->checkCombinationStock($uid, $activity_id, $cartNum, $unique);
  265. break;
  266. case 4://积分
  267. /** @var StoreIntegralServices $storeIntegralServices */
  268. $storeIntegralServices = app()->make(StoreIntegralServices::class);
  269. [$attrInfo, $unique, $productInfo] = $storeIntegralServices->checkoutProductStock($uid, $activity_id, $cartNum, $unique);
  270. break;
  271. case 5://套餐
  272. /** @var StoreDiscountsProductsServices $discountProduct */
  273. $discountProduct = app()->make(StoreDiscountsProductsServices::class);
  274. [$attrInfo, $unique, $productInfo] = $discountProduct->checkDiscountsStock($uid, $discount_product_id, $cartNum, $unique);
  275. break;
  276. case 7://新人专享
  277. if ($cartNum > 1) {
  278. throw new ValidateException('新人专享商品限购一件');
  279. }
  280. /** @var StoreNewcomerServices $newcomerServices */
  281. $newcomerServices = app()->make(StoreNewcomerServices::class);
  282. [$attrInfo, $unique, $productInfo] = $newcomerServices->checkNewcomerStock($uid, $activity_id, $cartNum, $unique);
  283. break;
  284. case 8://抽奖
  285. if (!$activity_id) {
  286. throw new ValidateException('缺少中奖信息,请返回刷新重试');
  287. }
  288. /** @var LuckLotteryRecordServices $luckRecordServices */
  289. $luckRecordServices = app()->make(LuckLotteryRecordServices::class);
  290. $record = $luckRecordServices->get($activity_id);
  291. if (!$record) {
  292. throw new ValidateException('缺少中奖信息,请返回刷新重试');
  293. }
  294. if ($record['oid']) {
  295. throw new ValidateException('已经领取成功,不要重复领取');
  296. }
  297. if ($unique == '') {
  298. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  299. }
  300. /** @var StoreProductServices $productServices */
  301. $productServices = app()->make(StoreProductServices::class);
  302. $productInfo = $productServices->isValidProduct($productId);
  303. if (!$productInfo) {
  304. throw new ValidateException('该商品已下架或删除');
  305. }
  306. $attrInfo = $attrValueServices->getOne(['unique' => $unique, 'type' => 0]);
  307. if (!$unique || !$attrInfo || $attrInfo['product_id'] != $productId) {
  308. throw new ValidateException('请选择有效的商品属性');
  309. }
  310. $nowStock = $attrInfo['stock'];//现有平台库存
  311. if ($cartNum > $nowStock) {
  312. throw new ValidateException('该商品库存不足' . $cartNum);
  313. }
  314. break;
  315. case 9://拼单
  316. if ($unique == '') {
  317. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  318. }
  319. /** @var StoreProductServices $productServices */
  320. $productServices = app()->make(StoreProductServices::class);
  321. $productInfo = $productServices->isValidProduct($productId);
  322. if (!$productInfo) {
  323. throw new ValidateException('该商品已下架或删除');
  324. }
  325. $attrInfo = $attrValueServices->getOne(['unique' => $unique, 'type' => 0]);
  326. if (!$unique || !$attrInfo || $attrInfo['product_id'] != $productId) {
  327. throw new ValidateException('请选择有效的商品属性');
  328. }
  329. $nowStock = $attrInfo['stock'];//现有平台库存
  330. if (bcadd((string)$cartNum, (string)$sum_cart_num, 0) > $nowStock) {
  331. throw new ValidateException('拼单中该商品库存不足' . bcadd((string)$cartNum, (string)$sum_cart_num, 0));
  332. }
  333. break;
  334. case 10://桌码
  335. if ($unique == '') {
  336. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  337. }
  338. /** @var StoreProductServices $productServices */
  339. $productServices = app()->make(StoreProductServices::class);
  340. $productInfo = $productServices->isValidProduct($productId);
  341. if (!$productInfo) {
  342. throw new ValidateException('该商品已下架或删除');
  343. }
  344. $attrInfo = $attrValueServices->getOne(['unique' => $unique, 'type' => 0]);
  345. if (!$unique || !$attrInfo || $attrInfo['product_id'] != $productId) {
  346. throw new ValidateException('请选择有效的商品属性');
  347. }
  348. $nowStock = $attrInfo['stock'];//现有平台库存
  349. if (bcadd((string)$cartNum, (string)$sum_cart_num, 0) > $nowStock) {
  350. throw new ValidateException('桌码中该商品库存不足' . bcadd((string)$cartNum, (string)$sum_cart_num, 0));
  351. }
  352. break;
  353. default:
  354. throw new ValidateException('请刷新后重试');
  355. break;
  356. }
  357. if (in_array($type, [1, 2, 3])) {
  358. //根商品规格库存
  359. $product_stock = $attrValueServices->value(['product_id' => $productInfo['product_id'], 'suk' => $attrInfo['suk'], 'type' => 0], 'stock');
  360. if ($product_stock < $cartNum) {
  361. throw new ValidateException('商品库存不足' . $cartNum);
  362. }
  363. if (!CacheService::checkStock($unique, (int)$cartNum, $type)) {
  364. throw new ValidateException('商品库存不足' . $cartNum . ',无法购买请选择其他商品!');
  365. }
  366. }
  367. return [$attrInfo, $unique, $bargainUserInfo['bargain_price_min'] ?? 0, $cartNum, $productInfo];
  368. }
  369. /**
  370. * 添加购物车
  371. * @param int $uid
  372. * @param int $product_id
  373. * @param int $cart_num
  374. * @param string $product_attr_unique
  375. * @param int $type
  376. * @param bool $new
  377. * @param int $activity_id
  378. * @param int $discount_product_id
  379. * @return array
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\DbException
  382. * @throws \think\db\exception\ModelNotFoundException
  383. */
  384. public function setCart(int $uid, int $product_id, int $cart_num = 1, string $product_attr_unique = '', int $type = 0, bool $new = true, int $activity_id = 0, int $discount_product_id = 0)
  385. {
  386. if ($cart_num < 1) $cart_num = 1;
  387. //检测库存限量
  388. $store_id = $this->getItem('store_id', 0);
  389. $staff_id = $this->getItem('staff_id', 0);
  390. $tourist_uid = $this->getItem('tourist_uid', '');
  391. [$attrInfo, $product_attr_unique, $bargainPriceMin, $cart_num, $productInfo] = $this->checkProductStock(
  392. $uid,
  393. $product_id,
  394. $cart_num,
  395. $store_id,
  396. $product_attr_unique,
  397. $new,
  398. $type, $activity_id,
  399. $discount_product_id
  400. );
  401. $product_type = $productInfo['product_type'];
  402. if ($new) {
  403. /** @var StoreOrderCreateServices $storeOrderCreateService */
  404. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  405. $key = $storeOrderCreateService->getNewOrderId((string)$uid);
  406. //普通订单 && 商品是预售商品 订单类型改为预售订单
  407. if ($type == 0 && $productInfo['is_presale_product']) {
  408. $type = 6;
  409. }
  410. $info['id'] = $key;
  411. $info['type'] = $type;
  412. $info['store_id'] = $store_id;
  413. $info['tourist_uid'] = $tourist_uid;
  414. $info['product_type'] = $product_type;
  415. if ($type == 10 || $type == 9) {
  416. $info['collate_code_id'] = $activity_id;
  417. $activity_id = 0;
  418. }
  419. $info['activity_id'] = $activity_id;
  420. $info['discount_product_id'] = $discount_product_id;
  421. $info['product_id'] = $product_id;
  422. $info['product_attr_unique'] = $product_attr_unique;
  423. $info['cart_num'] = $cart_num;
  424. $info['productInfo'] = [];
  425. if ($productInfo) {
  426. $info['productInfo'] = is_object($productInfo) ? $productInfo->toArray() : $productInfo;
  427. }
  428. $info['attrInfo'] = $attrInfo->toArray();
  429. $info['productInfo']['attrInfo'] = $info['attrInfo'];
  430. $info['sum_price'] = $info['productInfo']['attrInfo']['price'] ?? $info['productInfo']['price'] ?? 0;
  431. //砍价
  432. if ($type == 2 && $activity_id) {
  433. $info['truePrice'] = $bargainPriceMin;
  434. $info['productInfo']['attrInfo']['price'] = $bargainPriceMin;
  435. } else {
  436. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? $info['productInfo']['price'] ?? 0;
  437. }
  438. //活动商品不参与会员价
  439. if ($type > 0 && $activity_id) {
  440. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? 0;
  441. $info['vip_truePrice'] = 0;
  442. }
  443. if ($type == 8) $info['is_luck'] = true;
  444. $info['trueStock'] = $info['productInfo']['attrInfo']['stock'] ?? 0;
  445. $info['costPrice'] = $info['productInfo']['attrInfo']['cost'] ?? 0;
  446. try {
  447. CacheService::redisHandler()->set($key, $info, 3600);
  448. } catch (\Throwable $e) {
  449. throw new ValidateException($e->getMessage());
  450. }
  451. return [$key, $cart_num];
  452. } else {//加入购物车记录
  453. ProductLogJob::dispatch(['cart', ['uid' => $uid, 'product_id' => $product_id, 'cart_num' => $cart_num]]);
  454. $cart = $this->dao->getOne(['type' => $type, 'uid' => $uid, 'tourist_uid' => $tourist_uid, 'product_id' => $product_id, 'product_attr_unique' => $product_attr_unique, 'is_del' => 0, 'is_new' => 0, 'is_pay' => 0, 'status' => 1, 'store_id' => $store_id, 'staff_id' => $staff_id]);
  455. if ($cart) {
  456. $cart->cart_num = $cart_num + $cart->cart_num;
  457. $cart->add_time = time();
  458. $cart->save();
  459. return [$cart->id, $cart->cart_num];
  460. } else {
  461. $add_time = time();
  462. $id = $this->dao->save(compact('uid', 'tourist_uid', 'store_id', 'staff_id', 'product_id', 'product_type', 'cart_num', 'product_attr_unique', 'type', 'activity_id', 'add_time'))->id;
  463. event('cart.add', [$uid, $tourist_uid, $store_id, $staff_id]);
  464. return [$id, $cart_num];
  465. }
  466. }
  467. }
  468. // /**拼单/桌码商品写入购物车
  469. // * @param int $uid
  470. // * @param int $product_id
  471. // * @param int $cart_num
  472. // * @param string $product_attr_unique
  473. // * @param int $type
  474. // * @param bool $new
  475. // * @param int $activity_id
  476. // * @param int $store_id
  477. // * @param int $discount_product_id
  478. // * @return mixed
  479. // * @throws \think\db\exception\DataNotFoundException
  480. // * @throws \think\db\exception\DbException
  481. // * @throws \think\db\exception\ModelNotFoundException
  482. // */
  483. // public function addCollageCart(int $uid, int $product_id, int $cart_num = 1, string $product_attr_unique = '', int $type = 0, bool $new = true, int $activity_id = 0, int $collate_code_id = 0, int $store_id = 0, int $is_settle = 0)
  484. // {
  485. // if ($cart_num < 1) $cart_num = 1;
  486. // //检测库存限量
  487. // $staff_id = $this->getItem('staff_id', 0);
  488. // $tourist_uid = $this->getItem('tourist_uid', '');
  489. // [$attrInfo, $product_attr_unique, $bargainPriceMin, $cart_num, $productInfo] = $this->checkProductStock(
  490. // $uid,
  491. // $product_id,
  492. // $cart_num,
  493. // $store_id,
  494. // $product_attr_unique,
  495. // $new,
  496. // $type,
  497. // $activity_id,
  498. // 0
  499. // );
  500. // $product_type = $productInfo['product_type'];
  501. // ProductLogJob::dispatch(['cart', ['uid' => $uid, 'product_id' => $product_id, 'cart_num' => $cart_num]]);
  502. // $cart = $this->dao->getOne(['type' => $type, 'uid' => $uid, 'product_id' => $product_id, 'product_attr_unique' => $product_attr_unique, 'activity_id' => $activity_id, 'collate_code_id' => $collate_code_id, 'is_del' => 0, 'is_new' => 0, 'is_pay' => 0, 'status' => 1, 'store_id' => $store_id]);
  503. // if ($cart) {
  504. // if (!$is_settle) {
  505. // $cart->cart_num = $cart_num + $cart->cart_num;
  506. // }
  507. // $cart->add_time = time();
  508. // $cart->save();
  509. // return $cart->id;
  510. // } else {
  511. // $add_time = time();
  512. // $id = $this->dao->save(compact('uid', 'store_id', 'staff_id', 'product_id', 'product_type', 'cart_num', 'product_attr_unique', 'type', 'activity_id', 'collate_code_id', 'add_time'))->id;
  513. // event('cart.add', [$uid, $tourist_uid, $store_id, $staff_id]);
  514. // return $id;
  515. // }
  516. // }
  517. /**
  518. * 移除购物车商品
  519. * @param int $uid
  520. * @param array $ids
  521. * @return StoreCartDao|bool
  522. */
  523. public function removeUserCart(int $uid, array $ids)
  524. {
  525. return $this->dao->removeUserCart($uid, $ids) !== false;
  526. }
  527. /**
  528. * 购物车 修改商品数量
  529. * @param $id
  530. * @param $number
  531. * @param $uid
  532. * @return bool|\crmeb\basic\BaseModel
  533. * @throws \think\db\exception\DataNotFoundException
  534. * @throws \think\db\exception\DbException
  535. * @throws \think\db\exception\ModelNotFoundException
  536. */
  537. public function changeUserCartNum($id, $number, $uid)
  538. {
  539. if (!$id || !$number) return false;
  540. $where = ['uid' => $uid, 'id' => $id];
  541. $carInfo = $this->dao->getOne($where, 'product_id,type,activity_id,product_attr_unique,cart_num');
  542. /** @var StoreProductServices $StoreProduct */
  543. $StoreProduct = app()->make(StoreProductServices::class);
  544. $stock = $StoreProduct->getProductStock($carInfo->product_id, $carInfo->product_attr_unique);
  545. if (!$stock) throw new ValidateException('暂无库存');
  546. if (!$number) throw new ValidateException('库存错误');
  547. if ($stock < $number) throw new ValidateException('库存不足' . $number);
  548. if ($carInfo->cart_num == $number) return true;
  549. $this->checkProductStock($uid, (int)$carInfo->product_id, (int)$number, 0, $carInfo->product_attr_unique, true);
  550. return $this->dao->changeUserCartNum(['uid' => $uid, 'id' => $id], (int)$number);
  551. }
  552. /**
  553. * 获取购物车列表
  554. * @param int $uid
  555. * @param int $status
  556. * @param array $cartIds
  557. * @param int $storeId
  558. * @param int $staff_id
  559. * @param int $shipping_type
  560. * @param int $touristUid
  561. * @param int $numType
  562. * @param bool $new
  563. * @param bool $isCart
  564. * @return array
  565. * @throws \think\db\exception\DataNotFoundException
  566. * @throws \think\db\exception\DbException
  567. * @throws \think\db\exception\ModelNotFoundException
  568. */
  569. public function getUserCartList(int $uid, int $status, array $cartIds = [], int $storeId = 0, int $staff_id = 0, int $shipping_type = -1, int $touristUid = 0, int $numType = 0, bool $new = false, bool $isCart = true)
  570. {
  571. // [$page, $limit] = $this->getPageValue();
  572. if ($new) {
  573. $cartIds = $cartIds && is_string($cartIds) ? explode(',', $cartIds) : (is_array($cartIds) ? $cartIds : []);
  574. $list = [];
  575. if ($cartIds) {
  576. foreach ($cartIds as $key) {
  577. $info = CacheService::redisHandler()->get($key);
  578. if ($info) {
  579. $list[] = $info;
  580. }
  581. }
  582. }
  583. } else {
  584. $where = ['uid' => $uid, 'store_id' => $storeId, 'tourist_uid' => $touristUid, 'cart_ids' => $cartIds];
  585. //有店员就证明在收银台中
  586. if ($staff_id) {
  587. $where['staff_id'] = $staff_id;
  588. }
  589. if ($status != -1) $where = array_merge($where, ['status' => $status]);
  590. $list = $this->dao->getCartList($where, 0, 0, ['productInfo', 'attrInfo']);
  591. }
  592. $count = $promotionsPrice = $coupon_price = $firstOrderPrice = 0;
  593. $cartList = $valid = $promotions = $coupon = $invalid = $type = $activity_id = [];
  594. if ($list) {
  595. [$list, $valid, $invalid] = $this->handleCartList($uid, $list, [], $shipping_type, $storeId);
  596. $activity_id = array_unique(array_column($list, 'activity_id'));
  597. $type = array_unique(array_column($list, 'type'));
  598. if (!($activity_id[0] ?? 0)) {
  599. /** @var StoreNewcomerServices $newcomerServices */
  600. $newcomerServices = app()->make(StoreNewcomerServices::class);
  601. $newcomerArr = $newcomerServices->checkUserFirstDiscount($uid);
  602. if ($newcomerArr) {
  603. //计算首单优惠
  604. [$valid, $firstOrderPrice, $first_discount, $first_discount_limit] = $this->computedFirstDiscount($uid, $valid, $newcomerArr);
  605. } else {
  606. /** @var StorePromotionsServices $storePromotionsServices */
  607. $storePromotionsServices = app()->make(StorePromotionsServices::class);
  608. //计算相关优惠活动
  609. [$valid, $coupon_price, $coupon, $promotions, $giveIntegral, $giveCoupon, $giveCartList] = $storePromotionsServices->computedPromotions($uid, $valid, $storeId, 0, $isCart);
  610. $cartList = array_merge($valid, $giveCartList);
  611. foreach ($cartList as $key => $cart) {
  612. if (isset($cart['promotions_true_price']) && isset($cart['price_type']) && $cart['price_type'] == 'promotions') {
  613. $promotionsPrice = bcadd((string)$promotionsPrice, (string)bcmul((string)$cart['promotions_true_price'], (string)$cart['cart_num'], 2), 2);
  614. }
  615. }
  616. }
  617. }
  618. if ($numType) {
  619. $count = count($valid);
  620. } else {
  621. $count = array_sum(array_column($valid, 'cart_num'));
  622. }
  623. }
  624. $deduction = ['type' => $type[0] ?? 0, 'activity_id' => $activity_id[0] ?? 0];
  625. $deduction['promotions_price'] = $promotionsPrice;
  626. $deduction['coupon_price'] = $coupon_price;
  627. $deduction['first_order_price'] = $firstOrderPrice;
  628. $user_store_id = $this->getItem('store_id', 0);
  629. $invalid_key = 'invalid_' . $user_store_id . '_' . $uid;
  630. //写入缓存
  631. if ($status == 1) {
  632. CacheService::redisHandler()->delete($invalid_key);
  633. if ($invalid) CacheService::redisHandler()->set($invalid_key, $invalid, 60);
  634. }
  635. //读取缓存
  636. if ($status == 0) {
  637. $other_invalid = CacheService::redisHandler()->get($invalid_key);
  638. if ($other_invalid) $invalid = array_merge($invalid, $other_invalid);
  639. }
  640. return ['promotions' => $promotions, 'coupon' => $coupon, 'valid' => $valid, 'invalid' => $invalid, 'deduction' => $deduction, 'count' => $count];
  641. }
  642. /**
  643. * 购物车重选
  644. * @param int $cart_id
  645. * @param int $product_id
  646. * @param string $unique
  647. */
  648. public function modifyCart(int $cart_id, int $product_id, string $unique)
  649. {
  650. /** @var StoreProductAttrValueServices $attrService */
  651. $attrService = app()->make(StoreProductAttrValueServices::class);
  652. $stock = $attrService->value(['product_id' => $product_id, 'unique' => $unique, 'type' => 0], 'stock');
  653. if ($stock > 0) {
  654. $this->dao->update($cart_id, ['product_attr_unique' => $unique, 'cart_num' => 1]);
  655. } else {
  656. throw new ValidateException('选择的规格库存不足');
  657. }
  658. }
  659. /**
  660. * 重选购物车
  661. * @param $id
  662. * @param $uid
  663. * @param $productId
  664. * @param $unique
  665. * @param $num
  666. * @param int $store_id
  667. * @throws \think\db\exception\DataNotFoundException
  668. * @throws \think\db\exception\DbException
  669. * @throws \think\db\exception\ModelNotFoundException
  670. */
  671. public function resetCart($id, $uid, $productId, $unique, $num, int $store_id = 0)
  672. {
  673. $res = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique, 'store_id' => $store_id]);
  674. if ($res) {
  675. /** @var StoreProductServices $StoreProduct */
  676. $StoreProduct = app()->make(StoreProductServices::class);
  677. $stock = $StoreProduct->getProductStock((int)$productId, $unique);
  678. $cart_num = $res->cart_num + $num;
  679. if ($cart_num > $stock) {
  680. $cart_num = $stock;
  681. }
  682. $res->cart_num = $cart_num;
  683. $res->save();
  684. if ($res['id'] != $id) $this->dao->delete($id);
  685. } else {
  686. $this->dao->update($id, ['product_attr_unique' => $unique, 'cart_num' => $num]);
  687. }
  688. }
  689. /**
  690. * 首页加入购物车
  691. * @param int $uid
  692. * @param int $productId
  693. * @param int $num
  694. * @param string $unique
  695. * @param int $type
  696. * @return mixed
  697. * @throws \think\db\exception\DataNotFoundException
  698. * @throws \think\db\exception\DbException
  699. * @throws \think\db\exception\ModelNotFoundException
  700. */
  701. public function setCartNum(int $uid, int $productId, int $num, string $unique, int $type)
  702. {
  703. /** @var StoreProductAttrValueServices $attrValueServices */
  704. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  705. if ($unique == '') {
  706. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  707. }
  708. /** @var StoreProductServices $productServices */
  709. $productServices = app()->make(StoreProductServices::class);
  710. $productInfo = $productServices->isValidProduct((int)$productId);
  711. if (!$productInfo) {
  712. throw new ValidateException('该商品已下架或删除');
  713. }
  714. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  715. throw new ValidateException('请选择有效的商品属性');
  716. }
  717. $stock = $productServices->getProductStock((int)$productId, $unique);
  718. if ($stock < $num) {
  719. throw new ValidateException('该商品库存不足' . $num);
  720. }
  721. //预售商品
  722. if ($productInfo['is_presale_product']) {
  723. if ($productInfo['presale_start_time'] > time()) throw new ValidateException('预售活动未开始');
  724. if ($productInfo['presale_end_time'] < time()) throw new ValidateException('预售活动已结束');
  725. }
  726. //检查限购
  727. if ($type != 0) $this->checkLimit($uid, $productId, $num);
  728. $cart = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique, 'store_id' => 0]);
  729. if ($cart) {
  730. if ($type == -1) {
  731. $cart->cart_num = $num;
  732. } elseif ($type == 0) {
  733. $cart->cart_num = $cart->cart_num - $num;
  734. } elseif ($type == 1) {
  735. if ($cart->cart_num >= $stock) {
  736. throw new ValidateException('该商品库存只有' . $stock);
  737. }
  738. $new_cart_num = $cart->cart_num + $num;
  739. if ($new_cart_num > $stock) {
  740. $new_cart_num = $stock;
  741. }
  742. $cart->cart_num = $new_cart_num;
  743. }
  744. if ($cart->cart_num === 0) {
  745. return $this->dao->delete($cart->id);
  746. } else {
  747. $cart->add_time = time();
  748. $cart->save();
  749. return $cart->id;
  750. }
  751. } else {
  752. $data = [
  753. 'uid' => $uid,
  754. 'product_id' => $productId,
  755. 'product_type' => $productInfo['product_type'],
  756. 'cart_num' => $num,
  757. 'product_attr_unique' => $unique,
  758. 'type' => 0,
  759. 'add_time' => time()
  760. ];
  761. $id = $this->dao->save($data)->id;
  762. event('cart.add', [$uid, 0, 0, 0]);
  763. return $id;
  764. }
  765. }
  766. /**
  767. * 用户购物车商品统计
  768. * @param int $uid
  769. * @param string $numType
  770. * @throws \think\db\exception\DataNotFoundException
  771. * @throws \think\db\exception\DbException
  772. * @throws \think\db\exception\ModelNotFoundException
  773. */
  774. public function getUserCartCount(int $uid, string $numType = '0', int $store_id = 0)
  775. {
  776. $count = 0;
  777. $ids = [];
  778. $cartNums = [];
  779. $sum_price = 0;
  780. $cartList = $this->dao->getUserCartList(['uid' => $uid, 'status' => 1, 'store_id' => 0], 'id,cart_num,product_id,product_attr_unique');
  781. if ($cartList) {
  782. /** @var StoreProductServices $storeProductServices */
  783. $storeProductServices = app()->make(StoreProductServices::class);
  784. $productInfos = $storeProductServices->getColumn([['id', 'in', array_column($cartList, 'product_id')]], 'id,pid,type,relation_id', 'id');
  785. /** @var StoreProductAttrValueServices $storePrdouctAttrValueServices */
  786. $storePrdouctAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  787. $attrInfos = $storePrdouctAttrValueServices->getColumn([['unique', 'in', array_column($cartList, 'product_attr_unique')]], 'id,unique', 'unique');
  788. foreach ($cartList as $cart) {
  789. $productInfo = $productInfos[$cart['product_id']] ?? [];
  790. if (!$productInfo) continue;
  791. $attrInfo = $attrInfos[$cart['product_attr_unique']] ?? [];
  792. if (!$attrInfo) continue;
  793. if ($store_id) {//某门店加入购物车商品数量
  794. if (in_array($productInfo['type'], [0, 2]) || ($productInfo['type'] == 1 && $productInfo['relation_id'] == $store_id) || ($productInfo['type'] == 1 && $productInfo['pid'] > 0)) {
  795. $ids[] = $cart['id'];
  796. $cartNums[] = $cart['cart_num'];
  797. }
  798. } else {
  799. if (in_array($productInfo['type'], [0, 2]) || ($productInfo['type'] == 1 && $productInfo['pid'] > 0)) {
  800. $ids[] = $cart['id'];
  801. $cartNums[] = $cart['cart_num'];
  802. }
  803. }
  804. }
  805. if ($numType) {
  806. $count = count($ids);
  807. } else {
  808. $count = array_sum($cartNums);
  809. }
  810. }
  811. return compact('count', 'ids', 'sum_price');
  812. }
  813. /**
  814. * 处理购物车数据
  815. * @param int $uid
  816. * @param array $cartList
  817. * @param array $addr
  818. * @param int $shipping_type
  819. * @param int $store_id
  820. * @return array
  821. * @throws \think\db\exception\DataNotFoundException
  822. * @throws \think\db\exception\DbException
  823. * @throws \think\db\exception\ModelNotFoundException
  824. */
  825. public function handleCartList(int $uid, array $cartList, array $addr = [], int $shipping_type = 1, int $store_id = 0)
  826. {
  827. if (!$cartList) {
  828. return [$cartList, [], [], [], 0, [], []];
  829. }
  830. /** @var StoreProductServices $productServices */
  831. $productServices = app()->make(StoreProductServices::class);
  832. /** @var MemberCardServices $memberCardService */
  833. $memberCardService = app()->make(MemberCardServices::class);
  834. $vipStatus = $memberCardService->isOpenMemberCardCache('vip_price', false);
  835. $tempIds = [];
  836. $userInfo = [];
  837. $discount = 100;
  838. $productIds = $allStock = $attrUniquesArr = [];
  839. if ($uid) {
  840. /** @var UserServices $user */
  841. $user = app()->make(UserServices::class);
  842. $userInfo = $user->getUserCacheInfo($uid);
  843. if (!$userInfo) {
  844. throw new ValidateException('用户不存在');
  845. }
  846. $userInfo = $userInfo->toArray();
  847. //用户等级是否开启
  848. if (sys_config('member_func_status', 1) && $userInfo) {
  849. /** @var SystemUserLevelServices $systemLevel */
  850. $systemLevel = app()->make(SystemUserLevelServices::class);
  851. $discount = $systemLevel->getDiscount($uid, (int)$userInfo['level'] ?? 0);
  852. }
  853. }
  854. //不送达运费模板
  855. if ($shipping_type == 1 && $addr) {
  856. $cityId = (int)($addr['city_id'] ?? 0);
  857. if ($cityId) {
  858. /** @var CityAreaServices $cityAreaServices */
  859. $cityAreaServices = app()->make(CityAreaServices::class);
  860. $cityIds = $cityAreaServices->getRelationCityIds($cityId);
  861. foreach ($cartList as $item) {
  862. $tempIds[] = $item['productInfo']['temp_id'];
  863. }
  864. $tempIds = array_unique($tempIds);
  865. /** @var ShippingTemplatesServices $shippingService */
  866. $shippingService = app()->make(ShippingTemplatesServices::class);
  867. $tempIds = $shippingService->getColumn([['id', 'in', $tempIds], ['no_delivery', '=', 1]], 'id');
  868. if ($tempIds) {
  869. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  870. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  871. $tempIds = $noDeliveryServices->isNoDelivery($tempIds, $cityIds);
  872. }
  873. }
  874. }
  875. $latitude = $this->getItem('latitude', '');
  876. $longitude = $this->getItem('longitude', '');
  877. $user_store_id = $this->getItem('store_id', 0);
  878. $store_id = $store_id ?: $user_store_id;
  879. //平台是否开启门店自提
  880. $store_self_mention = sys_config('store_func_status', 1) && sys_config('store_self_mention');
  881. $cart_type = 0;
  882. if ($store_id) {//平台商品,在门店购买 验证门店库存
  883. /** @var StoreProductAttrValueServices $skuValueServices */
  884. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  885. /** @var StoreBranchProductServices $branchProductServics */
  886. $branchProductServics = app()->make(StoreBranchProductServices::class);
  887. foreach ($cartList as $cart) {
  888. $productInfo = $cart['productInfo'] ?? [];
  889. if (!$productInfo) continue;
  890. $product_id = 0;
  891. if (in_array($productInfo['type'], [0, 2])) {
  892. $product_id = $productInfo['id'];
  893. } else {//门店商品
  894. if ($productInfo['pid'] && $productInfo['relation_id'] != $store_id) {//平台共享商品到另一个门店购买
  895. $product_id = $productInfo['pid'];
  896. }
  897. }
  898. if (!$product_id) {//自己门店购买不用再次验证库存
  899. continue;
  900. }
  901. $productIds[] = $cart['product_id'];
  902. $suk = '';
  903. $cart_type = $cart['type'];
  904. //类型 0:普通、1:秒杀、2:砍价、3:拼团、4:积分、5:套餐、6:预售、7:新人礼、8:抽奖、9:拼单、10:桌码
  905. switch ($cart['type']) {
  906. case 0:
  907. case 6:
  908. case 8:
  909. case 9:
  910. case 10:
  911. $suk = $skuValueServices->value(['unique' => $cart['product_attr_unique'], 'product_id' => $cart['product_id'], 'type' => 0], 'suk');
  912. break;
  913. case 1:
  914. case 2:
  915. case 3:
  916. case 5:
  917. case 7:
  918. if ($cart['type'] == 5 && isset($cart['discount_product_id'])) {
  919. $product_id = $cart['discount_product_id'];
  920. } else {
  921. $product_id = $cart['activity_id'];
  922. }
  923. $suk = $skuValueServices->value(['unique' => $cart['product_attr_unique'], 'product_id' => $product_id, 'type' => $cart['type']], 'suk');
  924. break;
  925. }
  926. $branchProductInfo = $branchProductServics->isValidStoreProduct((int)$cart['product_id'], $store_id);
  927. if (!$branchProductInfo) {
  928. continue;
  929. }
  930. $attrValue = '';
  931. if ($suk) {
  932. $attrValue = $skuValueServices->get(['suk' => $suk, 'product_id' => $branchProductInfo['id'], 'type' => 0]);
  933. }
  934. if (!$attrValue) {
  935. continue;
  936. }
  937. $allStock[$attrValue['unique']] = $attrValue['stock'];
  938. $attrUniquesArr[$cart['product_attr_unique']] = $attrValue['unique'];
  939. }
  940. } else {
  941. $productIds = array_unique(array_column($cartList, 'product_id'));
  942. }
  943. $storeInfo = [];
  944. /** @var SystemStoreServices $storeServices */
  945. $storeServices = app()->make(SystemStoreServices::class);
  946. if ($user_store_id) {
  947. $storeInfo = $storeServices->getNearbyStore(['id' => $user_store_id], '', '', '', 1);
  948. } else if ($latitude && $longitude) {
  949. $storeInfo = $storeServices->getNearbyStore([], $latitude, $longitude, '', 1);
  950. }
  951. $valid = $invalid = [];
  952. foreach ($cartList as &$item) {
  953. $item['is_gift'] = 0;
  954. if (isset($item['productInfo']['delivery_type'])) {
  955. $item['productInfo']['delivery_type'] = is_string($item['productInfo']['delivery_type']) ? explode(',', $item['productInfo']['delivery_type']) : $item['productInfo']['delivery_type'];
  956. } else {
  957. $item['productInfo']['delivery_type'] = [];
  958. }
  959. $item['productInfo']['express_delivery'] = in_array(1, $item['productInfo']['delivery_type']);
  960. $item['productInfo']['store_mention'] = in_array(2, $item['productInfo']['delivery_type']);
  961. $item['productInfo']['store_delivery'] = in_array(3, $item['productInfo']['delivery_type']);
  962. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  963. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  964. }
  965. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  966. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  967. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  968. if (isset($item['productInfo']['attrInfo'])) {
  969. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  970. }
  971. $item['productInfo'] = get_thumb_water($item['productInfo']);
  972. $productInfo = $item['productInfo'];
  973. $item['vip_truePrice'] = 0;
  974. //门店独立商品
  975. $isBranchProduct = isset($productInfo['type']) && isset($productInfo['pid']) && $productInfo['type'] == 1 && !$productInfo['pid'];
  976. $product_store_id = $isBranchProduct ? $productInfo['relation_id'] : 0;
  977. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  978. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  979. $item['trueStock'] = $item['branch_stock'] = $productInfo['attrInfo']['stock'] ?? 0;
  980. $item['branch_sales'] = $productInfo['attrInfo']['sales'] ?? 0;
  981. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  982. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  983. if ((!$item['type'] || !$item['activity_id']) && !$isBranchProduct) {
  984. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  985. $item['truePrice'] = $truePrice;
  986. $item['vip_truePrice'] = $vip_truePrice;
  987. $item['price_type'] = $type;
  988. }
  989. } else {
  990. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  991. $item['trueStock'] = $item['branch_sales'] = $item['productInfo']['stock'] ?? 0;
  992. $item['branch_sales'] = $item['productInfo']['sales'] ?? 0;
  993. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  994. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  995. if ((!$item['type'] || !$item['activity_id']) && !$isBranchProduct) {
  996. [$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);
  997. $item['truePrice'] = $truePrice;
  998. $item['vip_truePrice'] = $vip_truePrice;
  999. $item['price_type'] = $type;
  1000. }
  1001. }
  1002. $applicable_type = $item['productInfo']['applicable_type'] ?? 1;
  1003. $applicable_store_id = [];
  1004. if (isset($item['productInfo']['applicable_store_id'])) {
  1005. $applicable_store_id = is_string($item['productInfo']['applicable_store_id']) ? explode(',', $item['productInfo']['applicable_store_id']) : $item['productInfo']['applicable_store_id'];
  1006. }
  1007. $applicableStatus = !$store_id || $applicable_type == 1 || ($applicable_type == 2 && in_array($store_id, $applicable_store_id));
  1008. if (isset($item['status']) && $item['status'] == 0) {
  1009. $item['is_valid'] = 0;
  1010. $item['invalid_desc'] = '此商品已失效';
  1011. $invalid[] = $item;
  1012. } elseif (($item['productInfo']['type'] ?? 0) == 1 && ($item['productInfo']['pid'] ?? 0) == 0 && $storeInfo && ($item['productInfo']['relation_id'] ?? 0) != $storeInfo['id'] && $cart_type != 10) {
  1013. $item['is_valid'] = 0;
  1014. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1015. $invalid[] = $item;
  1016. } elseif ((isset($item['productInfo']['delivery_type']) && !$item['productInfo']['delivery_type']) || in_array($item['productInfo']['product_type'], [1, 2, 3])) {
  1017. $item['is_valid'] = 1;
  1018. $valid[] = $item;
  1019. } elseif (!$applicableStatus) {
  1020. $item['is_valid'] = 0;
  1021. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1022. $invalid[] = $item;
  1023. } else {
  1024. $condition = !in_array(isset($item['productInfo']['product_id']) ? $item['productInfo']['product_id'] : $item['productInfo']['id'], $productIds) || $item['cart_num'] > ($allStock[$attrUniquesArr[$item['product_attr_unique']] ?? ''] ?? 0);
  1025. switch ($shipping_type) {
  1026. case -1://购物车列表展示
  1027. if ($isBranchProduct && $store_id && ($store_id != $product_store_id)) {
  1028. $item['is_valid'] = 0;
  1029. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1030. $invalid[] = $item;
  1031. } else {
  1032. $item['is_valid'] = 1;
  1033. $valid[] = $item;
  1034. }
  1035. break;
  1036. case 1:
  1037. //不送达
  1038. if (in_array($item['productInfo']['temp_id'], $tempIds) || (isset($item['productInfo']['delivery_type']) && !in_array(1, $item['productInfo']['delivery_type']) && !in_array(3, $item['productInfo']['delivery_type']))) {
  1039. $item['is_valid'] = 0;
  1040. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1041. $invalid[] = $item;
  1042. } elseif ($isBranchProduct && $store_id && ($store_id != $product_store_id || !in_array(3, $item['productInfo']['delivery_type']))) {
  1043. $item['is_valid'] = 0;
  1044. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1045. $invalid[] = $item;
  1046. } elseif ((in_array($productInfo['type'], [0, 2]) || $productInfo['relation_id'] != $store_id) && $store_id && ($condition || (!in_array(2, $item['productInfo']['delivery_type']) && !in_array(3, $item['productInfo']['delivery_type'])))) {//平台商品 在门店购买 验证门店库存
  1047. $item['is_valid'] = 0;
  1048. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1049. $invalid[] = $item;
  1050. } elseif (!$storeServices->checkStoreDeliveryScope($store_id, $addr, $latitude, $longitude)) {
  1051. $item['is_valid'] = 0;
  1052. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1053. $invalid[] = $item;
  1054. } else {
  1055. $item['is_valid'] = 1;
  1056. $valid[] = $item;
  1057. }
  1058. break;
  1059. case 2:
  1060. //不支持到店自提
  1061. if (!$store_self_mention) {
  1062. $item['is_valid'] = 0;
  1063. $item['invalid_desc'] = '平台/门店已关闭自提';
  1064. $invalid[] = $item;
  1065. } elseif (isset($item['productInfo']['delivery_type']) && $item['productInfo']['delivery_type'] && !in_array(2, $item['productInfo']['delivery_type'])) {
  1066. $item['is_valid'] = 0;
  1067. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1068. $invalid[] = $item;
  1069. } elseif ($isBranchProduct && $store_id && $store_id != $product_store_id) {
  1070. $item['is_valid'] = 0;
  1071. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1072. $invalid[] = $item;
  1073. } elseif ($item['productInfo']['product_type'] == 1) {
  1074. $item['is_valid'] = 0;
  1075. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1076. $invalid[] = $item;
  1077. } elseif ((in_array($productInfo['type'], [0, 2]) || $productInfo['relation_id'] != $store_id) && $store_id && $condition) {//平台、供应商商品 在门店购买 验证门店库存
  1078. $item['is_valid'] = 0;
  1079. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1080. $invalid[] = $item;
  1081. } else {
  1082. $item['is_valid'] = 1;
  1083. $valid[] = $item;
  1084. }
  1085. break;
  1086. case 4:
  1087. //无库存||下架
  1088. if ($isBranchProduct && $store_id && $store_id != $product_store_id) {
  1089. $item['is_valid'] = 0;
  1090. $item['invalid_desc'] = '此商品超出配送/自提范围';
  1091. $invalid[] = $item;
  1092. } elseif (in_array($productInfo['type'], [0, 2]) && $store_id && $condition) {
  1093. $item['is_valid'] = 0;
  1094. $invalid[] = $item;
  1095. } else {
  1096. $item['is_valid'] = 1;
  1097. $valid[] = $item;
  1098. }
  1099. break;
  1100. default:
  1101. $item['is_valid'] = 1;
  1102. $valid[] = $item;
  1103. break;
  1104. }
  1105. }
  1106. unset($item['attrInfo']);
  1107. }
  1108. return [$cartList, $valid, $invalid];
  1109. }
  1110. /**
  1111. * 门店给用户加入购物车
  1112. * @param int $uid
  1113. * @param int $productId
  1114. * @param int $cartNum
  1115. * @param string $unique
  1116. * @param int $staff_id
  1117. * @return mixed
  1118. * @throws \think\db\exception\DataNotFoundException
  1119. * @throws \think\db\exception\DbException
  1120. * @throws \think\db\exception\ModelNotFoundException
  1121. */
  1122. public function addCashierCart(int $uid, int $productId, int $cartNum, string $unique, int $staff_id = 0)
  1123. {
  1124. $store_id = $this->getItem('store_id', 0);
  1125. $tourist_uid = $this->getItem('tourist_uid', '');
  1126. if (!$store_id) {
  1127. throw new ValidateException('缺少门店ID');
  1128. }
  1129. [$nowStock, $unique, $bargainPriceMin, $cart_num, $productInfo] = $this->checkProductStock($uid, $productId, $cartNum, $store_id, $unique, true);
  1130. ProductLogJob::dispatch(['cart', ['uid' => $uid, 'product_id' => $productId, 'cart_num' => $cartNum]]);
  1131. $cart = $this->dao->getOne([
  1132. 'uid' => $uid,
  1133. 'product_id' => $productId,
  1134. 'product_attr_unique' => $unique,
  1135. 'store_id' => $store_id,
  1136. 'staff_id' => $staff_id,
  1137. 'tourist_uid' => $tourist_uid,
  1138. 'is_del' => 0,
  1139. 'is_new' => 0,
  1140. 'is_pay' => 0,
  1141. 'status' => 1
  1142. ]);
  1143. if ($cart) {
  1144. if ($nowStock < ($cartNum + $cart['cart_num'])) {
  1145. $cartNum = $nowStock - $cartNum;//剩余库存
  1146. }
  1147. if ($cartNum == 0) throw new ValidateException('库存不足');
  1148. $cart->cart_num = $cartNum + $cart->cart_num;
  1149. $cart->add_time = time();
  1150. $cart->save();
  1151. return $cart->id;
  1152. } else {
  1153. $add_time = time();
  1154. $data = compact('uid', 'store_id', 'add_time', 'tourist_uid');
  1155. $data['type'] = 0;
  1156. $data['product_id'] = $productId;
  1157. $data['product_type'] = $productInfo['product_type'];
  1158. $data['cart_num'] = $cartNum;
  1159. $data['product_attr_unique'] = $unique;
  1160. $data['store_id'] = $store_id;
  1161. $data['staff_id'] = $staff_id;
  1162. $id = $this->dao->save($data)->id;
  1163. event('cart.add', [$uid, $tourist_uid, $store_id, $staff_id]);
  1164. return $id;
  1165. }
  1166. }
  1167. /**
  1168. * @param int $id
  1169. * @param int $number
  1170. * @param int $uid
  1171. * @param int $storeId
  1172. * @return bool|\crmeb\basic\BaseModel
  1173. * @throws \think\db\exception\DataNotFoundException
  1174. * @throws \think\db\exception\DbException
  1175. * @throws \think\db\exception\ModelNotFoundException
  1176. */
  1177. public function changeCashierCartNum(int $id, int $number, int $uid, int $storeId)
  1178. {
  1179. if (!$id || !$number) return false;
  1180. $where = ['uid' => $uid, 'id' => $id, 'store_id' => $storeId];
  1181. $carInfo = $this->dao->getOne($where, 'product_id,product_attr_unique,cart_num');
  1182. /** @var StoreBranchProductServices $storeProduct */
  1183. $storeProduct = app()->make(StoreBranchProductServices::class);
  1184. $stock = $storeProduct->getProductStock($carInfo->product_id, $storeId, $carInfo->product_attr_unique);
  1185. if (!$stock) throw new ValidateException('暂无库存');
  1186. if ($stock < $number) throw new ValidateException('库存不足' . $number);
  1187. if ($carInfo->cart_num == $number) return true;
  1188. $this->setItem('is_set', 1);
  1189. $this->checkProductStock($uid, (int)$carInfo->product_id, $number, $storeId, $carInfo->product_attr_unique, true);
  1190. $this->reset();
  1191. return $this->dao->changeUserCartNum(['uid' => $uid, 'id' => $id], (int)$number);
  1192. }
  1193. /**
  1194. * 购物车重选
  1195. * @param int $cart_id
  1196. * @param int $product_id
  1197. * @param string $unique
  1198. */
  1199. public function modifyCashierCart(int $storeId, int $cart_id, int $product_id, string $unique)
  1200. {
  1201. /** @var StoreProductAttrValueServices $attrService */
  1202. $attrService = app()->make(StoreProductAttrValueServices::class);
  1203. $stock = $attrService->value(['product_id' => $product_id, 'unique' => $unique, 'type' => 0], 'stock');
  1204. if ($stock > 0) {
  1205. $this->dao->update($cart_id, ['product_attr_unique' => $unique, 'cart_num' => 1]);
  1206. } else {
  1207. throw new ValidateException('选择的规格库存不足');
  1208. }
  1209. }
  1210. /**
  1211. * 批量加入购物车
  1212. * @param array $cart
  1213. * @param int $storeId
  1214. * @param int $uid
  1215. * @return array
  1216. * @throws \think\db\exception\DataNotFoundException
  1217. * @throws \think\db\exception\DbException
  1218. * @throws \think\db\exception\ModelNotFoundException
  1219. */
  1220. public function batchAddCart(array $cart, int $storeId, int $uid)
  1221. {
  1222. $this->setItem('store_id', $storeId);
  1223. $cartIds = [];
  1224. foreach ($cart as $item) {
  1225. if (!isset($item['productId'])) {
  1226. throw new ValidateException('缺少商品ID');
  1227. }
  1228. if (!isset($item['cartNum'])) {
  1229. throw new ValidateException('缺少购买商品数量');
  1230. }
  1231. if (!isset($item['uniqueId'])) {
  1232. throw new ValidateException('缺少唯一值');
  1233. }
  1234. $cartIds[] = $this->addCashierCart($uid, (int)$item['productId'], (int)$item['cartNum'], $item['uniqueId']);
  1235. }
  1236. $this->reset();
  1237. return $cartIds;
  1238. }
  1239. /**
  1240. * 组合前端购物车需要的数据结构
  1241. * @param array $cartList
  1242. * @param array $protmoions
  1243. * @return array
  1244. */
  1245. public function getReturnCartList(array $cartList, array $promotions)
  1246. {
  1247. $result = [];
  1248. if ($cartList) {
  1249. if ($promotions) $promotions = array_combine(array_column($promotions, 'id'), $promotions);
  1250. $i = 0;
  1251. foreach ($cartList as $key => $cart) {
  1252. $data = ['promotions' => [], 'pids' => [], 'cart' => []];
  1253. if ($result && isset($cart['promotions_id']) && $cart['promotions_id'] && (!isset($cart['collate_code_id']) || $cart['collate_code_id'] <= 0)) {
  1254. $isTure = false;
  1255. foreach ($result as $key => &$res) {
  1256. if (array_intersect($res['pids'], $cart['promotions_id'])) {
  1257. $res['pids'] = array_unique(array_merge($res['pids'], $cart['promotions_id'] ?? []));
  1258. $res['cart'][] = $cart;
  1259. $isTure = true;
  1260. break;
  1261. }
  1262. }
  1263. if (!$isTure) {
  1264. $data['cart'][] = $cart;
  1265. $data['pids'] = array_unique($cart['promotions_id'] ?? []);
  1266. $result[$i] = $data;
  1267. $i++;
  1268. }
  1269. } else {
  1270. $data['cart'][] = $cart;
  1271. $data['pids'] = array_unique($cart['promotions_id'] ?? []);
  1272. $result[$i] = $data;
  1273. $i++;
  1274. }
  1275. }
  1276. foreach ($result as $key => &$item) {
  1277. if ($item['pids']) {
  1278. foreach ($item['pids'] as $key => $id) {
  1279. $item['promotions'][] = $promotions[$id] ?? [];
  1280. }
  1281. }
  1282. }
  1283. }
  1284. return $result;
  1285. }
  1286. /**
  1287. * 控制购物车加入商品最大数量
  1288. * @param int $uid
  1289. * @param int $tourist_uid
  1290. * @param int $store_id
  1291. * @param int $staff_id
  1292. * @return bool
  1293. * @throws \think\db\exception\DataNotFoundException
  1294. * @throws \think\db\exception\DbException
  1295. * @throws \think\db\exception\ModelNotFoundException
  1296. */
  1297. public function controlCartNum(int $uid, int $tourist_uid = 0, int $store_id = 0, int $staff_id = 0)
  1298. {
  1299. $maxCartNum = $this->maxCartNum;
  1300. $where = [
  1301. 'is_del' => 0,
  1302. 'is_new' => 0,
  1303. 'is_pay' => 0,
  1304. 'status' => 1
  1305. ];
  1306. if ($uid) $where['uid'] = $uid;
  1307. if ($tourist_uid) $where['tourist_uid'] = $tourist_uid;
  1308. if ($store_id) $where['store_id'] = $store_id;
  1309. if ($staff_id) $where['staff_id'] = $staff_id;
  1310. try {
  1311. $count = $this->dao->count($where);
  1312. if ($count >= $maxCartNum) {//删除一个最早加入购物车商品
  1313. $one = $this->dao->search($where)->order('id asc')->find();
  1314. if ($one) {
  1315. $this->dao->delete($one['id']);
  1316. }
  1317. }
  1318. } catch (\Throwable $e) {
  1319. \think\facade\Log::error('自动控制购物车数量,删除最早加入商品失败:' . $e->getMessage());
  1320. }
  1321. return true;
  1322. }
  1323. /**
  1324. * 检测限购
  1325. * @param int $uid
  1326. * @param int $product_id
  1327. * @param int $num
  1328. * @param bool $new
  1329. * @param int $store_id
  1330. * @return bool
  1331. */
  1332. public function checkLimit(int $uid, int $product_id, int $num, bool $new = false, int $store_id = 0)
  1333. {
  1334. /** @var StoreProductServices $productServices */
  1335. $productServices = app()->make(StoreProductServices::class);
  1336. /** @var StoreOrderCartInfoServices $orderCartServices */
  1337. $orderCartServices = app()->make(StoreOrderCartInfoServices::class);
  1338. $limitInfo = $productServices->get($product_id, ['id', 'pid', 'is_limit', 'limit_type', 'limit_num']);
  1339. if (!$limitInfo) throw new ValidateException('商品不存在');
  1340. $limitInfo = $limitInfo->toArray();
  1341. if (!$limitInfo['is_limit']) return true;
  1342. $cartNum = 0;
  1343. //收银台游客限购
  1344. $tourist_uid = 0;
  1345. if (!$uid) {
  1346. $tourist_uid = $this->getItem('tourist_uid', '');
  1347. }
  1348. $pid = $limitInfo['pid'] ? $limitInfo['pid'] : $limitInfo['id'];
  1349. $product_ids = $productServices->getColumn(['pid' => $pid], 'id');
  1350. $product_ids[] = $pid;
  1351. if (!$new) {// 购物车商品数量
  1352. $cartNum = $this->dao->sum(['uid' => $uid, 'tourist_uid' => $tourist_uid, 'product_id' => $product_ids, 'store_id' => $store_id, 'status' => 1, 'is_del' => 0], 'cart_num', true);
  1353. }
  1354. if ($limitInfo['limit_type'] == 1) {
  1355. if (($num + $cartNum) > $limitInfo['limit_num']) {
  1356. throw new ValidateException('单次购买不能超过 ' . $limitInfo['limit_num'] . ' 件');
  1357. }
  1358. } else if ($limitInfo['limit_type'] == 2) {
  1359. $orderPayNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_ids], 'cart_num');
  1360. $orderRefundNum = $orderCartServices->sum(['uid' => $uid, 'product_id' => $product_ids], 'refund_num');
  1361. $orderNum = $cartNum + $orderPayNum - $orderRefundNum;
  1362. if (($num + $orderNum) > $limitInfo['limit_num']) {
  1363. throw new ValidateException('该商品限购 ' . $limitInfo['limit_num'] . ' 件,您已经购买 ' . $orderNum . ' 件');
  1364. }
  1365. }
  1366. return true;
  1367. }
  1368. /**
  1369. * 计算用户购物车商品(优惠活动、最优优惠券)
  1370. * @param array $user
  1371. * @param $cartId
  1372. * @param bool $new
  1373. * @param int $addressId
  1374. * @param int $shipping_type
  1375. * @param int $store_id
  1376. * @return array
  1377. * @throws \Psr\SimpleCache\InvalidArgumentException
  1378. * @throws \think\db\exception\DataNotFoundException
  1379. * @throws \think\db\exception\DbException
  1380. * @throws \think\db\exception\ModelNotFoundException
  1381. * @throws \throwable
  1382. */
  1383. public function computeUserCart(array $user, $cartId, bool $new, int $addressId, int $shipping_type = 1, int $store_id = 0)
  1384. {
  1385. $addr = $data = [];
  1386. $uid = (int)$user['uid'];
  1387. /** @var UserAddressServices $addressServices */
  1388. $addressServices = app()->make(UserAddressServices::class);
  1389. if ($addressId) {
  1390. $addr = $addressServices->getAdderssCache($addressId);
  1391. }
  1392. //没传地址id或地址已删除未找到 ||获取默认地址
  1393. if (!$addr) {
  1394. $addr = $addressServices->getUserDefaultAddressCache($uid);
  1395. }
  1396. $data['upgrade_addr'] = 0;
  1397. if ($addr) {
  1398. $addr = is_object($addr) ? $addr->toArray() : $addr;
  1399. if (isset($addr['upgrade']) && $addr['upgrade'] == 0) {
  1400. $data['upgrade_addr'] = 1;
  1401. }
  1402. } else {
  1403. $addr = [];
  1404. }
  1405. if ($store_id) {
  1406. /** @var SystemStoreServices $storeServices */
  1407. $storeServices = app()->make(SystemStoreServices::class);
  1408. $storeServices->getStoreInfo($store_id);
  1409. }
  1410. //获取购物车信息
  1411. $cartGroup = $this->getUserProductCartListV1($uid, $cartId, $new, $addr, $shipping_type, $store_id, 0, true);
  1412. $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;//满额包邮金额
  1413. $valid = $cartGroup['valid'] ?? [];
  1414. /** @var StoreOrderComputedServices $computedServices */
  1415. $computedServices = app()->make(StoreOrderComputedServices::class);
  1416. $priceGroup = $computedServices->getOrderPriceGroup($valid, $addr, $user, $storeFreePostage);
  1417. $invalid = $cartGroup['invalid'] ?? [];
  1418. $deduction = $cartGroup['deduction'] ?? [];
  1419. $coupon = $cartGroup['useCoupon'] ?? [];
  1420. $promotions = $cartGroup['promotions'] ?? [];
  1421. $giveCartList = $cartGroup['giveCartList'] ?? [];
  1422. $couponPrice = $cartGroup['couponPrice'] ?? 0;
  1423. $firstOrderPrice = $cartGroup['firstOrderPrice'];
  1424. $cartList = array_merge($valid, $giveCartList);
  1425. $promotionsPrice = 0;
  1426. if ($cartList) {
  1427. foreach ($cartList as $key => $cart) {
  1428. if (isset($cart['promotions_true_price']) && isset($cart['price_type']) && $cart['price_type'] == 'promotions') {
  1429. $promotionsPrice = bcadd((string)$promotionsPrice, (string)bcmul((string)$cart['promotions_true_price'], (string)$cart['cart_num'], 2), 2);
  1430. }
  1431. }
  1432. }
  1433. $deduction['promotions_price'] = $promotionsPrice;
  1434. $deduction['coupon_price'] = $couponPrice;
  1435. $deduction['first_order_price'] = $firstOrderPrice;
  1436. $deduction['sum_price'] = $priceGroup['sumPrice'];
  1437. $deduction['vip_price'] = $priceGroup['vipPrice'];
  1438. $payPrice = (float)$priceGroup['totalPrice'];
  1439. if ($couponPrice < $payPrice) {//优惠券金额
  1440. $payPrice = bcsub((string)$payPrice, (string)$couponPrice, 2);
  1441. } else {
  1442. $payPrice = 0;
  1443. }
  1444. if ($firstOrderPrice < $payPrice) {//首单优惠金额
  1445. $payPrice = bcsub((string)$payPrice, (string)$firstOrderPrice, 2);
  1446. } else {
  1447. $payPrice = 0;
  1448. }
  1449. $deduction['pay_price'] = $payPrice;
  1450. return compact('promotions', 'coupon', 'deduction');
  1451. }
  1452. }