购物车后台完成

master
DongZeLiang 2024-04-08 21:00:05 +08:00
parent 702046bc3e
commit 99df84c76b
5 changed files with 85 additions and 0 deletions

View File

@ -34,6 +34,11 @@ public class CartSkuModel {
*/
private Long projectId;
/**
* Sku
*/
private String projectSku;
/**
*
*/

View File

@ -0,0 +1,33 @@
package com.muyu.shop.cart.domain.req;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author DongZl
* @description:
* @Date 2024/4/8 8:02
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CartInfoIsSelectedUpdReq {
/**
* ID
*/
private Long projectId;
/**
* SKU
*/
private String projectSku;
/**
*
*/
private String isSelected;
}

View File

@ -3,6 +3,7 @@ package com.muyu.shop.cart.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.muyu.shop.cart.domain.req.CartInfoIsSelectedUpdReq;
import com.muyu.shop.cart.domain.resp.CartDetailResp;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
@ -104,6 +105,12 @@ public class CartInfoController extends BaseController {
return toAjax(cartInfoService.updateById(CartInfo.editBuild(id,cartInfoEditReq)));
}
@PostMapping("/selected")
public Result<String> cartInfoIsSelected(@RequestBody List<CartInfoIsSelectedUpdReq> cartInfoIsSelectedUpdReqList){
cartInfoService.cartInfoIsSelected(cartInfoIsSelectedUpdReqList);
return Result.success();
}
/**
*
*/

View File

@ -3,6 +3,7 @@ package com.muyu.shop.cart.service;
import java.util.List;
import com.muyu.shop.cart.domain.CartInfo;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.shop.cart.domain.req.CartInfoIsSelectedUpdReq;
import com.muyu.shop.cart.domain.resp.CartDetailResp;
/**
@ -32,4 +33,11 @@ public interface CartInfoService extends IService<CartInfo> {
* @return
*/
CartDetailResp detail ();
/**
*
* @param cartInfoIsSelectedUpdReqList
*/
void cartInfoIsSelected (List<CartInfoIsSelectedUpdReq> cartInfoIsSelectedUpdReqList);
}

View File

@ -24,6 +24,7 @@ import com.muyu.shop.cart.cache.key.CartHashKey;
import com.muyu.shop.cart.domain.model.CartSkuModel;
import com.muyu.shop.cart.domain.model.SkuRuleModel;
import com.muyu.shop.cart.domain.model.StatisticsCartModel;
import com.muyu.shop.cart.domain.req.CartInfoIsSelectedUpdReq;
import com.muyu.shop.cart.domain.resp.CartDetailResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -33,6 +34,7 @@ import com.muyu.shop.cart.domain.CartInfo;
import com.muyu.shop.cart.service.CartInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
/**
* Service
@ -177,6 +179,7 @@ public class CartInfoServiceImpl extends ServiceImpl<CartInfoMapper, CartInfo>
}
return CartSkuModel.builder()
.projectId(cartInfo.getProjectId())
.projectSku(cartInfo.getProjectSku())
.name(projectInfo.getName())
.image(projectSkuInfo.getImage())
.stock(stock)
@ -193,24 +196,28 @@ public class CartInfoServiceImpl extends ServiceImpl<CartInfoMapper, CartInfo>
StatisticsCartModel statisticsCartModel = StatisticsCartModel.builder()
.total(
cartSkuModelList.stream()
.filter(cartSkuModel -> cartSkuModel.getStock() > 0)
.mapToLong(CartSkuModel::getNum)
.sum()
)
.selectTotal(
cartSkuModelList.stream()
.filter(cartSkuModel -> "Y".equals(cartSkuModel.getIsSelected()))
.filter(cartSkuModel -> cartSkuModel.getStock() > 0)
.mapToLong(CartSkuModel::getNum)
.sum()
)
.priceTotal(
cartSkuModelList.stream()
.filter(cartSkuModel -> "Y".equals(cartSkuModel.getIsSelected()))
.filter(cartSkuModel -> cartSkuModel.getStock() > 0)
.map(CartSkuModel::getSubtotal)
.reduce(BigDecimal.ZERO, BigDecimal::add)
)
.actualTotal(
cartSkuModelList.stream()
.filter(cartSkuModel -> "Y".equals(cartSkuModel.getIsSelected()))
.filter(cartSkuModel -> cartSkuModel.getStock() > 0)
.map(CartSkuModel::getSubtotal)
.reduce(BigDecimal.ZERO, BigDecimal::add)
)
@ -221,4 +228,29 @@ public class CartInfoServiceImpl extends ServiceImpl<CartInfoMapper, CartInfo>
.build();
}
/**
*
*
* @param cartInfoIsSelectedUpdReqList
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void cartInfoIsSelected (List<CartInfoIsSelectedUpdReq> cartInfoIsSelectedUpdReqList) {
for (CartInfoIsSelectedUpdReq cartInfoIsSelectedUpdReq : cartInfoIsSelectedUpdReqList) {
LambdaUpdateWrapper<CartInfo> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(CartInfo::getIsSelected, cartInfoIsSelectedUpdReq.getIsSelected());
updateWrapper.eq(CartInfo::getProjectId, cartInfoIsSelectedUpdReq.getProjectId());
updateWrapper.eq(CartInfo::getProjectSku, cartInfoIsSelectedUpdReq.getProjectSku());
this.update(updateWrapper);
CartHashKey cartHashKey = CartHashKey.builder()
.projectId(cartInfoIsSelectedUpdReq.getProjectId())
.projectSku(cartInfoIsSelectedUpdReq.getProjectSku())
.build();
Long userId = SecurityUtils.getUserId();
CartInfo cartInfo = this.cartCache.get(userId, cartHashKey);
cartInfo.setIsSelected(cartInfoIsSelectedUpdReq.getIsSelected());
this.cartCache.put(userId, cartHashKey, cartInfo);
}
}
}