|
@@ -304,18 +304,21 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
TOrder order = buildOrder(dto, project, maTechnician, address, merchantAddressList,
|
|
TOrder order = buildOrder(dto, project, maTechnician, address, merchantAddressList,
|
|
|
basePrice, trafficFee, couponDiscount, finalAmount, userId);
|
|
basePrice, trafficFee, couponDiscount, finalAmount, userId);
|
|
|
|
|
|
|
|
- // 8. 保存订单
|
|
|
|
|
|
|
+ // 8. 计算商户、平台分佣金额
|
|
|
|
|
+ this.calculateOrderIncome(order, finalAmount, project.getMerchantShareRatio());
|
|
|
|
|
+
|
|
|
|
|
+ // 9. 保存订单
|
|
|
boolean saveResult = this.save(order);
|
|
boolean saveResult = this.save(order);
|
|
|
if (!saveResult) {
|
|
if (!saveResult) {
|
|
|
throw new ServiceException("添加订单失败");
|
|
throw new ServiceException("添加订单失败");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 9. 更新优惠券关联的真实订单ID (因为刚才核销时订单还没生成,这里补上)
|
|
|
|
|
|
|
+ // 10. 更新优惠券关联的真实订单ID (因为刚才核销时订单还没生成,这里补上)
|
|
|
if (ObjectUtil.isNotNull(dto.getCouponId())) {
|
|
if (ObjectUtil.isNotNull(dto.getCouponId())) {
|
|
|
this.couponService.useCoupon(dto.getCouponId(), openId, order.getId(), 1);
|
|
this.couponService.useCoupon(dto.getCouponId(), openId, order.getId(), 1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 10. 处理余额支付逻辑
|
|
|
|
|
|
|
+ // 11. 处理余额支付逻辑
|
|
|
if (ObjectUtil.equals(PaymentMethodEnum.BALANCE.getCode(), dto.getPaymentMethod())) {
|
|
if (ObjectUtil.equals(PaymentMethodEnum.BALANCE.getCode(), dto.getPaymentMethod())) {
|
|
|
handleBalancePayment(userId, finalAmount, order);
|
|
handleBalancePayment(userId, finalAmount, order);
|
|
|
map.put("orderId", order.getId());
|
|
map.put("orderId", order.getId());
|
|
@@ -925,4 +928,35 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
return duration * factor;
|
|
return duration * factor;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 计算订单分佣金额
|
|
|
|
|
+ * 核心财务原则:先算出其中一方收益,另一方用总金额相减,避免精度丢失导致 1 分钱误差
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param order 订单对象
|
|
|
|
|
+ * @param finalAmount 最终实付金额
|
|
|
|
|
+ * @param merchantShareRatio 商户分佣比例(%)
|
|
|
|
|
+ */
|
|
|
|
|
+ private void calculateOrderIncome(TOrder order, BigDecimal finalAmount, BigDecimal merchantShareRatio) {
|
|
|
|
|
+ if (ObjectUtil.isNull(finalAmount) || finalAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
|
|
+ order.setPlatformIncome(BigDecimal.ZERO);
|
|
|
|
|
+ order.setMerchantIncome(BigDecimal.ZERO);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 防止分佣比例为空
|
|
|
|
|
+ BigDecimal ratio = Optional.ofNullable(merchantShareRatio).orElse(BigDecimal.ZERO);
|
|
|
|
|
+ BigDecimal hundred = new BigDecimal("100");
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 计算商户收益 = 实付金额 * 商户比例 / 100,保留两位小数,四舍五入
|
|
|
|
|
+ BigDecimal merchantIncome = finalAmount.multiply(ratio)
|
|
|
|
|
+ .divide(hundred, 2, RoundingMode.HALF_UP);
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 计算平台收益 = 实付金额 - 商户收益
|
|
|
|
|
+ BigDecimal platformIncome = finalAmount.subtract(merchantIncome);
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 赋值给订单对象
|
|
|
|
|
+ order.setMerchantIncome(merchantIncome);
|
|
|
|
|
+ order.setPlatformIncome(platformIncome);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|