StoreCart.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 (!$product_attr_unique) {
  40. $id = 0;
  41. $activity_type = 0;
  42. if ($seckill_id) {
  43. $id = $seckill_id;
  44. $activity_type = 1;
  45. } elseif ($bargain_id) {
  46. $id = $bargain_id;
  47. $activity_type = 2;
  48. } elseif ($combination_id) {//拼团
  49. $id = $combination_id;
  50. $activity_type = 3;
  51. }
  52. $unique = StoreProduct::getSingleAttrUnique($product_id, $id, $activity_type);
  53. if ($unique) {
  54. $product_attr_unique = $unique;
  55. }
  56. }
  57. if(!StoreOrder::checkProductStock($uid, $product_id,$cart_num,$product_attr_unique,$combination_id,$seckill_id,$bargain_id)){
  58. return self::setErrorInfo(StoreOrder::getErrorInfo());
  59. }
  60. 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()) {
  61. if ($is_new)
  62. $cart->cart_num = $cart_num;
  63. else
  64. $cart->cart_num = bcadd($cart_num, $cart->cart_num, 0);
  65. $cart->add_time = time();
  66. $cart->save();
  67. return $cart;
  68. } else {
  69. $add_time = time();
  70. return self::create(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'combination_id', 'add_time', 'bargain_id', 'seckill_id'));
  71. }
  72. }
  73. public static function removeUserCart($uid, $ids)
  74. {
  75. return self::where('uid', $uid)->where('id', 'IN', implode(',', $ids))->update(['is_del' => 1]);
  76. }
  77. public static function getUserCartNum($uid, $type, $numType)
  78. {
  79. if ($numType) {
  80. 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();
  81. } else {
  82. 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');
  83. }
  84. }
  85. /**
  86. * TODO 修改购物车库存
  87. * @param $cartId
  88. * @param $cartNum
  89. * @param $uid
  90. * @return StoreCart|bool
  91. * @throws \think\Exception
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. * @throws \think\exception\DbException
  95. */
  96. public static function changeUserCartNum($cartId, $cartNum, $uid)
  97. {
  98. $count = self::where('uid', $uid)->where('id', $cartId)->count();
  99. if (!$count) return self::setErrorInfo('参数错误');
  100. $cartInfo = self::where('uid', $uid)->where('id', $cartId)->field('product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num')->find()->toArray();
  101. $stock = 0;
  102. if ($cartInfo['bargain_id']) {
  103. //TODO 获取砍价产品的库存
  104. $stock = 0;
  105. } else if ($cartInfo['seckill_id']) {
  106. //TODO 获取秒杀产品的库存
  107. $stock = 0;
  108. } else if ($cartInfo['combination_id']) {
  109. //TODO 获取拼团产品的库存
  110. $stock = 0;
  111. } else if ($cartInfo['product_id']) {
  112. //TODO 获取普通产品的库存
  113. $stock = StoreProduct::getProductStock($cartInfo['product_id'], $cartInfo['product_attr_unique']);
  114. }
  115. if (!$stock) return self::setErrorInfo('暂无库存');
  116. if (!$cartNum) return self::setErrorInfo('库存错误');
  117. if ($stock < $cartNum) return self::setErrorInfo('库存不足' . $cartNum);
  118. if ($cartInfo['cart_num'] == $cartNum) return true;
  119. return self::where('uid', $uid)->where('id', $cartId)->update(['cart_num' => $cartNum]);
  120. }
  121. public static function getUserProductCartList($uid, $cartIds = '', $status = 0)
  122. {
  123. $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';
  124. $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';
  125. $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';
  126. $combinationInfoField = 'id,image,price,postage,sales,stock,title as store_name,is_show,is_del,is_postage,cost,temp_id,weight,volume';
  127. $model = new self();
  128. $valid = $invalid = [];
  129. $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)
  130. ->where('c.is_del', 0);
  131. if (!$status) $model = $model->where('c.is_new', 0);
  132. if ($cartIds) $model = $model->where('c.id', 'IN', $cartIds);
  133. $model = $model->order('c.add_time DESC');
  134. $list = $model->select()->toArray();
  135. if (!count($list)) return compact('valid', 'invalid');
  136. $now = time();
  137. foreach ($list as $k => $cart) {
  138. if ($cart['seckill_id']) {
  139. $product = StoreSeckill::field($seckillInfoField)
  140. ->find($cart['seckill_id'])->toArray();
  141. } elseif ($cart['bargain_id']) {
  142. $product = StoreBargain::field($bargainInfoField)
  143. ->find($cart['bargain_id'])->toArray();
  144. } elseif ($cart['combination_id']) {
  145. $product = StoreCombination::field($combinationInfoField)
  146. ->find($cart['combination_id'])->toArray();
  147. } else {
  148. $product = StoreProduct::field($productInfoField)
  149. ->find($cart['product_id'])->toArray();
  150. }
  151. $product['image'] = set_file_url($product['image']);
  152. $cart['productInfo'] = $product;
  153. //商品不存在
  154. if (!$product) {
  155. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  156. //商品删除或无库存
  157. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  158. $invalid[] = $cart;
  159. //秒杀产品未开启或者已结束
  160. } else if ($cart['seckill_id'] && ($product['start_time'] > $now || $product['stop_time'] < $now - 86400)) {
  161. $invalid[] = $product;
  162. //商品属性不对应
  163. } else if (!StoreProductAttr::issetProductUnique($cart['product_id'], $cart['product_attr_unique']) && !$cart['combination_id'] && !$cart['seckill_id'] && !$cart['bargain_id']) {
  164. $invalid[] = $cart;
  165. //正常商品
  166. } else {
  167. if ($cart['seckill_id']) {
  168. $config = SystemGroupData::get($product['time_id']);
  169. if ($config) {
  170. $arr = json_decode($config->value, true);
  171. $now_hour = date('H', time());
  172. $start_hour = $arr['time']['value'];
  173. $continued = $arr['continued']['value'];
  174. $end_hour = $start_hour + $continued;
  175. if ($start_hour > $now_hour) {
  176. //'活动未开启';
  177. $invalid[] = $cart;
  178. continue;
  179. } elseif ($end_hour < $now_hour) {
  180. //'活动已结束';
  181. $invalid[] = $cart;
  182. continue;
  183. }
  184. }
  185. }
  186. if ($cart['product_attr_unique']) {
  187. $attrInfo = StoreProductAttr::uniqueByAttrInfo($cart['product_attr_unique']);
  188. //商品没有对应的属性
  189. if (!$attrInfo || !$attrInfo['stock'])
  190. $invalid[] = $cart;
  191. else {
  192. $cart['productInfo']['attrInfo'] = $attrInfo;
  193. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  194. if ($cart['bargain_id']) {
  195. $cart['truePrice'] = $cart['productInfo']['price'];
  196. } else {
  197. $cart['truePrice'] = $attrInfo['price'];
  198. }
  199. $cart['vip_truePrice'] = 0;
  200. } else {
  201. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid, true);
  202. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid);
  203. }
  204. $cart['trueStock'] = $attrInfo['stock'];
  205. $cart['costPrice'] = $attrInfo['cost'];
  206. $cart['integral'] = $attrInfo['integral'];
  207. $cart['productInfo']['image'] = empty($attrInfo['image']) ? $cart['productInfo']['image'] : $attrInfo['image'];
  208. $valid[] = $cart;
  209. }
  210. } else {
  211. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  212. $cart['truePrice'] = $cart['productInfo']['price'];
  213. $cart['vip_truePrice'] = 0;
  214. if ($cart['bargain_id']) {
  215. $cart['productInfo']['attrInfo'] = StoreProductAttrValue::where('product_id', $cart['bargain_id'])->where('type', 2)->find();
  216. }
  217. $cart['productInfo']['attrInfo']['weight'] = $product['weight'];
  218. $cart['productInfo']['attrInfo']['volume'] = $product['volume'];
  219. } else {
  220. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid, true);
  221. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid);
  222. }
  223. $cart['trueStock'] = $cart['productInfo']['stock'];
  224. $cart['costPrice'] = $cart['productInfo']['cost'];
  225. $cart['integral'] = $cart['productInfo']['integral'] ?? 0;
  226. $valid[] = $cart;
  227. }
  228. }
  229. }
  230. foreach ($valid as $k => $cart) {
  231. if ($cart['trueStock'] < $cart['cart_num']) {
  232. $cart['cart_num'] = $cart['trueStock'];
  233. $model = new self();
  234. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  235. $valid[$k] = $cart;
  236. }
  237. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  238. if (isset($valid[$k]['productInfo'])) {
  239. unset($valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_show']);
  240. }
  241. }
  242. foreach ($invalid as $k => $cart) {
  243. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  244. if (isset($invalid[$k]['productInfo'])) {
  245. unset($invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_show']);
  246. }
  247. }
  248. return compact('valid', 'invalid');
  249. }
  250. /**
  251. * 拼团
  252. * @param $uid
  253. * @param string $cartIds
  254. * @return array
  255. */
  256. public static function getUserCombinationProductCartList($uid, $cartIds = '')
  257. {
  258. $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';
  259. $model = new self();
  260. $valid = $invalid = [];
  261. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  262. ->where('is_del', 0);
  263. if ($cartIds) $model->where('id', 'IN', $cartIds);
  264. $list = $model->select()->toArray();
  265. if (!count($list)) return compact('valid', 'invalid');
  266. foreach ($list as $k => $cart) {
  267. $product = StoreProduct::field($productInfoField)
  268. ->find($cart['product_id'])->toArray();
  269. $cart['productInfo'] = $product;
  270. //商品不存在
  271. if (!$product) {
  272. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  273. //商品删除或无库存
  274. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  275. $invalid[] = $cart;
  276. //商品属性不对应
  277. // }else if(!StoreProductAttr::issetProductUnique($cart['product_id'],$cart['product_attr_unique'])){
  278. // $invalid[] = $cart;
  279. //正常商品
  280. } else {
  281. $cart['truePrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('price');
  282. $cart['costPrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('cost');
  283. $cart['trueStock'] = StoreCombination::where('id', $cart['combination_id'])->value('stock');
  284. $valid[] = $cart;
  285. }
  286. }
  287. foreach ($valid as $k => $cart) {
  288. if ($cart['trueStock'] < $cart['cart_num']) {
  289. $cart['cart_num'] = $cart['trueStock'];
  290. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  291. $valid[$k] = $cart;
  292. }
  293. }
  294. return compact('valid', 'invalid');
  295. }
  296. /**
  297. * 产品编号
  298. * @param array $ids
  299. * @return array
  300. */
  301. public static function getCartIdsProduct(array $ids)
  302. {
  303. return self::whereIn('id', $ids)->column('product_id', 'id');
  304. }
  305. /**
  306. * 获取购物车内最新一张产品图
  307. */
  308. public static function getProductImage(array $cart_id)
  309. {
  310. return self::whereIn('a.id', $cart_id)->alias('a')->order('a.id desc')
  311. ->join('store_product p', 'p.id = a.product_id')->value('p.image');
  312. }
  313. }