邀请问题

This commit is contained in:
2026-01-23 16:35:56 +08:00
parent 3b28287f22
commit 1a18449d65
12 changed files with 79 additions and 260 deletions

View File

@@ -18,7 +18,7 @@
and amount - #{money} >= 0
</update>
<update id="updateOneOrTwoAmount">
update tb_shop_user_invite
update tb_shop_user
<set>
<if test="isOne != null and isOne == 1">
one_income = one_income + #{amount}
@@ -27,8 +27,7 @@
two_income = two_income + #{amount}
</if>
</set>
where shop_user_id = #{shopUserId}
and shop_id = #{shopId}
where id = #{shopUserId}
</update>
@@ -257,10 +256,10 @@
</select>
<select id="getInviteUser" resultType="com.czg.market.vo.InviteUserVO">
SELECT
u.id AS shopUserId,
u.head_img AS headImg,
u.nick_name AS shopUserName,
u.phone AS shopUserPhone,
invite.id AS shopUserId,
invite.head_img AS headImg,
invite.nick_name AS shopUserName,
invite.phone AS shopUserPhone,
invite.one_income AS oneIncome,
invite.invite_time AS inviteTime,
dist.total_income AS totalIncome,
@@ -270,13 +269,12 @@
dist.status AS status,
dist.distribution_level_id AS levelId,
dist.distribution_level_name AS levelName,
u.distribution_shops AS distributionShops
FROM tb_shop_user_invite invite
left join tb_shop_user u on invite.shop_user_id = u.id
invite.distribution_shops AS distributionShops
FROM tb_shop_user invite
left join mk_distribution_user dist on u.id = dist.id and dist.shop_id = #{shopId}
WHERE invite.`distribution_user_id` = #{distributionUserId} and invite.shop_id = #{shopId}
WHERE invite.`parent_user_id` = #{distributionUserId}
<if test="distributionLevelId != null">and dist.distribution_level_id = #{distributionLevelId}</if>
<if test="shopUserId != null">and invite.shop_user_id = #{shopUserId}</if>
<if test="shopUserId != null">and invite.id = #{shopUserId}</if>
ORDER BY invite.`invite_time` DESC
</select>
</mapper>

View File

@@ -1,14 +0,0 @@
package com.czg.service.market.mapper;
import com.mybatisflex.core.BaseMapper;
import com.czg.market.entity.ShopUserInvite;
/**
* 邀请与上级关联表 映射层。
*
* @author ww
* @since 2025-11-06
*/
public interface ShopUserInviteMapper extends BaseMapper<ShopUserInvite> {
}

View File

