StoreCart.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. }
  83. 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()){
  84. if($is_new)
  85. $cart->cart_num = $cart_num;
  86. else
  87. $cart->cart_num = bcadd($cart_num, $cart->cart_num, 0);
  88. $cart->add_time = time();
  89. $cart->save();
  90. return $cart;
  91. }else{
  92. $add_time = time();
  93. return self::create(compact('uid','product_id','cart_num','product_attr_unique','is_new','type','combination_id','add_time','bargain_id','seckill_id'));
  94. }
  95. }
  96. public static function removeUserCart($uid,$ids)
  97. {
  98. return self::where('uid',$uid)->where('id','IN',implode(',',$ids))->update(['is_del'=>1]);
  99. }
  100. public static function getUserCartNum($uid, $type, $numType)
  101. {
  102. if($numType){
  103. return self::where('uid',$uid)->where('type',$type)->where('is_pay',0)->where('is_del',0)->where('is_new',0)->count();
  104. }else{
  105. return self::where('uid',$uid)->where('type',$type)->where('is_pay',0)->where('is_del',0)->where('is_new',0)->sum('cart_num');
  106. }
  107. }
  108. /**
  109. * TODO 修改购物车库存
  110. * @param $cartId
  111. * @param $cartNum
  112. * @param $uid
  113. * @return StoreCart|bool
  114. * @throws \think\Exception
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. * @throws \think\exception\DbException
  118. */
  119. public static function changeUserCartNum($cartId,$cartNum,$uid)
  120. {
  121. $count = self::where('uid',$uid)->where('id',$cartId)->count();
  122. if(!$count) return self::setErrorInfo('参数错误');
  123. $cartInfo = self::where('uid',$uid)->where('id',$cartId)->field('product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num')->find()->toArray();
  124. $stock = 0;
  125. if($cartInfo['bargain_id']){
  126. //TODO 获取砍价产品的库存
  127. $stock = 0;
  128. }else if($cartInfo['seckill_id']){
  129. //TODO 获取秒杀产品的库存
  130. $stock = 0;
  131. }else if($cartInfo['combination_id']){
  132. //TODO 获取拼团产品的库存
  133. $stock = 0;
  134. }else if($cartInfo['product_id']){
  135. //TODO 获取普通产品的库存
  136. $stock = StoreProduct::getProductStock($cartInfo['product_id'],$cartInfo['product_attr_unique']);
  137. }
  138. if(!$stock) return self::setErrorInfo('暂无库存');
  139. if(!$cartNum) return self::setErrorInfo('库存错误');
  140. if($stock < $cartNum) return self::setErrorInfo('库存不足'.$cartNum);
  141. if($cartInfo['cart_num'] == $cartNum) return true;
  142. return self::where('uid',$uid)->where('id',$cartId)->update(['cart_num'=>$cartNum]);
  143. }
  144. public static function getUserProductCartList($uid,$cartIds='',$status=0)
  145. {
  146. $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';
  147. $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';
  148. $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';
  149. $combinationInfoField = 'id,image,price,postage,sales,stock,title as store_name,is_show,is_del,is_postage,cost,temp_id,weight,volume';
  150. $model = new self();
  151. $valid = $invalid = [];
  152. $model = $model->where('uid',$uid)->where('type','product')->where('is_pay',0)
  153. ->where('is_del',0);
  154. if(!$status) $model = $model->where('is_new',0);
  155. if($cartIds) $model = $model->where('id','IN',$cartIds);
  156. $model = $model->order('add_time DESC');
  157. $list = $model->select()->toArray();
  158. if(!count($list)) return compact('valid','invalid');
  159. $now = time();
  160. foreach ($list as $k=>$cart){
  161. if($cart['seckill_id']){
  162. $product = StoreSeckill::field($seckillInfoField)
  163. ->find($cart['seckill_id'])->toArray();
  164. }elseif($cart['bargain_id']){
  165. $product = StoreBargain::field($bargainInfoField)
  166. ->find($cart['bargain_id'])->toArray();
  167. }elseif($cart['combination_id']){
  168. $product = StoreCombination::field($combinationInfoField)
  169. ->find($cart['combination_id'])->toArray();
  170. }else{
  171. $product = StoreProduct::field($productInfoField)
  172. ->find($cart['product_id'])->toArray();
  173. }
  174. $product['image'] = set_file_url($product['image']);
  175. $cart['productInfo'] = $product;
  176. //商品不存在
  177. if(!$product){
  178. $model->where('id',$cart['id'])->update(['is_del'=>1]);
  179. //商品删除或无库存
  180. }else if(!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  181. $invalid[] = $cart;
  182. //秒杀产品未开启或者已结束
  183. } else if ($cart['seckill_id'] && ($product['start_time'] > $now || $product['stop_time'] < $now-86400)) {
  184. $invalid[] = $product;
  185. //商品属性不对应
  186. } else if(!StoreProductAttr::issetProductUnique($cart['product_id'],$cart['product_attr_unique']) && !$cart['combination_id'] && !$cart['seckill_id']&& !$cart['bargain_id']){
  187. $invalid[] = $cart;
  188. //正常商品
  189. }else{
  190. if ($cart['seckill_id']) {
  191. $config = SystemGroupData::get($product['time_id']);
  192. if ($config) {
  193. $arr = json_decode($config->value, true);
  194. $now_hour = date('H', time());
  195. $start_hour = $arr['time']['value'];
  196. $continued = $arr['continued']['value'];
  197. $end_hour = $start_hour + $continued;
  198. if ($start_hour > $now_hour) {
  199. //'活动未开启';
  200. $invalid[] = $cart;
  201. continue;
  202. } elseif ($end_hour < $now_hour) {
  203. //'活动已结束';
  204. $invalid[] = $cart;
  205. continue;
  206. }
  207. }
  208. }
  209. if($cart['product_attr_unique']){
  210. $attrInfo = StoreProductAttr::uniqueByAttrInfo($cart['product_attr_unique']);
  211. //商品没有对应的属性
  212. if(!$attrInfo || !$attrInfo['stock'])
  213. $invalid[] = $cart;
  214. else{
  215. $cart['productInfo']['attrInfo'] = $attrInfo;
  216. if($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  217. $cart['truePrice'] = $attrInfo['price'];
  218. $cart['vip_truePrice'] = 0;
  219. }else {
  220. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'],$uid,true);
  221. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid);
  222. }
  223. $cart['trueStock'] = $attrInfo['stock'];
  224. $cart['costPrice'] = $attrInfo['cost'];
  225. $cart['productInfo']['image'] = empty($attrInfo['image']) ? $cart['productInfo']['image'] : $attrInfo['image'];
  226. $valid[] = $cart;
  227. }
  228. }else{
  229. if($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  230. $cart['truePrice'] = $cart['productInfo']['price'];
  231. $cart['vip_truePrice'] = 0;
  232. if($cart['bargain_id']){
  233. $cart['productInfo']['attrInfo'] = StoreProductAttrValue::where('product_id',$cart['bargain_id'])->where('type',2)->find();
  234. }
  235. $cart['productInfo']['attrInfo']['weight'] = $product['weight'];
  236. $cart['productInfo']['attrInfo']['volume'] = $product['volume'];
  237. }else {
  238. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'],$uid,true);
  239. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid);
  240. }
  241. $cart['trueStock'] = $cart['productInfo']['stock'];
  242. $cart['costPrice'] = $cart['productInfo']['cost'];
  243. $valid[] = $cart;
  244. }
  245. }
  246. }
  247. foreach ($valid as $k=>$cart){
  248. if($cart['trueStock'] < $cart['cart_num']){
  249. $cart['cart_num'] = $cart['trueStock'];
  250. $model->where('id',$cart['id'])->update(['cart_num'=>$cart['cart_num']]);
  251. $valid[$k] = $cart;
  252. }
  253. unset($valid[$k]['uid'],$valid[$k]['is_del'],$valid[$k]['is_new'],$valid[$k]['is_pay'],$valid[$k]['add_time']);
  254. if(isset($valid[$k]['productInfo'])){
  255. unset($valid[$k]['productInfo']['is_del'],$valid[$k]['productInfo']['is_del'],$valid[$k]['productInfo']['is_show']);
  256. }
  257. }
  258. foreach ($invalid as $k=>$cart){
  259. unset($valid[$k]['uid'],$valid[$k]['is_del'],$valid[$k]['is_new'],$valid[$k]['is_pay'],$valid[$k]['add_time']);
  260. if(isset($invalid[$k]['productInfo'])){
  261. unset($invalid[$k]['productInfo']['is_del'],$invalid[$k]['productInfo']['is_del'],$invalid[$k]['productInfo']['is_show']);
  262. }
  263. }
  264. return compact('valid','invalid');
  265. }
  266. /**
  267. * 拼团
  268. * @param $uid
  269. * @param string $cartIds
  270. * @return array
  271. */
  272. public static function getUserCombinationProductCartList($uid,$cartIds='')
  273. {
  274. $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';
  275. $model = new self();
  276. $valid = $invalid = [];
  277. $model = $model->where('uid',$uid)->where('type','product')->where('is_pay',0)
  278. ->where('is_del',0);
  279. if($cartIds) $model->where('id','IN',$cartIds);
  280. $list = $model->select()->toArray();
  281. if(!count($list)) return compact('valid','invalid');
  282. foreach ($list as $k=>$cart){
  283. $product = StoreProduct::field($productInfoField)
  284. ->find($cart['product_id'])->toArray();
  285. $cart['productInfo'] = $product;
  286. //商品不存在
  287. if(!$product){
  288. $model->where('id',$cart['id'])->update(['is_del'=>1]);
  289. //商品删除或无库存
  290. }else if(!$product['is_show'] || $product['is_del'] || !$product['stock']){
  291. $invalid[] = $cart;
  292. //商品属性不对应
  293. // }else if(!StoreProductAttr::issetProductUnique($cart['product_id'],$cart['product_attr_unique'])){
  294. // $invalid[] = $cart;
  295. //正常商品
  296. }else{
  297. $cart['truePrice'] = (float)StoreCombination::where('id',$cart['combination_id'])->value('price');
  298. $cart['costPrice'] = (float)StoreCombination::where('id',$cart['combination_id'])->value('cost');
  299. $cart['trueStock'] = StoreCombination::where('id',$cart['combination_id'])->value('stock');
  300. $valid[] = $cart;
  301. }
  302. }
  303. foreach ($valid as $k=>$cart){
  304. if($cart['trueStock'] < $cart['cart_num']){
  305. $cart['cart_num'] = $cart['trueStock'];
  306. $model->where('id',$cart['id'])->update(['cart_num'=>$cart['cart_num']]);
  307. $valid[$k] = $cart;
  308. }
  309. }
  310. return compact('valid','invalid');
  311. }
  312. /**
  313. * 产品编号
  314. * @param array $ids
  315. * @return array
  316. */
  317. public static function getCartIdsProduct(array $ids)
  318. {
  319. return self::whereIn('id',$ids)->column('product_id','id');
  320. }
  321. /**
  322. * 获取购物车内最新一张产品图
  323. */
  324. public static function getProductImage(array $cart_id){
  325. return self::whereIn('a.id',$cart_id)->alias('a')->order('a.id desc')
  326. ->join('store_product p','p.id = a.product_id')->value('p.image');
  327. }
  328. }