Search in sources :

Example 1 with SkuFeignDTO

use of com.whoiszxl.dto.SkuFeignDTO in project shopzz by whoiszxl.

the class CartServiceImpl method addCart.

@Override
public void addCart(CartAddCommand cartAddCommand) {
    // 0. 对sku进行有效性校验
    SkuFeignDTO sku = null;
    if (StringUtils.isBlank(cartAddCommand.getSkuCode())) {
        ResponseResult<SkuFeignDTO> skuInfoResponse = productFeignClient.getSkuInfoBySkuId(cartAddCommand.getSkuId());
        sku = skuInfoResponse.getData();
    } else {
        ResponseResult<SkuFeignDTO> skuInfoResponse = productFeignClient.getSkuInfoBySkuCode(cartAddCommand.getSkuCode());
        sku = skuInfoResponse.getData();
    }
    if (sku == null) {
        ExceptionCatcher.catchValidateEx(ResponseResult.buildError("商品无效"));
    }
    Long memberId = AuthUtils.getMemberId();
    // 1. 判断redis中是否存在当前的购物车信息
    BoundHashOperations<String, Object, Object> cartHashOps = redisUtils.getHashOps(RedisKeyPrefixConstants.MEMBER_CART_PREFIX + memberId);
    String cartItemJson = (String) cartHashOps.get(sku.getId().toString());
    if (StringUtils.isBlank(cartItemJson)) {
        CartItemVO cartItemVO = new CartItemVO();
        cartItemVO.setMemberId(memberId);
        cartItemVO.setChecked(StatusEnum.OPEN.getCode());
        cartItemVO.setQuantity(cartAddCommand.getQuantity());
        cartItemVO.setSkuPic(sku.getSkuImg());
        cartItemVO.setPrice(sku.getPromotionPrice() == null ? sku.getSalePrice() : sku.getPromotionPrice());
        cartItemVO.setSkuId(sku.getId());
        cartItemVO.setSkuName(sku.getSkuName());
        cartItemVO.setProductId(sku.getSpuId());
        String jsonValue = JsonUtil.toJson(cartItemVO);
        cartHashOps.put(sku.getId().toString(), jsonValue);
    } else {
        // 在原有购物车商品里累加
        CartItemVO cartItemVO = JsonUtil.fromJson(cartItemJson, CartItemVO.class);
        cartItemVO.setQuantity(cartItemVO.getQuantity() + cartAddCommand.getQuantity());
        cartItemVO.setSkuPic(sku.getSkuImg());
        cartItemVO.setPrice(sku.getPromotionPrice() == null ? sku.getSalePrice() : sku.getPromotionPrice());
        cartItemVO.setSkuName(sku.getSkuName());
        cartItemVO.setProductId(sku.getSpuId());
        String jsonValue = JsonUtil.toJson(cartItemVO);
        cartHashOps.put(sku.getId().toString(), jsonValue);
    }
// TODO 发送MQ,落库
}
Also used : SkuFeignDTO(com.whoiszxl.dto.SkuFeignDTO) CartItemVO(com.whoiszxl.cqrs.response.CartItemVO)

Aggregations

CartItemVO (com.whoiszxl.cqrs.response.CartItemVO)1 SkuFeignDTO (com.whoiszxl.dto.SkuFeignDTO)1