StoreCart.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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,is_best';
  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. $store_order = 0;
  163. $user = 0;
  164. foreach ($list as $k => $cart) {
  165. if ($cart['seckill_id']) {
  166. $product = StoreSeckill::field($seckillInfoField)
  167. ->find($cart['seckill_id'])->toArray();
  168. } elseif ($cart['bargain_id']) {
  169. $product = StoreBargain::field($bargainInfoField)
  170. ->find($cart['bargain_id'])->toArray();
  171. } elseif ($cart['combination_id']) {
  172. $product = StoreCombination::field($combinationInfoField)
  173. ->find($cart['combination_id'])->toArray();
  174. } else {
  175. $product = StoreProduct::field($productInfoField)
  176. ->find($cart['product_id'])->toArray();
  177. }
  178. $product['image'] = set_file_url($product['image']);
  179. $cart['productInfo'] = $product;
  180. //商品不存在
  181. if (!$product) {
  182. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  183. //商品删除或无库存
  184. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  185. $invalid[] = $cart;
  186. //秒杀产品未开启或者已结束
  187. } else if ($cart['seckill_id'] && ($product['start_time'] > $now || $product['stop_time'] < $now - 86400)) {
  188. $invalid[] = $product;
  189. //商品属性不对应
  190. } else if (!StoreProductAttr::issetProductUnique($cart['product_id'], $cart['product_attr_unique']) && !$cart['combination_id'] && !$cart['seckill_id'] && !$cart['bargain_id']) {
  191. $invalid[] = $cart;
  192. //正常商品
  193. } else {
  194. if ($cart['seckill_id']) {
  195. $config = SystemGroupData::get($product['time_id']);
  196. if ($config) {
  197. $arr = json_decode($config->value, true);
  198. $now_hour = date('H', time());
  199. $start_hour = $arr['time']['value'];
  200. $continued = $arr['continued']['value'];
  201. $end_hour = $start_hour + $continued;
  202. if ($start_hour > $now_hour) {
  203. //'活动未开启';
  204. $invalid[] = $cart;
  205. continue;
  206. } elseif ($end_hour < $now_hour) {
  207. //'活动已结束';
  208. $invalid[] = $cart;
  209. continue;
  210. }
  211. }
  212. }
  213. if ($cart['product_attr_unique']) {
  214. $attrInfo = StoreProductAttr::uniqueByAttrInfo($cart['product_attr_unique']);
  215. //商品没有对应的属性
  216. if (!$attrInfo || !$attrInfo['stock'])
  217. $invalid[] = $cart;
  218. else {
  219. $cart['productInfo']['attrInfo'] = $attrInfo;
  220. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  221. $cart['truePrice'] = $attrInfo['price'];
  222. $cart['vip_truePrice'] = 0;
  223. } else {
  224. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid, true);
  225. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid);
  226. }
  227. $cart['trueStock'] = $attrInfo['stock'];
  228. $cart['costPrice'] = $attrInfo['cost'];
  229. $cart['integral'] = $attrInfo['integral'];
  230. $cart['productInfo']['image'] = empty($attrInfo['image']) ? $cart['productInfo']['image'] : $attrInfo['image'];
  231. $valid[] = $cart;
  232. }
  233. } else {
  234. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  235. $cart['truePrice'] = $cart['productInfo']['price'];
  236. $cart['vip_truePrice'] = 0;
  237. if ($cart['bargain_id']) {
  238. $cart['productInfo']['attrInfo'] = StoreProductAttrValue::where('product_id', $cart['bargain_id'])->where('type', 2)->find();
  239. }
  240. $cart['productInfo']['attrInfo']['weight'] = $product['weight'];
  241. $cart['productInfo']['attrInfo']['volume'] = $product['volume'];
  242. } else {
  243. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid, true);
  244. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid);
  245. }
  246. $cart['trueStock'] = $cart['productInfo']['stock'];
  247. $cart['costPrice'] = $cart['productInfo']['cost'];
  248. $cart['integral'] = $cart['productInfo']['integral'] ?? 0;
  249. $valid[] = $cart;
  250. }
  251. }
  252. }
  253. foreach ($valid as $k => $cart) {
  254. if ($cart['trueStock'] < $cart['cart_num']) {
  255. $cart['cart_num'] = $cart['trueStock'];
  256. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  257. $valid[$k] = $cart;
  258. }
  259. if (($valid[$k]['productInfo']['is_best'] ?? 0) > 0) {
  260. $store_order = 1;
  261. }
  262. if (($valid[$k]['productInfo']['is_best'] ?? 0) > 0) {
  263. $user = 1;
  264. }
  265. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  266. if (isset($valid[$k]['productInfo'])) {
  267. unset($valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_show']);
  268. }
  269. }
  270. foreach ($invalid as $k => $cart) {
  271. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  272. if (isset($invalid[$k]['productInfo'])) {
  273. unset($invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_show']);
  274. }
  275. }
  276. return compact('valid', 'invalid', 'store_order','user');
  277. }
  278. /**
  279. * 拼团
  280. * @param $uid
  281. * @param string $cartIds
  282. * @return array
  283. */
  284. public static function getUserCombinationProductCartList($uid, $cartIds = '')
  285. {
  286. $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';
  287. $model = new self();
  288. $valid = $invalid = [];
  289. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  290. ->where('is_del', 0);
  291. if ($cartIds) $model->where('id', 'IN', $cartIds);
  292. $list = $model->select()->toArray();
  293. if (!count($list)) return compact('valid', 'invalid');
  294. foreach ($list as $k => $cart) {
  295. $product = StoreProduct::field($productInfoField)
  296. ->find($cart['product_id'])->toArray();
  297. $cart['productInfo'] = $product;
  298. //商品不存在
  299. if (!$product) {
  300. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  301. //商品删除或无库存
  302. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  303. $invalid[] = $cart;
  304. //商品属性不对应
  305. // }else if(!StoreProductAttr::issetProductUnique($cart['product_id'],$cart['product_attr_unique'])){
  306. // $invalid[] = $cart;
  307. //正常商品
  308. } else {
  309. $cart['truePrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('price');
  310. $cart['costPrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('cost');
  311. $cart['trueStock'] = StoreCombination::where('id', $cart['combination_id'])->value('stock');
  312. $valid[] = $cart;
  313. }
  314. }
  315. foreach ($valid as $k => $cart) {
  316. if ($cart['trueStock'] < $cart['cart_num']) {
  317. $cart['cart_num'] = $cart['trueStock'];
  318. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  319. $valid[$k] = $cart;
  320. }
  321. }
  322. return compact('valid', 'invalid');
  323. }
  324. /**
  325. * 产品编号
  326. * @param array $ids
  327. * @return array
  328. */
  329. public static function getCartIdsProduct(array $ids)
  330. {
  331. return self::whereIn('id', $ids)->column('product_id', 'id');
  332. }
  333. /**
  334. * 获取购物车内最新一张产品图
  335. */
  336. public static function getProductImage(array $cart_id)
  337. {
  338. return self::whereIn('a.id', $cart_id)->alias('a')->order('a.id desc')
  339. ->join('store_product p', 'p.id = a.product_id')->value('p.image');
  340. }
  341. }