CartService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\common\Service;
  3. use app\common\model\Carts;
  4. use think\Exception;
  5. use app\common\Enum\GoodsEnum;
  6. use app\common\exception\BusinessException;
  7. class CartService
  8. {
  9. /**
  10. * 将购物车数据转换为标准的商品列表格式
  11. * @param array $cart_ids 购物车ID列表
  12. * @param int $user_id 用户ID
  13. * @return array
  14. * @throws Exception
  15. */
  16. public static function convertCartToGoodsList($cart_ids, $user_id)
  17. {
  18. // 查询购物车数据
  19. $cartItems = Carts::where('id', 'in', $cart_ids)
  20. ->where('user_id', $user_id)
  21. ->select();
  22. if (empty($cartItems)) {
  23. throw new BusinessException("购物车数据不存在");
  24. }
  25. $goods_list = [];
  26. foreach ($cartItems as $cart) {
  27. $goods_list[] = [
  28. 'goods_id' => $cart->goods_id,
  29. 'goods_sku_id' => $cart->goods_sku_id,
  30. 'nums' => $cart->nums
  31. ];
  32. }
  33. return $goods_list;
  34. }
  35. /**
  36. * @ DateTime 2021-05-31
  37. * @ 获取购物车商品列表
  38. * @param string $ids
  39. * @param integer $user_id
  40. * @param integer $sceneval
  41. * @return array
  42. */
  43. public static function getGoodsList($ids, $user_id)
  44. {
  45. return Carts::field("c.*,GROUP_CONCAT(sp.name,':',sv.value order by sp.id asc) sku_attr")
  46. ->with([
  47. 'Goods' => function ($query) {
  48. $query->where('status', GoodsEnum::STATUS_ON_SALE);
  49. },
  50. 'Sku'
  51. ])
  52. ->alias('c')
  53. ->join('shop_goods_sku sku', 'c.goods_sku_id=sku.id', 'LEFT')
  54. ->join('shop_goods_sku_spec p', "FIND_IN_SET(p.id,sku.spec_value_ids)", 'LEFT')
  55. ->join('shop_spec sp', 'sp.id=p.spec_id', 'LEFT')
  56. ->join('shop_spec_value sv', 'sv.id=p.spec_value_id', 'LEFT')
  57. ->where(function ($query) use ($ids) {
  58. if ($ids) {
  59. $query->where('c.id', 'in', $ids);
  60. }
  61. })
  62. ->where('c.user_id', $user_id)
  63. ->order('c.createtime desc')
  64. ->group('c.id')
  65. ->select();
  66. }
  67. /**
  68. * 添加商品到购物车
  69. *
  70. * @param string $goods_id 商品ID
  71. * @param string $goods_sku_id 商品SKUID
  72. * @param int $nums 数量
  73. * @param int $user_id 会员ID
  74. * @return mixed
  75. */
  76. public static function push($goods_id, $goods_sku_id, $nums = 1, $user_id = 0)
  77. {
  78. $row = Carts::where('goods_id', $goods_id)
  79. ->where('goods_sku_id', $goods_sku_id)
  80. ->where('user_id', $user_id)
  81. ->find();
  82. //已存在,数量加
  83. if ($row) {
  84. $row->setInc('nums', $nums);
  85. } else {
  86. $row = new Carts();
  87. $row->save([
  88. 'goods_id' => $goods_id,
  89. 'goods_sku_id' => $goods_sku_id,
  90. 'user_id' => $user_id,
  91. 'nums' => $nums
  92. ]);
  93. }
  94. return $row->id;
  95. }
  96. /**
  97. * 清空购物车
  98. */
  99. public static function clear($cartIds,$userId)
  100. {
  101. Carts::where('id', 'IN', $cartIds)->where('user_id', $userId)->delete();
  102. }
  103. // 删除购物车
  104. public static function deleteCart($cartIds =[], $userId = 0)
  105. {
  106. return Carts::where('id', 'IN', $cartIds)
  107. ->where('user_id', $userId)->delete();
  108. }
  109. /**
  110. * 批量更新购物车商品数量
  111. * @param array $cart_ids 购物车ID数组
  112. * @param array $goods_list 商品列表(包含goods_id, goods_sku_id, nums)
  113. * @param int $user_id 用户ID
  114. * @return void
  115. */
  116. public static function updateCartByGoodsList($cartIds =[], $goodsList=[], $userId = 0)
  117. {
  118. if (empty($cartIds) || empty($goodsList)) return;
  119. // 以goods_id+goods_sku_id为key,nums为值
  120. $goodsMap = [];
  121. foreach ($goodsList as $item) {
  122. $key = $item['goods_id'] . '_' . $item['goods_sku_id'];
  123. $goodsMap[$key] = $item['nums'];
  124. }
  125. // 查询购物车
  126. $carts = Carts::where('id', 'in', $cartIds)->where('user_id', $userId)->select();
  127. foreach ($carts as $cart) {
  128. $key = $cart->goods_id . '_' . $cart->goods_sku_id;
  129. if (isset($goodsMap[$key])) {
  130. // 只更新数量
  131. $cart->nums = $goodsMap[$key];
  132. $cart->save();
  133. }
  134. }
  135. }
  136. public static function updateChecked($userId, $cartIds=[], $isChecked=0)
  137. {
  138. return Carts::where('user_id', $userId)
  139. ->whereIn('id', $cartIds)
  140. ->update(['checked' => $isChecked]);
  141. }
  142. }