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