|
|
|
|
@@ -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);
|
|
|
|
|
}
|
|
|
|
|
|