分销关系

This commit is contained in:
2026-01-31 14:37:03 +08:00
parent 77334e3e73
commit 9c5b8ea53f
9 changed files with 203 additions and 79 deletions

View File

@@ -1,6 +1,5 @@
package com.czg.account.entity;
import com.alibaba.fastjson2.annotation.JSONField;
import com.czg.utils.CzgRandomUtils;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
@@ -152,22 +151,6 @@ public class ShopUser implements Serializable {
private LocalDateTime nextDeliverTime;
// 是否分销员
private String distributionShops;
/**
* 上级分销员id
*/
private Long parentUserId;
/**
* 上上级分销员id
*/
private Long gradeUserId;
/**
* 一级分销收入
*/
private BigDecimal oneIncome;
/**
* 二级分销收入
*/
private BigDecimal twoIncome;
private String memberCircleName;
private Integer memberCircleReward;
@@ -179,8 +162,6 @@ public class ShopUser implements Serializable {
private Integer discount;
@Column(ignore = true)
private Integer isMemberPrice;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime inviteTime;
/**
* 添加或更新分销店铺若shopId已存在无论后缀则替换为新的id_后缀否则插入

View File

@@ -0,0 +1,75 @@
package com.czg.market.entity;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.io.Serial;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 全民股东邀请关系 实体类。
*
* @author ww
* @since 2026-01-31
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("mk_distribution_invite")
public class MkDistributionInvite implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id(keyType = KeyType.Auto)
private Long id;
/**
* 店铺Id
*/
private Long shopId;
/**
* 用户Id
*/
private Long userId;
/**
* 用户Id
*/
private Long shopUserId;
/**
* 邀请人ID mk_distribution_user.id
*/
private Long parentUserId;
/**
* 邀请人上级ID mk_distribution_user.id
*/
private Long gradeUserId;
/**
* 一级分润
*/
private BigDecimal oneIncome;
/**
* 二级分润
*/
private BigDecimal twoIncome;
/**
* 邀请时间
*/
private LocalDateTime inviteTime;
}

View File

@@ -0,0 +1,15 @@
package com.czg.market.service;
import com.mybatisflex.core.service.IService;
import com.czg.market.entity.MkDistributionInvite;
/**
* 全民股东邀请关系 服务层。
*
* @author ww
* @since 2026-01-31
*/
public interface MkDistributionInviteService extends IService<MkDistributionInvite> {
MkDistributionInvite getByShopIdAndShopUserId(Long shopId, Long shopUserId);
}

View File

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

View File

@@ -0,0 +1,14 @@
package com.czg.service.market.mapper;
import com.mybatisflex.core.BaseMapper;
import com.czg.market.entity.MkDistributionInvite;
/**
* 全民股东邀请关系 映射层。
*
* @author ww
* @since 2026-01-31
*/
public interface MkDistributionInviteMapper extends BaseMapper<MkDistributionInvite> {
}

View File

@@ -0,0 +1,22 @@
package com.czg.service.market.service.impl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkDistributionInvite;
import com.czg.market.service.MkDistributionInviteService;
import com.czg.service.market.mapper.MkDistributionInviteMapper;
import org.springframework.stereotype.Service;
/**
* 全民股东邀请关系 服务层实现。
*
* @author ww
* @since 2026-01-31
*/
@Service
public class MkDistributionInviteServiceImpl extends ServiceImpl<MkDistributionInviteMapper, MkDistributionInvite> implements MkDistributionInviteService{
@Override
public MkDistributionInvite getByShopIdAndShopUserId(Long shopId, Long shopUserId) {
return getOne(query().eq(MkDistributionInvite::getShopId, shopId).eq(MkDistributionInvite::getShopUserId, shopUserId));
}
}

View File

@@ -76,6 +76,8 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
@Resource
private MkDistributionDeliverService distributionDeliverService;
@Resource
private MkDistributionInviteService distributionInviteService;
@Resource
private AppWxServiceImpl appWxService;
@DubboReference
private ShopUserService shopUserService;
@@ -148,9 +150,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());
if (shopUser.getParentUserId() != null) {
MkDistributionUser mkDistributionUser = getMkDistributionUserByIdAndShopId(shopUser.getParentUserId(), shopId);
MkDistributionInvite invite = distributionInviteService.getByShopIdAndShopUserId(shopId, shopUser.getUserId());
if (invite != null && invite.getParentUserId() != null) {
MkDistributionUser mkDistributionUser = getMkDistributionUserByIdAndShopId(invite.getParentUserId(), shopId);
AssertUtil.isNull(mkDistributionUser, "上级分销员不存在");
ShopUser shopUserParent = shopUserService.getById(mkDistributionUser.getId());
result.put("parentName", shopUserParent.getNickName());
@@ -234,8 +236,9 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
public void bindInviteUser(MkDistributionUserDTO param) throws CzgException, ValidateException {
ShopUser shopUser = shopUserService.getById(param.getId());
AssertUtil.isNull(shopUser, "店铺用户不存在");
if (shopUser.getParentUserId() != null) {
throw new CzgException("店铺用户已绑定分销员");
MkDistributionInvite shopUserInvite = distributionInviteService.getByShopIdAndShopUserId(param.getShopId(), shopUser.getId());
if (shopUserInvite != null && shopUserInvite.getParentUserId() != null) {
throw new CzgException("店铺用户已存在上级");
}
MkDistributionUser parent = getOne(QueryWrapper.create().eq(MkDistributionUser::getInviteCode, param.getInviteCode()));
AssertUtil.isNull(parent, "邀请人不存在");
@@ -250,28 +253,28 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
}
ShopUser parentShopUser = shopUserService.getById(parent.getId());
AssertUtil.isNull(parentShopUser, "邀请人不存在");
MkDistributionInvite parentShopUserInvite = distributionInviteService.getByShopIdAndShopUserId(param.getShopId(), parentShopUser.getId());
if (parentShopUserInvite != null && parentShopUserInvite.getParentUserId() != null && parentShopUserInvite.getParentUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
if (parentShopUserInvite != null && parentShopUserInvite.getGradeUserId() != null && parentShopUserInvite.getGradeUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
if (parentShopUser.getParentUserId() != null && parentShopUser.getParentUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
if (parentShopUser.getGradeUserId() != null && parentShopUser.getGradeUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
//更新自己的上级
shopUser.setParentUserId(parentShopUser.getId());
shopUser.setGradeUserId(parentShopUser.getParentUserId());
//更新自己的下级 的上级的上级 为自己的上级
ShopUser upShopUser1 = new ShopUser();
upShopUser1.setParentUserId(parentShopUser.getId());
upShopUser1.setGradeUserId(parentShopUser.getParentUserId());
shopUserService.update(upShopUser1, QueryWrapper.create().eq(ShopUser::getId, shopUser.getId()));
if (shopUser.getParentUserId() != null) {
MkDistributionInvite newShopUserInvite = new MkDistributionInvite();
newShopUserInvite.setShopId(param.getShopId());
newShopUserInvite.setUserId(shopUser.getUserId());
newShopUserInvite.setShopUserId(shopUser.getId());
newShopUserInvite.setParentUserId(parentShopUser.getId());
newShopUserInvite.setGradeUserId(parentShopUserInvite == null ? null : parentShopUserInvite.getParentUserId());
newShopUserInvite.setInviteTime(LocalDateTime.now());
distributionInviteService.save(newShopUserInvite);
if (parentShopUserInvite != null && parentShopUserInvite.getParentUserId() != null) {
//更新自己的下级 的上级的上级 为自己的上级
ShopUser upShopUser = new ShopUser();
upShopUser.setGradeUserId(shopUser.getParentUserId());
shopUserService.update(upShopUser, QueryWrapper.create().eq(ShopUser::getParentUserId, shopUser.getId()));
MkDistributionInvite childShopUserInvite = new MkDistributionInvite();
childShopUserInvite.setGradeUserId(parentShopUser.getId());
distributionInviteService.update(childShopUserInvite, QueryWrapper.create().eq(MkDistributionInvite::getParentUserId, shopUser.getId()));
}
MkDistributionUser newDistributionUser = new MkDistributionUser();
@@ -320,8 +323,9 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
public void bindInviteUser(Long fromUserId, Long toUserId, Long shopId) throws CzgException, ValidateException {
ShopUser shopUser = shopUserService.getById(fromUserId);
AssertUtil.isNull(shopUser, "店铺用户不存在");
if (shopUser.getParentUserId() != null) {
throw new CzgException("店铺用户已绑定上级");
MkDistributionInvite shopUserInvite = distributionInviteService.getByShopIdAndShopUserId(shopId, shopUser.getId());
if (shopUserInvite != null && shopUserInvite.getParentUserId() != null) {
throw new CzgException("店铺用户已存在上级");
}
MkDistributionUser parent = getOne(QueryWrapper.create().eq(MkDistributionUser::getId, toUserId));
AssertUtil.isNull(parent, "邀请人不存在");
@@ -337,27 +341,28 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
ShopUser parentShopUser = shopUserService.getById(parent.getId());
AssertUtil.isNull(parentShopUser, "邀请人不存在");
if (parentShopUser.getParentUserId() != null && parentShopUser.getParentUserId().equals(shopUser.getId())) {
MkDistributionInvite parentShopUserInvite = distributionInviteService.getByShopIdAndShopUserId(shopId, parentShopUser.getId());
if (parentShopUserInvite != null && parentShopUserInvite.getParentUserId() != null && parentShopUserInvite.getParentUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
if (parentShopUser.getGradeUserId() != null && parentShopUser.getGradeUserId().equals(shopUser.getId())) {
if (parentShopUserInvite != null && parentShopUserInvite.getGradeUserId() != null && parentShopUserInvite.getGradeUserId().equals(shopUser.getId())) {
throw new CzgException("存在绑定关系,不可绑定");
}
//更新自己的上级
shopUser.setParentUserId(parentShopUser.getId());
shopUser.setGradeUserId(parentShopUser.getParentUserId());
//更新自己的下级 的上级的上级 为自己的上级
ShopUser upShopUser1 = new ShopUser();
upShopUser1.setParentUserId(parentShopUser.getId());
upShopUser1.setGradeUserId(parentShopUser.getParentUserId());
shopUserService.update(upShopUser1, QueryWrapper.create().eq(ShopUser::getId, shopUser.getId()));
if (shopUser.getParentUserId() != null) {
MkDistributionInvite newShopUserInvite = new MkDistributionInvite();
newShopUserInvite.setShopId(shopId);
newShopUserInvite.setUserId(shopUser.getUserId());
newShopUserInvite.setShopUserId(shopUser.getId());
newShopUserInvite.setParentUserId(parentShopUser.getId());
newShopUserInvite.setGradeUserId(parentShopUserInvite == null ? null : parentShopUserInvite.getParentUserId());
newShopUserInvite.setInviteTime(LocalDateTime.now());
distributionInviteService.save(newShopUserInvite);
if (parentShopUserInvite != null && parentShopUserInvite.getParentUserId() != null) {
//更新自己的下级 的上级的上级 为自己的上级
ShopUser upShopUser = new ShopUser();
upShopUser.setGradeUserId(shopUser.getParentUserId());
shopUserService.update(upShopUser, QueryWrapper.create().eq(ShopUser::getParentUserId, shopUser.getId()));
MkDistributionInvite childShopUserInvite = new MkDistributionInvite();
childShopUserInvite.setGradeUserId(parentShopUser.getId());
distributionInviteService.update(childShopUserInvite, QueryWrapper.create().eq(MkDistributionInvite::getParentUserId, shopUser.getId()));
}
MkDistributionUser newDistributionUser = new MkDistributionUser();
@@ -405,8 +410,9 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
public void costUpgradeLevelBefore(Long userId, Long shopId) {
ShopUser shopUser = shopUserService.getShopUserInfo(shopId, userId);
costUpgradeLevel(shopUser.getId(), shopId);
if (shopUser.getParentUserId() != null) {
costUpgradeLevel(shopUser.getParentUserId(), shopId);
MkDistributionInvite shopUserInvite = distributionInviteService.getByShopIdAndShopUserId(shopId, shopUser.getId());
if (shopUserInvite != null && shopUserInvite.getParentUserId() != null) {
costUpgradeLevel(shopUserInvite.getParentUserId(), shopId);
}
}
@@ -773,18 +779,19 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
AssertUtil.isTrue(config.getIsEnable() != 1, "分销未开启");
// 产生消费的用户
ShopUser curUser = shopUserService.getShopUserInfo(shopId, sourceUserId);
if (curUser == null || curUser.getParentUserId() == null) {
MkDistributionInvite sourceInviteUser = distributionInviteService.getByShopIdAndShopUserId(shopId, curUser.getId());
if (sourceInviteUser.getParentUserId() == null) {
return;
}
log.info("开始分销, 当前来源用户: {}, shopId: {}, 邀请人id: {}", sourceUserId, shopId, curUser.getParentUserId());
log.info("开始分销, 当前来源用户: {}, shopId: {}, 邀请人id: {}", sourceUserId, shopId, sourceInviteUser.getParentUserId());
MkDistributionUser distributionUser = getMkDistributionUserByIdAndShopId(curUser.getParentUserId(), shopId);
MkDistributionUser distributionUser = getMkDistributionUserByIdAndShopId(sourceInviteUser.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);
if (sourceInviteUser.getGradeUserId() != null) {
MkDistributionUser parentDis = getMkDistributionUserByIdAndShopId(sourceInviteUser.getGradeUserId(), shopId);
MkDistributionLevelConfig parentDisLevel = levelConfigService.getById(parentDis.getDistributionLevelId());
deepReward(curUser, parentDisLevel.getCommission().subtract(level.getCommission()), config, parentDis,
amount, sourceId, type, orderNo, 2);

View File

@@ -0,0 +1,7 @@
<?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.MkDistributionInviteMapper">
</mapper>

View File

@@ -65,8 +65,10 @@
<select id="getOrderConsumeAmountByList" resultType="java.math.BigDecimal">
SELECT IFNULL(SUM(ord.pay_amount), 0) AS totalAmount
FROM tb_shop_user invite
INNER JOIN tb_order_info ord ON invite.user_id = ord.user_id
FROM mk_distribution_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}
AND ord.shop_id = #{shopId}
AND ord.STATUS = 'done'
AND ord.pay_type NOT IN ('vip_pay', 'credit_pay')