完成购物车的修改~

This commit is contained in:
YunaiV 2020-08-15 18:27:55 +08:00
parent 8818b350d6
commit 9fb421360f
13 changed files with 89 additions and 62 deletions

View File

@ -5,7 +5,7 @@ import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
/**
* 购物车更新是否选中 Request DTO
@ -23,7 +23,7 @@ public class CartItemUpdateSelectedReqDTO implements Serializable {
* 商品 SKU 编号列表
*/
@NotNull(message = "商品 SKU 编号列表不能为空")
private List<Integer> skuIds;
private Collection<Integer> skuIds;
/**
* 是否选中
*/

View File

@ -43,7 +43,7 @@ public interface CartItemMapper extends BaseMapper<CartItemDO> {
default List<CartItemDO> selectList(CartItemListQueryBO queryBO) {
return selectList(new QueryWrapperX<CartItemDO>().eq("user_id", queryBO.getUserId())
.eq("selected", queryBO.getSelected()));
.eqIfPresent("selected", queryBO.getSelected()));
}
}

View File

@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.mall.orderservice.enums.OrderErrorCodeConstants.CARD_ITEM_NOT_FOUND;
@ -84,7 +85,7 @@ public class CartService {
* @param skuIds 商品 SKU 编号数组
* @param selected 是否选中
*/
public void updateCartItemSelected(Integer userId, List<Integer> skuIds, Boolean selected) {
public void updateCartItemSelected(Integer userId, Collection<Integer> skuIds, Boolean selected) {
// 查询 CartItemDO 列表
List<CartItemDO> itemDOs = cartItemMapper.selectListByUserIdAndSkuIds(userId, skuIds);
if (skuIds.size() != itemDOs.size()) {

View File

@ -17,44 +17,8 @@ public class UsersCartController {
// private CouponService couponService;
//
//
// @PostMapping("update_quantity")
// public CommonResult<UsersCartDetailVO> updateQuantity(@RequestParam("skuId") Integer skuId, // TODO 芋艿先暂用这个 VO 等促销活动出来后做调整
// @RequestParam("quantity") Integer quantity) {
// // 添加到购物车
// cartService.updateQuantity(UserSecurityContextHolder.getContext().getUserId(),
// skuId, quantity);
// // 获得目前购物车明细
// return getCartDetail();
// }
//
// @PostMapping("update_selected")
// public CommonResult<UsersCartDetailVO> updateSelected(@RequestParam("skuIds") Set<Integer> skuIds, // TODO 芋艿先暂用这个 VO 等促销活动出来后做调整
// @RequestParam("selected") Boolean selected) {
// // 添加到购物车
// cartService.updateSelected(UserSecurityContextHolder.getContext().getUserId(), skuIds, selected);
// // 获得目前购物车明细
// return getCartDetail();
// }
//
//
// private CommonResult<UsersCartDetailVO> getCartDetail() {
// // 获得购物车中选中的
// List<CartItemBO> cartItems = cartService.list(UserSecurityContextHolder.getContext().getUserId(), null);
// // 购物车为空时构造空的 UsersOrderConfirmCreateVO 返回
// if (cartItems.isEmpty()) {
// UsersCartDetailVO result = new UsersCartDetailVO();
// result.setItemGroups(Collections.emptyList());
// result.setFee(new UsersCartDetailVO.Fee(0, 0, 0, 0));
// return success(result);
// }
// // 计算商品价格
// CalcOrderPriceBO calcOrder = list0(cartItems, null);
// // 执行数据拼装
// return success(CartConvert.INSTANCE.convert2(calcOrder));
// }
//
// @GetMapping("/confirm_create_order")
// public CommonResult<UsersOrderConfirmCreateVO> getConfirmCreateOrder(@RequestParam(value = "couponCardId", required = false) Integer couponCardId) {

View File

@ -36,7 +36,7 @@ public class PriceProductCalcReqDTO implements Serializable {
*/
@Data
@Accessors(chain = true)
public static class Item {
public static class Item implements Serializable {
/**
* SKU 编号

View File

@ -47,7 +47,7 @@ public class PriceProductCalcRespDTO implements Serializable {
*/
@Data
@Accessors(chain = true)
public static class ItemGroup {
public static class ItemGroup implements Serializable {
/**
* 优惠活动
@ -79,7 +79,7 @@ public class PriceProductCalcRespDTO implements Serializable {
@Data
@Accessors(chain = true)
public static class Item {
public static class Item implements Serializable {
/**
* 商品 SPU 编号
@ -149,7 +149,7 @@ public class PriceProductCalcRespDTO implements Serializable {
*/
@Data
@Accessors(chain = true)
public static class Fee {
public static class Fee implements Serializable {
/**
* 购买总价
@ -188,7 +188,7 @@ public class PriceProductCalcRespDTO implements Serializable {
*/
@Data
@Accessors(chain = true)
public static class Postage {
public static class Postage implements Serializable {
/**
* 需要满足多少钱可以包邮单位

View File

@ -80,9 +80,9 @@ public class PriceManager {
int discountTotal = 0;
int presentTotal = 0;
for (PriceProductCalcRespDTO.ItemGroup itemGroup : calcRespDTO.getItemGroups()) {
buyTotal += itemGroup.getItems().stream().mapToInt(PriceProductCalcRespDTO.Item::getBuyTotal).sum();
discountTotal += itemGroup.getItems().stream().mapToInt(PriceProductCalcRespDTO.Item::getDiscountTotal).sum();
presentTotal += itemGroup.getItems().stream().mapToInt(PriceProductCalcRespDTO.Item::getPresentTotal).sum();
buyTotal += itemGroup.getItems().stream().mapToInt(item -> item.getSelected() ? item.getBuyTotal() : 0).sum();
discountTotal += itemGroup.getItems().stream().mapToInt(item -> item.getSelected() ? item.getDiscountTotal() : 0).sum();
presentTotal += itemGroup.getItems().stream().mapToInt(item -> item.getSelected() ? item.getPresentTotal() : 0).sum();
}
Assert.isTrue(buyTotal - discountTotal == presentTotal,
String.format("价格合计( %d - %d == %d )不正确", buyTotal, discountTotal, presentTotal));
@ -106,6 +106,7 @@ public class PriceManager {
PriceProductCalcReqDTO.Item calcOrderItem = calcProductItemDTOMap.get(sku.getId());
item.setSpuId(sku.getSpuId()).setSkuId(sku.getId());
item.setCid(spuIdCategoryIdMap.get(sku.getSpuId()));
item.setSelected(calcOrderItem.getSelected());
item.setBuyQuantity(calcOrderItem.getQuantity());
// 计算初始价格
item.setOriginPrice(sku.getPrice());
@ -235,7 +236,8 @@ public class PriceManager {
Assert.isTrue(PromotionActivityTypeEnum.FULL_PRIVILEGE.getValue().equals(activity.getActivityType()),
"传入的必须的满减送活动必须是满减送");
// 获得优惠信息
List<PriceProductCalcRespDTO.Item> items = itemGroup.getItems();
List<PriceProductCalcRespDTO.Item> items = itemGroup.getItems().stream().filter(PriceProductCalcRespDTO.Item::getSelected)
.collect(Collectors.toList());
Integer itemCnt = items.stream().mapToInt(PriceProductCalcRespDTO.Item::getBuyQuantity).sum();
Integer originalTotal = items.stream().mapToInt(PriceProductCalcRespDTO.Item::getPresentTotal).sum();
List<PromotionActivityRespDTO.FullPrivilege.Privilege> privileges = activity.getFullPrivilege().getPrivileges().stream()

View File

@ -20,8 +20,8 @@ public class PriceManagerTest {
@Test
public void testCalcProductPrice() {
PriceProductCalcReqDTO calcReqDTO = new PriceProductCalcReqDTO();
PriceProductCalcReqDTO.Item item01 = new PriceProductCalcReqDTO.Item(33, 2); // 满足满减送的商品
PriceProductCalcReqDTO.Item item02 = new PriceProductCalcReqDTO.Item(34, 2); // 满足限时折扣的商品
PriceProductCalcReqDTO.Item item01 = new PriceProductCalcReqDTO.Item(33, 2, true); // 满足满减送的商品
PriceProductCalcReqDTO.Item item02 = new PriceProductCalcReqDTO.Item(34, 2, true); // 满足限时折扣的商品
calcReqDTO.setItems(Arrays.asList(item01, item02));
PriceProductCalcRespDTO calcRespDTO = priceManager.calcProductPrice(calcReqDTO);
System.out.println(calcRespDTO);

View File

@ -4,6 +4,7 @@ import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.security.user.core.context.UserSecurityContextHolder;
import cn.iocoder.mall.shopweb.controller.order.vo.cart.CartDetailVO;
import cn.iocoder.mall.shopweb.manager.order.cart.CartManager;
import cn.iocoder.security.annotations.RequiresAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
@ -12,6 +13,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Set;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@Api(tags = "购物车 API")
@ -29,6 +32,7 @@ public class CartController {
@ApiImplicitParam(name = "skuId", value = "商品 SKU 编号", required = true, example = "1"),
@ApiImplicitParam(name = "quantity", value = "增加数量", required = true, example = "1024")
})
@RequiresAuthenticate
public CommonResult<Boolean> addCartItem(@RequestParam("skuId") Integer skuId,
@RequestParam("quantity") Integer quantity) {
cartManager.addCartItem(UserSecurityContextHolder.getUserId(), skuId, quantity);
@ -37,14 +41,43 @@ public class CartController {
@GetMapping("sum-quantity")
@ApiOperation("查询用户在购物车中的商品数量")
@RequiresAuthenticate
public CommonResult<Integer> sumCartItemQuantity() {
return success(cartManager.sumCartItemQuantity(UserSecurityContextHolder.getUserId()));
}
@GetMapping("/get-detail")
@ApiOperation("查询用户的购物车的商品列表")
@RequiresAuthenticate
public CommonResult<CartDetailVO> getCartDetail() {
return success(cartManager.getCartDetail(UserSecurityContextHolder.getUserId()));
}
@PostMapping("update-quantity")
@ApiOperation("更新购物车商品数量")
@ApiImplicitParams({
@ApiImplicitParam(name = "skuId", value = "商品 SKU 编号", required = true, example = "1"),
@ApiImplicitParam(name = "quantity", value = "增加数量", required = true, example = "1024")
})
@RequiresAuthenticate
public CommonResult<Boolean> updateCartItemQuantity(@RequestParam("skuId") Integer skuId,
@RequestParam("quantity") Integer quantity) {
cartManager.updateCartItemQuantity(UserSecurityContextHolder.getUserId(), skuId, quantity);
return success(true);
}
@PostMapping("update-selected")
@ApiOperation("更新购物车商品是否选中")
@ApiImplicitParams({
@ApiImplicitParam(name = "skuIds", value = "商品 SKU 编号数组", required = true, example = "1,3"),
@ApiImplicitParam(name = "selected", value = "是否选中", required = true, example = "true")
})
@RequiresAuthenticate
public CommonResult<Boolean> updateCartItemSelected(@RequestParam("skuIds") Set<Integer> skuIds,
@RequestParam("selected") Boolean selected) {
cartManager.updateCartItemSelected(UserSecurityContextHolder.getUserId(), skuIds, selected);
// 获得目前购物车明细
return success(true);
}
}

View File

@ -1,9 +1,11 @@
package cn.iocoder.mall.shopweb.convert.order;
import cn.iocoder.mall.productservice.rpc.sku.dto.ProductSkuRespDTO;
import cn.iocoder.mall.promotion.api.rpc.activity.dto.PromotionActivityRespDTO;
import cn.iocoder.mall.promotion.api.rpc.price.dto.PriceProductCalcRespDTO;
import cn.iocoder.mall.shopweb.controller.order.vo.cart.CartDetailVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
@Mapper
@ -13,6 +15,7 @@ public interface CartConvert {
CartDetailVO.Fee convert(PriceProductCalcRespDTO.Fee bean);
CartDetailVO.Sku convert(PriceProductCalcRespDTO.Item item, ProductSkuRespDTO sku);
@Mapping(source = "sku.id", target = "id")
CartDetailVO.Sku convert(PriceProductCalcRespDTO.Item item, ProductSkuRespDTO sku, PromotionActivityRespDTO activity);
}

View File

@ -3,9 +3,7 @@ package cn.iocoder.mall.shopweb.manager.order.cart;
import cn.iocoder.common.framework.util.CollectionUtils;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.orderservice.rpc.cart.CartRpc;
import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemAddReqDTO;
import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemListReqDTO;
import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemRespDTO;
import cn.iocoder.mall.orderservice.rpc.cart.dto.*;
import cn.iocoder.mall.productservice.enums.sku.ProductSkuDetailFieldEnum;
import cn.iocoder.mall.productservice.rpc.sku.ProductSkuRpc;
import cn.iocoder.mall.productservice.rpc.sku.dto.ProductSkuListQueryReqDTO;
@ -64,6 +62,32 @@ public class CartManager {
return sumCartItemQuantityResult.getData();
}
/**
* 更新购物车商品数量
*
* @param userId 用户编号
* @param skuId 商品 SKU 编号
* @param quantity 数量
*/
public void updateCartItemQuantity(Integer userId, Integer skuId, Integer quantity) {
CommonResult<Boolean> updateCartItemQuantityResult = cartRpc.updateCartItemQuantity(new CartItemUpdateQuantityReqDTO()
.setUserId(userId).setSkuId(skuId).setQuantity(quantity));
updateCartItemQuantityResult.checkError();
}
/**
* 更新购物车商品是否选中
*
* @param userId 用户编号
* @param skuIds 商品 SKU 编号数组
* @param selected 是否选中
*/
public void updateCartItemSelected(Integer userId, Set<Integer> skuIds, Boolean selected) {
CommonResult<Boolean> updateCartItemSelectedResult = cartRpc.updateCartItemSelected(new CartItemUpdateSelectedReqDTO()
.setUserId(userId).setSkuIds(skuIds).setSelected(selected));
updateCartItemSelectedResult.checkError();
}
/**
* 查询用户的购物车的商品列表
*
@ -94,7 +118,7 @@ public class CartManager {
CartDetailVO cartDetailVO = new CartDetailVO();
cartDetailVO.setFee(CartConvert.INSTANCE.convert(calcProductPriceResult.getData().getFee()));
cartDetailVO.setItemGroups(new ArrayList<>());
calcProductPriceResult.getData().getItemGroups().forEach(itemGroupDTO -> {
for (PriceProductCalcRespDTO.ItemGroup itemGroupDTO : calcProductPriceResult.getData().getItemGroups()) {
CartDetailVO.ItemGroup itemGroupVO = new CartDetailVO.ItemGroup();
cartDetailVO.getItemGroups().add(itemGroupVO);
// 活动信息
@ -104,10 +128,9 @@ public class CartManager {
}
// 商品 SKU 信息
itemGroupVO.setItems(new ArrayList<>());
itemGroupDTO.getItems().forEach(item -> {
itemGroupVO.getItems().add(CartConvert.INSTANCE.convert(item, productSkuMap.get(item.getSkuId())));
});
});
itemGroupDTO.getItems().forEach(item -> itemGroupVO.getItems().add(CartConvert.INSTANCE.convert(item,
productSkuMap.get(item.getSkuId()), promotionActivityMap.get(item.getActivityId()))));
}
return cartDetailVO;
}
@ -124,7 +147,7 @@ public class CartManager {
}
});
});
if (!CollectionUtils.isEmpty(activeIds)) {
if (CollectionUtils.isEmpty(activeIds)) {
return Collections.emptyMap();
}
// 查询促销活动列表

View File

@ -37,6 +37,8 @@ dubbo:
version: 1.0.0
ProductSpuRpc:
version: 1.0.0
ProductSkuRpc:
version: 1.0.0
SearchProductRpc:
version: 1.0.0
PriceRpc: