StoreCart.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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\UserLevel;
  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 date("Y-m-d H:i:s");
  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, $is_consumer = 0, $store_id = 0)
  38. {
  39. if ($cart_num < 0.001) $cart_num = 1;
  40. if (!$product_attr_unique) {
  41. $id = 0;
  42. $activity_type = 0;
  43. if ($seckill_id) {
  44. $id = $seckill_id;
  45. $activity_type = 1;
  46. } elseif ($bargain_id) {
  47. $id = $bargain_id;
  48. $activity_type = 2;
  49. } elseif ($combination_id) {//拼团
  50. $id = $combination_id;
  51. $activity_type = 3;
  52. }
  53. $unique = StoreProduct::getSingleAttrUnique($product_id, $id, $activity_type);
  54. if ($unique) {
  55. $product_attr_unique = $unique;
  56. }
  57. }
  58. // @file_put_contents("cart.txt", $cart_num);
  59. if (!StoreOrder::checkProductStock($uid, $product_id, $cart_num, $product_attr_unique, $combination_id, $seckill_id, $bargain_id)) {
  60. return self::setErrorInfo(StoreOrder::getErrorInfo());
  61. }
  62. 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()) {
  63. if ($is_new)
  64. $cart->cart_num = $cart_num;
  65. else
  66. $cart->cart_num = bcadd($cart_num, $cart->cart_num, 3);
  67. $stock = 0;
  68. if ($cart['bargain_id']) {
  69. //TODO 获取砍价产品的库存
  70. $stock = StoreBargain::getBargainStock($cart['bargain_id']);
  71. } else if ($cart['seckill_id']) {
  72. //TODO 获取秒杀产品的库存
  73. $stock = StoreSeckill::getProductStock($cart['seckill_id']);
  74. } else if ($cart['combination_id']) {
  75. //TODO 获取拼团产品的库存
  76. $stock = StoreCombination::getCombinationStock($cart['combination_id'], $cart->cart_num);
  77. } else if ($cart['product_id']) {
  78. //TODO 获取普通产品的库存
  79. $stock = StoreProduct::getProductStock($cart['product_id'], $cart['product_attr_unique']);
  80. }
  81. if (!$stock) return self::setErrorInfo('暂无库存');
  82. if (!$cart->cart_num) return self::setErrorInfo('库存错误');
  83. if ($stock < $cart->cart_num) return self::setErrorInfo('库存不足' . $cart->cart_num);
  84. $cart->add_time = time();
  85. $cart->save();
  86. return $cart;
  87. } else {
  88. $stock = 0;
  89. if ($bargain_id) {
  90. //TODO 获取砍价产品的库存
  91. $stock = StoreBargain::getBargainStock($bargain_id);
  92. } else if ($seckill_id) {
  93. //TODO 获取秒杀产品的库存
  94. $stock = StoreSeckill::getProductStock($seckill_id);
  95. } else if ($combination_id) {
  96. //TODO 获取拼团产品的库存
  97. $stock = StoreCombination::getCombinationStock($combination_id, $cart_num);
  98. } else if ($product_id) {
  99. //TODO 获取普通产品的库存
  100. $stock = StoreProduct::getProductStock($product_id, $product_attr_unique);
  101. }
  102. if (!$stock) return self::setErrorInfo('暂无库存');
  103. if (!$cart_num) return self::setErrorInfo('库存错误');
  104. if ($stock < $cart_num) return self::setErrorInfo('库存不足' . $cart_num);
  105. $add_time = time();
  106. $id = self::getkeytoid('cart_id');
  107. $is_suit = (!$combination_id && !$seckill_id && !$bargain_id && StoreProduct::where('id', $product_id)->value('is_suit')) ? 1 : 0;
  108. $is_award = (!$combination_id && !$seckill_id && !$bargain_id && StoreProduct::where('id', $product_id)->value('is_award')) ? 1 : 0;
  109. $store_bag = (!$combination_id && !$seckill_id && !$bargain_id && StoreProduct::where('id', $product_id)->value('store_bag')) ? 1 : 0;
  110. $is_bind = (!$combination_id && !$seckill_id && !$bargain_id && StoreProduct::where('id', $product_id)->value('is_bind')) ? 1 : 0;
  111. $is_integral = (!$combination_id && !$seckill_id && !$bargain_id && (StoreProduct::where('id', $product_id)->value('max_use_integral') > 0)) ? 1 : 0;
  112. // var_dump(compact('uid', 'is_award', 'product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'combination_id', 'add_time', 'bargain_id', 'seckill_id', 'id', 'is_consumer', 'is_suit', 'store_id'));
  113. $rs = self::create(compact('is_integral', 'store_bag', 'is_bind', 'uid', 'is_award', 'product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'combination_id', 'add_time', 'bargain_id', 'seckill_id', 'id', 'is_consumer', 'is_suit', 'store_id'));
  114. // @file_put_contents("cart.txt", self::getlastsql(), 8);
  115. return $rs;
  116. }
  117. }
  118. public static function removeUserCart($uid, $ids)
  119. {
  120. return self::where('uid', $uid)->where('id', 'IN', implode(',', $ids))->update(['is_del' => 1]);
  121. }
  122. public static function getUserCartNum($uid, $type, $numType)
  123. {
  124. if ($numType) {
  125. return self::where('c.uid', $uid)->alias('c')->join('store_product p', 'p.id = c.product_id')->where('c.type', $type)->where('c.is_pay', 0)->where('c.is_del', 0)->where('c.is_new', 0)->count();
  126. } else {
  127. return self::where('c.uid', $uid)->alias('c')->join('store_product p', 'p.id = c.product_id')->where('c.type', $type)->where('c.is_pay', 0)->where('c.is_del', 0)->where('c.is_new', 0)->sum('c.cart_num');
  128. }
  129. }
  130. /**
  131. * TODO 修改购物车库存
  132. * @param $cartId
  133. * @param $cartNum
  134. * @param $uid
  135. * @return StoreCart|bool
  136. * @throws \think\Exception
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. * @throws \think\exception\DbException
  140. */
  141. public static function changeUserCartNum($cartId, $cartNum, $uid)
  142. {
  143. $count = self::where('uid', $uid)->where('id', $cartId)->count();
  144. if (!$count) return self::setErrorInfo('参数错误');
  145. $cartInfo = self::where('uid', $uid)->where('id', $cartId)->field('product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num')->find()->toArray();
  146. $stock = 0;
  147. if ($cartInfo['bargain_id']) {
  148. //TODO 获取砍价产品的库存
  149. $stock = 0;
  150. } else if ($cartInfo['seckill_id']) {
  151. //TODO 获取秒杀产品的库存
  152. $stock = 0;
  153. } else if ($cartInfo['combination_id']) {
  154. //TODO 获取拼团产品的库存
  155. $stock = 0;
  156. } else if ($cartInfo['product_id']) {
  157. //TODO 获取普通产品的库存
  158. $stock = StoreProduct::getProductStock($cartInfo['product_id'], $cartInfo['product_attr_unique']);
  159. }
  160. if (!$stock) return self::setErrorInfo('暂无库存');
  161. if (!$cartNum) return self::setErrorInfo('库存错误');
  162. if ($stock < $cartNum) return self::setErrorInfo('库存不足' . $cartNum);
  163. if ($cartInfo['cart_num'] == $cartNum) return true;
  164. return self::where('uid', $uid)->where('id', $cartId)->update(['cart_num' => $cartNum]);
  165. }
  166. public static function getUserProductCartList($uid, $cartIds = '', $status = 0, $store_id = 0, $type = -1)
  167. {
  168. $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,is_consumer,is_suit,is_bind,max_use_integral,store_id,store_bag,store_spread,time_area_discount';
  169. $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';
  170. $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';
  171. $combinationInfoField = 'id,image,price,postage,sales,stock,title as store_name,is_show,is_del,is_postage,cost,temp_id,weight,volume';
  172. $model = new self();
  173. $valid = $invalid = [];
  174. $suit = 0;
  175. $store_bag = false;
  176. $store_spread = 0;
  177. $model = $model->alias('c')->field('c.*')->join('store_product p', 'c.product_id = p.id')->where('c.uid', $uid)->where('c.type', 'product')->where('c.is_pay', 0)
  178. ->where('c.is_del', 0);
  179. if (!$status) $model = $model->where('c.is_new', 0);
  180. if ($cartIds) $model = $model->where('c.id', 'IN', $cartIds);
  181. if ($store_id) $model = $model->where('c.store_id', 'IN', $store_id);
  182. if ($type >= 0) switch ($type) {
  183. case 1:
  184. $model = $model->where('c.is_suit', 1);
  185. break;
  186. case 2:
  187. $model = $model->where('c.store_bag', 1);
  188. break;
  189. case 3:
  190. $model = $model->where('c.is_bind', 1);
  191. break;
  192. case 4:
  193. $model = $model->where('c.is_integral', 1);
  194. break;
  195. case 5:
  196. $model = $model->where('c.is_consumer', 1);
  197. break;
  198. default:
  199. $model = $model->where('c.is_suit', 0)->where('c.is_consumer', 0)->where('c.store_bag', 0)->where('c.is_bind', 0)->where('c.is_integral', 0);
  200. break;
  201. }
  202. $model = $model->order('c.add_time DESC');
  203. $list = $model->select()->toArray();
  204. if (!count($list)) return compact('valid', 'invalid');
  205. $now = time();
  206. foreach ($list as $k => $cart) {
  207. if ($cart['seckill_id']) {
  208. $product = StoreSeckill::field($seckillInfoField)
  209. ->find($cart['seckill_id'])->toArray();
  210. } elseif ($cart['bargain_id']) {
  211. $product = StoreBargain::field($bargainInfoField)
  212. ->find($cart['bargain_id'])->toArray();
  213. } elseif ($cart['combination_id']) {
  214. $product = StoreCombination::field($combinationInfoField)
  215. ->find($cart['combination_id'])->toArray();
  216. } else {
  217. $product = StoreProduct::field($productInfoField)
  218. ->find($cart['product_id'])->toArray();
  219. }
  220. $product['image'] = set_file_url($product['image']);
  221. $cart['productInfo'] = $product;
  222. //商品不存在
  223. if (!$product) {
  224. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  225. //商品删除或无库存
  226. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  227. $invalid[] = $cart;
  228. //秒杀产品未开启或者已结束
  229. } else if ($cart['seckill_id'] && ($product['start_time'] > $now || $product['stop_time'] < $now - 86400)) {
  230. $invalid[] = $product;
  231. //商品属性不对应
  232. } else if (!StoreProductAttr::issetProductUnique($cart['product_id'], $cart['product_attr_unique']) && !$cart['combination_id'] && !$cart['seckill_id'] && !$cart['bargain_id']) {
  233. $invalid[] = $cart;
  234. //正常商品
  235. } else {
  236. if ($cart['seckill_id']) {
  237. $config = SystemGroupData::get($product['time_id']);
  238. if ($config) {
  239. $arr = json_decode($config->value, true);
  240. $now_hour = date('H', time());
  241. $start_hour = $arr['time']['value'];
  242. $continued = $arr['continued']['value'];
  243. $end_hour = $start_hour + $continued;
  244. if ($start_hour > $now_hour) {
  245. //'活动未开启';
  246. $invalid[] = $cart;
  247. continue;
  248. } elseif ($end_hour < $now_hour) {
  249. //'活动已结束';
  250. $invalid[] = $cart;
  251. continue;
  252. }
  253. }
  254. }
  255. if ($cart['product_attr_unique']) {
  256. $attrInfo = StoreProductAttr::uniqueByAttrInfo($cart['product_attr_unique']);
  257. //商品没有对应的属性
  258. if (!$attrInfo || !$attrInfo['stock'])
  259. $invalid[] = $cart;
  260. else {
  261. $cart['productInfo']['attrInfo'] = $attrInfo;
  262. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id'] || $cart['is_consumer'] || $cart['is_suit'] || (($cart['productInfo']['max_use_integral'] ?? 0) > 0)) {
  263. if ($cart['bargain_id']) {
  264. $cart['truePrice'] = $cart['productInfo']['price'];
  265. } else {
  266. $cart['truePrice'] = $attrInfo['price'];
  267. }
  268. $cart['vip_truePrice'] = 0;
  269. $cart['use_integral'] = $cart['productInfo']['max_use_integral'] ?? 0;
  270. } else {
  271. $discounts = json_decode($product['time_area_discount'] ?: '[]', true);
  272. $discount = 0;
  273. if (count($discounts) > 0) {
  274. foreach ($discounts as $kk => $v) {
  275. $times = explode('-', $kk);
  276. if (!(count($times) == 2)) {
  277. break;
  278. }
  279. $start = strtotime(date('Y-m-d') . ' ' . $times[0]);
  280. $end = strtotime(date('Y-m-d') . ' ' . $times[1]);
  281. if (time() > $start && time() <= $end) {
  282. $discount = $v;
  283. }
  284. }
  285. }
  286. if ($discount > 0) {
  287. $attrInfo['price'] = bcmul(bcdiv($discount, 100, 4), $attrInfo['price'], 2);
  288. }
  289. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid, true);
  290. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid);
  291. $cart['use_integral'] = 0;
  292. }
  293. $cart['trueStock'] = $attrInfo['stock'];
  294. $cart['costPrice'] = $attrInfo['cost'];
  295. $cart['productInfo']['image'] = empty($attrInfo['image']) ? $cart['productInfo']['image'] : $attrInfo['image'];
  296. $valid[] = $cart;
  297. }
  298. } else {
  299. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id'] || $cart['is_consumer'] || $cart['is_suit'] || (($cart['productInfo']['max_use_integral'] ?? 0) > 0)) {
  300. $cart['truePrice'] = $cart['productInfo']['price'];
  301. $cart['vip_truePrice'] = 0;
  302. $cart['use_integral'] = $cart['productInfo']['max_use_integral'] ?? 0;
  303. if ($cart['bargain_id']) {
  304. $cart['productInfo']['attrInfo'] = StoreProductAttrValue::where('product_id', $cart['bargain_id'])->where('type', 2)->find();
  305. }
  306. $cart['productInfo']['attrInfo']['weight'] = $product['weight'];
  307. $cart['productInfo']['attrInfo']['volume'] = $product['volume'];
  308. } else {
  309. $discounts = json_decode($product['time_area_discount'] ?: '[]', true);
  310. $discount = 0;
  311. if (count($discounts) > 0) {
  312. foreach ($discounts as $kk => $v) {
  313. $times = explode('-', $kk);
  314. if (!(count($times) == 2)) {
  315. break;
  316. }
  317. $start = strtotime(date('Y-m-d') . ' ' . $times[0]);
  318. $end = strtotime(date('Y-m-d') . ' ' . $times[1]);
  319. if (time() > $start && time() <= $end) {
  320. $discount = $v;
  321. }
  322. }
  323. }
  324. if ($discount > 0) {
  325. $cart['productInfo']['price'] = bcmul(bcdiv($discount, 100, 4), $cart['productInfo']['price'], 2);
  326. }
  327. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid, true);
  328. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid);
  329. $cart['use_integral'] = 0;
  330. }
  331. $cart['trueStock'] = $cart['productInfo']['stock'];
  332. $cart['costPrice'] = $cart['productInfo']['cost'];
  333. $valid[] = $cart;
  334. }
  335. }
  336. }
  337. $is_bind = 0;
  338. foreach ($valid as $k => $cart) {
  339. if ($cart['trueStock'] < $cart['cart_num']) {
  340. $cart['cart_num'] = $cart['trueStock'];
  341. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  342. $valid[$k] = $cart;
  343. }
  344. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  345. if (isset($valid[$k]['productInfo'])) {
  346. unset($valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_show']);
  347. }
  348. $suit += ($cart['productInfo']['is_suit'] ?? 0) * $cart['cart_num'];
  349. $is_bind = $cart['productInfo']['is_bind'] ?? 0;
  350. $store_bag = $store_bag || ($cart['productInfo']['store_bag'] ?? 0);
  351. $store_spread += $cart['productInfo']['store_spread'];
  352. }
  353. foreach ($invalid as $k => $cart) {
  354. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  355. if (isset($invalid[$k]['productInfo'])) {
  356. unset($invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_show']);
  357. }
  358. }
  359. return compact('valid', 'invalid', 'suit', 'store_bag', 'is_bind', 'store_spread');
  360. }
  361. /**
  362. * 拼团
  363. * @param $uid
  364. * @param string $cartIds
  365. * @return array
  366. */
  367. public static function getUserCombinationProductCartList($uid, $cartIds = '')
  368. {
  369. $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';
  370. $model = new self();
  371. $valid = $invalid = [];
  372. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  373. ->where('is_del', 0);
  374. if ($cartIds) $model->where('id', 'IN', $cartIds);
  375. $list = $model->select()->toArray();
  376. if (!count($list)) return compact('valid', 'invalid');
  377. foreach ($list as $k => $cart) {
  378. $product = StoreProduct::field($productInfoField)
  379. ->find($cart['product_id'])->toArray();
  380. $cart['productInfo'] = $product;
  381. //商品不存在
  382. if (!$product) {
  383. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  384. //商品删除或无库存
  385. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  386. $invalid[] = $cart;
  387. //商品属性不对应
  388. // }else if(!StoreProductAttr::issetProductUnique($cart['product_id'],$cart['product_attr_unique'])){
  389. // $invalid[] = $cart;
  390. //正常商品
  391. } else {
  392. $cart['truePrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('price');
  393. $cart['costPrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('cost');
  394. $cart['trueStock'] = StoreCombination::where('id', $cart['combination_id'])->value('stock');
  395. $valid[] = $cart;
  396. }
  397. }
  398. foreach ($valid as $k => $cart) {
  399. if ($cart['trueStock'] < $cart['cart_num']) {
  400. $cart['cart_num'] = $cart['trueStock'];
  401. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  402. $valid[$k] = $cart;
  403. }
  404. }
  405. return compact('valid', 'invalid');
  406. }
  407. /**
  408. * 产品编号
  409. * @param array $ids
  410. * @return array
  411. */
  412. public static function getCartIdsProduct(array $ids)
  413. {
  414. return self::whereIn('id', $ids)->column('product_id', 'id');
  415. }
  416. /**
  417. * 获取购物车内最新一张产品图
  418. */
  419. public static function getProductImage(array $cart_id)
  420. {
  421. return self::whereIn('a.id', $cart_id)->alias('a')->order('a.id desc')
  422. ->join('store_product p', 'p.id = a.product_id')->value('p.image');
  423. }
  424. }