更新购物车选中状态

main
王宇航 2024-09-02 10:07:51 +08:00
parent 029d0710a0
commit bb97d7a4b4
5 changed files with 85 additions and 5 deletions

View File

@ -1,6 +1,5 @@
package com.wyh.common.vo;
import com.wyh.common.entity.goods.Goods;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ -15,9 +14,13 @@ public class CartVo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "购物车商品列表")
private List<Goods> goodsList;
private List<GoodsUserVo> goodsList;
@ApiModelProperty(value = "购物车商品总价")
private BigDecimal totalPrice;
@ApiModelProperty(value = "购物车选中商品总金额")
private BigDecimal checkTotalPrice;
@ApiModelProperty(value = "购物车选中商品数量")
private Integer checkNum;
@ApiModelProperty(value = "购物车商品总数量")
private Integer totalNum;
}

View File

@ -0,0 +1,20 @@
package com.wyh.common.vo;
import com.wyh.common.entity.goods.Goods;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
@ApiModel(value = "购物车返回vo")
public class GoodsUserVo extends Goods implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "商品数量")
private Integer num;
@ApiModelProperty(value = "是否选中")
private Integer isCheck;
}

View File

@ -3,9 +3,11 @@ package com.wyh.front.controller;
import com.wyh.common.core.AjaxResult;
import com.wyh.common.validator.UserGoodsCreateValidate;
import com.wyh.common.vo.CartVo;
import com.wyh.front.ZJFrontThreadLocal;
import com.wyh.front.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -35,4 +37,12 @@ public class CartController {
CartVo cart = cartService.getCart();
return AjaxResult.success(cart);
}
@GetMapping("updateCheck")
@ApiOperation(value = "更新购物车选中状态")
public AjaxResult<Object> updateCheck(@RequestParam(name = "type" ) @ApiParam(value = " 0:全部取消1:全部选中") Integer type) {
Integer userId = ZJFrontThreadLocal.getUserId();
CartVo cartVo = cartService.updateCheck(userId, type);
return AjaxResult.success(cartVo);
}
}

View File

@ -9,4 +9,6 @@ public interface CartService {
void addCart( List<UserGoodsCreateValidate> cartCreateValited);
CartVo getCart();
CartVo updateCheck(Integer userId,Integer type);
}

View File

@ -7,13 +7,17 @@ import com.wyh.common.mapper.UserGoodsMapper;
import com.wyh.common.mapper.goods.GoodsMapper;
import com.wyh.common.validator.UserGoodsCreateValidate;
import com.wyh.common.vo.CartVo;
import com.wyh.common.vo.GoodsUserVo;
import com.wyh.front.ZJFrontThreadLocal;
import com.wyh.front.service.CartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
@ -51,18 +55,23 @@ public class CartServiceImpl implements CartService {
if (userGoodsList.isEmpty()) {
return new CartVo();
}
List<Integer> ids
= userGoodsList.stream().map(UserGoods::getGoodsId).collect(Collectors.toList());
List<Integer> ids = userGoodsList.stream().map(UserGoods::getGoodsId).collect(Collectors.toList());
List<Goods> goods = goodsMapper.selectList(Wrappers.<Goods>lambdaQuery().in(Goods::getId, ids));
List<GoodsUserVo> goodsUserVoList = new ArrayList<>();
BigDecimal totalPrice = new BigDecimal(0);
BigDecimal checkTotalPrice = new BigDecimal(0);
for (UserGoods userGoods : userGoodsList) {
Integer goodsId = userGoods.getGoodsId();
for (Goods good : goods) {
if (good.getId().equals(goodsId)) {
GoodsUserVo goodsUserVo = new GoodsUserVo();
BeanUtils.copyProperties(good, goodsUserVo);
BigDecimal price = good.getPrice().multiply(new BigDecimal(userGoods.getNum()));
totalPrice = totalPrice.add(price);
goodsUserVo.setNum(userGoods.getNum());
goodsUserVo.setIsCheck(userGoods.getIsCheck());
goodsUserVoList.add(goodsUserVo);
}
}
}
@ -77,13 +86,49 @@ public class CartServiceImpl implements CartService {
}
}
//获取选中商品数量
Integer checkNum = collect.stream()
.filter(obj -> obj.getIsCheck() == 1)
.map(UserGoods::getNum)
.reduce(Integer::sum)
.orElse(0);
int totalNum = userGoodsList.stream()
.map(UserGoods::getNum)
.filter(Objects::nonNull) // Filter out any null values
.reduce(0, Integer::sum);
//设置总数量和总价格
CartVo cartVo = new CartVo();
cartVo.setGoodsList(goods);
cartVo.setGoodsList(goodsUserVoList);
cartVo.setCheckNum(checkNum);
cartVo.setTotalNum(totalNum);
cartVo.setTotalPrice(totalPrice);
cartVo.setCheckTotalPrice(checkTotalPrice);
return cartVo;
}
@Override
public CartVo updateCheck(Integer userId , Integer type) {
if (type == 1) {
updateCheckUp(userId);
} else {
updateCheckDown(userId);
}
CartVo cart = getCart();
return cart;
}
private void updateCheckDown(Integer userId) {
userGoodsMapper.update(null, Wrappers.<UserGoods>lambdaUpdate()
.set(UserGoods::getIsCheck, 0)
.eq(UserGoods::getUserId, userId));
}
private void updateCheckUp(Integer userId) {
userGoodsMapper.update(null, Wrappers.<UserGoods>lambdaUpdate()
.set(UserGoods::getIsCheck, 1)
.eq(UserGoods::getUserId, userId));
}
public static void main(String[] args) {
Integer num = 3;