|
|
@@ -41,6 +41,7 @@ public class DistributionService {
|
|
|
if (system.getName() != null) existing.setName(system.getName());
|
|
|
if (system.getDescription() != null) existing.setDescription(system.getDescription());
|
|
|
if (system.getProfitRate() != null) existing.setProfitRate(system.getProfitRate());
|
|
|
+ if (system.getOwnerId() != null) existing.setOwnerId(system.getOwnerId());
|
|
|
existing.setUpdatedAt(new Date());
|
|
|
distributionSystemMapper.updateById(existing);
|
|
|
return Result.success("更新成功");
|
|
|
@@ -138,47 +139,70 @@ public class DistributionService {
|
|
|
return Result.success(chain);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 计算销售体系佣金(T37 负责人结算口径)。
|
|
|
+ * <p>
|
|
|
+ * 平台向团队结算,结算对象 = 系统唯一负责人(DistributionSystem.ownerId);
|
|
|
+ * 金额 = 佣金总额(团队整体拿 totalCommission,平台不再从团队佣金里二次切分)。
|
|
|
+ * 不再输出 level1Amount/level2Amount/platformAmount 三方拆分(70/30 L1/L2)。
|
|
|
+ * <p>
|
|
|
+ * ownerId 为空时降级:取该体系树 root 节点(parentId=null)的用户作为负责人;
|
|
|
+ * 仍无负责人则走「无团队结算」分支(settleToUserId=null,settleAmount=0)。
|
|
|
+ * <p>
|
|
|
+ * 保留「每个成员都有自己的团队」表达:以任一成员为 root 的子树即该成员的团队,
|
|
|
+ * 平台按其子树整体结算给该成员(作为其子团队负责人),不沿 parent 链拆分。
|
|
|
+ * <p>
|
|
|
+ * 兼容:旧字段 level1Amount/level1User/level2Amount/level2User/platformAmount
|
|
|
+ * 一期置 null(过渡),二期移除。
|
|
|
+ *
|
|
|
+ * @param systemId 销售体系 ID
|
|
|
+ * @param buyerId 下单用户 ID(T37 起不再用于沿 parent 链拆分,保留参数以兼容调用方)
|
|
|
+ * @param orderAmount 订单金额(分)
|
|
|
+ */
|
|
|
public Result<Map<String, Object>> calculateCommission(Long systemId, Long buyerId, Integer orderAmount) {
|
|
|
DistributionSystem system = distributionSystemMapper.selectById(systemId);
|
|
|
if (system == null || !Boolean.TRUE.equals(system.getEnabled())) {
|
|
|
return Result.error("销售体系不可用");
|
|
|
}
|
|
|
+ if (orderAmount == null || orderAmount < 0) {
|
|
|
+ return Result.error("订单金额不合法");
|
|
|
+ }
|
|
|
|
|
|
+ // 佣金总额:平台抽成后归团队的整体佣金(千分比),平台不再从团队佣金里二次切分
|
|
|
int totalCommission = orderAmount * system.getProfitRate() / 1000;
|
|
|
|
|
|
- List<DistributionRelation> chain = new ArrayList<>();
|
|
|
- Long currentUserId = buyerId;
|
|
|
- for (int i = 0; i < 2; i++) {
|
|
|
- DistributionRelation relation = distributionRelationMapper.selectOne(
|
|
|
- new LambdaQueryWrapper<DistributionRelation>()
|
|
|
- .eq(DistributionRelation::getSystemId, systemId)
|
|
|
- .eq(DistributionRelation::getUserId, currentUserId)
|
|
|
- );
|
|
|
- if (relation == null || relation.getParentId() == null) break;
|
|
|
- DistributionRelation parent = distributionRelationMapper.selectOne(
|
|
|
- new LambdaQueryWrapper<DistributionRelation>()
|
|
|
- .eq(DistributionRelation::getSystemId, systemId)
|
|
|
- .eq(DistributionRelation::getUserId, relation.getParentId())
|
|
|
- );
|
|
|
- if (parent == null) break;
|
|
|
- chain.add(parent);
|
|
|
- currentUserId = relation.getParentId();
|
|
|
- }
|
|
|
-
|
|
|
- int level1Amount = totalCommission * 70 / 100;
|
|
|
- int level2Amount = totalCommission * 30 / 100;
|
|
|
+ // 结算对象 = 系统唯一负责人;为空时降级取体系树 root(parentId=null)用户
|
|
|
+ Long settleToUserId = resolveSettlementOwner(systemId, system);
|
|
|
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
|
result.put("totalCommission", totalCommission);
|
|
|
- result.put("level1Amount", chain.size() >= 1 ? level1Amount : 0);
|
|
|
- result.put("level1User", chain.size() >= 1 ? chain.get(0).getUserId() : null);
|
|
|
- result.put("level2Amount", chain.size() >= 2 ? level2Amount : 0);
|
|
|
- result.put("level2User", chain.size() >= 2 ? chain.get(1).getUserId() : null);
|
|
|
- result.put("platformAmount", totalCommission - (chain.size() >= 1 ? level1Amount : 0)
|
|
|
- - (chain.size() >= 2 ? level2Amount : 0));
|
|
|
+ // T37 新口径:单一结算对象 = 系统唯一负责人,金额 = 佣金总额
|
|
|
+ result.put("settleToUserId", settleToUserId);
|
|
|
+ result.put("settleAmount", settleToUserId != null ? totalCommission : 0);
|
|
|
+ // 兼容:旧 70/30 拆分字段一期置 null(@Deprecated 一期过渡,二期移除)
|
|
|
+ result.put("level1Amount", null);
|
|
|
+ result.put("level1User", null);
|
|
|
+ result.put("level2Amount", null);
|
|
|
+ result.put("level2User", null);
|
|
|
+ result.put("platformAmount", null);
|
|
|
return Result.success(result);
|
|
|
}
|
|
|
|
|
|
+ /** 解析销售体系结算负责人(唯一):优先 ownerId,为空时取体系树 root 节点用户 */
|
|
|
+ private Long resolveSettlementOwner(Long systemId, DistributionSystem system) {
|
|
|
+ if (system.getOwnerId() != null) {
|
|
|
+ return system.getOwnerId();
|
|
|
+ }
|
|
|
+ List<DistributionRelation> roots = distributionRelationMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DistributionRelation>()
|
|
|
+ .eq(DistributionRelation::getSystemId, systemId)
|
|
|
+ .isNull(DistributionRelation::getParentId)
|
|
|
+ .orderByAsc(DistributionRelation::getDepth)
|
|
|
+ .last("LIMIT 1")
|
|
|
+ );
|
|
|
+ return roots.isEmpty() ? null : roots.get(0).getUserId();
|
|
|
+ }
|
|
|
+
|
|
|
public Result<List<DistributionSystem>> getUserTeams(Long userId) {
|
|
|
List<DistributionRelation> relations = distributionRelationMapper.selectList(
|
|
|
new LambdaQueryWrapper<DistributionRelation>()
|