Compare commits

2 Commits

Author SHA1 Message Date
xm
49ce3c40e5 1 2026-06-10 15:07:57 +08:00
xm
97db34f141 1、拆分门店、服务商角色及调整对应订单计算算法
2、推荐人关系列表增加删除标记,方便后期数据硕源
2026-06-05 17:31:19 +08:00
9 changed files with 180 additions and 64 deletions

View File

@@ -11,6 +11,7 @@ import com.gxwebsoft.common.system.mapper.UserMapper;
import com.gxwebsoft.common.system.redis.OrderNoUtils;
import com.gxwebsoft.glt.entity.GltTicketTemplate;
import com.gxwebsoft.glt.service.GltTicketTemplateService;
import com.gxwebsoft.glt.vo.UserRelationVO;
import com.gxwebsoft.shop.dto.ShopDealerSettlementDto;
import com.gxwebsoft.shop.dto.ShopDealerSettlementItemDto;
import com.gxwebsoft.shop.dto.ShopDealerUserReduceDto;
@@ -202,7 +203,7 @@ public class DealerOrderSettlement10584Task {
if (!claimOrderToSettle(order.getOrderId(), waterFormIds)) {
return;
}
settleOneOrderV2(order, level1ParentCache, shopRoleCache, totalDealerUser, dealerBasicSetting.level);
settleOneOrderV2(order, dealerBasicSetting.level);
});
} catch (Exception e) {
log.error("订单结算失败,将回滚本订单并在下次任务重试 - orderId={}, orderNo={}", order.getOrderId(), order.getOrderNo(), e);
@@ -236,7 +237,7 @@ public class DealerOrderSettlement10584Task {
if (!claimOrderToSettleV2(order.getOrderId())) {
return;
}
settleOneOrderV2(order, level1ParentCache, shopRoleCache, totalDealerUser, dealerBasicSetting.level);
settleOneOrderV2(order, dealerBasicSetting.level);
});
}
}
@@ -428,8 +429,7 @@ public class DealerOrderSettlement10584Task {
log.info("订单结算完成 - orderId={}, orderNo={}, baseAmount={}", order.getOrderId(), order.getOrderNo(), baseAmount);
}
private void settleOneOrderV2(ShopOrder order, Map<Integer, Integer> level1ParentCache, Map<Integer, Boolean> shopRoleCache,
ShopDealerUser totalDealerUser, int dealerLevel) {
private void settleOneOrderV2(ShopOrder order, int dealerLevel) {
if (order.getUserId() == null || order.getOrderNo() == null) {
throw new IllegalStateException("订单关键信息缺失,无法结算 - orderId=" + order.getOrderId());
}
@@ -451,11 +451,7 @@ public class DealerOrderSettlement10584Task {
// 2) 门店分润上级:从下单用户开始逐级向上找,命中 ShopDealerUser.type=1 的最近两级(直推门店/间推门店)【只统计数据,不对分销账户进行处理,
// 已日结形式统计分销记录表shop_dealer_order 做对应一级二级管理津贴结算】
ShopRoleCommission shopRoleCommission = settleShopRoleRefereeCommissionV2(order, rate, orderGoodsVOList, level1ParentCache, shopRoleCache);
// // 3) 分红:固定比率,每个订单都分
// int goodsQty = orderGoodsVOList.stream().mapToInt(ShopOrderGoodsVO::getTotalNum).sum();
// TotalDealerCommission totalDealerCommission = settleTotalDealerCommissionV2(order, goodsQty, totalDealerUser);
ShopRoleCommission shopRoleCommission = settleShopRoleRefereeCommissionV2(order, rate, orderGoodsVOList);
// 3) 写入分销订单记录(用于排查/统计;详细分佣以 ShopDealerCapital 为准)
createDealerOrderRecordV2(order, dealerRefereeCommission, shopRoleCommission);
@@ -594,13 +590,13 @@ public class DealerOrderSettlement10584Task {
//一级分销员存在(type = 0)且单项实付金额大于0及商品设置了一级分销比例/金额
if(finalDirectDealerId != null && itemRatePrice.compareTo(BigDecimal.ZERO) > 0 && firstMoney.compareTo(BigDecimal.ZERO) > 0){
BigDecimal one = calcMoneyByCommissionType(itemRatePrice, firstMoney, orderGoodsVO.getTotalNum(), 2, orderGoodsVO.getCommissionType());
BigDecimal one = calcMoneyByCommissionType(itemRatePrice, firstMoney, orderGoodsVO.getTotalNum(), 3, orderGoodsVO.getCommissionType());
directMoney.accumulateAndGet(one, BigDecimal::add);
}
//一级分销员存在(type = 0)且单项实付金额大于0及商品设置了一级分销比例/金额
if(finalSimpleDealerId != null && itemRatePrice.compareTo(BigDecimal.ZERO) > 0 && secondMoney.compareTo(BigDecimal.ZERO) > 0 ){
BigDecimal two = calcMoneyByCommissionType(itemRatePrice, secondMoney, orderGoodsVO.getTotalNum(), 2, orderGoodsVO.getCommissionType());
BigDecimal two = calcMoneyByCommissionType(itemRatePrice, secondMoney, orderGoodsVO.getTotalNum(), 3, orderGoodsVO.getCommissionType());
simpleMoney.accumulateAndGet(two, BigDecimal::add);
}
});
@@ -716,28 +712,18 @@ public class DealerOrderSettlement10584Task {
return new ShopRoleCommission(shopRoleReferees.get(0), storeDirectMoney, shopRoleReferees.get(1), storeSimpleMoney);
}
private ShopRoleCommission settleShopRoleRefereeCommissionV2(ShopOrder order, BigDecimal rate, List<ShopOrderGoodsVO> orderGoodsVOList, Map<Integer, Integer> level1ParentCache, Map<Integer, Boolean> shopRoleCache) {
List<Integer> shopRoleReferees = findFirstTwoShopRoleReferees(order.getUserId(), level1ParentCache, shopRoleCache);
log.info("门店分润命中结果(type=1门店角色取前两级) - orderNo={}, buyerUserId={}, shopRoleReferees={}",
order.getOrderNo(), order.getUserId(), shopRoleReferees);
if (shopRoleReferees.isEmpty()) {
private ShopRoleCommission settleShopRoleRefereeCommissionV2(ShopOrder order, BigDecimal rate, List<ShopOrderGoodsVO> orderGoodsVOList) {
UserRelationVO userSuperior = shopDealerUserService.getUserSuperior(order.getUserId());
Integer storeDirectUserId = userSuperior.getStoreUserId();
Integer storeSimpleUserId = userSuperior.getServiceUserId();
if(storeDirectUserId == null && storeSimpleUserId == null){
return ShopRoleCommission.empty();
}
if(CollectionUtils.isNotEmpty(shopRoleReferees)){
Integer storeDirectUserId;
Integer storeSimpleUserId = null;
AtomicReference<BigDecimal> storeDirectMoney = new AtomicReference<>(BigDecimal.ZERO);
AtomicReference<BigDecimal> storeSimpleMoney = new AtomicReference<>(BigDecimal.ZERO);
if(shopRoleReferees.size() == 1){
storeDirectUserId = shopRoleReferees.get(0);
}else {
storeDirectUserId = shopRoleReferees.get(0);
storeSimpleUserId = shopRoleReferees.get(1);
}
Integer finalStoreDirectUserId = storeDirectUserId;
Integer finalStoreSimpleUserId = storeSimpleUserId;
orderGoodsVOList.forEach(orderGoodsVO ->{
@@ -749,19 +735,16 @@ public class DealerOrderSettlement10584Task {
BigDecimal itemRatePrice = orderGoodsVO.getPrice().multiply(BigDecimal.valueOf(orderGoodsVO.getTotalNum())).multiply(rate);
if(finalStoreDirectUserId != null && itemRatePrice.compareTo(BigDecimal.ZERO) > 0){
BigDecimal one = calcMoneyByCommissionType(itemRatePrice, firstMoney, orderGoodsVO.getTotalNum(), 2, orderGoodsVO.getCommissionType());
BigDecimal one = calcMoneyByCommissionType(itemRatePrice, firstMoney, orderGoodsVO.getTotalNum(), 3, orderGoodsVO.getCommissionType());
storeDirectMoney.accumulateAndGet(one, BigDecimal::add);
}
if(finalStoreSimpleUserId != null && itemRatePrice.compareTo(BigDecimal.ZERO) > 0){
BigDecimal two = calcMoneyByCommissionType(itemRatePrice, secondMoney, orderGoodsVO.getTotalNum(), 2, orderGoodsVO.getCommissionType());
BigDecimal two = calcMoneyByCommissionType(itemRatePrice, secondMoney, orderGoodsVO.getTotalNum(), 3, orderGoodsVO.getCommissionType());
storeSimpleMoney.accumulateAndGet(two, BigDecimal::add);
}
});
return new ShopRoleCommission(storeDirectUserId, storeDirectMoney.get(), storeSimpleUserId, storeSimpleMoney.get());
}else {
return null;
}
}
private TotalDealerCommission settleTotalDealerCommission(

View File

@@ -0,0 +1,16 @@
package com.gxwebsoft.glt.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
public class UserRelationVO {
@Schema(description = "当前用户ID")
private Integer userId;
@Schema(description = "上级门店ID")
private Integer storeUserId;
@Schema(description = "上级服务商ID")
private Integer serviceUserId;
}

View File

@@ -90,7 +90,7 @@ public class ShopDealerUserController extends BaseController {
@Operation(summary = "修改分销商用户记录表")
@PutMapping()
public ApiResult<?> update(@RequestBody ShopDealerUser shopDealerUser) {
if (shopDealerUserService.updateById(shopDealerUser)) {
if (shopDealerUserService.updateInfo(shopDealerUser)) {
return success("修改成功");
}
return fail("修改失败");

View File

@@ -1,11 +1,10 @@
package com.gxwebsoft.shop.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import io.swagger.v3.oas.annotations.media.Schema;
@@ -21,6 +20,7 @@ import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = false)
@Schema(name = "ShopDealerReferee对象", description = "分销商推荐关系表")
@TableName("shop_dealer_referee")
public class ShopDealerReferee implements Serializable {
private static final long serialVersionUID = 1L;
@@ -69,6 +69,10 @@ public class ShopDealerReferee implements Serializable {
@Schema(description = "推荐关系层级(弃用)")
private Integer level;
@Schema(description = "是否删除 0-未删 1-已删")
@TableLogic
private Integer deleted;
@Schema(description = "来源(如 goods_share)")
// NOTE: 表 shop_dealer_referee 若未新增该字段,需要 exist=false避免 MyBatis-Plus 自动生成SQL时报 Unknown column。
@TableField(exist = false)

View File

@@ -29,7 +29,7 @@ public class ShopDealerUser implements Serializable {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@Schema(description = "0经销商,1企业也,2集团)")
@Schema(description = "0-分销员 1-门店 2-服务商 3-合伙人")
private Integer type;
@Schema(description = "自增ID")

View File

@@ -17,6 +17,7 @@
INNER JOIN gxwebsoft_core.sys_user d ON a.dealer_id = d.user_id AND d.deleted = 0
INNER JOIN gxwebsoft_core.sys_user u ON a.user_id = u.user_id AND u.deleted = 0
<where>
a.deleted = 0
<if test="param.tenantId != null">
AND a.tenant_id = #{param.tenantId}
</if>
@@ -65,12 +66,11 @@
d.type simpleUserType
FROM
shop_dealer_referee a
LEFT JOIN shop_dealer_user b ON a.dealer_id = b.user_id
LEFT JOIN shop_dealer_referee c ON b.user_id = c.user_id
LEFT JOIN shop_dealer_user d ON d.user_id = c.dealer_id
LEFT JOIN shop_dealer_user b ON a.dealer_id = b.user_id AND b.deleted = 0
LEFT JOIN shop_dealer_referee c ON b.user_id = c.user_id AND c.deleted = 0
LEFT JOIN shop_dealer_user d ON c.dealer_id = d.user_id and d.deleted = 0
WHERE
b.is_delete = 0
and d.is_delete = 0
a.deleted = 0
AND a.user_id = #{userId}
</select>

View File

@@ -94,7 +94,9 @@ public class OrderBusinessService {
ShopOrder shopOrder = buildShopOrder(request, loginUser);
//3. 处理收货地址信息
if(!(shopOrder.getOrderType() != null && shopOrder.getOrderType() == 2)){
processDeliveryAddress(shopOrder, request, loginUser);
}
//4. 下单时校验配送范围(电子围栏)
validateDeliveryFenceIfNeeded(shopOrder, loginUser);
@@ -554,7 +556,9 @@ public class OrderBusinessService {
return;
}
// 5. 如果用户没有任何收货地址,抛出异常
if(Arrays.asList(1, 3, 4).contains(shopOrder.getOrderType())){
throw new BusinessException("请先添加收货地址");
}
} catch (BusinessException e) {
throw e;

View File

@@ -2,6 +2,7 @@ package com.gxwebsoft.shop.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.glt.vo.UserRelationVO;
import com.gxwebsoft.shop.dto.ShopDealerRefundDto;
import com.gxwebsoft.shop.dto.ShopDealerSettlementDto;
import com.gxwebsoft.shop.dto.ShopDealerUserReduceDto;
@@ -75,6 +76,8 @@ public interface ShopDealerUserService extends IService<ShopDealerUser> {
*/
Boolean orderProfit(ShopDealerSettlementDto dto);
Boolean updateInfo(ShopDealerUser shopDealerUser);
/**
* 开启/关闭分销商用户核销权限
* @param id
@@ -82,4 +85,11 @@ public interface ShopDealerUserService extends IService<ShopDealerUser> {
*/
Boolean verifyEnable(Integer id);
/**
* 查询用户上级门店、上级服务商
* @param userId
* @return
*/
UserRelationVO getUserSuperior(Integer userId);
}

View File

@@ -5,12 +5,15 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.enums.ShopDealerCapitalUpdateEnum;
import com.gxwebsoft.common.core.enums.ShopDealerTypeEnum;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.exception.enums.GlobalErrorCodeConstants;
import com.gxwebsoft.common.core.utils.LoginUserUtil;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.mapper.UserMapper;
import com.gxwebsoft.common.system.redis.OrderNoUtils;
import com.gxwebsoft.glt.vo.UserRelationVO;
import com.gxwebsoft.shop.dto.ShopDealerRefundDto;
import com.gxwebsoft.shop.dto.ShopDealerSettlementDto;
import com.gxwebsoft.shop.dto.ShopDealerSettlementItemDto;
@@ -19,11 +22,13 @@ import com.gxwebsoft.shop.entity.ShopDealerCapital;
import com.gxwebsoft.shop.entity.ShopDealerUser;
import com.gxwebsoft.shop.entity.ShopOrder;
import com.gxwebsoft.shop.mapper.ShopDealerCapitalMapper;
import com.gxwebsoft.shop.mapper.ShopDealerRefereeMapper;
import com.gxwebsoft.shop.mapper.ShopDealerUserMapper;
import com.gxwebsoft.shop.mapper.ShopOrderMapper;
import com.gxwebsoft.shop.param.ShopDealerUserParam;
import com.gxwebsoft.shop.service.ShopDealerCapitalService;
import com.gxwebsoft.shop.service.ShopDealerUserService;
import com.gxwebsoft.shop.vo.ShopDealerRefereeVO;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
@@ -58,6 +63,8 @@ public class ShopDealerUserServiceImpl extends ServiceImpl<ShopDealerUserMapper,
private ShopOrderMapper shopOrderMapper;
private ShopDealerRefereeMapper shopDealerRefereeMapper;
private static final int TENANT_ID = 10584;
@Override
@@ -478,6 +485,34 @@ public class ShopDealerUserServiceImpl extends ServiceImpl<ShopDealerUserMapper,
return Boolean.TRUE;
}
@Override
public Boolean updateInfo(ShopDealerUser entity) {
if(entity.getId() == null){
throw new RuntimeException("数据ID必传");
}
ShopDealerUser dealerUser = baseMapper.selectById(entity.getId());
if(dealerUser != null){
if(!dealerUser.getType().equals(entity.getType()) && Arrays.asList(1, 2).contains(entity.getType())){
UserRelationVO relationVO = getUserSuperior(dealerUser.getUserId());
if(entity.getType() == 1 && relationVO.getStoreUserId() != null){
throw new BusinessException("当前用户推荐关系已存在门店角色,不可升级为门店!");
}
if(entity.getType() == 2){
if(relationVO.getServiceUserId() != null){
throw new BusinessException("当前用户推荐关系已存在服务商角色,不可升级为服务商!");
}
if(relationVO.getStoreUserId() != null){
throw new BusinessException("当前用户推荐关系已存在门店角色,不可升级为服务商[等级限定]");
}
}
}
return baseMapper.updateById(entity) > 0;
}else {
throw new BusinessException(GlobalErrorCodeConstants.NOT_FOUND.getMsg());
}
}
@Override
public Boolean verifyEnable(Integer id) {
ShopDealerUser dealerUser = baseMapper.selectById(id);
@@ -497,6 +532,70 @@ public class ShopDealerUserServiceImpl extends ServiceImpl<ShopDealerUserMapper,
return Boolean.TRUE;
}
@Override
public UserRelationVO getUserSuperior(Integer userId) {
UserRelationVO vo = new UserRelationVO();
vo.setUserId(userId);
// 1. 先找到当前用户的直接上级ID推荐人
Integer superiorId = getDirectSuperiorId(userId);
if (superiorId == null || superiorId == 0) {
return vo; // 无上级,直接返回
}
// 2. 逐级向上查找 门店(type=1) 和 服务商(type=2)
findStoreAndService(superiorId, vo);
return vo;
}
/**
* 1. 获取直接上级ID从推荐关系表
*/
private Integer getDirectSuperiorId(Integer userId) {
ShopDealerRefereeVO refereeVO = shopDealerRefereeMapper.getDealerIdByUserId(userId);
if(refereeVO != null && refereeVO.getDirectUserId() != null){
return refereeVO.getDirectUserId();
}
return null;
}
/**
* 2. 递归/逐级查找门店、服务商最多查5级防止死循环
*/
private void findStoreAndService(Integer currentUserId, UserRelationVO vo) {
int maxLevel = 10; // 限制最大层级,避免死循环
Integer tempUserId = currentUserId;
for (int i = 0; i < maxLevel; i++) {
if (tempUserId == null) break;
// 查询当前用户的等级类型
ShopDealerUser dealerUser = getOne(new LambdaQueryWrapper<ShopDealerUser>().eq(ShopDealerUser::getUserId, tempUserId));
if (dealerUser == null) break;
// 找到门店,且还没赋值
if (vo.getStoreUserId() == null && dealerUser.getType() == 1) {
vo.setStoreUserId(tempUserId);
}
// 找到服务商,且还没赋值
if (vo.getServiceUserId() == null && dealerUser.getType() == 2) {
vo.setServiceUserId(tempUserId);
}
// 都找到了,提前退出
if (vo.getStoreUserId() != null && vo.getServiceUserId() != null) {
break;
}
// 继续向上找上级
tempUserId = getDirectSuperiorId(tempUserId);
}
}
public ShopDealerUser getDealerUser(Integer userId){
ShopDealerUser shopDealerUser = baseMapper.selectOne(new LambdaQueryWrapper<ShopDealerUser>().eq(ShopDealerUser::getUserId, userId));
if(shopDealerUser == null){