StoreCart.php 16 KB

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