✅P280_商城业务-订单服务-订单验价
大约 2 分钟
计算购物项应付金额
见:6、当前订单项的实际金额,总额 - 各种优惠价格
**com.gyz.cfmall.order.service.impl.OrderServiceImpl#builderOrderItem**
/**
* 构建某一个订单项的数据
*/
private OrderItemEntity builderOrderItem(OrderItemVo items) {
OrderItemEntity orderItemEntity = new OrderItemEntity();
//1、商品的spu信息
Long skuId = items.getSkuId();
//获取spu的信息
R spuInfo = productFeignService.getSpuInfoBySkuId(skuId);
SpuInfoVo spuInfoData = spuInfo.getData("data", new TypeReference<SpuInfoVo>() {
});
orderItemEntity.setSpuId(spuInfoData.getId());
orderItemEntity.setSpuName(spuInfoData.getSpuName());
orderItemEntity.setSpuBrand(spuInfoData.getBrandName());
orderItemEntity.setCategoryId(spuInfoData.getCatalogId());
//2、商品的sku信息
orderItemEntity.setSkuId(skuId);
orderItemEntity.setSkuName(items.getTitle());
orderItemEntity.setSkuPic(items.getImage());
orderItemEntity.setSkuPrice(items.getPrice());
orderItemEntity.setSkuQuantity(items.getCount());
//使用StringUtils.collectionToDelimitedString将list集合转换为String
String skuAttrValues = StringUtils.collectionToDelimitedString(items.getSkuAttrValues(), ";");
orderItemEntity.setSkuAttrsVals(skuAttrValues);
//3、商品的优惠信息
//4、商品的积分信息
orderItemEntity.setGiftGrowth(items.getPrice().multiply(new BigDecimal(items.getCount())).intValue());
orderItemEntity.setGiftIntegration(items.getPrice().multiply(new BigDecimal(items.getCount())).intValue());
//5、订单项的价格信息
orderItemEntity.setPromotionAmount(BigDecimal.ZERO);
orderItemEntity.setCouponAmount(BigDecimal.ZERO);
orderItemEntity.setIntegrationAmount(BigDecimal.ZERO);
//6、当前订单项的实际金额.总额 - 各种优惠价格
//原来的价格
BigDecimal origin = orderItemEntity.getSkuPrice().multiply(new BigDecimal(orderItemEntity.getSkuQuantity().toString()));
//原价减去优惠价得到最终的价格
BigDecimal subtract = origin.subtract(orderItemEntity.getCouponAmount())
.subtract(orderItemEntity.getPromotionAmount())
.subtract(orderItemEntity.getIntegrationAmount());
orderItemEntity.setRealAmount(subtract);
return orderItemEntity;
}
订单信息赋值
**cfmall-order/src/main/java/com/gyz/cfmall/order/service/impl/OrderServiceImpl.java**
计算订单总额,应付金额,设置积分等信息
private OrderCreateTo createOrder() {
OrderCreateTo createTo = new OrderCreateTo();
//构建订单信息
OrderEntity orderEntity = this.builderOrder();
//构建订单项信息
List<OrderItemEntity> itemEntityList = this.builderOrderItems(orderEntity.getOrderSn());
//验价(计算价格、积分等信息)
this.computePrice(orderEntity, itemEntityList);
createTo.setOrder(orderEntity);
createTo.setOrderItems(itemEntityList);
return createTo;
}
private void computePrice(OrderEntity orderEntity, List<OrderItemEntity> itemEntityList) {
//总价
BigDecimal total = new BigDecimal("0.0");
//优惠价
BigDecimal coupon = new BigDecimal("0.0");
BigDecimal intergration = new BigDecimal("0.0");
BigDecimal promotion = new BigDecimal("0.0");
//积分、成长值
Integer integrationTotal = 0;
Integer growthTotal = 0;
//订单总额,叠加每一个订单项的总额信息
for (OrderItemEntity orderItem : itemEntityList) {
//优惠价格信息
coupon = coupon.add(orderItem.getCouponAmount());
promotion = promotion.add(orderItem.getPromotionAmount());
intergration = intergration.add(orderItem.getIntegrationAmount());
//总价
total = total.add(orderItem.getRealAmount());
//积分信息和成长值信息
integrationTotal += orderItem.getGiftIntegration();
growthTotal += orderItem.getGiftGrowth();
}
//1、订单价格相关的
orderEntity.setTotalAmount(total);
//设置应付总额(总额+运费)
orderEntity.setPayAmount(total.add(orderEntity.getFreightAmount()));
orderEntity.setCouponAmount(coupon);
orderEntity.setPromotionAmount(promotion);
orderEntity.setIntegrationAmount(intergration);
//设置积分成长值信息
orderEntity.setIntegration(integrationTotal);
orderEntity.setGrowth(growthTotal);
//设置删除状态(0-未删除,1-已删除)
orderEntity.setDeleteStatus(0);
}
订单状态、订单确认状态、自动确认时间赋值,见:设置订单相关的状态信息
private OrderEntity builderOrder() {
OrderEntity orderEntity = new OrderEntity();
String orderSn = IdWorker.getTimeId();
orderEntity.setOrderSn(orderSn);
OrderSubmitVo orderSubmitVo = submitVoThreadLocal.get();
Long addrId = orderSubmitVo.getAddrId();
R r = wmsFeignService.getFare(addrId);
AddrAndFareVo addrAndFareVo = r.getData("data", new TypeReference<AddrAndFareVo>() {
});
//订单相关信息
orderEntity.setFreightAmount(new BigDecimal(addrAndFareVo.getFare()));
orderEntity.setReceiverName(addrAndFareVo.getMemberAddressVo().getName());
orderEntity.setReceiverProvince(addrAndFareVo.getMemberAddressVo().getProvince());
orderEntity.setReceiverCity(addrAndFareVo.getMemberAddressVo().getCity());
orderEntity.setReceiverDetailAddress(addrAndFareVo.getMemberAddressVo().getDetailAddress());
orderEntity.setReceiverPostCode(addrAndFareVo.getMemberAddressVo().getPostCode());
orderEntity.setReceiverPhone(addrAndFareVo.getMemberAddressVo().getPhone());
orderEntity.setReceiverRegion(addrAndFareVo.getMemberAddressVo().getRegion());
//设置订单相关的状态信息
orderEntity.setStatus(OrderStatusEnum.CREATE_NEW.getCode());
orderEntity.setAutoConfirmDay(7);
orderEntity.setConfirmStatus(0);
return orderEntity;
}
验价
**cfmall-order/src/main/java/com/gyz/cfmall/order/service/impl/OrderServiceImpl.java**