StoreCart.php 22 KB

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