|
|
|
|
@@ -84,6 +84,8 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
|
|
|
|
|
private MkDistributionFlowService distributionFlowService;
|
|
|
|
|
@Resource
|
|
|
|
|
private MkDistributionWithdrawFlowService withdrawFlowService;
|
|
|
|
|
@Resource
|
|
|
|
|
private MkDistributionDeliverService distributionDeliverService;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private AppWxServiceImpl appWxService;
|
|
|
|
|
@@ -556,37 +558,45 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void deepReward(ShopUser orderSourceShopUser, MkDistributionConfigVO config, ShopUser sourceShopUser, BigDecimal amount, Long sourceId, String type, String orderNo, Integer currentLevel) {
|
|
|
|
|
private void deepReward(ShopUser orderSourceShopUser, MkDistributionLevelConfig parentLevel, MkDistributionConfigVO config, MkDistributionUser currentDistributionUser, BigDecimal amount, Long sourceId, String type, String orderNo, Integer currentLevel) {
|
|
|
|
|
if (currentLevel > 2) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
MkDistributionUser distributionUser = getOne(new QueryWrapper().eq(MkDistributionUser::getId, sourceShopUser.getId()));
|
|
|
|
|
AssertUtil.isNull(distributionUser, "分销员不存在");
|
|
|
|
|
AssertUtil.isTrue(distributionUser.getStatus() != 1, "分销员未开启");
|
|
|
|
|
// 当前分销员
|
|
|
|
|
AssertUtil.isNull(currentDistributionUser, "分销员不存在");
|
|
|
|
|
AssertUtil.isTrue(currentDistributionUser.getStatus() != 1, "分销员未开启");
|
|
|
|
|
|
|
|
|
|
ShopUser currentShopUser = shopUserService.getById(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 (sourceShopUser.getDistributionUserId() != null) {
|
|
|
|
|
MkDistributionUser parent = getOne(new QueryWrapper().eq(MkDistributionUser::getId, sourceShopUser.getDistributionUserId()));
|
|
|
|
|
|
|
|
|
|
if (currentShopUser.getDistributionUserId() != null) {
|
|
|
|
|
MkDistributionUser parent = getOne(new QueryWrapper().eq(MkDistributionUser::getId, currentDistributionUser.getDistributionLevelId()));
|
|
|
|
|
if (parent == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
deepReward(orderSourceShopUser, config, shopUserService.getById(parent.getId()), amount, sourceId, type, orderNo, currentLevel + 1);
|
|
|
|
|
deepReward(orderSourceShopUser, level, config, parent, amount, sourceId, type, orderNo, currentLevel + 1);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.warn("分销奖励失败: {}", e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MkDistributionLevelConfig level = levelConfigService.getById(distributionUser.getDistributionLevelId());
|
|
|
|
|
AssertUtil.isNull(level, "分销等级不存在");
|
|
|
|
|
log.info("当前分销员: {}, 上级分销员: {}, 分销等级: {}", distributionUser.getId(), sourceShopUser.getDistributionUserId(), level.getId());
|
|
|
|
|
log.info("当前分销员: {}, 上级分销员: {}, 分销等级: {}", currentDistributionUser.getId(), currentShopUser.getDistributionUserId(), level.getId());
|
|
|
|
|
|
|
|
|
|
if (config.getRewardCount() != null && config.getRewardCount() > 0) {
|
|
|
|
|
long count = distributionFlowService.count(new QueryWrapper().eq(MkDistributionFlow::getSourceShopUserId, sourceShopUser.getId())
|
|
|
|
|
.eq(MkDistributionFlow::getShopUserId, distributionUser.getId()));
|
|
|
|
|
long count = distributionFlowService.count(new QueryWrapper().eq(MkDistributionFlow::getSourceShopUserId, currentDistributionUser.getId())
|
|
|
|
|
.eq(MkDistributionFlow::getShopUserId, currentDistributionUser.getId()));
|
|
|
|
|
if (count >= config.getRewardCount()) {
|
|
|
|
|
log.info("分销员{}已达到奖励次数上限, 次数: {}", distributionUser.getId(), config.getRewardCount());
|
|
|
|
|
log.info("分销员{}已达到奖励次数上限, 次数: {}", currentDistributionUser.getId(), config.getRewardCount());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -598,29 +608,29 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
|
|
|
|
|
// } else {
|
|
|
|
|
// rewardAmount = amount.multiply(level.getLevelTwoCommission().divide(BigDecimal.valueOf(100), RoundingMode.DOWN));
|
|
|
|
|
// }
|
|
|
|
|
rewardAmount = amount.multiply(level.getLevelOneCommission().divide(BigDecimal.valueOf(100), RoundingMode.DOWN));
|
|
|
|
|
rewardAmount = amount.multiply(finalCommission.divide(BigDecimal.valueOf(100), RoundingMode.DOWN));
|
|
|
|
|
|
|
|
|
|
boolean flag = true;
|
|
|
|
|
BigDecimal finalAmount = BigDecimal.ZERO;
|
|
|
|
|
try {
|
|
|
|
|
finalAmount = shopInfoService.updateAmount(distributionUser.getShopId(), rewardAmount.negate());
|
|
|
|
|
finalAmount = shopInfoService.updateAmount(currentDistributionUser.getShopId(), rewardAmount.negate());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
flag = false;
|
|
|
|
|
}
|
|
|
|
|
ShopUser shopUser = shopUserService.getById(distributionUser.getId());
|
|
|
|
|
ShopUser shopUser = shopUserService.getById(currentDistributionUser.getId());
|
|
|
|
|
|
|
|
|
|
MkDistributionFlow mkDistributionFlow = new MkDistributionFlow().setShopUserId(distributionUser.getId())
|
|
|
|
|
.setShopId(distributionUser.getShopId()).setDistributionUserId(distributionUser.getId())
|
|
|
|
|
MkDistributionFlow mkDistributionFlow = new MkDistributionFlow().setShopUserId(currentDistributionUser.getId())
|
|
|
|
|
.setShopId(currentDistributionUser.getShopId()).setDistributionUserId(currentDistributionUser.getId())
|
|
|
|
|
.setNickName(shopUser.getNickName()).setSourceShopUserId(orderSourceShopUser.getId()).setSourceNickName(orderSourceShopUser.getNickName())
|
|
|
|
|
.setSourceDistributionUserId(orderSourceShopUser.getId()).setDeliverTime(config.getSettlementDay() == 0 ? null : DateUtil.offsetDay(DateUtil.date(), config.getSettlementDay()).toLocalDateTime())
|
|
|
|
|
.setLevelId(distributionUser.getDistributionLevelId()).setLevel(currentLevel == 1 ? 1 : 2).setOrderNo(orderNo)
|
|
|
|
|
.setCommission(finalCommission).setParentCommission(parentLevel.getLevelOneCommission())
|
|
|
|
|
.setLevelId(currentDistributionUser.getDistributionLevelId()).setLevel(currentLevel == 1 ? 1 : 2).setOrderNo(orderNo)
|
|
|
|
|
.setSourceId(sourceId).setAmount(amount).setType(type).setStatus(flag ? TableValueConstant.DistributionFlow.Status.SUCCESS.getCode() :
|
|
|
|
|
TableValueConstant.DistributionFlow.Status.PENDING.getCode())
|
|
|
|
|
.setRewardAmount(rewardAmount).setBillNo(IdUtil.simpleUUID());
|
|
|
|
|
distributionFlowService.save(mkDistributionFlow);
|
|
|
|
|
|
|
|
|
|
updateIncome(!flag ? rewardAmount : BigDecimal.ZERO,
|
|
|
|
|
flag ? rewardAmount : BigDecimal.ZERO, BigDecimal.ZERO, distributionUser.getId(), shopUser.getUserId(), shopUser.getMainShopId(), currentLevel == 1 ? 1 : 2);
|
|
|
|
|
flag ? rewardAmount : BigDecimal.ZERO, BigDecimal.ZERO, currentDistributionUser.getId(), shopUser.getUserId(), shopUser.getMainShopId(), currentLevel == 1 ? 1 : 2);
|
|
|
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
|
distributionAmountFlowService.save(new MkDistributionAmountFlow()
|
|
|
|
|
@@ -632,12 +642,24 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void distribute(Long sourceId, String orderNo, BigDecimal amount, Long sourceUserId, Long shopId, String type) {
|
|
|
|
|
public void distribute(Long sourceId, String orderNo, BigDecimal amount, Long sourceUserId, Long shopId, String type, boolean isTask) {
|
|
|
|
|
MkDistributionDeliver deliver = new MkDistributionDeliver().setSourceId(sourceId).setOrderNo(orderNo).setShopId(shopId).setType(type).setStatus("pending");
|
|
|
|
|
FunUtils.safeRunVoid(() -> {
|
|
|
|
|
AssertUtil.isTrue(amount.compareTo(BigDecimal.ZERO) == 0, "分销金额不能为0");
|
|
|
|
|
MkDistributionConfigVO config = mkDistributionConfigService.detail(shopId);
|
|
|
|
|
AssertUtil.isTrue(config.getIsEnable() != 1, "分销未开启");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isTask) {
|
|
|
|
|
if (config.getSettlementDay() != null && config.getSettlementDay() != 0) {
|
|
|
|
|
deliver.setShouldDeliverTime(DateUtil.offsetDay(DateUtil.date(), config.getSettlementDay()).toLocalDateTime());
|
|
|
|
|
log.info("延时发放, {}", config.getSettlementDay());
|
|
|
|
|
return;
|
|
|
|
|
}else {
|
|
|
|
|
deliver.setStatus("success");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 当前用户上级分销员
|
|
|
|
|
ShopUser sourceShopUserInfo = shopUserService.getShopUserInfo(shopId, sourceUserId);
|
|
|
|
|
if (sourceShopUserInfo.getDistributionUserId() == null) {
|
|
|
|
|
@@ -645,9 +667,12 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
|
|
|
|
|
}
|
|
|
|
|
log.info("开始分销, 当前来源用户: {}, shopId: {}, 邀请人id: {}", sourceUserId, shopId, sourceShopUserInfo.getDistributionUserId());
|
|
|
|
|
|
|
|
|
|
ShopUser parentUserInfo = shopUserService.getById(sourceShopUserInfo.getDistributionUserId());
|
|
|
|
|
deepReward(sourceShopUserInfo, config, parentUserInfo, amount, sourceId, type, orderNo, 1);
|
|
|
|
|
MkDistributionUser distributionUser = getById(sourceShopUserInfo.getDistributionUserId());
|
|
|
|
|
deepReward(sourceShopUserInfo, null, config, distributionUser, amount, sourceId, type, orderNo, 1);
|
|
|
|
|
});
|
|
|
|
|
if (!isTask) {
|
|
|
|
|
distributionDeliverService.save(deliver);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|