CartLogic.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop有特色的全开源社交分销电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
  7. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  8. // | 微信公众号:好象科技
  9. // | 访问官网:http://www.likemarket.net
  10. // | 访问社区:http://bbs.likemarket.net
  11. // | 访问手册:http://doc.likemarket.net
  12. // | 好象科技开发团队 版权所有 拥有最终解释权
  13. // +----------------------------------------------------------------------
  14. // | Author: LikeShopTeam-段誉
  15. // +----------------------------------------------------------------------
  16. namespace app\shopapi\logic\Order;
  17. use app\common\enum\CartEnum;
  18. use app\common\enum\FootprintEnum;
  19. use app\common\enum\OrderEnum;
  20. use app\common\model\Cart;
  21. use app\common\model\Goods;
  22. use app\common\model\OrderGoods;
  23. use app\common\service\FileService;
  24. /**
  25. * 购物车逻辑
  26. * Class CartLogic
  27. * @package app\shopapi\logic
  28. */
  29. class CartLogic
  30. {
  31. /**
  32. * @notes 购物车列表
  33. * @param $userId
  34. * @return array
  35. * @author 段誉
  36. * @date 2021/7/20 16:32
  37. */
  38. public static function getCartLists($userId, array $carts = null)
  39. {
  40. //列表数据
  41. $lists = [];
  42. //购物车数量
  43. $cartNum = 0;
  44. //合计
  45. $totalAmount = 0;
  46. $carts = is_null($carts) ? (new Cart())->getCartLists($userId) : $carts;
  47. foreach ($carts as $item) {
  48. //购物车状态
  49. $item['cart_status'] = CartEnum::NORMAL;
  50. //商品已被删除
  51. if (empty($item['goods']) || $item['goods']['delete_time'] > 0) {
  52. continue;
  53. }
  54. //商品已被下架
  55. if ($item['goods']['status'] == 0) {
  56. $item['cart_status'] = CartEnum::SALE_OUT;
  57. }
  58. $item['sku_is_delete'] = 0;
  59. // sku 删除
  60. if (empty($item['goods_item'])) {
  61. $item['selected'] = 0;
  62. $item['sku_is_delete'] = 1;
  63. $item['goods_item'] = new \stdClass();
  64. $lists[] = $item;
  65. continue;
  66. }
  67. $item['sku_stock_none'] = 0;
  68. // sku 库存不足
  69. if ($item['goods_item']['stock'] < $item['goods_num']) {
  70. $item['selected'] = 0;
  71. $item['sku_stock_none'] = 1;
  72. $lists[] = $item;
  73. continue;
  74. }
  75. //小计
  76. $item['sub_price'] = bcadd(0, $item['goods_item']['sell_price'] * $item['goods_num'], 2);
  77. //选中的商品且状态正常的才统计
  78. if ($item['selected'] == CartEnum::IS_SELECTED && $item['cart_status'] == CartEnum::NORMAL) {
  79. //购物车数量
  80. $cartNum += $item['goods_num'];
  81. //合计
  82. $totalAmount = bcadd($item['sub_price'], $totalAmount, 2);
  83. }
  84. //商品图片
  85. if (!empty($item['goods']['image'])) {
  86. $item['goods']['image'] = FileService::getFileUrl($item['goods']['image']);
  87. }
  88. if (!empty($item['goods_item']['image'])) {
  89. $item['goods_item']['image'] = FileService::getFileUrl($item['goods_item']['image']);
  90. }
  91. //购物车状态不正常时,选中状态 重置为 未选中
  92. if ($item['cart_status'] != CartEnum::NORMAL) {
  93. $item['selected'] = 0;
  94. }
  95. //用户已购买数量
  96. $item['buy_num'] = OrderGoods::alias('og')
  97. ->join('order o', 'o.id = og.order_id')
  98. ->where(['og.goods_id'=>$item['goods']['id'],'o.order_status'=>[OrderEnum::STATUS_WAIT_PAY,OrderEnum::STATUS_WAIT_DELIVERY,OrderEnum::STATUS_WAIT_RECEIVE,OrderEnum::STATUS_FINISH],'o.user_id'=>$userId,'o.order_type'=>[OrderEnum::NORMAL_ORDER,OrderEnum::VIRTUAL_ORDER]])
  99. ->sum('og.goods_num');
  100. $lists[] = $item;
  101. }
  102. return [
  103. 'lists' => $lists,
  104. 'total_num' => $cartNum,
  105. 'total_amount' => bcadd(0, $totalAmount, 2),
  106. ];
  107. }
  108. /**
  109. * @notes 添加购物车
  110. * @param $params
  111. * @param $userId
  112. * @return bool
  113. * @author 段誉
  114. * @date 2021/7/16 19:08
  115. */
  116. public static function addCart($params, $userId)
  117. {
  118. //商品信息
  119. $goods = (new Goods())->alias('g')
  120. ->field(['g.status', 'g.delete_time', 'gi.stock', 'gi.goods_id'])
  121. ->join('goods_item gi', 'gi.goods_id = g.id')
  122. ->where(['gi.id' => $params['item_id']])
  123. ->find();
  124. // 汽泡足迹
  125. event('Footprint', ['type' => FootprintEnum::ADD_CART, 'user_id' => $userId, 'foreign_id'=>$goods->goods_id]);
  126. //购物车
  127. $cart = (new Cart())->getCartByItem($params['item_id'], $userId);
  128. //添加后的购物车数量(在验证器已验证是否足够库存)
  129. $cartNum = $params['goods_num'] + ($cart['goods_num'] ?? 0);
  130. if (!$cart->isEmpty()) {
  131. //购物车内已有该商品
  132. Cart::where('id', $cart['id'])->update([
  133. 'goods_num' => $cartNum,
  134. 'update_time' => time(),
  135. ]);
  136. } else {
  137. //新增购物车记录
  138. Cart::create([
  139. 'user_id' => $userId,
  140. 'goods_id' => $goods['goods_id'],
  141. 'goods_num' => $cartNum,
  142. 'item_id' => $params['item_id'],
  143. 'create_time' => time(),
  144. ]);
  145. }
  146. return true;
  147. }
  148. /**
  149. * @notes 修改购物车数量
  150. * @param $params
  151. * @return bool|string
  152. * @author 段誉
  153. * @date 2021/7/19 19:11
  154. */
  155. public static function changeCartNum($params)
  156. {
  157. $cart = (new Cart())->getCartById($params['cart_id'], $params['user_id']);
  158. if (intval($params['goods_num']) > $cart['goods_item']['stock']) {
  159. return '很抱歉,库存不足!';
  160. }
  161. $cart->goods_num = intval($params['goods_num']) <= 0 ? 1 : intval($params['goods_num']);
  162. $cart->save();
  163. return true;
  164. }
  165. /**
  166. * @notes 购物车选中状态
  167. * @param $params
  168. * @return Cart
  169. * @author 段誉
  170. * @date 2021/7/19 18:56
  171. */
  172. public static function selected($params)
  173. {
  174. return Cart::update(
  175. ['selected' => $params['selected']],
  176. ['id' => $params['cart_id'], 'user_id' => $params['user_id']]
  177. );
  178. }
  179. /**
  180. * @notes 购物车数量
  181. * @param $userId
  182. * @return array
  183. * @author 段誉
  184. * @date 2021/7/19 18:57
  185. */
  186. public static function getCartNum($userId)
  187. {
  188. $lists = Cart::withoutField(['create_time', 'update_time'])
  189. ->with([
  190. 'goods' => function ($query) {
  191. $query->field([ 'id', 'status', 'delete_time' ]);
  192. },
  193. 'goods_item' => function ($query) {
  194. $query->field(['id', 'sell_price', 'stock' ] );
  195. }
  196. ])
  197. ->where(['user_id' => $userId])
  198. ->order(['id' => 'desc'])
  199. ->select()->toArray();
  200. $carts = static::getCartLists($userId, $lists);
  201. return [ 'num' => $carts['total_num'] ?? 0 ];
  202. }
  203. /**
  204. * @notes 删除购物车
  205. * @param $id
  206. * @param $userId
  207. * @return bool
  208. * @author 段誉
  209. * @date 2021/7/19 18:57
  210. */
  211. public static function del($id, $userId)
  212. {
  213. //TODO 是否需要使用软删除
  214. return Cart::where(['id' => $id, 'user_id' => $userId])->delete();
  215. }
  216. static function del_ids($ids, $userId): bool
  217. {
  218. return Cart::where('user_id' , $userId)->where('id', 'in', $ids)->delete();
  219. }
  220. static function edit_goods_item($params)
  221. {
  222. return Cart::update([ 'item_id' => $params['item_id'], 'goods_num' => $params['goods_num'] ], [
  223. [ 'id', '=', $params['cart_id'] ]
  224. ]);
  225. }
  226. }