回调分销实现

This commit is contained in:
张松
2025-10-27 16:28:14 +08:00
parent bf083f698e
commit b73ca2c6e7
3 changed files with 84 additions and 55 deletions

View File

@@ -191,6 +191,67 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
}
private void deepReward(MkDistributionConfigVO config, ShopUser sourceShopUser, BigDecimal amount, Long sourceId, String type, String orderNo, Integer currentLevel) {
MkDistributionUser distributionUser = getOne(new QueryWrapper().eq(MkDistributionUser::getId, sourceShopUser.getId()));
AssertUtil.isNull(distributionUser, "分销员不存在");
AssertUtil.isTrue(distributionUser.getStatus() != 1, "分销员未开启");
// 上上级分销员
if (distributionUser.getParentId() != null) {
MkDistributionUser parent = getOne(new QueryWrapper().eq(MkDistributionUser::getId, distributionUser.getParentId()));
if (parent == null) {
return;
}
try {
deepReward(config, shopUserService.getById(parent.getShopUserId()), amount, sourceId, type, orderNo, ++currentLevel);
}catch (Exception e) {
log.warn("分销奖励失败: {}", e.getMessage());
}
}
MkDistributionLevelConfig level = levelConfigService.getById(distributionUser.getDistributionLevelId());
AssertUtil.isNull(level, "分销等级不存在");
if (config.getRewardCount() != null && config.getRewardCount() > 0) {
long count = distributionFlowService.count(new QueryWrapper().eq(MkDistributionFlow::getSourceShopUserId, sourceShopUser.getId())
.eq(MkDistributionFlow::getShopUserId, distributionUser.getShopUserId()));
if (count >= config.getRewardCount()) {
log.info("分销员{}已达到奖励次数上限, 次数: {}", distributionUser.getShopUserId(), config.getRewardCount());
return;
}
}
// 店铺信息
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));
}
boolean flag = true;
BigDecimal finalAmount = BigDecimal.ZERO;
try {
shopInfoService.updateAmount(distributionUser.getShopId(), rewardAmount.negate());
}catch (Exception e) {
flag = false;
}
MkDistributionFlow mkDistributionFlow = new MkDistributionFlow().setShopUserId(distributionUser.getShopUserId())
.setShopId(distributionUser.getShopId()).setDistributionUserId(distributionUser.getId())
.setLevelId(distributionUser.getDistributionLevelId()).setLevel(level.getLevel()).setSourceNickName(sourceShopUser.getNickName()).setOrderNo(orderNo)
.setSourceId(sourceId).setAmount(amount).setType(type).setStatus(flag ? TableValueConstant.DistributionFlow.Status.SUCCESS.getCode() :
TableValueConstant.DistributionFlow.Status.PENDING.getCode())
.setRewardAmount(rewardAmount);
distributionFlowService.save(mkDistributionFlow);
if (flag) {
distributionAmountFlowService.save(new MkDistributionAmountFlow()
.setType(TableValueConstant.DistributionAmountFlow.Type.SELF_RECHARGE.getCode())
.setShopId(config.getShopId()).setAmount(finalAmount).setChangeAmount(amount).setSourceId(mkDistributionFlow.getId())
.setRemark("自助充值").setOpAccount(StpKit.USER.getAccount()));
}
}
@Override
public void distribute(Long sourceId, String orderNo, BigDecimal amount, Long sourceUserId, Long shopId, String type) {
@@ -204,54 +265,8 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
if (sourceShopUserInfo.getDistributionUserId() == null) {
return;
}
MkDistributionUser distributionUser = getOne(new QueryWrapper().eq(MkDistributionUser::getId, sourceShopUserInfo));
AssertUtil.isNull(distributionUser, "分销员不存在");
AssertUtil.isTrue(distributionUser.getStatus() != 1, "分销员未开启");
// 上上级分销员
if (distributionUser.getParentId() != null) {
}
MkDistributionLevelConfig level = levelConfigService.getById(distributionUser.getDistributionLevelId());
AssertUtil.isNull(level, "分销等级不存在");
if (config.getRewardCount() != null && config.getRewardCount() > 0) {
long count = distributionFlowService.count(new QueryWrapper().eq(MkDistributionFlow::getSourceShopUserId, sourceUserId)
.eq(MkDistributionFlow::getShopUserId, distributionUser.getShopUserId()));
if (count >= config.getRewardCount()) {
log.info("分销员{}已达到奖励次数上限, 次数: {}", distributionUser.getShopUserId(), config.getRewardCount());
return;
}
}
// 店铺信息
ShopInfo shopInfo = shopInfoService.getById(shopId);
BigDecimal oneAmount = amount.multiply(level.getLevelOneCommission().divide(BigDecimal.valueOf(100), RoundingMode.DOWN));
boolean flag = true;
BigDecimal finalAmount = BigDecimal.ZERO;
try {
shopInfoService.updateAmount(shopId, oneAmount.negate());
}catch (Exception e) {
flag = false;
}
MkDistributionFlow mkDistributionFlow = new MkDistributionFlow().setShopUserId(distributionUser.getShopUserId()).setShopId(distributionUser.getShopId()).setDistributionUserId(distributionUser.getId())
.setLevelId(distributionUser.getDistributionLevelId()).setLevel(level.getLevel()).setSourceNickName(sourceShopUserInfo.getNickName()).setOrderNo(orderNo)
.setSourceId(sourceId).setAmount(amount).setType(type).setStatus(flag ? TableValueConstant.DistributionFlow.Status.SUCCESS.getCode() :
TableValueConstant.DistributionFlow.Status.PENDING.getCode())
.setRewardAmount(oneAmount);
distributionFlowService.save(mkDistributionFlow);
if (flag) {
distributionAmountFlowService.save(new MkDistributionAmountFlow()
.setType(TableValueConstant.DistributionAmountFlow.Type.SELF_RECHARGE.getCode())
.setShopId(shopId).setAmount(finalAmount).setChangeAmount(amount).setSourceId(mkDistributionFlow.getId())
.setRemark("自助充值").setOpAccount(StpKit.USER.getAccount()));
}
deepReward(config, sourceShopUserInfo, amount, sourceId, type, orderNo, 1);
}, "分销方法执行失败");
}
}