StoreCart.php 18 KB

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