@@ -73,12 +73,8 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
private MkDistributionWithdrawFlowService withdrawFlowService;
@Resource
private MkDistributionDeliverService distributionDeliverService;
@Resource
private ShopUserInviteService shopUserInviteService;
@Resource
private AppWxServiceImpl appWxService;
@DubboReference
private ShopUserService shopUserService;
@DubboReference
@@ -150,9 +146,9 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
AssertUtil.isNull(shopUser, "店铺用户不存在");
UserInfo userInfo = userInfoService.getById(shopUser.getUserId());
result.put("cashOutAmount", userInfo.getDistributionAmount() == null ? 0.0 : userInfo.getDistributionAmount());
ShopUserInvite shopUserInvite = shopUserInviteService.getOneByShopIdAndShopUserId(shopId, shopUser.getId());
if (shopUserInvite != null && shopUserInvite.getDistributionUserId() != null) {
MkDistributionUser mkDistributionUser = getMkDistributionUserByIdAndShopId(shopUserInvite.getDistributionUserId(), shopId);
if (shopUser.getParentUserId() != null) {
MkDistributionUser mkDistributionUser = getMkDistributionUserByIdAndShopId(shopUser.getParentUserId(), shopId);
AssertUtil.isNull(mkDistributionUser, "上级分销员不存在");
ShopUser shopUserParent = shopUserService.getById(mkDistributionUser.getId());
result.put("parentName", shopUserParent.getNickName());
@@ -181,7 +177,7 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
if (levelConfig.getId().equals(mkDistributionUser.getDistributionLevelId())) {
distributionUser.put("level", levelConfig.getLevel());
distributionUser.put("levelName", levelConfig.getName());
distributionUser.put("levelOneCommission", levelConfig.getLevelOneCommission());
distributionUser.put("levelOneCommission", levelConfig.getCommission());
isNextLevel = true;
}
}
@@ -234,8 +230,7 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
public void bindInviteUser(MkDistributionUserDTO param) throws CzgException, ValidateException {
ShopUser shopUser = shopUserService.getById(param.getId());
AssertUtil.isNull(shopUser, "店铺用户不存在");
ShopUserInvite shopUserInvite = shopUserInviteService.getOneByShopIdAndShopUserId(param.getShopId(), shopUser.getId());
if (shopUserInvite != null && shopUserInvite.getDistributionUserId() != null) {
if (shopUser.getParentUserId() != null) {
throw new CzgException("店铺用户已绑定分销员");
}
MkDistributionUser parent = getOne(QueryWrapper.create().eq(MkDistributionUser::getInviteCode, param.getInviteCode()));
@@ -251,26 +246,21 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
}
ShopUser parentShopUser = shopUserService.getById(parent.getId());
AssertUtil.isNull(parentShopUser, "邀请人不存在");
ShopUserInvite parentShopUserInvite = shopUserInviteService.getOneByShopIdAndShopUserId(param.getShopId(), parentShopUser.getId());
if (parentShopUserInvite != null && parentShopUserInvite.getDistributionUserId() != null && parentShopUserInvite.getDistributionUserId().equals(shopUser.getId())) {
if (parentShopUser.getParentUserId() != null && parentShopUser.getParentUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
if (parentShopUserInvite != null && parentShopUserInvite.getDistributionUserParentId() != null && parentShopUserInvite.getDistributionUserParentId().equals(shopUser.getId())) {
if (parentShopUser.getGradeUserId() != null && parentShopUser.getGradeUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
//更新自己的上级
ShopUserInvite newShopUserInvite = new ShopUserInvite();
newShopUserInvite.setShopId(param.getShopId());
newShopUserInvite.setShopUserId(shopUser.getId());
newShopUserInvite.setDistributionUserId(parentShopUser.getId());
newShopUserInvite.setDistributionUserParentId(parentShopUserInvite == null ? null : parentShopUserInvite.getDistributionUserParentId());
newShopUserInvite.setInviteTime(LocalDateTime.now());
shopUserInviteService.save(newShopUserInvite);
if (parentShopUserInvite != null && parentShopUserInvite.getDistributionUserParentId() != null) {
shopUser.setParentUserId(parentShopUser.getId());
shopUser.setGradeUserId(parentShopUser.getParentUserId());
if (shopUser.getParentUserId() != null) {
//更新自己的下级 的上级的上级 为自己的上级
ShopUserInvite childShopUserInvite = new ShopUserInvite();
childShopUserInvite.setDistributionUserParentId(parentShopUser.getId());
shopUserInviteService.update(childShopUserInvite, QueryWrapper.create().eq(ShopUserInvite::getDistributionUserId, shopUser.getId()));
ShopUser upShopUser = new ShopUser();
upShopUser.setGradeUserId(shopUser.getParentUserId());
shopUserService.update(upShopUser, QueryWrapper.create().eq(ShopUser::getParentUserId, shopUser.getId()));
}
MkDistributionUser newDistributionUser = new MkDistributionUser();
@@ -318,9 +308,8 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
public void costUpgradeLevelBefore(Long userId, Long shopId) {
ShopUser shopUser = shopUserService.getShopUserInfo(shopId, userId);
costUpgradeLevel(shopUser.getId(), shopId);
ShopUserInvite shopUserInvite = shopUserInviteService.getOneByShopIdAndShopUserId(shopId, shopUser.getId());
if (shopUserInvite != null && shopUserInvite.getDistributionUserId() != null) {
costUpgradeLevel(shopUserInvite.getDistributionUserId(), shopId);
if (shopUser.getParentUserId() != null) {
costUpgradeLevel(shopUser.getParentUserId(), shopId);
}
}
@@ -569,70 +558,37 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
@GlobalTransactional
protected void deepReward(ShopUser orderSourceShopUser, MkDistributionLevelConfig parentLevel, MkDistributionConfigVO config,
MkDistributionUser currentDistributionUser, BigDecimal amount, Long sourceId, String type, String orderNo, Integer currentLevel) {
if (currentLevel > 2) {
return;
}
protected boolean deepReward(ShopUser orderSourceShopUser, BigDecimal commission, MkDistributionConfigVO config,
MkDistributionUser currentDistributionUser, BigDecimal amount, Long sourceId,
String type, String orderNo, Integer currentLevel) {
// 当前分销员
AssertUtil.isNull(currentDistributionUser, "分销员不存在");
AssertUtil.isTrue(currentDistributionUser.getStatus() != 1, "分销员未开启");
// ShopUser currentShopUser = shopUserService.getById(currentDistributionUser.getId());
ShopUserInvite currentShopUser = shopUserInviteService.getOneByShopIdAndShopUserId(config.getShopId(), currentDistributionUser.getId());
MkDistributionLevelConfig level = levelConfigService.getById(currentDistributionUser.getDistributionLevelId());
// 校验剩余分成比例
BigDecimal finalCommission = parentLevel == null ? level.getLevelOneCommission() : level.getLevelOneCommission().subtract(parentLevel.getLevelOneCommission());
if (finalCommission.compareTo(BigDecimal.ZERO) <= 0) {
log.info("当前分销员: {}, 分销等级: {}, 剩余比例: {}, 不参与分销", currentDistributionUser.getId(), level.getId(), finalCommission);
return;
if (commission.compareTo(BigDecimal.ZERO) <= 0) {
return false;
}
// 上上级分销员
if (currentShopUser != null && currentShopUser.getDistributionUserId() != null) {
MkDistributionUser parent = getMkDistributionUserByIdAndShopId(currentShopUser.getDistributionUserId(), config.getShopId());
try {
deepReward(orderSourceShopUser, level, config, parent, amount, sourceId, type, orderNo, currentLevel + 1);
} catch (Exception e) {
log.warn("分销奖励失败: {}", e.getMessage());
}
}
AssertUtil.isNull(level, "分销等级不存在");
log.info("当前分销员: {}, 上级分销员: {}, 分销等级: {}", currentDistributionUser.getId(), currentShopUser == null ? null : currentShopUser.getDistributionUserId(), level.getId());
if (config.getRewardCount() != null && config.getRewardCount() > 0) {
long count = distributionFlowService.count(new QueryWrapper()
.eq(MkDistributionFlow::getShopId, config.getShopId()).eq(MkDistributionFlow::getShopUserId, currentDistributionUser.getId()));
if (count >= config.getRewardCount()) {
log.info("分销员{}已达到奖励次数上限, 次数: {}", currentDistributionUser.getId(), config.getRewardCount());
return;
} else {
log.info("分销员奖励次数: {}", count);
return false;
}
}
// 店铺信息
BigDecimal rewardAmount;
// if (currentLevel == 1) {
// rewardAmount = amount.multiply(level.getLevelOneCommission().divide(BigDecimal.valueOf(100), RoundingMode.DOWN));
// } else {
// rewardAmount = amount.multiply(level.getLevelTwoCommission().divide(BigDecimal.valueOf(100), RoundingMode.DOWN));
// }
rewardAmount = amount.multiply(finalCommission.divide(BigDecimal.valueOf(100), RoundingMode.DOWN));
BigDecimal rewardAmount = amount.multiply(commission.divide(BigDecimal.valueOf(100), RoundingMode.DOWN));
// 延时发放时间
LocalDateTime delayTime = config.getSettlementDay() == null || config.getSettlementDay() == 0 ? null : DateUtil.offsetDay(DateUtil.date(), config.getSettlementDay()).toLocalDateTime();
// 用户分账明细
ShopUser shopUser = shopUserService.getById(currentDistributionUser.getId());
MkDistributionFlow mkDistributionFlow = new MkDistributionFlow().setShopUserId(currentDistributionUser.getId())
.setShopId(currentDistributionUser.getShopId()).setDistributionUserId(currentDistributionUser.getId())
.setUserId(shopUser.getUserId())
.setNickName(shopUser.getNickName()).setSourceShopUserId(orderSourceShopUser.getId()).setSourceNickName(orderSourceShopUser.getNickName())
.setCommission(finalCommission).setParentCommission(parentLevel != null ? parentLevel.getLevelOneCommission() : null)
.setLevelId(currentDistributionUser.getDistributionLevelId()).setLevel(currentLevel == 1 ? 1 : 2).setOrderNo(orderNo)
.setCommission(commission)
.setLevelId(currentDistributionUser.getDistributionLevelId()).setLevel(currentLevel).setOrderNo(orderNo)
.setSourceId(sourceId).setAmount(amount).setType(type)
.setRewardAmount(rewardAmount).setBillNo(IdUtil.simpleUUID());
@@ -655,6 +611,7 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
log.info("延时分销开始");
updateIncome(rewardAmount, BigDecimal.ZERO, BigDecimal.ZERO, currentDistributionUser.getId(), shopUser.getUserId(), shopUser.getId(), config.getShopId(), currentLevel == 1 ? 1 : 2);
}
return true;
}
@Override
@@ -712,19 +669,24 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
AssertUtil.isTrue(amount.compareTo(BigDecimal.ZERO) == 0, "分销金额不能为0");
MkDistributionConfigVO config = mkDistributionConfigService.detail(shopId);
AssertUtil.isTrue(config.getIsEnable() != 1, "分销未开启");
// 当前用户上级分销员
ShopUser sourceShopUserInfo = shopUserService.getShopUserInfo(shopId, sourceUserId);
if (sourceShopUserInfo == null) {
// 产生消费的用户
ShopUser curUser = shopUserService.getShopUserInfo(shopId, sourceUserId);
if (curUser == null || curUser.getParentUserId() == null) {
return;
}
ShopUserInvite sourceInviteUser = shopUserInviteService.getOneByShopIdAndShopUserId(shopId, sourceShopUserInfo.getId());
if (sourceInviteUser.getDistributionUserId() == null) {
return;
}
log.info("开始分销, 当前来源用户: {}, shopId: {}, 邀请人id: {}", sourceUserId, shopId, sourceInviteUser.getDistributionUserId());
MkDistributionUser distributionUser = getMkDistributionUserByIdAndShopId(sourceInviteUser.getDistributionUserId(), shopId);
deepReward(sourceShopUserInfo, null, config, distributionUser, amount, sourceId, type, orderNo, 1);
log.info("开始分销, 当前来源用户: {}, shopId: {}, 邀请人id: {}", sourceUserId, shopId, curUser.getParentUserId());
MkDistributionUser distributionUser = getMkDistributionUserByIdAndShopId(curUser.getParentUserId(), shopId);
MkDistributionLevelConfig level = levelConfigService.getById(distributionUser.getDistributionLevelId());
deepReward(curUser, level.getCommission(), config, distributionUser,
amount, sourceId, type, orderNo, 1);
if (curUser.getGradeUserId() != null) {
MkDistributionUser parentDis = getMkDistributionUserByIdAndShopId(curUser.getGradeUserId(), shopId);
MkDistributionLevelConfig parentDisLevel = levelConfigService.getById(parentDis.getDistributionLevelId());
deepReward(curUser, parentDisLevel.getCommission().subtract(level.getCommission()), config, parentDis,
amount, sourceId, type, orderNo, 2);
}
});
distributionDeliverService.save(deliver);
}

View File

@@ -1,25 +0,0 @@
package com.czg.service.market.service.impl;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.ShopUserInvite;
import com.czg.market.service.ShopUserInviteService;
import com.czg.service.market.mapper.ShopUserInviteMapper;
import org.springframework.stereotype.Service;
/**
* 邀请与上级关联表 服务层实现。
*
* @author ww
* @since 2025-11-06
*/
@Service
public class ShopUserInviteServiceImpl extends ServiceImpl<ShopUserInviteMapper, ShopUserInvite> implements ShopUserInviteService {
@Override
public ShopUserInvite getOneByShopIdAndShopUserId(Long shopId, Long shopUserId) {
return getOne(QueryWrapper.create()
.eq(ShopUserInvite::getShopId, shopId)
.eq(ShopUserInvite::getShopUserId, shopUserId));
}
}

View File

@@ -65,15 +65,13 @@
<select id="getOrderConsumeAmountByList" resultType="java.math.BigDecimal">
SELECT IFNULL(SUM(ord.pay_amount), 0) AS totalAmount
FROM tb_shop_user_invite invite
INNER JOIN tb_shop_user su on invite.shop_user_id = su.id
INNER JOIN tb_order_info ord ON su.user_id = ord.user_id
and invite.shop_id = #{shopId}
FROM tb_shop_user invite
INNER JOIN tb_order_info ord ON invite.user_id = ord.user_id
AND ord.shop_id = #{shopId}
AND ord.STATUS = 'done'
AND ord.pay_type NOT IN ('vip_pay', 'credit_pay')
AND ord.paid_time >= invite.invite_time
WHERE invite.distribution_user_id = #{inviterId}
WHERE invite.parent_user_id = #{inviterId}
</select>
<select id="getOrderConsumeAmountById" resultType="java.math.BigDecimal">

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czg.service.market.mapper.ShopUserInviteMapper">
</mapper>