|
@ -14,6 +14,7 @@ import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
import javax.annotation.Resource; |
|
|
import java.math.BigDecimal; |
|
|
import java.math.BigDecimal; |
|
|
|
|
|
import java.math.RoundingMode; |
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
@ -46,6 +47,8 @@ public class OrderBusinessService { |
|
|
|
|
|
|
|
|
@Resource |
|
|
@Resource |
|
|
private ShopUserAddressService shopUserAddressService; |
|
|
private ShopUserAddressService shopUserAddressService; |
|
|
|
|
|
@Resource |
|
|
|
|
|
private ShopUserCouponService shopUserCouponService; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 创建订单 |
|
|
* 创建订单 |
|
@ -104,7 +107,7 @@ public class OrderBusinessService { |
|
|
|
|
|
|
|
|
// 检查前端传入的总金额是否正确(允许小的误差,比如0.01)
|
|
|
// 检查前端传入的总金额是否正确(允许小的误差,比如0.01)
|
|
|
if (request.getTotalPrice() != null && |
|
|
if (request.getTotalPrice() != null && |
|
|
request.getTotalPrice().subtract(calculatedTotal).abs().compareTo(new BigDecimal("0.01")) > 0) { |
|
|
|
|
|
|
|
|
request.getTotalPrice().subtract(calculatedTotal).abs().compareTo(new BigDecimal("0.01")) > 0) { |
|
|
log.warn("订单金额计算不一致,前端传入:{},后台计算:{}", request.getTotalPrice(), calculatedTotal); |
|
|
log.warn("订单金额计算不一致,前端传入:{},后台计算:{}", request.getTotalPrice(), calculatedTotal); |
|
|
throw new BusinessException("订单金额计算错误,请刷新重试"); |
|
|
throw new BusinessException("订单金额计算错误,请刷新重试"); |
|
|
} |
|
|
} |
|
@ -126,7 +129,7 @@ public class OrderBusinessService { |
|
|
*/ |
|
|
*/ |
|
|
private BigDecimal validateAndCalculateTotal(OrderCreateRequest request) { |
|
|
private BigDecimal validateAndCalculateTotal(OrderCreateRequest request) { |
|
|
if (CollectionUtils.isEmpty(request.getGoodsItems())) { |
|
|
if (CollectionUtils.isEmpty(request.getGoodsItems())) { |
|
|
throw new BusinessException("订单商品列表不能为空"); |
|
|
|
|
|
|
|
|
throw new BusinessException("订单商品列表不能为空"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
BigDecimal total = BigDecimal.ZERO; |
|
|
BigDecimal total = BigDecimal.ZERO; |
|
@ -193,7 +196,7 @@ public class OrderBusinessService { |
|
|
|
|
|
|
|
|
// 验证购买数量限制(使用商品级别的限制)
|
|
|
// 验证购买数量限制(使用商品级别的限制)
|
|
|
if (goods.getCanBuyNumber() != null && goods.getCanBuyNumber() > 0 && |
|
|
if (goods.getCanBuyNumber() != null && goods.getCanBuyNumber() > 0 && |
|
|
item.getQuantity() > goods.getCanBuyNumber()) { |
|
|
|
|
|
|
|
|
item.getQuantity() > goods.getCanBuyNumber()) { |
|
|
throw new BusinessException("商品购买数量超过限制:" + productName + ",最大购买数量:" + goods.getCanBuyNumber()); |
|
|
throw new BusinessException("商品购买数量超过限制:" + productName + ",最大购买数量:" + goods.getCanBuyNumber()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -202,7 +205,7 @@ public class OrderBusinessService { |
|
|
total = total.add(itemTotal); |
|
|
total = total.add(itemTotal); |
|
|
|
|
|
|
|
|
log.debug("商品验证通过 - ID:{},SKU ID:{},名称:{},单价:{},数量:{},小计:{}", |
|
|
log.debug("商品验证通过 - ID:{},SKU ID:{},名称:{},单价:{},数量:{},小计:{}", |
|
|
goods.getGoodsId(), item.getSkuId(), productName, actualPrice, item.getQuantity(), itemTotal); |
|
|
|
|
|
|
|
|
goods.getGoodsId(), item.getSkuId(), productName, actualPrice, item.getQuantity(), itemTotal); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
log.info("订单商品验证完成,总金额:{}", total); |
|
|
log.info("订单商品验证完成,总金额:{}", total); |
|
@ -274,6 +277,29 @@ public class OrderBusinessService { |
|
|
shopOrder.setPayType(1); // 默认微信支付
|
|
|
shopOrder.setPayType(1); // 默认微信支付
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 优惠券
|
|
|
|
|
|
if (shopOrder.getCouponId() != null && shopOrder.getCouponId() > 0) { |
|
|
|
|
|
ShopUserCoupon coupon = shopUserCouponService.getById(shopOrder.getCouponId()); |
|
|
|
|
|
if (coupon != null) { |
|
|
|
|
|
BigDecimal reducePrice = BigDecimal.ZERO; |
|
|
|
|
|
boolean doReduce = true; |
|
|
|
|
|
if (coupon.getType().equals(10)) { |
|
|
|
|
|
reducePrice = coupon.getReducePrice(); |
|
|
|
|
|
if (shopOrder.getTotalPrice().compareTo(coupon.getMinPrice()) < 0) doReduce = false; |
|
|
|
|
|
} else if (coupon.getType().equals(20)) { |
|
|
|
|
|
reducePrice = shopOrder.getTotalPrice() |
|
|
|
|
|
.multiply(BigDecimal.valueOf(coupon.getDiscount()).divide(new BigDecimal(100), RoundingMode.HALF_UP)); |
|
|
|
|
|
} else if (coupon.getType().equals(30)) { |
|
|
|
|
|
reducePrice = shopOrder.getTotalPrice(); |
|
|
|
|
|
} |
|
|
|
|
|
if (doReduce) { |
|
|
|
|
|
shopOrder.setReducePrice(shopOrder.getReducePrice().add(reducePrice)); |
|
|
|
|
|
shopOrder.setPayPrice(shopOrder.getPayPrice().subtract(reducePrice)); |
|
|
|
|
|
} |
|
|
|
|
|
// todo 商品/分类限制
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return shopOrder; |
|
|
return shopOrder; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -334,7 +360,7 @@ public class OrderBusinessService { |
|
|
*/ |
|
|
*/ |
|
|
private boolean isAddressInfoComplete(OrderCreateRequest request) { |
|
|
private boolean isAddressInfoComplete(OrderCreateRequest request) { |
|
|
return request.getAddress() != null && !request.getAddress().trim().isEmpty() && |
|
|
return request.getAddress() != null && !request.getAddress().trim().isEmpty() && |
|
|
request.getRealName() != null && !request.getRealName().trim().isEmpty(); |
|
|
|
|
|
|
|
|
request.getRealName() != null && !request.getRealName().trim().isEmpty(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -375,7 +401,7 @@ public class OrderBusinessService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
log.debug("地址信息快照创建完成 - 地址ID:{},收货人:{},地址:{}", |
|
|
log.debug("地址信息快照创建完成 - 地址ID:{},收货人:{},地址:{}", |
|
|
userAddress.getId(), userAddress.getName(), shopOrder.getAddress()); |
|
|
|
|
|
|
|
|
userAddress.getId(), userAddress.getName(), shopOrder.getAddress()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -511,7 +537,7 @@ public class OrderBusinessService { |
|
|
orderGoodsList.add(orderGoods); |
|
|
orderGoodsList.add(orderGoods); |
|
|
|
|
|
|
|
|
log.debug("准备保存订单商品 - 商品ID:{},名称:{},单价:{},数量:{},小计:{}", |
|
|
log.debug("准备保存订单商品 - 商品ID:{},名称:{},单价:{},数量:{},小计:{}", |
|
|
goods.getGoodsId(), goods.getName(), goods.getPrice(), item.getQuantity(), itemTotal); |
|
|
|
|
|
|
|
|
goods.getGoodsId(), goods.getName(), goods.getPrice(), item.getQuantity(), itemTotal); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 批量保存订单商品
|
|
|
// 批量保存订单商品
|
|
@ -542,7 +568,7 @@ public class OrderBusinessService { |
|
|
sku.setStock(newStock); |
|
|
sku.setStock(newStock); |
|
|
shopGoodsSkuService.updateById(sku); |
|
|
shopGoodsSkuService.updateById(sku); |
|
|
log.debug("扣减SKU库存 - SKU ID:{},扣减数量:{},剩余库存:{}", |
|
|
log.debug("扣减SKU库存 - SKU ID:{},扣减数量:{},剩余库存:{}", |
|
|
item.getSkuId(), item.getQuantity(), newStock); |
|
|
|
|
|
|
|
|
item.getSkuId(), item.getQuantity(), newStock); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
// 单规格商品,扣减商品库存
|
|
|
// 单规格商品,扣减商品库存
|
|
@ -555,7 +581,7 @@ public class OrderBusinessService { |
|
|
goods.setStock(newStock); |
|
|
goods.setStock(newStock); |
|
|
shopGoodsService.updateById(goods); |
|
|
shopGoodsService.updateById(goods); |
|
|
log.debug("扣减商品库存 - 商品ID:{},扣减数量:{},剩余库存:{}", |
|
|
log.debug("扣减商品库存 - 商品ID:{},扣减数量:{},剩余库存:{}", |
|
|
item.getGoodsId(), item.getQuantity(), newStock); |
|
|
|
|
|
|
|
|
item.getGoodsId(), item.getQuantity(), newStock); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|