use of com.whoiszxl.cqrs.response.CartItemVO in project shopzz by whoiszxl.
the class OrderServiceImpl method checkOrder.
/**
* 校验订单
* @param orderSubmitCommand 订单提交命令
* @return
*/
private OrderCheckDTO checkOrder(OrderSubmitCommand orderSubmitCommand) {
OrderCheckDTO orderCheckDTO = new OrderCheckDTO();
// 1. 获取用户登录信息
Long memberId = AuthUtils.getMemberId();
// 2. 获取购物车中选中的sku信息
CartDetailApiResponse cartDetail = cartService.getCartDetail();
List<CartItemVO> cartItemList = cartDetail.getCartItemVOList();
if (ObjectUtils.isEmpty(cartItemList)) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("购物车不存在选中商品"));
}
List<Long> skuIds = cartItemList.stream().map(CartItemVO::getSkuId).collect(Collectors.toList());
String feignParams = ParamUtils.array2Str(skuIds);
ResponseResult<List<SkuFeignDTO>> skuListResult = productFeignClient.getSkuListBySkuIdList(feignParams);
if (!skuListResult.isOk()) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError(skuListResult.getMessage()));
}
List<SkuFeignDTO> skuFeignDTOList = skuListResult.getData();
// 2.1 校验订单中是否存在下架的SKU
// 2.2 校验订单中的商品库存是否足够
ResponseResult<List<SkuStockFeignDTO>> skuStockResult = productFeignClient.getStockBySkuIdList(feignParams);
if (!skuStockResult.isOk()) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError(skuStockResult.getMessage()));
}
List<SkuStockFeignDTO> skuStockList = skuStockResult.getData();
skuStockList.forEach(stock -> cartItemList.forEach(cartItemVO -> {
if (stock.getSkuId().equals(cartItemVO.getSkuId())) {
if (stock.getSaleStockQuantity() < cartItemVO.getQuantity()) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("返回刷新下吧,有SKU库存不够了"));
}
}
}));
BigDecimal serverFinalPrice = BigDecimal.ZERO;
for (SkuFeignDTO skuFeignDTO : skuFeignDTOList) {
for (CartItemVO cartItemVO : cartItemList) {
if (skuFeignDTO.getId().equals(cartItemVO.getSkuId())) {
serverFinalPrice = serverFinalPrice.add(skuFeignDTO.getPromotionPrice().multiply(new BigDecimal(cartItemVO.getQuantity())));
}
}
}
orderCheckDTO.setFinalPrice(serverFinalPrice);
orderCheckDTO.setFinalDiscountPrice(serverFinalPrice);
// 3. 已使用优惠券逻辑
Long couponId = orderSubmitCommand.getCouponId();
if (couponId != null) {
// 3.1 校验优惠券是否有效
ResponseResult<CouponFeignDTO> couponResult = promotionFeignClient.getCoupon(couponId);
if (!couponResult.isOk()) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("优惠券无效"));
}
CouponFeignDTO couponDTO = couponResult.getData();
LocalDateTime now = LocalDateTime.now();
if (couponDTO.getStartTime().isBefore(now)) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("未到使用时间"));
}
if (couponDTO.getEndTime().isAfter(now)) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("优惠券已过期"));
}
// 3.2 校验是否领取过、使用过
ResponseResult<Boolean> isUsedResponse = promotionFeignClient.checkCouponIsUsed(couponId);
if (!isUsedResponse.isOk()) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("优惠券已领取过或使用过"));
}
// 3.3 校验订单是否满足使用阈值,如果是满减券或满减折扣券,并且是全场使用券,并且订单金额小于了使用阈值
if ((couponDTO.getType().equals(CouponTypeEnum.FULL_DISCOUNT.getCode()) || couponDTO.getType().equals(CouponTypeEnum.FULL_RATE.getCode())) && couponDTO.getFullLimited().equals(CouponFullLimitedEnum.NOT_LIMIT.getCode()) && serverFinalPrice.compareTo(couponDTO.getUseThreshold()) < 0) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("未达到优惠券使用阈值"));
}
// 3.4 校验订单是否满足使用阈值,如果是满减券或满减折扣券,并且是限制类目使用券
if ((couponDTO.getType().equals(CouponTypeEnum.FULL_DISCOUNT.getCode()) || couponDTO.getType().equals(CouponTypeEnum.FULL_RATE.getCode())) && couponDTO.getFullLimited().equals(CouponFullLimitedEnum.LIMIT.getCode())) {
// 获取到优惠券能使用的分类
List<Long> categoryIdList = couponDTO.getCategoryIdList();
BigDecimal categoryPrice = BigDecimal.ZERO;
for (SkuFeignDTO skuFeignDTO : skuFeignDTOList) {
if (categoryIdList.contains(skuFeignDTO.getCategoryId())) {
CartItemVO cartItemVO = cartItemList.stream().filter(e -> e.getSkuId().equals(skuFeignDTO.getId())).findFirst().get();
BigDecimal skuPrice = skuFeignDTO.getPromotionPrice().multiply(new BigDecimal(cartItemVO.getQuantity()));
categoryPrice = categoryPrice.add(skuPrice);
}
}
if (categoryPrice.compareTo(couponDTO.getUseThreshold()) < 0) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("未达到优惠券使用阈值"));
}
}
// 4. 计算前端传入最终金额是否与服务端计算金额一致
BigDecimal finalDiscountPrice = serverFinalPrice;
if (CouponTypeEnum.FULL_DISCOUNT.getCode().equals(couponDTO.getType()) || CouponTypeEnum.UNLIMITED.getCode().equals(couponDTO.getType())) {
// 满减和无门槛券直接减去折扣
finalDiscountPrice = serverFinalPrice.subtract(couponDTO.getDiscountAmount());
if (finalDiscountPrice.compareTo(orderSubmitCommand.getFinalPrice()) != 0) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("价格错误"));
}
}
if (CouponTypeEnum.FULL_RATE.getCode().equals(couponDTO.getType())) {
// 满减折扣直接乘以折扣比率
finalDiscountPrice = serverFinalPrice.multiply(couponDTO.getDiscountRate());
if (finalDiscountPrice.compareTo(orderSubmitCommand.getFinalPrice()) != 0) {
ExceptionCatcher.catchValidateEx(ResponseResult.buildError("价格错误"));
}
}
orderCheckDTO.setFinalDiscountPrice(finalDiscountPrice);
}
return orderCheckDTO;
}
use of com.whoiszxl.cqrs.response.CartItemVO in project shopzz by whoiszxl.
the class CartServiceImpl method checkOrUncheckCartItem.
@Override
public Boolean checkOrUncheckCartItem(Integer isChecked, Long skuId) {
BoundHashOperations<String, Object, Object> cartHashOps = redisUtils.getHashOps(RedisKeyPrefixConstants.MEMBER_CART_PREFIX + StpUtil.getLoginIdAsString());
String cartItemJson = (String) cartHashOps.get(skuId.toString());
if (StringUtils.isBlank(cartItemJson)) {
return false;
}
CartItemVO cartItemVO = JsonUtil.fromJson(cartItemJson, CartItemVO.class);
cartItemVO.setChecked(isChecked);
String jsonValue = JsonUtil.toJson(cartItemVO);
cartHashOps.put(skuId.toString(), jsonValue);
return true;
}
use of com.whoiszxl.cqrs.response.CartItemVO 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,落库
}
use of com.whoiszxl.cqrs.response.CartItemVO in project shopzz by whoiszxl.
the class CartServiceImpl method cartUpdate.
@Override
public void cartUpdate(Long skuId, Integer quantity) {
BoundHashOperations<String, Object, Object> cartHashOps = redisUtils.getHashOps(RedisKeyPrefixConstants.MEMBER_CART_PREFIX + StpUtil.getLoginIdAsString());
String cartItemJson = (String) cartHashOps.get(skuId.toString());
CartItemVO cartItemVO = JsonUtil.fromJson(cartItemJson, CartItemVO.class);
if ((cartItemVO.getQuantity() + quantity) == 0) {
// 删除
cartHashOps.delete(skuId.toString());
} else {
cartItemVO.setQuantity(quantity);
String jsonValue = JsonUtil.toJson(cartItemVO);
cartHashOps.put(skuId.toString(), jsonValue);
}
}
use of com.whoiszxl.cqrs.response.CartItemVO in project shopzz by whoiszxl.
the class CartServiceImpl method clearCheckedCart.
@Override
public void clearCheckedCart() {
BoundHashOperations<String, Object, Object> cartHashOps = redisUtils.getHashOps(RedisKeyPrefixConstants.MEMBER_CART_PREFIX + StpUtil.getLoginIdAsString());
CartDetailApiResponse cartDetail = getCartDetail();
for (CartItemVO cartItemVO : cartDetail.getCartItemVOList()) {
if (cartItemVO.getChecked() == 1) {
cartHashOps.delete(cartItemVO.getSkuId().toString());
}
}
}
Aggregations