StoreCart.php 22 KB

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