Compare commits
No commits in common. "4cda885a01985db1cd0a00f87461f2002e468370" and "2ffc04318fcb2b8b4dd17f4decdb58be7a111c15" have entirely different histories.
4cda885a01
...
2ffc04318f
|
@ -1,19 +1,19 @@
|
|||
package com.sky.controller.user;
|
||||
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
import com.sky.entity.ShoppingCart;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.ShoppingCartService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user/shoppingCart")
|
||||
@RequestMapping("/user/shopping")
|
||||
@Slf4j
|
||||
@Api(tags = "C端购物车相关接口")
|
||||
public class ShoppingCartController {
|
||||
|
@ -28,50 +28,10 @@ public class ShoppingCartController {
|
|||
* @return Result
|
||||
*/
|
||||
@ApiOperation("添加购物车")
|
||||
@PostMapping("/add")
|
||||
public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
|
||||
@PostMapping("add")
|
||||
public Result<String> add(ShoppingCartDTO shoppingCartDTO) {
|
||||
log.info("添加购物车:{}", shoppingCartDTO);
|
||||
shoppingCartService.addShoppingCart(shoppingCartDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除购物车一个商品
|
||||
*
|
||||
* @param shoppingCartDTO ShoppingCartDTO
|
||||
* @return Result<String>
|
||||
*/
|
||||
@ApiOperation("删除购物车一个商品")
|
||||
@PostMapping("/sub")
|
||||
public Result<String> sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
|
||||
log.info("删除购物车一个商品:{}", shoppingCartDTO);
|
||||
shoppingCartService.subShoppingCart(shoppingCartDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看购物车
|
||||
*
|
||||
* @return 购物车集合
|
||||
*/
|
||||
@ApiOperation("查看购物车")
|
||||
@GetMapping("/list")
|
||||
public Result<List<ShoppingCart>> list() {
|
||||
log.info("查看购物车");
|
||||
List<ShoppingCart> list = shoppingCartService.showShoppingCart();
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空购物车
|
||||
*
|
||||
* @return Result<String>
|
||||
*/
|
||||
@ApiOperation("清空购物车")
|
||||
@DeleteMapping("/clean")
|
||||
public Result<String> clean() {
|
||||
log.info("清空购物车");
|
||||
shoppingCartService.clean();
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import java.util.List;
|
|||
@Mapper
|
||||
public interface ShoppingCartMapper {
|
||||
/**
|
||||
* 查看购物车商品
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCart ShoppingCart
|
||||
* @return List<ShoppingCart>
|
||||
|
@ -28,18 +28,4 @@ public interface ShoppingCartMapper {
|
|||
* @param shoppingCart ShoppingCart
|
||||
*/
|
||||
void insert(ShoppingCart shoppingCart);
|
||||
|
||||
/**
|
||||
* 清空购物车
|
||||
*
|
||||
* @param userId 用户id
|
||||
*/
|
||||
void deleteByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 根据id删除购物车内容
|
||||
*
|
||||
* @param id 购物车中id
|
||||
*/
|
||||
void deleteById(Long id);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
import com.sky.entity.ShoppingCart;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ShoppingCartService {
|
||||
/**
|
||||
|
@ -12,24 +9,4 @@ public interface ShoppingCartService {
|
|||
* @param shoppingCartDTO 购物车参数
|
||||
*/
|
||||
void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||
|
||||
|
||||
/**
|
||||
* 查看购物车
|
||||
*
|
||||
* @return 购物车集合
|
||||
*/
|
||||
List<ShoppingCart> showShoppingCart();
|
||||
|
||||
/**
|
||||
* 清空购物车
|
||||
*/
|
||||
void clean();
|
||||
|
||||
/**
|
||||
* 删除购物车一个商品
|
||||
*
|
||||
* @param shoppingCartDTO 当前购物车信息
|
||||
*/
|
||||
void subShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||
}
|
||||
|
|
|
@ -64,61 +64,9 @@ public class ShoppingCartServiceImplImpl implements ShoppingCartService {
|
|||
shoppingCart.setImage(setmeal.getImage());
|
||||
shoppingCart.setAmount(setmeal.getPrice());
|
||||
}
|
||||
// 因为都是新增的,所以无论是菜品还是套餐都设为1
|
||||
shoppingCart.setNumber(1);
|
||||
shoppingCart.setCreateTime(LocalDateTime.now());
|
||||
shoppingCartMapper.insert(shoppingCart);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看购物车
|
||||
*
|
||||
* @return 购物车列表
|
||||
*/
|
||||
@Override
|
||||
public List<ShoppingCart> showShoppingCart() {
|
||||
// 当前微信用户id
|
||||
Long currentId = BaseContext.getCurrentId();
|
||||
ShoppingCart shoppingCart = ShoppingCart.builder().userId(currentId).build();
|
||||
return shoppingCartMapper.list(shoppingCart);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空购物车
|
||||
*/
|
||||
@Override
|
||||
public void clean() {
|
||||
Long currentId = BaseContext.getCurrentId();
|
||||
shoppingCartMapper.deleteByUserId(currentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除购物车一个商品
|
||||
*
|
||||
* @param shoppingCartDTO 当前购物车信息
|
||||
*/
|
||||
@Override
|
||||
public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
|
||||
ShoppingCart shoppingCart = new ShoppingCart();
|
||||
BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
|
||||
// 设置当前用户id
|
||||
shoppingCart.setUserId(BaseContext.getCurrentId());
|
||||
List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
|
||||
|
||||
if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
|
||||
// // 当前购物车
|
||||
shoppingCart = shoppingCartList.get(0);
|
||||
Integer number = shoppingCart.getNumber();
|
||||
Long id = shoppingCart.getId();
|
||||
// 如果当前值为1直接删除
|
||||
if (number == 1) {
|
||||
shoppingCartMapper.deleteById(id);
|
||||
} else {
|
||||
// 如果当前值不为1需要将数量减一
|
||||
shoppingCart.setNumber(shoppingCart.getNumber() - 1);
|
||||
shoppingCartMapper.updateNumberById(shoppingCart);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,27 +16,13 @@
|
|||
where id = #{id};
|
||||
</update>
|
||||
|
||||
<!-- 清空购物车 -->
|
||||
<delete id="deleteByUserId">
|
||||
delete
|
||||
from shopping_cart
|
||||
where user_id = #{userId};
|
||||
</delete>
|
||||
|
||||
<!-- 根据id删除购物车内容 -->
|
||||
<delete id="deleteById">
|
||||
delete
|
||||
from shopping_cart
|
||||
where id = #{id};
|
||||
</delete>
|
||||
|
||||
<!-- 查看购物车商品 -->
|
||||
<!-- 添加购物车 -->
|
||||
<select id="list" resultType="com.sky.entity.ShoppingCart">
|
||||
select *
|
||||
from shopping_cart
|
||||
<where>
|
||||
<if test="userId != null">
|
||||
and user_id = #{userId}
|
||||
and userId = #{userId}
|
||||
</if>
|
||||
<if test="setmealId != null">
|
||||
and setmeal_id = #{setmealId}
|
||||
|
|
Loading…
Reference in New Issue