CartService.php 4.4 KB

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