删除购物车一个商品
This commit is contained in:
parent
5514212ad7
commit
ce01726571
|
@ -35,6 +35,20 @@ public class ShoppingCartController {
|
|||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看购物车
|
||||
*
|
||||
|
@ -43,6 +57,7 @@ public class ShoppingCartController {
|
|||
@ApiOperation("查看购物车")
|
||||
@GetMapping("/list")
|
||||
public Result<List<ShoppingCart>> list() {
|
||||
log.info("查看购物车");
|
||||
List<ShoppingCart> list = shoppingCartService.showShoppingCart();
|
||||
return Result.success(list);
|
||||
}
|
||||
|
@ -55,6 +70,7 @@ public class ShoppingCartController {
|
|||
@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>
|
||||
|
@ -35,4 +35,11 @@ public interface ShoppingCartMapper {
|
|||
* @param userId 用户id
|
||||
*/
|
||||
void deleteByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 根据id删除购物车内容
|
||||
*
|
||||
* @param id 购物车中id
|
||||
*/
|
||||
void deleteById(Long id);
|
||||
}
|
||||
|
|
|
@ -25,4 +25,11 @@ public interface ShoppingCartService {
|
|||
* 清空购物车
|
||||
*/
|
||||
void clean();
|
||||
|
||||
/**
|
||||
* 删除购物车一个商品
|
||||
*
|
||||
* @param shoppingCartDTO 当前购物车信息
|
||||
*/
|
||||
void subShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||
}
|
||||
|
|
|
@ -91,4 +91,33 @@ public class ShoppingCartServiceImplImpl implements ShoppingCartService {
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,14 @@
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue