StoreCart.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\models\store;
  8. use app\admin\model\store\StoreProductAttrValue;
  9. use app\admin\model\system\SystemGroupData;
  10. use app\models\system\SystemUserLevel;
  11. use app\models\user\User;
  12. use app\models\user\UserLevel;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\services\UtilService;
  15. use crmeb\traits\ModelTrait;
  16. /**
  17. * TODO 购物车Model
  18. * Class StoreCart
  19. * @package app\models\store
  20. */
  21. class StoreCart extends BaseModel
  22. {
  23. /**
  24. * 数据表主键
  25. * @var string
  26. */
  27. protected $pk = 'id';
  28. /**
  29. * 模型名称
  30. * @var string
  31. */
  32. protected $name = 'store_cart';
  33. use ModelTrait;
  34. protected $insert = ['add_time'];
  35. protected function setAddTimeAttr()
  36. {
  37. return time();
  38. }
  39. public static function setCart($uid, $product_id, $cart_num = 1, $product_attr_unique = '', $type = 'product', $is_new = 0, $combination_id = 0, $seckill_id = 0, $bargain_id = 0, $exchange_id = 0)
  40. {
  41. if ($cart_num < 1) $cart_num = 1;
  42. if ($seckill_id) {
  43. $StoreSeckillinfo = StoreSeckill::getValidProduct($seckill_id);
  44. if (!$StoreSeckillinfo)
  45. return self::setErrorInfo('该产品已下架或删除');
  46. $userbuycount = StoreOrder::where('uid', $uid)->where('paid', 1)->where('seckill_id', $seckill_id)->count();
  47. if ($StoreSeckillinfo['num'] <= $userbuycount || $StoreSeckillinfo['num'] < $cart_num)
  48. return self::setErrorInfo('每人限购' . $StoreSeckillinfo['num'] . '件');
  49. $res = StoreProductAttrValue::where('product_id', $seckill_id)->where('unique', $product_attr_unique)->where('type', 1)->field('suk,quota')->find();
  50. if ($cart_num > $res['quota'])
  51. return self::setErrorInfo('该产品库存不足' . $cart_num);
  52. $product_stock = StoreProductAttrValue::where('product_id', $StoreSeckillinfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  53. if ($product_stock < $cart_num)
  54. return self::setErrorInfo('该产品库存不足' . $cart_num);
  55. } elseif ($bargain_id) {
  56. if (!StoreBargain::validBargain($bargain_id))
  57. return self::setErrorInfo('该产品已下架或删除');
  58. $StoreBargainInfo = StoreBargain::getBargain($bargain_id);
  59. $res = StoreProductAttrValue::where('product_id', $bargain_id)->where('type', 2)->field('suk,quota')->find();
  60. if ($cart_num > $res['quota'])
  61. return self::setErrorInfo('该产品库存不足' . $cart_num);
  62. $product_stock = StoreProductAttrValue::where('product_id', $StoreBargainInfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  63. if ($product_stock < $cart_num)
  64. return self::setErrorInfo('该产品库存不足' . $cart_num);
  65. } elseif ($combination_id) {//拼团
  66. $StoreCombinationInfo = StoreCombination::getCombinationOne($combination_id);
  67. if (!$StoreCombinationInfo)
  68. return self::setErrorInfo('该产品已下架或删除');
  69. $userbuycount = StoreOrder::where('uid', $uid)->where('paid', 1)->where('combination_id', $combination_id)->count();
  70. if ($StoreCombinationInfo['num'] <= $userbuycount || $StoreCombinationInfo['num'] < $cart_num)
  71. return self::setErrorInfo('每人限购' . $StoreCombinationInfo['num'] . '件');
  72. $res = StoreProductAttrValue::where('product_id', $combination_id)->where('unique', $product_attr_unique)->where('type', 3)->field('suk,quota')->find();
  73. if ($cart_num > $res['quota'])
  74. return self::setErrorInfo('该产品库存不足' . $cart_num);
  75. $product_stock = StoreProductAttrValue::where('product_id', $StoreCombinationInfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  76. if ($product_stock < $cart_num)
  77. return self::setErrorInfo('该产品库存不足' . $cart_num);
  78. } elseif ($exchange_id) {//兑换
  79. $StoreExchangeInfo = StoreExchange::getValidProduct($exchange_id);
  80. if (!$StoreExchangeInfo)
  81. return self::setErrorInfo('该产品已下架或删除');
  82. $userbuycount = StoreOrder::where('uid', $uid)->where('paid', 1)->where('exchange_id', $exchange_id)->count();
  83. if ($StoreExchangeInfo['num'] <= $userbuycount || $StoreExchangeInfo['num'] < $cart_num)
  84. return self::setErrorInfo('每人限购' . $StoreExchangeInfo['num'] . '件');
  85. $res = StoreProductAttrValue::where('product_id', $exchange_id)->where('unique', $product_attr_unique)->where('type', 4)->field('suk,quota')->find();
  86. if ($cart_num > $res['quota'])
  87. return self::setErrorInfo('该产品库存不足' . $cart_num);
  88. $product_stock = StoreProductAttrValue::where('product_id', $StoreExchangeInfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  89. if ($product_stock < $cart_num)
  90. return self::setErrorInfo('该产品库存不足' . $cart_num);
  91. } else {
  92. if (!StoreProduct::isValidProduct($product_id))
  93. return self::setErrorInfo('该产品已下架或删除');
  94. if (!StoreProductAttr::issetProductUnique($product_id, $product_attr_unique))
  95. return self::setErrorInfo('请选择有效的产品属性');
  96. if (StoreProduct::getProductStock($product_id, $product_attr_unique) < $cart_num)
  97. return self::setErrorInfo('该产品库存不足' . $cart_num);
  98. }
  99. if ($cart = self::where('type', $type)->where('uid', $uid)->where('product_id', $product_id)->where('product_attr_unique', $product_attr_unique)->where('is_new', $is_new)->where('is_pay', 0)->where('is_del', 0)->where('combination_id', $combination_id)->where('bargain_id', $bargain_id)->where('seckill_id', $seckill_id)->where('exchange_id', $exchange_id)->find()) {
  100. if ($is_new)
  101. $cart->cart_num = $cart_num;
  102. else
  103. $cart->cart_num = bcadd($cart_num, $cart->cart_num, 0);
  104. $cart->add_time = time();
  105. $cart->save();
  106. return $cart;
  107. } else {
  108. $add_time = time();
  109. return self::create(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'combination_id', 'add_time', 'bargain_id', 'seckill_id', 'exchange_id'));
  110. }
  111. }
  112. public static function removeUserCart($uid, $ids)
  113. {
  114. return self::where('uid', $uid)->where('id', 'IN', implode(',', $ids))->update(['is_del' => 1]);
  115. }
  116. public static function getUserCartNum($uid, $type, $numType)
  117. {
  118. if ($numType) {
  119. return self::where('uid', $uid)->where('type', $type)->where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->count();
  120. } else {
  121. return self::where('uid', $uid)->where('type', $type)->where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->sum('cart_num');
  122. }
  123. }
  124. /**
  125. * TODO 修改购物车库存
  126. * @param $cartId
  127. * @param $cartNum
  128. * @param $uid
  129. * @return StoreCart|bool
  130. * @throws \think\Exception
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. * @throws \think\exception\DbException
  134. */
  135. public static function changeUserCartNum($cartId, $cartNum, $uid)
  136. {
  137. $count = self::where('uid', $uid)->where('id', $cartId)->count();
  138. if (!$count) return self::setErrorInfo('参数错误');
  139. $cartInfo = self::where('uid', $uid)->where('id', $cartId)->field('product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num')->find()->toArray();
  140. $stock = 0;
  141. if ($cartInfo['bargain_id']) {
  142. //TODO 获取砍价产品的库存
  143. $stock = 0;
  144. } else if ($cartInfo['seckill_id']) {
  145. //TODO 获取秒杀产品的库存
  146. $stock = 0;
  147. } else if ($cartInfo['combination_id']) {
  148. //TODO 获取拼团产品的库存
  149. $stock = 0;
  150. } else if ($cartInfo['product_id']) {
  151. //TODO 获取普通产品的库存
  152. $stock = StoreProduct::getProductStock($cartInfo['product_id'], $cartInfo['product_attr_unique']);
  153. }
  154. if (!$stock) return self::setErrorInfo('暂无库存');
  155. if (!$cartNum) return self::setErrorInfo('库存错误');
  156. if ($stock < $cartNum) return self::setErrorInfo('库存不足' . $cartNum);
  157. if ($cartInfo['cart_num'] == $cartNum) return true;
  158. return self::where('uid', $uid)->where('id', $cartId)->update(['cart_num' => $cartNum]);
  159. }
  160. public static function getUserProductCartList($uid, $cartIds = '', $status = 0)
  161. {
  162. $productInfoField = 'id,image,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,store_type,store_id,return_money_type,return_money,return_day';
  163. $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,store_id';
  164. $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,store_id';
  165. $combinationInfoField = 'id,image,price,postage,sales,stock,title as store_name,is_show,is_del,is_postage,cost,temp_id,weight,volume,store_id';
  166. $exchangeInfoField = 'id,image,price,postage,sales,stock,title as store_name,is_show,is_del,is_postage,cost,temp_id,weight,volume,money,money_type,store_id';
  167. $model = new self();
  168. $valid = $invalid = [];
  169. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  170. ->where('is_del', 0);
  171. if (!$status) $model = $model->where('is_new', 0);
  172. if ($cartIds) $model = $model->where('id', 'IN', $cartIds);
  173. $model = $model->order('add_time DESC');
  174. $list = $model->select()->toArray();
  175. if (!count($list)) return compact('valid', 'invalid');
  176. $now = time();
  177. foreach ($list as $k => $cart) {
  178. if ($cart['seckill_id']) {
  179. $product = StoreSeckill::field($seckillInfoField)
  180. ->find($cart['seckill_id'])->toArray();
  181. } elseif ($cart['bargain_id']) {
  182. $product = StoreBargain::field($bargainInfoField)
  183. ->find($cart['bargain_id'])->toArray();
  184. } elseif ($cart['combination_id']) {
  185. $product = StoreCombination::field($combinationInfoField)
  186. ->find($cart['combination_id'])->toArray();
  187. } elseif ($cart['exchange_id']) {
  188. $product = StoreExchange::field($exchangeInfoField)
  189. ->find($cart['exchange_id'])->toArray();
  190. } else {
  191. $product = StoreProduct::field($productInfoField)
  192. ->find($cart['product_id'])->toArray();
  193. }
  194. $product['image'] = set_file_url($product['image']);
  195. $cart['productInfo'] = $product;
  196. //商品不存在
  197. if (!$product) {
  198. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  199. //商品删除或无库存
  200. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  201. $invalid[] = $cart;
  202. //秒杀产品未开启或者已结束
  203. } else if ($cart['seckill_id'] && ($product['start_time'] > $now || $product['stop_time'] < $now - 86400)) {
  204. $invalid[] = $product;
  205. //商品属性不对应
  206. } else if (!StoreProductAttr::issetProductUnique($cart['product_id'], $cart['product_attr_unique']) && !$cart['combination_id'] && !$cart['seckill_id'] && !$cart['bargain_id'] && !$cart['exchange_id']) {
  207. $invalid[] = $cart;
  208. //正常商品
  209. } else {
  210. if ($cart['seckill_id']) {
  211. $config = SystemGroupData::get($product['time_id']);
  212. if ($config) {
  213. $arr = json_decode($config->value, true);
  214. $now_hour = date('H', time());
  215. $start_hour = $arr['time']['value'];
  216. $continued = $arr['continued']['value'];
  217. $end_hour = $start_hour + $continued;
  218. if ($start_hour > $now_hour) {
  219. //'活动未开启';
  220. $invalid[] = $cart;
  221. continue;
  222. } elseif ($end_hour < $now_hour) {
  223. //'活动已结束';
  224. $invalid[] = $cart;
  225. continue;
  226. }
  227. }
  228. }
  229. if ($cart['product_attr_unique']) {
  230. $attrInfo = StoreProductAttr::uniqueByAttrInfo($cart['product_attr_unique']);
  231. //商品没有对应的属性
  232. if (!$attrInfo || !$attrInfo['stock'])
  233. $invalid[] = $cart;
  234. else {
  235. $cart['productInfo']['attrInfo'] = $attrInfo;
  236. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id'] || $cart['exchange_id']) {
  237. $cart['truePrice'] = $attrInfo['price'];
  238. $cart['vip_truePrice'] = 0;
  239. $cart['money_type'] = $attrInfo['money_type'] ?? '';
  240. $cart['money_type_name'] = get_money_name($cart['money_type']);
  241. $cart['money'] = $attrInfo['money'] ?? 0;
  242. } else {
  243. $user_level_id = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($uid), 'level_id');
  244. if (!$user_level_id) $user_level_id = 0;
  245. $level_price = json_decode($attrInfo['level_price'], true);
  246. if ($user_level_id > 0) {
  247. if (isset($level_price['level_' . $user_level_id]) && $level_price['level_' . $user_level_id]) {
  248. $cart['truePrice'] = (float)$level_price['level_' . $user_level_id];
  249. $cart['vip_truePrice'] = (float)bcsub($attrInfo['price'], $level_price['level_' . $user_level_id], 2);
  250. $cart['money_type'] = '';
  251. $cart['money_type_name'] = '';
  252. $cart['money'] = 0;
  253. } else {
  254. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid, true);
  255. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid);
  256. $cart['money_type'] = '';
  257. $cart['money_type_name'] = '';
  258. $cart['money'] = 0;
  259. }
  260. } else {
  261. $cart['truePrice'] = $attrInfo['price'];
  262. $cart['vip_truePrice'] = 0;
  263. $cart['money_type'] = '';
  264. $cart['money_type_name'] = '';
  265. $cart['money'] = 0;
  266. }
  267. $user = User::get($uid);
  268. if ($user['store_id'] > 0 && $user['store_id'] == $cart['productInfo']['store_id']) {
  269. $level_price = json_decode($attrInfo['level_price'], true);
  270. if (count($level_price) > 0) {
  271. $cart['truePrice'] = (float)min($level_price);
  272. $cart['vip_truePrice'] = (float)bcsub($attrInfo['price'], min($level_price), 2);
  273. $cart['money_type'] = '';
  274. $cart['money_type_name'] = '';
  275. $cart['money'] = 0;
  276. } else {
  277. $discount = SystemUserLevel::setWhere()->min('discount');
  278. $discount = bcdiv($discount, 100, 2);
  279. $cart['truePrice'] = (float)bcmul($discount, $attrInfo['price'], 2);
  280. $cart['vip_truePrice'] = (float)bcsub($attrInfo['price'], (float)bcmul($discount, $attrInfo['price'], 2), 2);
  281. $cart['money_type'] = '';
  282. $cart['money_type_name'] = '';
  283. $cart['money'] = 0;
  284. }
  285. }
  286. }
  287. $cart['trueStock'] = $attrInfo['stock'];
  288. $cart['costPrice'] = $attrInfo['cost'];
  289. $cart['productInfo']['image'] = empty($attrInfo['image']) ? $cart['productInfo']['image'] : $attrInfo['image'];
  290. $valid[] = $cart;
  291. }
  292. } else {
  293. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id'] || $cart['exchange_id']) {
  294. $cart['truePrice'] = $cart['productInfo']['price'];
  295. $cart['vip_truePrice'] = 0;
  296. $cart['money_type'] = $cart['productInfo']['money_type'] ?? '';
  297. $cart['money'] = $cart['productInfo']['money'] ?? 0;
  298. $cart['money_type_name'] = get_money_name($cart['money_type']);
  299. if ($cart['bargain_id']) {
  300. $cart['productInfo']['attrInfo'] = StoreProductAttrValue::where('product_id', $cart['bargain_id'])->where('type', 2)->find();
  301. }
  302. $cart['productInfo']['attrInfo']['weight'] = $product['weight'];
  303. $cart['productInfo']['attrInfo']['volume'] = $product['volume'];
  304. } else {
  305. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid, true);
  306. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid);
  307. $cart['money_type'] = '';
  308. $cart['money_type_name'] = '';
  309. $cart['money'] = 0;
  310. $user = User::get($uid);
  311. if ($user['store_id'] > 0 && $user['store_id'] == $cart['productInfo']['store_id']) {
  312. $discount = SystemUserLevel::setWhere()->min('discount');
  313. $discount = bcdiv($discount, 100, 2);
  314. $cart['truePrice'] = (float)bcmul($discount, $cart['productInfo']['price'], 2);
  315. $cart['vip_truePrice'] = (float)bcsub($cart['productInfo']['price'], (float)bcmul($discount, $cart['productInfo']['price'], 2), 2);
  316. $cart['money_type'] = '';
  317. $cart['money'] = 0;
  318. }
  319. }
  320. $cart['trueStock'] = $cart['productInfo']['stock'];
  321. $cart['costPrice'] = $cart['productInfo']['cost'];
  322. $valid[] = $cart;
  323. }
  324. }
  325. }
  326. foreach ($valid as $k => $cart) {
  327. if ($cart['trueStock'] < $cart['cart_num']) {
  328. $cart['cart_num'] = $cart['trueStock'];
  329. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  330. $valid[$k] = $cart;
  331. }
  332. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  333. if (isset($valid[$k]['productInfo'])) {
  334. unset($valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_show']);
  335. }
  336. }
  337. foreach ($invalid as $k => $cart) {
  338. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  339. if (isset($invalid[$k]['productInfo'])) {
  340. unset($invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_show']);
  341. }
  342. }
  343. return compact('valid', 'invalid');
  344. }
  345. /**
  346. * 拼团
  347. * @param $uid
  348. * @param string $cartIds
  349. * @return array
  350. */
  351. public static function getUserCombinationProductCartList($uid, $cartIds = '')
  352. {
  353. $productInfoField = 'id,image,slider_image,price,cost,ot_price,vip_price,postage,mer_id,give_integral,cate_id,sales,stock,store_name,unit_name,is_show,is_del,is_postage';
  354. $model = new self();
  355. $valid = $invalid = [];
  356. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  357. ->where('is_del', 0);
  358. if ($cartIds) $model->where('id', 'IN', $cartIds);
  359. $list = $model->select()->toArray();
  360. if (!count($list)) return compact('valid', 'invalid');
  361. foreach ($list as $k => $cart) {
  362. $product = StoreProduct::field($productInfoField)
  363. ->find($cart['product_id'])->toArray();
  364. $cart['productInfo'] = $product;
  365. //商品不存在
  366. if (!$product) {
  367. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  368. //商品删除或无库存
  369. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  370. $invalid[] = $cart;
  371. //商品属性不对应
  372. // }else if(!StoreProductAttr::issetProductUnique($cart['product_id'],$cart['product_attr_unique'])){
  373. // $invalid[] = $cart;
  374. //正常商品
  375. } else {
  376. $cart['truePrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('price');
  377. $cart['costPrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('cost');
  378. $cart['trueStock'] = StoreCombination::where('id', $cart['combination_id'])->value('stock');
  379. $valid[] = $cart;
  380. }
  381. }
  382. foreach ($valid as $k => $cart) {
  383. if ($cart['trueStock'] < $cart['cart_num']) {
  384. $cart['cart_num'] = $cart['trueStock'];
  385. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  386. $valid[$k] = $cart;
  387. }
  388. }
  389. return compact('valid', 'invalid');
  390. }
  391. /**
  392. * 产品编号
  393. * @param array $ids
  394. * @return array
  395. */
  396. public static function getCartIdsProduct(array $ids)
  397. {
  398. return self::whereIn('id', $ids)->column('product_id', 'id');
  399. }
  400. /**
  401. * 获取购物车内最新一张产品图
  402. */
  403. public static function getProductImage(array $cart_id)
  404. {
  405. return self::whereIn('a.id', $cart_id)->alias('a')->order('a.id desc')
  406. ->join('store_product p', 'p.id = a.product_id')->value('p.image');
  407. }
  408. }