StoreCart.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\models\store;
  8. use app\models\system\SystemGroupDataMerchant;
  9. use app\models\system\SystemProductStore;
  10. use crmeb\basic\BaseModel;
  11. use crmeb\traits\ModelTrait;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. /**
  16. * TODO 购物车Model
  17. * Class StoreCart
  18. * @package app\models\store
  19. */
  20. class StoreCart extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'id';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'store_cart';
  32. use ModelTrait;
  33. protected $insert = ['add_time'];
  34. protected function setAddTimeAttr()
  35. {
  36. return time();
  37. }
  38. public static function setCart($uid, $product_id, $cart_num = 1, $product_attr_unique = '', $type = 'product', $is_new = 0, $assistance_id = 0, $combination_id = 0, $seckill_id = 0, $bargain_id = 0, $mer_id = false)
  39. {
  40. if ($cart_num < 1) $cart_num = 1;
  41. if ($seckill_id) {
  42. if (!StoreSeckill::isSeckillEnd($seckill_id, $mer_id))
  43. return self::setErrorInfo('活动已结束');
  44. $StoreSeckillinfo = StoreSeckill::getValidProduct($seckill_id, $mer_id);
  45. if (!$StoreSeckillinfo)
  46. return self::setErrorInfo('该商品已下架或删除');
  47. $userbuycount = StoreOrder::merSet($mer_id)->where('uid', $uid)->where('paid', 1)->where('seckill_id', $seckill_id)->count();
  48. if ($StoreSeckillinfo['num'] <= $userbuycount || $StoreSeckillinfo['num'] < $cart_num)
  49. return self::setErrorInfo('每人限购' . $StoreSeckillinfo['num'] . '件');
  50. $res = StoreProductAttrValue::where('product_id', $seckill_id)->where('unique', $product_attr_unique)->where('type', 1)->field('suk,quota')->find();
  51. if ($cart_num > $res['quota'])
  52. return self::setErrorInfo('该商品库存不足' . $cart_num);
  53. $product_stock = StoreProductAttrValue::where('product_id', $StoreSeckillinfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  54. if ($product_stock < $cart_num)
  55. return self::setErrorInfo('该商品库存不足' . $cart_num);
  56. } elseif ($bargain_id) {
  57. if (!StoreBargain::validBargain($bargain_id))
  58. return self::setErrorInfo('该商品已下架或删除');
  59. $StoreBargainInfo = StoreBargain::getBargain($bargain_id);
  60. $res = StoreProductAttrValue::where('product_id', $bargain_id)->where('type', 2)->field('suk,quota')->find();
  61. if ($cart_num > $res['quota'])
  62. return self::setErrorInfo('该商品库存不足' . $cart_num);
  63. $product_stock = StoreProductAttrValue::where('product_id', $StoreBargainInfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  64. if ($product_stock < $cart_num)
  65. return self::setErrorInfo('该商品库存不足' . $cart_num);
  66. } elseif ($combination_id) {//拼团
  67. $StoreCombinationInfo = StoreCombination::getCombinationOne($combination_id);
  68. if (!$StoreCombinationInfo)
  69. return self::setErrorInfo('该商品已下架或删除');
  70. $userbuycount = StoreOrder::merSet($mer_id)->where('uid', $uid)->where('paid', 1)->where('combination_id', $combination_id)->count();
  71. // if ($StoreCombinationInfo['num'] <= $userbuycount || $StoreCombinationInfo['num'] < $cart_num)
  72. // return self::setErrorInfo('每人限购' . $StoreCombinationInfo['num'] . '件');
  73. $res = StoreProductAttrValue::where('product_id', $combination_id)->where('unique', $product_attr_unique)->where('type', 3)->field('suk,quota')->find();
  74. if ($cart_num > $res['quota'])
  75. return self::setErrorInfo('该商品库存不足' . $cart_num);
  76. $product_stock = StoreProductAttrValue::where('product_id', $StoreCombinationInfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  77. if ($product_stock < $cart_num)
  78. return self::setErrorInfo('该商品库存不足' . $cart_num);
  79. } elseif (isset($assistance_id) && $assistance_id) {//助力
  80. $StoreAssistanceInfo = StoreAssistance::getAssistanceOne($assistance_id);
  81. if (!$StoreAssistanceInfo)
  82. return self::setErrorInfo('该商品已下架或删除');
  83. $res = StoreProductAttrValue::where('product_id', $assistance_id)->where('unique', $product_attr_unique)->where('type', 4)->field('suk,quota')->find();
  84. if ($cart_num > $res['quota'])
  85. return self::setErrorInfo('该商品库存不足' . $cart_num);
  86. $product_stock = StoreProductAttrValue::where('product_id', $StoreAssistanceInfo['product_id'])->where('suk', $res['suk'])->where('type', 0)->value('stock');
  87. if ($product_stock < $cart_num)
  88. return self::setErrorInfo('该商品库存不足' . $cart_num);
  89. } else {
  90. if (StoreProduct::isValidOneProduct($product_id, $mer_id)){
  91. $userbuycount = StoreOrder::getOrderProductCount($uid, $product_id, $mer_id);
  92. if ($userbuycount >= 1 || $cart_num > 1)
  93. return self::setErrorInfo('此商品限购1件');
  94. }else if (!StoreProduct::isValidProduct($product_id, $mer_id))
  95. return self::setErrorInfo('该商品已下架或删除');
  96. if (!StoreProductAttr::issetProductUnique($product_id, $product_attr_unique))
  97. return self::setErrorInfo('请选择有效的商品属性');
  98. if (StoreProduct::getProductStock($product_id, $product_attr_unique) < $cart_num)
  99. return self::setErrorInfo('该商品库存不足' . $cart_num);
  100. }
  101. 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('assistance_id', $assistance_id)->where('combination_id', $combination_id)->where('bargain_id', $bargain_id)->where('seckill_id', $seckill_id)->find()) {
  102. if ($is_new)
  103. $cart->cart_num = $cart_num;
  104. else
  105. $cart->cart_num = bcadd($cart_num, $cart->cart_num, 0);
  106. $cart->add_time = time();
  107. $cart->save();
  108. return $cart;
  109. } else {
  110. $add_time = time();
  111. return self::create(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'assistance_id', 'combination_id', 'add_time', 'bargain_id', 'seckill_id'));
  112. }
  113. }
  114. public static function removeUserCart($uid, $ids)
  115. {
  116. return self::where('uid', $uid)->where('id', 'IN', implode(',', $ids))->update(['is_del' => 1]);
  117. }
  118. public static function getUserCartNum($uid, $type, $numType)
  119. {
  120. if ($numType) {
  121. return self::where('uid', $uid)->where('type', $type)->where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->count();
  122. } else {
  123. return self::where('uid', $uid)->where('type', $type)->where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->sum('cart_num');
  124. }
  125. }
  126. /**
  127. * TODO 修改购物车库存
  128. * @param $cartId
  129. * @param $cartNum
  130. * @param $uid
  131. * @param bool $mer_id
  132. * @return StoreCart|bool
  133. * @throws DataNotFoundException
  134. * @throws DbException
  135. * @throws ModelNotFoundException
  136. */
  137. public static function changeUserCartNum($cartId, $cartNum, $uid, $mer_id = false)
  138. {
  139. $count = self::where('uid', $uid)->where('id', $cartId)->count();
  140. if (!$count) return self::setErrorInfo('参数错误');
  141. $cartInfo = self::where('uid', $uid)->where('id', $cartId)->field('product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num')->find()->toArray();
  142. $stock = 0;
  143. if ($cartInfo['bargain_id']) {
  144. //TODO 获取砍价商品的库存
  145. $stock = 0;
  146. } else if ($cartInfo['seckill_id']) {
  147. //TODO 获取秒杀商品的库存
  148. $stock = 0;
  149. } else if ($cartInfo['combination_id']) {
  150. //TODO 获取拼团商品的库存
  151. $stock = 0;
  152. } else if ($cartInfo['product_id']) {
  153. //TODO 获取普通商品的库存
  154. $stock = StoreProduct::getProductStock($cartInfo['product_id'], $cartInfo['product_attr_unique'], $mer_id);
  155. }
  156. if (!$stock) return self::setErrorInfo('暂无库存');
  157. if (!$cartNum) return self::setErrorInfo('库存错误');
  158. if ($stock < $cartNum) return self::setErrorInfo('库存不足' . $cartNum);
  159. if ($cartInfo['cart_num'] == $cartNum) return true;
  160. return self::where('uid', $uid)->where('id', $cartId)->update(['cart_num' => $cartNum]);
  161. }
  162. /**
  163. * 优化新方法
  164. * @param $uid
  165. * @param string $cartIds
  166. * @param int $status
  167. * @param bool $mer_id
  168. * @return mixed
  169. * @throws DataNotFoundException
  170. * @throws DbException
  171. * @throws ModelNotFoundException
  172. */
  173. public static function getUserProductCartListV1($uid, $cartIds = '', $status = 0, $mer_id = false)
  174. {
  175. $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_ban,is_alone,delivery';
  176. $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';
  177. $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';
  178. $combinationInfoField = 'id,image,price,postage,sales,stock,title as store_name,is_show,is_del,is_postage,cost,temp_id,weight,volume';
  179. $assistanceInfoField = 'id,image,price,postage,sales,stock,title as store_name,is_show,is_del,is_postage,cost,temp_id,weight,volume';
  180. $model = new self();
  181. $valid = $invalid = [];
  182. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  183. ->where('is_del', 0);
  184. if (!$status) $model = $model->where('is_new', 0);
  185. if ($cartIds) $model = $model->where('id', 'IN', $cartIds);
  186. $model = $model->order('add_time DESC');
  187. $list = $model->select()->toArray();
  188. if (!count($list)) return compact('valid', 'invalid');
  189. $now = time();
  190. $seckillProduct = [];
  191. $bargainProduct = [];
  192. $combinationProduct = [];
  193. $product_all = [];
  194. $systemGroupDataMerchant = [];
  195. $seckill_ids = array_unique(array_column($list, 'seckill_id'));
  196. if (!empty($seckill_ids)) {
  197. $seckillProduct = StoreSeckill::merSet($mer_id)->field($seckillInfoField)->where('id', 'in', $seckill_ids)->select()->toArray();
  198. if (!empty($seckillProduct)) {
  199. $time_ids = array_unique(array_column($seckillProduct, 'time_id'));
  200. $seckillProduct = array_combine(array_column($seckillProduct, 'id'), $seckillProduct);
  201. if (!empty($time_ids)) {
  202. $systemGroupDataMerchant = SystemGroupDataMerchant::where('id', 'in', $time_ids)->select()->toArray();
  203. if (!empty($systemGroupDataMerchant))
  204. $systemGroupDataMerchant = array_combine(array_column($systemGroupDataMerchant, 'id'), $systemGroupDataMerchant);
  205. }
  206. }
  207. }
  208. $bargain_ids = array_unique(array_column($list, 'bargain_id'));
  209. if (!empty($bargain_ids)) {
  210. $bargainProduct = StoreBargain::merSet($mer_id)->field($bargainInfoField)->where('id', 'in', $bargain_ids)->select()->toArray();
  211. if (!empty($bargainProduct))
  212. $bargainProduct = array_combine(array_column($bargainProduct, 'id'), $bargainProduct);
  213. }
  214. $combination_ids = array_unique(array_column($list, 'combination_id'));
  215. if (!empty($combination_ids)) {
  216. $combinationProduct = StoreCombination::merSet($mer_id)->field($combinationInfoField)->where('id', 'in', $combination_ids)->select()->toArray();
  217. if (!empty($combinationProduct))
  218. $combinationProduct = array_combine(array_column($combinationProduct, 'id'), $combinationProduct);
  219. }
  220. $assistance_ids = array_unique(array_column($list, 'assistance_id'));
  221. if (!empty($assistance_ids)) {
  222. $assistanceProduct = StoreAssistance::merSet($mer_id)->field($assistanceInfoField)->where('id', 'in', $assistance_ids)->select()->toArray();
  223. if (!empty($assistanceProduct))
  224. $assistanceProduct = array_combine(array_column($assistanceProduct, 'id'), $assistanceProduct);
  225. }
  226. $product_ids = array_unique(array_column($list, 'product_id'));
  227. if (!empty($product_ids)) {
  228. $product_all = StoreProduct::merSet($mer_id)->field($productInfoField)->where('id', 'in', $product_ids)->select()->toArray();
  229. if (!empty($product_all))
  230. $product_all = array_combine(array_column($product_all, 'id'), $product_all);
  231. }
  232. $deduction = ['seckill_id' => $seckill_ids, 'bargain_id' => $bargain_ids, 'combination_id' => $combination_ids, 'assistance_id' => $assistance_ids];
  233. //商品属性
  234. $product_attr_all = [];
  235. $attr_unique_s = array_unique(array_column($list, 'product_attr_unique'));
  236. if (!empty($attr_unique_s)) {
  237. $product_attr_all = StoreProductAttr::uniqueByAttrInfoMore($attr_unique_s);
  238. if (!empty($product_attr_all))
  239. $product_attr_all = array_combine(array_column($product_attr_all, 'unique'), $product_attr_all);
  240. }
  241. //需要删除id集合
  242. $del_array = [];
  243. foreach ($list as $k => $cart) {
  244. if ($cart['seckill_id']) {
  245. $product = isset($seckillProduct[$cart['seckill_id']]) && !empty($seckillProduct[$cart['seckill_id']]) ? $seckillProduct[$cart['seckill_id']] : [];
  246. } elseif ($cart['bargain_id']) {
  247. $product = isset($bargainProduct[$cart['bargain_id']]) && !empty($bargainProduct[$cart['bargain_id']]) ? $bargainProduct[$cart['bargain_id']] : [];
  248. } elseif ($cart['combination_id']) {
  249. $product = isset($combinationProduct[$cart['combination_id']]) && !empty($combinationProduct[$cart['combination_id']]) ? $combinationProduct[$cart['combination_id']] : [];
  250. } elseif ($cart['assistance_id']) {
  251. $product = isset($assistanceProduct[$cart['assistance_id']]) && !empty($assistanceProduct[$cart['assistance_id']]) ? $assistanceProduct[$cart['assistance_id']] : [];
  252. } else {
  253. $product = isset($product_all[$cart['product_id']]) && !empty($product_all[$cart['product_id']]) ? $product_all[$cart['product_id']] : [];
  254. }
  255. $product['image'] = set_file_url($product['image']);
  256. $cart['productInfo'] = $product;
  257. //商品不存在
  258. if (!$product) {
  259. $del_array[] = $cart['id'];
  260. //商品删除或无库存
  261. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  262. $invalid[] = $cart;
  263. //秒杀商品未开启或者已结束
  264. } else if ($cart['seckill_id'] && ($product['start_time'] > $now || $product['stop_time'] < $now - 86400)) {
  265. $invalid[] = $product;
  266. //商品属性不对应
  267. } else if (!StoreProductAttr::issetProductUnique($cart['product_id'], $cart['product_attr_unique']) && !$cart['assistance_id'] && !$cart['combination_id'] && !$cart['seckill_id'] && !$cart['bargain_id']) {
  268. $invalid[] = $cart;
  269. //正常商品
  270. } else {
  271. if ($cart['seckill_id']) {
  272. $config = isset($systemGroupDataMerchant[$product['time_id']]) && !empty($systemGroupDataMerchant[$product['time_id']]) ? $systemGroupDataMerchant[$product['time_id']] : [];
  273. if ($config) {
  274. $arr = json_decode($config['value'], true);
  275. $now_hour = date('H', time());
  276. $start_hour = $arr['time']['value'];
  277. $continued = $arr['continued']['value'];
  278. $end_hour = $start_hour + $continued;
  279. if ($start_hour > $now_hour) {
  280. //'活动未开启';
  281. $invalid[] = $cart;
  282. continue;
  283. } elseif ($end_hour < $now_hour) {
  284. //'活动已结束';
  285. $invalid[] = $cart;
  286. continue;
  287. }
  288. }
  289. }
  290. if(isset($product['is_alone'])) $cart['is_alone'] = $product['is_alone'];
  291. if(isset($product['delivery'])) $cart['delivery'] = $product['delivery'];
  292. $cart['store_id'] = SystemProductStore::alias('a')->where('a.product_id', $product['id'])->join('system_store s', 'a.store_id = s.id')->column('store_id');
  293. $cart['tip'] = 0;
  294. if ($cart['product_attr_unique']) {
  295. $attrInfo = isset($product_attr_all[$cart['product_attr_unique']]) && !empty($product_attr_all[$cart['product_attr_unique']]) ? $product_attr_all[$cart['product_attr_unique']] : [];
  296. //商品没有对应的属性
  297. if (!$attrInfo || !$attrInfo['stock'])
  298. $invalid[] = $cart;
  299. else {
  300. $cart['productInfo']['attrInfo'] = $attrInfo;
  301. if ($cart['assistance_id'] || $cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  302. $cart['truePrice'] = $attrInfo['price'];
  303. $cart['vip_truePrice'] = 0;
  304. } else {
  305. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid, true, $mer_id);
  306. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid, false, $mer_id);
  307. }
  308. $cart['trueStock'] = $attrInfo['stock'];
  309. $cart['costPrice'] = $attrInfo['cost'];
  310. $cart['productInfo']['image'] = empty($attrInfo['image']) ? $cart['productInfo']['image'] : $attrInfo['image'];
  311. $valid[] = $cart;
  312. }
  313. } else {
  314. if ($cart['assistance_id'] || $cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  315. $cart['truePrice'] = $cart['productInfo']['price'];
  316. $cart['vip_truePrice'] = 0;
  317. if ($cart['bargain_id']) {
  318. $cart['productInfo']['attrInfo'] = StoreProductAttrValue::where('product_id', $cart['bargain_id'])->where('type', 2)->find();
  319. }
  320. $cart['productInfo']['attrInfo']['weight'] = $product['weight'];
  321. $cart['productInfo']['attrInfo']['volume'] = $product['volume'];
  322. } else {
  323. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid, true, $mer_id);
  324. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid, false, $mer_id);
  325. }
  326. $cart['trueStock'] = $cart['productInfo']['stock'];
  327. $cart['costPrice'] = $cart['productInfo']['cost'];
  328. $valid[] = $cart;
  329. }
  330. }
  331. }
  332. //统一删除
  333. if ($del_array) {
  334. $model->where('id', 'in', $del_array)->update(['is_del' => 1]);
  335. }
  336. $delivery = (int)sys_config('store_self_mention', '', $mer_id) ?? 0;
  337. foreach ($valid as $k => $cart) {
  338. if ($cart['trueStock'] < $cart['cart_num']) {
  339. $cart['cart_num'] = $cart['trueStock'];
  340. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  341. $valid[$k] = $cart;
  342. }
  343. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  344. if (isset($valid[$k]['productInfo'])) {
  345. unset($valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_show']);
  346. }
  347. if(isset($cart['is_alone']) && $cart['is_alone'] === 0){
  348. $valid[$k]['delivery'] = $delivery;
  349. }
  350. }
  351. $type = count($valid) && isset($valid[0]['delivery']) ? $valid[0]['delivery'] : '';
  352. $bool = true;
  353. foreach ($valid as $k => $cart) {
  354. if(isset($cart['delivery']) && $cart['delivery'] !== $type){
  355. $bool = false;
  356. }
  357. }
  358. foreach ($invalid as $k => $cart) {
  359. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  360. if (isset($invalid[$k]['productInfo'])) {
  361. unset($invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_show']);
  362. }
  363. }
  364. return compact('valid', 'invalid', 'deduction', 'bool', 'type');
  365. }
  366. /**
  367. * @param $uid
  368. * @param string $cartIds
  369. * @param int $status
  370. * @return array
  371. * @throws DataNotFoundException
  372. * @throws DbException
  373. * @throws ModelNotFoundException
  374. */
  375. public static function getUserProductCartList($uid, $cartIds = '', $status = 0, $mer_id = false)
  376. {
  377. $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_ban,is_alone,delivery';
  378. $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';
  379. $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';
  380. $combinationInfoField = 'id,image,price,postage,sales,stock,title as store_name,is_show,is_del,is_postage,cost,temp_id,weight,volume';
  381. $assistanceInfoField = 'id,image,price,postage,sales,stock,title as store_name,is_show,is_del,is_postage,cost,temp_id,weight,volume';
  382. $model = new self();
  383. $valid = $invalid = [];
  384. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  385. ->where('is_del', 0);
  386. if (!$status) $model = $model->where('is_new', 0);
  387. if ($cartIds) $model = $model->where('id', 'IN', $cartIds);
  388. $model = $model->order('add_time DESC');
  389. $list = $model->select()->toArray();
  390. if (!count($list)) return compact('valid', 'invalid');
  391. $now = time();
  392. foreach ($list as $k => $cart) {
  393. if ($cart['seckill_id']) {
  394. $product = StoreSeckill::merSet($mer_id)->field($seckillInfoField)
  395. ->find($cart['seckill_id'])->toArray();
  396. } elseif ($cart['bargain_id']) {
  397. $product = StoreBargain::merSet($mer_id)->field($bargainInfoField)
  398. ->find($cart['bargain_id'])->toArray();
  399. } elseif ($cart['combination_id']) {
  400. $product = StoreCombination::merSet($mer_id)->field($combinationInfoField)
  401. ->find($cart['combination_id'])->toArray();
  402. } elseif ($cart['assistance_id']) {
  403. $product = StoreAssistance::merSet($mer_id)->field($assistanceInfoField)
  404. ->find($cart['assistance_id'])->toArray();
  405. } else {
  406. $product = StoreProduct::merSet($mer_id)->field($productInfoField)
  407. ->find($cart['product_id'])->toArray();
  408. }
  409. $product['image'] = set_file_url($product['image']);
  410. $cart['productInfo'] = $product;
  411. //商品不存在
  412. if (!$product) {
  413. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  414. //商品删除或无库存
  415. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  416. $invalid[] = $cart;
  417. //秒杀商品未开启或者已结束
  418. } else if ($cart['seckill_id'] && ($product['start_time'] > $now || $product['stop_time'] < $now - 86400)) {
  419. $invalid[] = $product;
  420. //商品属性不对应
  421. } else if (!StoreProductAttr::issetProductUnique($cart['product_id'], $cart['product_attr_unique']) && !$cart['combination_id'] && !$cart['seckill_id'] && !$cart['bargain_id']) {
  422. $invalid[] = $cart;
  423. //正常商品
  424. } else {
  425. if ($cart['seckill_id']) {
  426. $config = SystemGroupDataMerchant::get($product['time_id']);
  427. if ($config) {
  428. $arr = json_decode($config->value, true);
  429. $now_hour = date('H', time());
  430. $start_hour = $arr['time']['value'];
  431. $continued = $arr['continued']['value'];
  432. $end_hour = $start_hour + $continued;
  433. if ($start_hour > $now_hour) {
  434. //'活动未开启';
  435. $invalid[] = $cart;
  436. continue;
  437. } elseif ($end_hour < $now_hour) {
  438. //'活动已结束';
  439. $invalid[] = $cart;
  440. continue;
  441. }
  442. }
  443. }
  444. if(isset($product['is_alone'])) $cart['is_alone'] = $product['is_alone'];
  445. if(isset($product['delivery'])) $cart['delivery'] = $product['delivery'];
  446. if ($cart['product_attr_unique']) {
  447. $attrInfo = StoreProductAttr::uniqueByAttrInfo($cart['product_attr_unique']);
  448. //商品没有对应的属性
  449. if (!$attrInfo || !$attrInfo['stock'])
  450. $invalid[] = $cart;
  451. else {
  452. $cart['productInfo']['attrInfo'] = $attrInfo;
  453. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  454. $cart['truePrice'] = $attrInfo['price'];
  455. $cart['vip_truePrice'] = 0;
  456. } else {
  457. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid, true, $mer_id);
  458. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($attrInfo['price'], $uid, false, $mer_id);
  459. }
  460. $cart['trueStock'] = $attrInfo['stock'];
  461. $cart['costPrice'] = $attrInfo['cost'];
  462. $cart['productInfo']['image'] = empty($attrInfo['image']) ? $cart['productInfo']['image'] : $attrInfo['image'];
  463. $valid[] = $cart;
  464. }
  465. } else {
  466. if ($cart['combination_id'] || $cart['seckill_id'] || $cart['bargain_id']) {
  467. $cart['truePrice'] = $cart['productInfo']['price'];
  468. $cart['vip_truePrice'] = 0;
  469. if ($cart['bargain_id']) {
  470. $cart['productInfo']['attrInfo'] = StoreProductAttrValue::where('product_id', $cart['bargain_id'])->where('type', 2)->find();
  471. }
  472. $cart['productInfo']['attrInfo']['weight'] = $product['weight'];
  473. $cart['productInfo']['attrInfo']['volume'] = $product['volume'];
  474. } else {
  475. $cart['truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid, true, $mer_id);
  476. $cart['vip_truePrice'] = (float)StoreProduct::setLevelPrice($cart['productInfo']['price'], $uid, false, $mer_id);
  477. }
  478. $cart['trueStock'] = $cart['productInfo']['stock'];
  479. $cart['costPrice'] = $cart['productInfo']['cost'];
  480. $valid[] = $cart;
  481. }
  482. }
  483. }
  484. foreach ($valid as $k => $cart) {
  485. if ($cart['trueStock'] < $cart['cart_num']) {
  486. $cart['cart_num'] = $cart['trueStock'];
  487. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  488. $valid[$k] = $cart;
  489. }
  490. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  491. if (isset($valid[$k]['productInfo'])) {
  492. unset($valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_del'], $valid[$k]['productInfo']['is_show']);
  493. }
  494. }
  495. foreach ($invalid as $k => $cart) {
  496. unset($valid[$k]['uid'], $valid[$k]['is_del'], $valid[$k]['is_new'], $valid[$k]['is_pay'], $valid[$k]['add_time']);
  497. if (isset($invalid[$k]['productInfo'])) {
  498. unset($invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_del'], $invalid[$k]['productInfo']['is_show']);
  499. }
  500. }
  501. $delivery = (int)sys_config('store_self_mention', '', $mer_id) ?? 0;
  502. return compact('valid', 'invalid', 'delivery');
  503. }
  504. /**
  505. * 拼团
  506. * @param $uid
  507. * @param string $cartIds
  508. * @return array
  509. * @throws DataNotFoundException
  510. * @throws DbException
  511. * @throws ModelNotFoundException
  512. */
  513. public static function getUserCombinationProductCartList($uid, $cartIds = '')
  514. {
  515. $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';
  516. $model = new self();
  517. $valid = $invalid = [];
  518. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  519. ->where('is_del', 0);
  520. if ($cartIds) $model->where('id', 'IN', $cartIds);
  521. $list = $model->select()->toArray();
  522. if (!count($list)) return compact('valid', 'invalid');
  523. foreach ($list as $k => $cart) {
  524. $product = StoreProduct::field($productInfoField)
  525. ->find($cart['product_id'])->toArray();
  526. $cart['productInfo'] = $product;
  527. //商品不存在
  528. if (!$product) {
  529. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  530. //商品删除或无库存
  531. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  532. $invalid[] = $cart;
  533. //商品属性不对应
  534. // }else if(!StoreProductAttr::issetProductUnique($cart['product_id'],$cart['product_attr_unique'])){
  535. // $invalid[] = $cart;
  536. //正常商品
  537. } else {
  538. $cart['truePrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('price');
  539. $cart['costPrice'] = (float)StoreCombination::where('id', $cart['combination_id'])->value('cost');
  540. $cart['trueStock'] = StoreCombination::where('id', $cart['combination_id'])->value('stock');
  541. $valid[] = $cart;
  542. }
  543. }
  544. foreach ($valid as $k => $cart) {
  545. if ($cart['trueStock'] < $cart['cart_num']) {
  546. $cart['cart_num'] = $cart['trueStock'];
  547. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  548. $valid[$k] = $cart;
  549. }
  550. }
  551. return compact('valid', 'invalid');
  552. }
  553. /**
  554. * 商品编号
  555. * @param array $ids
  556. * @return array
  557. */
  558. public static function getCartIdsProduct(array $ids)
  559. {
  560. return self::whereIn('id', $ids)->column('product_id', 'id');
  561. }
  562. /**
  563. * 获取购物车内最新一张商品图
  564. */
  565. public static function getProductImage(array $cart_id)
  566. {
  567. return self::whereIn('a.id', $cart_id)->alias('a')->order('a.id desc')
  568. ->join('store_product p', 'p.id = a.product_id')->value('p.image');
  569. }
  570. }