StoreCart.php 16 KB

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