StoreCart.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\store;
  12. use basic\ModelBasic;
  13. use traits\ModelTrait;
  14. /**商品购物车
  15. * Class StoreCart
  16. * @package app\wap\model\store
  17. */
  18. class StoreCart extends ModelBasic
  19. {
  20. use ModelTrait;
  21. protected $insert = ['add_time'];
  22. protected function setAddTimeAttr()
  23. {
  24. return time();
  25. }
  26. /**加入购物车
  27. * @param $uid
  28. * @param $product_id
  29. * @param int $cart_num
  30. * @param string $product_attr_unique
  31. * @param string $type
  32. * @param int $is_new
  33. * @param int $combination_id
  34. * @param int $seckill_id
  35. * @param int $integral_id
  36. * @return array|bool|false|object|\PDOStatement|string|\think\Model
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @throws \think\exception\DbException
  40. */
  41. public static function setCart($uid, $product_id, $cart_num = 1, $product_attr_unique = '', $type = 'product', $is_new = 0, $combination_id = 0, $seckill_id = 0, $integral_id = 0)
  42. {
  43. if ($cart_num < 1) $cart_num = 1;
  44. if (!StoreProduct::isValidProduct($product_id))
  45. return self::setErrorInfo('该产品已下架或删除');
  46. if (StoreProduct::getProductStock($product_id, $product_attr_unique) < $cart_num)
  47. return self::setErrorInfo('该产品库存不足');
  48. $where = [
  49. 'type' => $type,
  50. 'uid' => $uid,
  51. 'product_id' => $product_id,
  52. 'product_attr_unique' => $product_attr_unique,
  53. 'is_new' => $is_new,
  54. 'is_pay' => 0,
  55. 'is_del' => 0,
  56. 'combination_id' => $combination_id,
  57. 'seckill_id' => $seckill_id,
  58. 'integral_id' => $integral_id
  59. ];
  60. if ($cart = self::where($where)->find()) {
  61. $cart->cart_num = $cart_num;
  62. $cart->add_time = time();
  63. $cart->save();
  64. return $cart;
  65. } else {
  66. return self::set(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'combination_id', 'seckill_id', 'integral_id'));
  67. }
  68. }
  69. /**删除购物车
  70. * @param $uid
  71. * @param $ids
  72. * @return StoreCart
  73. */
  74. public static function removeUserCart($uid, $ids)
  75. {
  76. return self::where('uid', $uid)->where('id', 'IN', $ids)->update(['is_del' => 1]);
  77. }
  78. /**获取购物车数量
  79. * @param $uid
  80. * @param $type
  81. * @return int|string
  82. * @throws \think\Exception
  83. */
  84. public static function getUserCartNum($uid, $type)
  85. {
  86. return self::where('uid', $uid)->where('type', $type)->where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->count();
  87. }
  88. /**修改购物车商品数量
  89. * @param $cartId
  90. * @param $cartNum
  91. * @param $uid
  92. * @return StoreCart|bool
  93. */
  94. public static function changeUserCartNum($cartId, $cartNum, $uid)
  95. {
  96. if (!self::be(['uid' => $uid, 'id' => $cartId, 'cart_num' => $cartNum])) {
  97. return self::where('uid', $uid)->where('id', $cartId)->update(['cart_num' => $cartNum]);
  98. } else {
  99. return true;
  100. }
  101. }
  102. /**获取购物车数据
  103. * @param $uid
  104. * @param string $cartIds
  105. * @param int $status
  106. * @param $is_vip
  107. * @return array
  108. * @throws \think\Exception
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. * @throws \think\exception\DbException
  112. */
  113. public static function getUserProductCartList($uid, $cartIds = '', $status = 0, $is_vip)
  114. {
  115. $productInfoField = 'id,image,slider_image,price,cost,ot_price,vip_price,postage,mer_id,give_gold_num,free_shipping,cate_id,sales,stock,store_name,unit_name,is_show,is_del,is_postage';
  116. $model = new self();
  117. $valid = $invalid = [];
  118. if ($cartIds)
  119. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)
  120. ->where('is_del', 0);
  121. else
  122. $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)->where('is_new', 0)
  123. ->where('is_del', 0);
  124. if ($cartIds) $model->where('id', 'IN', $cartIds);
  125. $list = $model->select()->toArray();
  126. if (!count($list)) return compact('valid', 'invalid');
  127. foreach ($list as $k => $cart) {
  128. $product = StoreProduct::field($productInfoField)
  129. ->find($cart['product_id'])->toArray();
  130. $cart['productInfo'] = $product;
  131. //商品不存在
  132. if (!$product) {
  133. $model->where('id', $cart['id'])->update(['is_del' => 1]);
  134. //商品删除或无库存
  135. } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) {
  136. $invalid[] = $cart;
  137. } else {
  138. $cart['truePrice'] = $is_vip ? (isset($cart['productInfo']['vip_price']) ? (float)$cart['productInfo']['vip_price'] : (float)$cart['productInfo']['price']) : (float)$cart['productInfo']['price'];
  139. $cart['costPrice'] = (float)$cart['productInfo']['cost'];
  140. $cart['trueStock'] = $cart['productInfo']['stock'];
  141. $valid[] = $cart;
  142. }
  143. }
  144. foreach ($valid as $k => $cart) {
  145. if ($cart['trueStock'] < $cart['cart_num']) {
  146. $cart['cart_num'] = $cart['trueStock'];
  147. $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]);
  148. $valid[$k] = $cart;
  149. }
  150. }
  151. return compact('valid', 'invalid');
  152. }
  153. }