diff --git a/mall-common/src/main/java/com/suisung/mall/common/modules/esign/EsignPlatformInfo.java b/mall-common/src/main/java/com/suisung/mall/common/modules/esign/EsignPlatformInfo.java index ea3e155d..65fe2e04 100644 --- a/mall-common/src/main/java/com/suisung/mall/common/modules/esign/EsignPlatformInfo.java +++ b/mall-common/src/main/java/com/suisung/mall/common/modules/esign/EsignPlatformInfo.java @@ -18,6 +18,7 @@ import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; import java.io.Serializable; +import java.math.BigDecimal; import java.util.Date; @Data @@ -32,6 +33,9 @@ public class EsignPlatformInfo implements Serializable { @ApiModelProperty(value = "自增ID") private Long id; + @ApiModelProperty(value = "父代理商 Id(省级代理商Id)") + private Long parent_id; + @ApiModelProperty(value = "公司邮箱") private String email; @@ -110,6 +114,15 @@ public class EsignPlatformInfo implements Serializable { @ApiModelProperty(value = "邀请码,后期跟收益有关") private String invite_code; + @ApiModelProperty(value = "县级市代理商设定的配送费(单位:分)") + private Integer shipping_fee; + + @ApiModelProperty(value = "顺丰商家Id (用于县级代理商创建店铺的商家Id)") + private String supplier_id; + + @ApiModelProperty(value = "分账比例值,取值范围:[0.01,1.00]") + private BigDecimal split_ratio; + @ApiModelProperty(value = "记录状态:1-有效;2-无效;") private Integer status; diff --git a/mall-common/src/main/java/com/suisung/mall/common/modules/order/ShopOrderData.java b/mall-common/src/main/java/com/suisung/mall/common/modules/order/ShopOrderData.java index ff480e84..a52716fe 100644 --- a/mall-common/src/main/java/com/suisung/mall/common/modules/order/ShopOrderData.java +++ b/mall-common/src/main/java/com/suisung/mall/common/modules/order/ShopOrderData.java @@ -101,7 +101,7 @@ public class ShopOrderData implements Serializable { @ApiModelProperty(value = "平台费(分给平台或代理商的费用),根据不同的店铺分类,从商品原价中扣除相应的费用。") private BigDecimal platform_fee; - @ApiModelProperty(value = "拉卡拉手续费") + @ApiModelProperty(value = "拉卡拉手续费,单位:元") private BigDecimal lkl_fee; @ApiModelProperty(value = "店铺统一设置的打包费") diff --git a/mall-common/src/main/java/com/suisung/mall/common/pojo/dto/LklSeparateWithTotalAmountDTO.java b/mall-common/src/main/java/com/suisung/mall/common/pojo/dto/LklSeparateWithTotalAmountDTO.java new file mode 100644 index 00000000..c99bc491 --- /dev/null +++ b/mall-common/src/main/java/com/suisung/mall/common/pojo/dto/LklSeparateWithTotalAmountDTO.java @@ -0,0 +1,413 @@ +package com.suisung.mall.common.pojo.dto; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.data.util.Pair; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +/** + * 拉卡拉订单基于总金额分账信息处理类 + * 实现基于总金额的分账算法,支持平台、代理商和商家的多级分账,类里涉及的金额单位:分 + *

+ * 分账计算逻辑: + * 1、totalSeparateAmount,是必填参数,订单的总金额,必须大于0;lklRatio 是必填参数,拉卡拉的分账比例,如 0.0025 + * 先减去拉卡拉配送费=lklRatio*totalSeparateAmount(结果四舍五入,保留整数,以下计算结果一律四舍五入),余额得到可分账金额(分) canSeparateAmount + * 2、refCanSeparateAmount 是可选参数,如果有效(非空且大于0),往下计算分账基准依据此参数计算(实际的可分账金额A),而不是以 canSeparateAmount 为基准。 + * 3、shippingFee 是可选参数,如果有效(非空且大于0),往下实际的可分账金额B = 实际的可分账金额A -shippingFee。 + * 4、实际的可分账金额B 就是平台、一级代理商、二级代理商、商家的可分账金额,按照以下规则进行分账给四个对象。 + * 5、四个对象之前,需要知道他们的准则: + * 6、平台一定参加分账的,如果 platRatio 无效(空值或小于等于0),默认0.01,优先分账,凭条分账金额 =totalSeparateAmount* platRatio。 + * 7、平台和商家一定参加分账的,一级代理商和二级代理商,如果对应的比例agent1stRatio,agent2ndRatio 有效(非空且大于0)就参与分账,否则不参与。 + * 8、mchRatio 可以根据分账金额,动态调整到实际比例 + * 9、所有参与分账的主体,其分账金额都不能为负数,否则将抛出异常。 + * 10、分账顺序按照优先级:拉卡拉 -> 平台 -> 二级代理商 -> 一级代理商 -> 商家,商家获得剩余的所有金额。 + * 11、五方分账金额之和不能超过 totalSeparateAmount + * 12、分账计算完成后,会计算出 mchRatio 的实际比例 + * 13、如果商家实际分账比例低于0.2阈值,会抛出异常:商家分账低于阈值 + */ +@Data +@EqualsAndHashCode(callSuper = false) +public class LklSeparateWithTotalAmountDTO { + + // 常量定义 + private static final BigDecimal MCH_RATIO_THRESHOLD = new BigDecimal("0.2"); + + // 基础金额属性 + private Integer totalSeparateAmount; // 分账总金额(分) + private Integer canSeparateAmount; // 可分账金额(分) + private Integer refCanSeparateAmount; // 拉卡拉可分账金额参考(分) + private Integer shippingFee; // 配送费(分) + + // 分账比例属性 + private BigDecimal lklRatio; // 拉卡拉分账比例(如 0.0025=0.25%) + private BigDecimal mchRatio; // 商户分账比例(如 0.96=96%) + private BigDecimal platRatio; // 平台分账比例(如 0.01=1%) + private BigDecimal agent1stRatio; // 一级代理商分账比例(如 0.01=1%) + private BigDecimal agent2ndRatio; // 二级代理商分账比例(如 0.03=3%) + + // 分账金额结果属性 + private Integer lklAmount; // 拉卡拉分账金额(分) + private Integer mchAmount; // 商户分账金额(分) + private Integer platAmount; // 平台分账金额(分) + private Integer agent1stAmount; // 一级代理商分账金额(分) + private Integer agent2ndAmount; // 二级代理商分账金额(分) + + /** + * 测试方法 + * 包含多个测试用例,验证分账计算逻辑的正确性和异常处理机制 + */ + public static void main(String[] args) { + // 测试用例1: 所有参与方都参与分账(符合比例要求) + System.out.println("=== 测试用例1: 所有参与方都参与分账 ==="); + LklSeparateWithTotalAmountDTO dto1 = new LklSeparateWithTotalAmountDTO(); + dto1.setTotalSeparateAmount(1500); // 总金额100元(10000分) + dto1.setShippingFee(600); +// dto1.setRefCanSeparateAmount(1496); + dto1.setLklRatio(new BigDecimal("0.0025")); // 拉卡拉分账比例0.25% + dto1.setMchRatio(new BigDecimal("0.95")); // 商家分账比例94.75% + dto1.setPlatRatio(new BigDecimal("0.01")); // 平台分账比例1% +// dto1.setAgent2ndRatio(new BigDecimal("0.04")); // 二级代理商分账比例4% +// dto1.setAgent1stRatio(new BigDecimal("0.01")); // 一级代理商分账比例1% + + Pair result = dto1.calculateSeparateAmount(); + if (result.getFirst()) { + System.out.println("分账计算成功:"); + System.out.println(result.getSecond()); + System.out.println("JSON格式输出:"); + System.out.println(result.getSecond().toJSON()); + } else { + System.out.println("分账计算失败"); + if (result.getSecond() != null) { + System.out.println("部分结果:"); + System.out.println(result.getSecond()); + } + } + } + + /** + * 执行分账计算逻辑 + * + * @return Pair Boolean表示是否成功,LklSeparateWithTotalAmountDTO为分账结果 + */ + public Pair calculateSeparateAmount() { + try { + // 参数校验 + validateInputs(); + + // 1. 计算拉卡拉分账金额和可分账金额 + calculateLklAmountAndCanSeparateAmount(); + + // 2. 确定实际可分账金额 + int actualCanSeparateAmount = determineActualCanSeparateAmount(); + + // 3. 计算各参与方分账比例 + calculateDefaultRatios(); + + // 4. 根据优先级顺序计算各参与方分账金额 + calculateAmountsInPriorityOrder(actualCanSeparateAmount); + + // 5. 校验分账金额总和不能超过总金额 + validateSeparateAmountTotal(); + + // 6. 计算商家实际分账比例 + calculateActualMchRatio(); + + return Pair.of(true, this); + } catch (IllegalArgumentException e) { + // 参数校验异常,返回false和当前对象 + System.err.println("分账计算参数异常: " + e.getMessage()); + return Pair.of(false, this); + } catch (Exception e) { + // 其他异常,返回false和null + System.err.println("分账计算异常: " + e.getMessage()); + return Pair.of(false, null); + } + } + + /** + * 校验必要参数 + * + * @throws IllegalArgumentException 当参数不合法时抛出异常 + */ + private void validateInputs() { + // 校验totalSeparateAmount必须为有效值且大于0 + if (totalSeparateAmount == null || totalSeparateAmount <= 0) { + throw new IllegalArgumentException("分账计算缺少必要参数或参数不合法"); + } + + // 校验必要参数 + if (lklRatio == null || mchRatio == null) { + throw new IllegalArgumentException("分账计算缺少必要参数"); + } + + // 校验shippingFee不能大于等于totalSeparateAmount + if (shippingFee != null && shippingFee >= totalSeparateAmount) { + throw new IllegalArgumentException("配送费不能大于等于总金额"); + } + } + + /** + * 计算拉卡拉分账金额和初始可分账金额 + * 拉卡拉分账金额 = 总金额 * 拉卡拉分账比例(四舍五入) + * 可分账金额 = 总金额 - 拉卡拉分账金额 + * + * @throws IllegalArgumentException 当计算出的分账金额为负数时抛出异常 + */ + private void calculateLklAmountAndCanSeparateAmount() { + // 拉卡拉分账金额 = 总金额 * 拉卡拉分账比例(四舍五入) + lklAmount = lklRatio.multiply(new BigDecimal(totalSeparateAmount)) + .setScale(0, RoundingMode.HALF_UP) + .intValue(); + + // 校验拉卡拉分账金额不能为负数 + if (lklAmount < 0) { + // 记录详细日志 + System.err.println("拉卡拉分账金额计算结果为负数: " + lklAmount); + throw new IllegalArgumentException("拉卡拉分账金额计算异常"); + } + + // 可分账金额 = 总金额 - 拉卡拉分账金额 + canSeparateAmount = totalSeparateAmount - lklAmount; + + // 校验可分账金额不能为负数 + if (canSeparateAmount < 0) { + // 记录详细日志 + System.err.println("可分账金额计算结果为负数: " + canSeparateAmount); + throw new IllegalArgumentException("可分账金额计算异常"); + } + } + + /** + * 确定实际可分账金额 + * 实际可分账金额A: 如果refCanSeparateAmount有效则使用,否则使用canSeparateAmount + * 实际可分账金额B = 实际可分账金额A - 配送费(如果配送费有效) + * + * @return 实际可分账金额 + * @throws IllegalArgumentException 当实际可分账金额为负数时抛出异常 + */ + private int determineActualCanSeparateAmount() { + // 实际可分账金额A: 如果refCanSeparateAmount有效则使用,否则使用canSeparateAmount + int actualCanSeparateAmountA = (refCanSeparateAmount != null && refCanSeparateAmount > 0) + ? refCanSeparateAmount : canSeparateAmount; + + // 实际可分账金额B = 实际可分账金额A - 配送费(如果配送费有效) + int actualCanSeparateAmountB = actualCanSeparateAmountA; + if (shippingFee != null && shippingFee > 0) { + actualCanSeparateAmountB = actualCanSeparateAmountA - shippingFee; + } + + // 校验实际可分账金额不能为负数 + if (actualCanSeparateAmountB < 0) { + // 记录详细日志 + System.err.println("实际可分账金额计算结果为负数: " + actualCanSeparateAmountB); + throw new IllegalArgumentException("实际可分账金额计算异常"); + } + + return actualCanSeparateAmountB; + } + + /** + * 计算各参与方分账比例 + * 如果平台比例无效,设置默认值0.01 + */ + private void calculateDefaultRatios() { + // 如果平台比例无效,设置默认值0.01 + if (platRatio == null || platRatio.compareTo(BigDecimal.ZERO) <= 0) { + platRatio = new BigDecimal("0.01"); + } + } + + /** + * 根据优先级顺序计算各参与方分账金额 + * 分账顺序:拉卡拉 -> 平台 -> 二级代理商 -> 一级代理商 -> 商家 + * 商家获得所有剩余金额 + * + * @param actualCanSeparateAmount 实际可分账金额 + * @throws IllegalArgumentException 当计算出的分账金额为负数或分账金额超过可用金额时抛出异常 + */ + private void calculateAmountsInPriorityOrder(int actualCanSeparateAmount) { + // 重置所有分账金额 + platAmount = 0; + agent1stAmount = 0; + agent2ndAmount = 0; + mchAmount = 0; + + // 1. 计算拉卡拉分账金额(拉卡拉一定参与分账) + lklAmount = lklRatio.multiply(new BigDecimal(totalSeparateAmount)) + .setScale(0, RoundingMode.HALF_UP) + .intValue(); + + // 校验拉卡拉分账金额不能为负数 + if (lklAmount < 0) { + throw new IllegalArgumentException("拉卡拉分账金额计算异常"); + } + + // 2. 校验实际可分账金额不能小于0 + if (actualCanSeparateAmount < 0) { + throw new IllegalArgumentException("实际可分账金额不能为负数"); + } + + // 3. 初始化剩余金额为实际可分账金额 + int remainingAmount = actualCanSeparateAmount; + + // 4. 计算平台分账金额(平台一定参与分账) + platAmount = platRatio.multiply(new BigDecimal(totalSeparateAmount)) + .setScale(0, RoundingMode.HALF_UP) + .intValue(); + + // 校验平台分账金额不能为负数 + if (platAmount < 0) { + throw new IllegalArgumentException("平台分账金额计算异常"); + } + + // 确保不超过剩余金额 + if (platAmount > remainingAmount) { + platAmount = remainingAmount; + } + + // 更新剩余金额 + remainingAmount -= platAmount; + + // 5. 判断代理商是否参与分账 + boolean isAgent2ndParticipate = agent2ndRatio != null && agent2ndRatio.compareTo(BigDecimal.ZERO) > 0; + boolean isAgent1stParticipate = agent1stRatio != null && agent1stRatio.compareTo(BigDecimal.ZERO) > 0; + + // 6. 按优先级顺序计算代理商分账金额 + // 优先级:平台 -> 二级代理商 -> 一级代理商 + if (isAgent2ndParticipate) { + // 计算二级代理商分账金额 + agent2ndAmount = agent2ndRatio.multiply(new BigDecimal(totalSeparateAmount)) + .setScale(0, RoundingMode.HALF_UP) + .intValue(); + + // 校验二级代理商分账金额不能为负数 + if (agent2ndAmount < 0) { + throw new IllegalArgumentException("二级代理商分账金额计算异常"); + } + + // 确保不超过剩余金额 + if (agent2ndAmount > remainingAmount) { + agent2ndAmount = remainingAmount; + } + remainingAmount -= agent2ndAmount; + } + + if (isAgent1stParticipate) { + // 计算一级代理商分账金额 + agent1stAmount = agent1stRatio.multiply(new BigDecimal(totalSeparateAmount)) + .setScale(0, RoundingMode.HALF_UP) + .intValue(); + + // 校验一级代理商分账金额不能为负数 + if (agent1stAmount < 0) { + throw new IllegalArgumentException("一级代理商分账金额计算异常"); + } + + // 确保不超过剩余金额 + if (agent1stAmount > remainingAmount) { + agent1stAmount = remainingAmount; + } + remainingAmount -= agent1stAmount; + } + + // 7. 最后计算商家分账金额(使用剩余所有金额) + mchAmount = remainingAmount; + + // 校验商家分账金额不能为负数 + if (mchAmount < 0) { + throw new IllegalArgumentException("商家分账金额计算异常"); + } + } + + + /** + * 计算商家实际分账比例 + * + * @throws IllegalArgumentException 当商家实际分账比例低于阈值时抛出异常 + */ + private void calculateActualMchRatio() { + if (totalSeparateAmount != null && totalSeparateAmount > 0 && mchAmount != null) { + // 计算实际比例,保留6位小数 + mchRatio = new BigDecimal(mchAmount) + .divide(new BigDecimal(totalSeparateAmount), 6, RoundingMode.HALF_UP); + + // 如果计算出的实际比例低于阈值,打印日志并抛出异常 + if (mchRatio.compareTo(MCH_RATIO_THRESHOLD) < 0) { + System.err.println("警告: 商家实际分账比例低于阈值,当前比例: " + mchRatio + ",阈值: " + MCH_RATIO_THRESHOLD); + throw new IllegalArgumentException("商家分账低于阈值"); + } + } + } + + /** + * 校验分账金额总和不能超过总金额 + * + * @throws IllegalArgumentException 当分账金额总和超过总金额时抛出异常 + */ + private void validateSeparateAmountTotal() { + int totalAmount = 0; + totalAmount += (lklAmount != null ? lklAmount : 0); + totalAmount += (platAmount != null ? platAmount : 0); + totalAmount += (agent2ndAmount != null ? agent2ndAmount : 0); + totalAmount += (agent1stAmount != null ? agent1stAmount : 0); + totalAmount += (mchAmount != null ? mchAmount : 0); + + if (totalAmount > totalSeparateAmount) { + // 记录详细日志 + System.err.println("分账金额总和超过总金额,分账金额总和: " + totalAmount + ",总金额: " + totalSeparateAmount); + throw new IllegalArgumentException("分账金额总和超过总金额"); + } + } + + /** + * 生成对象的字符串表示 + * + * @return 对象的字符串表示 + */ + @Override + public String toString() { + return "分账计算结果{" + + "总分账金额(分) totalSeparateAmount=" + totalSeparateAmount + + ", 可分账金额(分) canSeparateAmount=" + canSeparateAmount + + ", 拉卡拉可分账金额参考(分) refCanSeparateAmount=" + refCanSeparateAmount + + ", 配送费(分) shippingFee=" + shippingFee + + ", 拉卡拉分账比例 lklRatio=" + lklRatio + + ", 商户分账比例 mchRatio=" + mchRatio + + ", 平台分账比例 platRatio=" + platRatio + + ", 一级代理商分账比例 agent1stRatio=" + agent1stRatio + + ", 二级代理商分账比例 agent2ndRatio=" + agent2ndRatio + + ", 拉卡拉分账金额(分) lklAmount=" + lklAmount + + ", 商户分账金额(分) mchAmount=" + mchAmount + + ", 平台分账金额(分) platAmount=" + platAmount + + ", 一级代理商分账金额(分) agent1stAmount=" + agent1stAmount + + ", 二级代理商分账金额(分) agent2ndAmount=" + agent2ndAmount + + '}'; + } + + /** + * 将对象转换为JSON格式字符串 + * + * @return JSON格式字符串 + */ + public String toJSON() { + String sb = "{" + + "\"totalSeparateAmount\":" + totalSeparateAmount + "," + + "\"canSeparateAmount\":" + canSeparateAmount + "," + + "\"refCanSeparateAmount\":" + refCanSeparateAmount + "," + + "\"shippingFee\":" + shippingFee + "," + + "\"lklRatio\":" + (lklRatio != null ? "\"" + lklRatio + "\"" : "null") + "," + + "\"mchRatio\":" + (mchRatio != null ? "\"" + mchRatio + "\"" : "null") + "," + + "\"platRatio\":" + (platRatio != null ? "\"" + platRatio + "\"" : "null") + "," + + "\"agent1stRatio\":" + (agent1stRatio != null ? "\"" + agent1stRatio + "\"" : "null") + "," + + "\"agent2ndRatio\":" + (agent2ndRatio != null ? "\"" + agent2ndRatio + "\"" : "null") + "," + + "\"lklAmount\":" + lklAmount + "," + + "\"mchAmount\":" + mchAmount + "," + + "\"platAmount\":" + platAmount + "," + + "\"agent1stAmount\":" + agent1stAmount + "," + + "\"agent2ndAmount\":" + agent2ndAmount + + "}"; + return sb; + } +} diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/LakalaApiService.java b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/LakalaApiService.java index 43fdabd5..f4c12f23 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/LakalaApiService.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/LakalaApiService.java @@ -11,7 +11,7 @@ package com.suisung.mall.shop.lakala.service; import cn.hutool.json.JSONObject; import com.suisung.mall.common.api.CommonResult; import com.suisung.mall.common.modules.store.ShopMchEntry; -import com.suisung.mall.common.pojo.dto.LklSeparateDTO; +import com.suisung.mall.common.pojo.dto.LklSeparateWithTotalAmountDTO; import org.springframework.data.util.Pair; import javax.servlet.http.HttpServletRequest; @@ -355,7 +355,6 @@ public interface LakalaApiService { /** * 商户分账参数计算及评估 * - * @param splitMode 分账模式:1-总金额为基准分账,2-可分账金额基准分账,必填参数 * @param orderPayAmount 订单支付总金额(单位:分)必填参数 * @param shippingFeeInner 平台内部配送费(单位:分)必填参数 * @param mchSplitRatioRaw 商户分账比例值(分子值,如10表示10%)必填参数 @@ -365,13 +364,12 @@ public interface LakalaApiService { * @param refCanSeparateAmt 参考可分金额(单位:分) 可选参数 * @return Pair 分账参数评估结果,第一个元素表示是否成功,第二个元素为分账参数对象 */ - Pair calculateAndEvaluateSharingParams(int splitMode, - Integer orderPayAmount, - Integer shippingFeeInner, - BigDecimal mchSplitRatioRaw, - BigDecimal platSplitRatio, - BigDecimal agent1stRatio, - BigDecimal agent2ndRatio, - Integer refCanSeparateAmt); + Pair calculateAndEvaluateSharingParams(Integer orderPayAmount, + Integer shippingFeeInner, + BigDecimal mchSplitRatioRaw, + BigDecimal platSplitRatio, + BigDecimal agent1stRatio, + BigDecimal agent2ndRatio, + Integer refCanSeparateAmt); } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaApiServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaApiServiceImpl.java index b72c8235..7b8b7135 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaApiServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/LakalaApiServiceImpl.java @@ -30,7 +30,7 @@ import com.suisung.mall.common.modules.lakala.*; import com.suisung.mall.common.modules.order.ShopOrderLkl; import com.suisung.mall.common.modules.store.ShopMchEntry; import com.suisung.mall.common.modules.store.ShopStoreBase; -import com.suisung.mall.common.pojo.dto.LklSeparateDTO; +import com.suisung.mall.common.pojo.dto.LklSeparateWithTotalAmountDTO; import com.suisung.mall.common.service.impl.CommonService; import com.suisung.mall.common.utils.*; import com.suisung.mall.core.web.service.RedisService; @@ -860,7 +860,7 @@ public class LakalaApiServiceImpl implements LakalaApiService { return JSONUtil.createObj().set("code", "FAIL").set("message", "交易状态未成功,不做任何处理!"); } - if (StrUtil.isBlank(logNo) || StrUtil.isBlank(merchantNo) || StrUtil.isBlank(originTradeNo) || StrUtil.isBlank(originLogNo)) { + if (StrUtil.hasBlank(logNo, merchantNo, originTradeNo, originLogNo)) { log.warn("[确认收货通知] 关键参数为空: logNo={}, merchantNo={}, originTradeNo={}, originLogNo={}", logNo, merchantNo, originTradeNo, originLogNo); return JSONUtil.createObj().set("code", "FAIL").set("message", "关键编号返回空值!"); @@ -888,7 +888,7 @@ public class LakalaApiServiceImpl implements LakalaApiService { } log.info("[确认收货通知] 订单信息更新成功: orderId={}", shopOrderLkl.getOrder_id()); - // 发起分账指令 + // 准备发起分账指令 log.info("[确认收货通知] 开始发起分账指令: merchantNo={}, receiveTradeNo={}, logNo={}", merchantNo, shopOrderLkl.getLkl_receive_trade_no(), logNo); Pair separateResult = innerDoOrderSeparateByMerchantAndLogNo(merchantNo, shopOrderLkl.getLkl_receive_trade_no(), shopOrderLkl.getLkl_receive_log_no()); @@ -2510,34 +2510,36 @@ public class LakalaApiServiceImpl implements LakalaApiService { } // 获取代理商分账信息 - BigDecimal platformSplitRatio = BigDecimal.valueOf(0.01); - BigDecimal distributorSplitRatio = BigDecimal.ZERO; - List distributorReceivers = lklLedgerMerReceiverBindService.selectDistributorByMerCupNo(merchantNo); - if (CollUtil.isNotEmpty(distributorReceivers)) { - distributorSplitRatio = BigDecimal.valueOf(1).subtract(mchSplitRatio).subtract(platformSplitRatio); - log.debug("[分账操作] 检测到代理商存在,调整分账比例: 代理商比例={}, 平台比例={}", distributorSplitRatio, platformSplitRatio); + BigDecimal platformSplitRatio = BigDecimal.valueOf(0.01); // 默认平台比例 1% + BigDecimal agent2ndSplitRatio = null; + + // 代理商绑定列表 + List lklLedgerMerReceiverBindList = lklLedgerMerReceiverBindService.selectDistributorByMerCupNo(merchantNo); + if (CollUtil.isNotEmpty(lklLedgerMerReceiverBindList)) { + // 县级代理商比例值 = 1-商家比例值-平台比例值 + agent2ndSplitRatio = BigDecimal.ONE.subtract(mchSplitRatio).subtract(platformSplitRatio); + log.debug("[分账操作] 检测到代理商存在,调整分账比例: 代理商比例={}, 平台比例={}", agent2ndSplitRatio, platformSplitRatio); } else { - platformSplitRatio = BigDecimal.valueOf(1).subtract(mchSplitRatio); - distributorSplitRatio = null; + // 平台比例值 = 1-商家比例值 + platformSplitRatio = BigDecimal.ONE.subtract(mchSplitRatio); } - // 内部配送费 + // 内部配送费(单位:分) Integer shoppingFeeInner = CheckUtil.isEmpty(shopOrderLkl.getShopping_fee_inner()) ? 0 : shopOrderLkl.getShopping_fee_inner(); // 计算拉卡拉手续费、商家分账金额、平台和代理商的分账金额 - Pair calcResult = calculateAndEvaluateSharingParams( - CommonConstant.SeparateCalcMode_CanSeparateAmt, + Pair calcResult = calculateAndEvaluateSharingParams( shopOrderLkl.getTotal_amt(), shoppingFeeInner, mchSplitRatio, platformSplitRatio, - null, distributorSplitRatio, refCanSeparateAmt); + null, agent2ndSplitRatio, refCanSeparateAmt); if (calcResult == null || !calcResult.getFirst() || calcResult.getSecond() == null) { log.error("[分账操作] 分账参数评估,结果无法分账"); return Pair.of(false, "分账数据评估,结果无法分账"); } - LklSeparateDTO lklSeparateDTO = calcResult.getSecond(); + LklSeparateWithTotalAmountDTO lklSeparateDTO = calcResult.getSecond(); log.debug("[分账操作] 分账参数计算结果:{}", lklSeparateDTO); @@ -2550,57 +2552,71 @@ public class LakalaApiServiceImpl implements LakalaApiService { shopOrderLkl.setSplit_amt_ref(refCanSeparateAmt); } - // 分账金额校验 + // 可分账金额校验 if (CheckUtil.isEmpty(refCanSeparateAmt)) { - String errorMsg = String.format("[分账操作] 店铺[%s]订单[%s]分账金额[%d]低于1分钱,跳过分账", + String errorMsg = String.format("[分账操作] 店铺[%s]订单[%s]可分账金额[%d]低于1分钱,跳过分账", shopOrderLkl.getStore_id(), orderId, refCanSeparateAmt); log.error(errorMsg); if (existingSeparateRecord != null) { lklOrderSeparateService.updateRemark(existingSeparateRecord.getId(), errorMsg); } - return Pair.of(false, "订单分账金额低于1分钱"); + return Pair.of(false, "订单可分账金额低于1分钱"); } - // 构建分账接收方列表 - List recvDatas = new ArrayList<>(); Integer merchantAmount = lklSeparateDTO.getMchAmount(); Integer platformAmount = lklSeparateDTO.getPlatAmount(); - Integer agentAmount = lklSeparateDTO.getAgent2ndAmount(); + Integer agent2ndAmount = lklSeparateDTO.getAgent2ndAmount(); - log.info("[分账操作] 金额计算结果:订单={}, 商户={}, 总金额={}分, 可分金额={}分, 商家比例={}, 商家分得={}分, 平台比例={}, 平台分得={}分, 代理商比例={}, 代理商分得={}分", + log.info("[分账操作] 金额计算结果:订单={}, 商户={}, 总金额={}分, 可分金额={}分, 商家比例={}, 商家分得={}分, 平台比例={}, 平台分得={}分, 县级代理商比例={}, 县级代理商分得={}分", orderId, merchantNo, shopOrderLkl.getTotal_amt(), refCanSeparateAmt, mchSplitRatio, merchantAmount, - platformSplitRatio, platformAmount, distributorSplitRatio, agentAmount); + platformSplitRatio, platformAmount, agent2ndSplitRatio, agent2ndAmount); - // 构建分账接收方分账参数 + // 构建分账接收方分账列表参数 + List recvDatas = new ArrayList<>(); // 商家分账参数 - if (merchantAmount != null && merchantAmount > 0) { + if (CheckUtil.isNotEmpty(merchantAmount)) { V3SacsSeparateRecvDatas receiver = new V3SacsSeparateRecvDatas(); receiver.setRecvMerchantNo(merchantNo); receiver.setSeparateValue(merchantAmount.toString()); recvDatas.add(receiver); + log.debug("[分账操作] 添加商家接收方: merchantNo={}, amount={}", merchantNo, merchantAmount); } // 平台分账参数 - if (platformAmount != null && platformAmount > 0) { + if (CheckUtil.isNotEmpty(platformAmount)) { V3SacsSeparateRecvDatas receiver = new V3SacsSeparateRecvDatas(); receiver.setRecvNo(platformReceiver.getReceiver_no()); receiver.setSeparateValue(platformAmount.toString()); recvDatas.add(receiver); + log.debug("[分账操作] 添加平台接收方: receiverNo={}, amount={}", platformReceiver.getReceiver_no(), platformAmount); } - // 二级代理商(县级)分账参数 - if (agentAmount != null && agentAmount > 0 && CollUtil.isNotEmpty(distributorReceivers)) { - LklLedgerMerReceiverBind distributorReceiver = distributorReceivers.get(0); - if (distributorReceiver != null && StrUtil.isNotBlank(distributorReceiver.getReceiver_no())) { + // 县级代理商分账参数 + if (CollUtil.isNotEmpty(lklLedgerMerReceiverBindList) && CheckUtil.isNotEmpty(agent2ndAmount)) { + LklLedgerMerReceiverBind agent2ndReceiver = lklLedgerMerReceiverBindList.get(0); + if (agent2ndReceiver != null && StrUtil.isNotBlank(agent2ndReceiver.getReceiver_no())) { V3SacsSeparateRecvDatas receiver = new V3SacsSeparateRecvDatas(); - receiver.setRecvNo(distributorReceiver.getReceiver_no()); - receiver.setSeparateValue(agentAmount.toString()); + receiver.setRecvNo(agent2ndReceiver.getReceiver_no()); + receiver.setSeparateValue(Convert.toStr(agent2ndAmount)); recvDatas.add(receiver); - log.debug("[分账操作] 添加代理商接收方: receiverNo={}, amount={}", distributorReceiver.getReceiver_no(), agentAmount); + + log.debug("[分账操作] 添加县级代理商接收方: receiverNo={}, amount={}", agent2ndReceiver.getReceiver_no(), agent2ndAmount); + + // 平台内部配送费,由县级代理商代收 add 2025-10-11 + V3SacsSeparateRecvDatas shippingFeeReceiver = new V3SacsSeparateRecvDatas(); + shippingFeeReceiver.setRecvNo(agent2ndReceiver.getReceiver_no()); + shippingFeeReceiver.setSeparateValue(Convert.toStr(shoppingFeeInner)); + recvDatas.add(shippingFeeReceiver); } + } else { + // 没有县级代理商的时候,配送费暂由平台代收 add 2025-10-11 + V3SacsSeparateRecvDatas shippingFeeReceiver = new V3SacsSeparateRecvDatas(); + shippingFeeReceiver.setRecvNo(platformReceiver.getReceiver_no()); + shippingFeeReceiver.setSeparateValue(Convert.toStr(shoppingFeeInner)); + recvDatas.add(shippingFeeReceiver); } // 初始化拉卡拉SDK @@ -2619,8 +2635,7 @@ public class LakalaApiServiceImpl implements LakalaApiService { separateRequest.setNotifyUrl(projectDomain + "/api/mobile/shop/lakala/sacs/separateNotify"); separateRequest.setRecvDatas(recvDatas); - log.info("[分账操作] 分账接收方数量={}", recvDatas.size()); - log.debug("[分账操作] 请求详细参数: {}", JSONUtil.toJsonStr(separateRequest)); + log.debug("[分账操作] 分账接收方数量={},请求详细参数: {}", recvDatas.size(), JSONUtil.toJsonStr(separateRequest)); // 发送分账请求 log.info("[分账操作] 向拉卡拉发送分账请求:订单={}, 商户={}, 分账流水号={}", @@ -3617,7 +3632,6 @@ public class LakalaApiServiceImpl implements LakalaApiService { /** * 商户分账参数计算及评估 * - * @param splitMode 分账模式:1-总金额为基准分账,2-可分账金额基准分账,必填参数 * @param orderPayAmount 订单支付总金额(单位:分)必填参数 * @param shippingFeeInner 平台内部配送费(单位:分)必填参数 * @param mchSplitRatioRaw 商户分账比例值(分子值,如10表示10%)必填参数 @@ -3628,17 +3642,17 @@ public class LakalaApiServiceImpl implements LakalaApiService { * @return Pair 分账参数评估结果,第一个元素表示是否成功,第二个元素为分账参数对象 */ @Override - public Pair calculateAndEvaluateSharingParams(int splitMode, - Integer orderPayAmount, - Integer shippingFeeInner, - BigDecimal mchSplitRatioRaw, - BigDecimal platSplitRatio, - BigDecimal agent1stRatio, - BigDecimal agent2ndRatio, - Integer refCanSeparateAmt) { - log.debug("[分账参数计算] 开始计算分账参数: splitMode={}, orderPayAmount={}, shippingFeeInner={}, " + + public Pair calculateAndEvaluateSharingParams( + Integer orderPayAmount, + Integer shippingFeeInner, + BigDecimal mchSplitRatioRaw, + BigDecimal platSplitRatio, + BigDecimal agent1stRatio, + BigDecimal agent2ndRatio, + Integer refCanSeparateAmt) { + log.debug("[分账参数计算] 开始计算分账参数: orderPayAmount={}, shippingFeeInner={}, " + "mchSplitRatioRaw={}, platSplitRatio={}, agent1stRatio={}, agent2ndRatio={}, refCanSeparateAmt={}", - splitMode, orderPayAmount, shippingFeeInner, mchSplitRatioRaw, platSplitRatio, + orderPayAmount, shippingFeeInner, mchSplitRatioRaw, platSplitRatio, agent1stRatio, agent2ndRatio, refCanSeparateAmt); // 参数校验 @@ -3652,10 +3666,6 @@ public class LakalaApiServiceImpl implements LakalaApiService { return Pair.of(false, null); } - if (splitMode != 1 && splitMode != 2) { - log.warn("[分账参数计算] 分账模式参数错误: splitMode={}", splitMode); - return Pair.of(false, null); - } // 计算商家分账比例(转换为小数) BigDecimal mchSplitRatio = mchSplitRatioRaw.divide(new BigDecimal(100)); @@ -3674,7 +3684,7 @@ public class LakalaApiServiceImpl implements LakalaApiService { log.debug("[分账参数计算] 配送费: {}, 拉卡拉费率: {}", actualShippingFeeInner, wxFeeRatio); // 构建分账参数对象 - LklSeparateDTO lklSeparateDTO = new LklSeparateDTO(); + LklSeparateWithTotalAmountDTO lklSeparateDTO = new LklSeparateWithTotalAmountDTO(); lklSeparateDTO.setTotalSeparateAmount(orderPayAmount); lklSeparateDTO.setShippingFee(actualShippingFeeInner); lklSeparateDTO.setLklRatio(wxFeeRatio); // 拉卡拉给的微信分账比例 0.0025 千分之2.5 @@ -3698,25 +3708,20 @@ public class LakalaApiServiceImpl implements LakalaApiService { log.debug("[分账参数计算] 设置参考可分账金额: {}", refCanSeparateAmt); } - // 根据分账模式执行不同的分账计算 - LklSeparateDTO.SharingResult canSeparateAmtResult; - if (splitMode == 1) { - // 总金额为基准分账 - log.debug("[分账参数计算] 使用总金额为基准分账模式"); - canSeparateAmtResult = lklSeparateDTO.sharingOnTotalAmount(); - } else { - // 可分金额基准分账 - log.debug("[分账参数计算] 使用可分账金额基准分账模式"); - canSeparateAmtResult = lklSeparateDTO.sharingOnCanSeparateAmount(); - } + try { + // 根据分账模式执行不同的分账计算 + Pair canSeparateAmtResult = lklSeparateDTO.calculateSeparateAmount(); + if (!canSeparateAmtResult.getFirst()) { + log.warn("[分账参数计算] 分账参数有误,分账估算失败"); + return Pair.of(false, lklSeparateDTO); + } - if (!canSeparateAmtResult.isSuccess()) { - log.warn("[分账参数计算] 分账参数评估失败: {}", canSeparateAmtResult.getErrorMessage()); + log.info("[分账参数计算] 分账估算成功, result={}", lklSeparateDTO); + return Pair.of(true, lklSeparateDTO); + } catch (Exception e) { + log.error("[分账参数计算] 分账参数有误,分账估算失败", e); return Pair.of(false, lklSeparateDTO); } - - log.info("[分账参数计算] 分账参数计算评估成功, result={}", lklSeparateDTO); - return Pair.of(true, lklSeparateDTO); } diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java index 26ddc3fe..1b47effe 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/order/service/impl/ShopOrderBaseServiceImpl.java @@ -56,7 +56,7 @@ import com.suisung.mall.common.modules.product.ShopProductItem; import com.suisung.mall.common.modules.product.ShopProductValidPeriod; import com.suisung.mall.common.modules.store.*; import com.suisung.mall.common.modules.user.*; -import com.suisung.mall.common.pojo.dto.LklSeparateDTO; +import com.suisung.mall.common.pojo.dto.LklSeparateWithTotalAmountDTO; import com.suisung.mall.common.pojo.dto.StandardAddressDTO; import com.suisung.mall.common.pojo.dto.WxOrderBaseInfoDTO; import com.suisung.mall.common.pojo.req.*; @@ -7679,8 +7679,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl calcResult = lakalaApiService.calculateAndEvaluateSharingParams( - CommonConstant.SeparateCalcMode_CanSeparateAmt, + Pair calcResult = lakalaApiService.calculateAndEvaluateSharingParams( Convert.toInt(order_payment_amount.multiply(BigDecimal.valueOf(100))), innerMinDeliverFee, storeSplitRatio, @@ -7690,7 +7689,7 @@ public class ShopOrderBaseServiceImpl extends BaseServiceImpl