StoreCart.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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,is_gift';
  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. $is_gift = false;
  161. foreach ($list as $k => $cart) {
  162. if ($cart['seckill_id']) {
  163. $product = StoreSeckill::field($seckillInfoField)
  164. ->find($cart['seckill_id'])->toArray();
  165. } elseif ($cart['bargain_id']) {
  166. $product = StoreBargain::field($bargainInfoField)
  167. ->find($cart['bargain_id'])->toArray();
  168. } elseif ($cart['combination_id']) {
  169. $product = StoreCombination::field($combinationInfoField)
  170. ->find($cart['combination_id'])->toArray();
  171. } else {
  172. $product = StoreProduct::field($productInfoField)
  173. ->find($cart['product_id'])->toArray();
  174. }
  175. $product['image'] = set_file_url($product['image']);
  176. $cart['productInfo'] = $product;
  177. //商品不存在
  178. if (!$product) {
  179. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  180. //商品删除或无库存
  181. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  182. $invalid[] = $cart;
  183. //秒杀产品未开启或者已结束
  184. } else if ($cart['seckill_id'] && ($product['start_time'] > $now || $product['stop_time'] < $now - 86400)) {
  185. $invalid[] = $product;
  186. //商品属性不对应
  187. } else if (!StoreProductAttr::issetProductUnique($cart['product_id'], $cart['product_attr_unique']) && !$cart['combination_id'] && !$cart['seckill_id'] && !$cart['bargain_id']) {
  188. $invalid[] = $cart;
  189. //正常商品
  190. } else {
  191. if (isset($product['is_gift']) && $product['is_gift']) $is_gift = true;
  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'] || (isset($cart['productInfo']['is_gift']) && $cart['productInfo']['is_gift'])) {
  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['productInfo']['image'] = empty($attrInfo['image']) ? $cart['productInfo']['image'] : $attrInfo['image'];
  228. $valid[] = $cart;
  229. }
  230. } else {
  231. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id'] || (isset($cart['productInfo']['is_gift']) && $cart['productInfo']['is_gift'])) {
  232. $cart['truePrice'] = $cart['productInfo']['price'];
  233. $cart['vip_truePrice'] = 0;
  234. if ($cart['bargain_id']) {
  235. $cart['productInfo']['attrInfo'] = StoreProductAttrValue::where('product_id', $cart['bargain_id'])->where('type', 2)->find();
  236. }
  237. $cart['productInfo']['attrInfo']['weight'] = $product['weight'];
  238. $cart['productInfo']['attrInfo']['volume'] = $product['volume'];
  239. } else {
  240. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid, true);
  241. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid);
  242. }
  243. $cart['trueStock'] = $cart['productInfo']['stock'];
  244. $cart['costPrice'] = $cart['productInfo']['cost'];
  245. $valid[] = $cart;
  246. }
  247. }
  248. }
  249. foreach ($valid as $k => $cart) {
  250. if ($cart['trueStock'] < $cart['cart_num']) {
  251. $cart['cart_num'] = $cart['trueStock'];
  252. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  253. $valid[$k] = $cart;
  254. }
  255. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  256. if (isset($valid[$k]['productInfo'])) {
  257. unset($valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_show']);
  258. }
  259. }
  260. foreach ($invalid as $k => $cart) {
  261. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  262. if (isset($invalid[$k]['productInfo'])) {
  263. unset($invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_show']);
  264. }
  265. }
  266. return compact('valid', 'invalid', 'is_gift');
  267. }
  268. /**
  269. * 拼团
  270. * @param $uid
  271. * @param string $cartIds
  272. * @return array
  273. */
  274. public static function getUserCombinationProductCartList($uid, $cartIds = '')
  275. {
  276. $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';
  277. $model = new self();
  278. $valid = $invalid = [];
  279. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  280. ->where('is_del', 0);
  281. if ($cartIds) $model->where('id', 'IN', $cartIds);
  282. $list = $model->select()->toArray();
  283. if (!count($list)) return compact('valid', 'invalid');
  284. foreach ($list as $k => $cart) {
  285. $product = StoreProduct::field($productInfoField)
  286. ->find($cart['product_id'])->toArray();
  287. $cart['productInfo'] = $product;
  288. //商品不存在
  289. if (!$product) {
  290. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  291. //商品删除或无库存
  292. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  293. $invalid[] = $cart;
  294. //商品属性不对应
  295. // }else if(!StoreProductAttr::issetProductUnique($cart['product_id'],$cart['product_attr_unique'])){
  296. // $invalid[] = $cart;
  297. //正常商品
  298. } else {
  299. $cart['truePrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('price');
  300. $cart['costPrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('cost');
  301. $cart['trueStock'] = StoreCombination::where('id', $cart['combination_id'])->value('stock');
  302. $valid[] = $cart;
  303. }
  304. }
  305. foreach ($valid as $k => $cart) {
  306. if ($cart['trueStock'] < $cart['cart_num']) {
  307. $cart['cart_num'] = $cart['trueStock'];
  308. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  309. $valid[$k] = $cart;
  310. }
  311. }
  312. return compact('valid', 'invalid');
  313. }
  314. /**
  315. * 产品编号
  316. * @param array $ids
  317. * @return array
  318. */
  319. public static function getCartIdsProduct(array $ids)
  320. {
  321. return self::whereIn('id', $ids)->column('product_id', 'id');
  322. }
  323. /**
  324. * 获取购物车内最新一张产品图
  325. */
  326. public static function getProductImage(array $cart_id)
  327. {
  328. return self::whereIn('a.id', $cart_id)->alias('a')->order('a.id desc')
  329. ->join('store_product p', 'p.id = a.product_id')->value('p.image');
  330. }
  331. }