回调分销实现

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

@@ -61,12 +61,12 @@ public interface MkDistributionUserService extends IService<MkDistributionUser>
/**
* 发放分销奖励
* @param orderId 订单id
* @param sourceId 来源id
* @param orderNo 订单编号
* @param amount 金额
* @param userId 用户id
* @param shopId 店铺id
*/
void distribute(Long orderId, String orderNo, BigDecimal amount, Long userId, Long shopId, String type);
void distribute(Long sourceId, String orderNo, BigDecimal amount, Long userId, Long shopId, String type);
}

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);
}, "分销方法执行失败");
}
}

View File

@@ -100,6 +100,8 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
private RedisService redisService;
@Resource
private RabbitPublisher rabbitPublisher;
@Resource
private MkDistributionUserService distributionUserService;
@DubboReference
private MemberPointsService pointsService;
@DubboReference
@@ -136,8 +138,6 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
private MkShopRechargeService shopRechargeService;
@Resource
private MkConsumeCashbackService consumeCashbackService;
@Resource
private MkDistributionUserService distributionUserService;
// 延迟 5 秒
private static final long DELAY = 5;
//重试次数
@@ -1069,9 +1069,12 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
memberConfigService.joinMemberByCondition(orderInfo.getShopId(), orderInfo.getUserId());
// 会员消费赠送成长值
memberConfigService.deliver(orderInfo.getShopId(), orderInfo.getUserId(), TableValueConstant.MemberExpFlow.Type.COST, orderInfo.getPayAmount(), null, orderInfo.getId());
// 分销奖励
distributionUserService.distribute(orderInfo.getId(), payment.getOrderNo(), payment.getAmount(), orderInfo.getUserId(), orderInfo.getShopId(), "order");
} else if ("memberIn".equals(payment.getPayType()) || "free".equals(payment.getPayType())) {
boolean isFree = "free".equals(payment.getPayType());
ShopUser shopUser = shopUserService.getById(payment.getSourceId());
OrderInfo orderInfo = null;
if (shopUser == null) {
log.error("会员充值失败会员不存在会员id{}", payment.getSourceId());
} else {
@@ -1084,7 +1087,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
bizEnum = ShopUserFlowBizEnum.CASH_IN;
}
if (isFree) {
OrderInfo orderInfo = getOne(new QueryWrapper().eq(OrderInfo::getId, payment.getRelatedId()));
orderInfo = getOne(new QueryWrapper().eq(OrderInfo::getId, payment.getRelatedId()));
if (orderInfo == null) {
log.error("霸王餐支付订单不存在支付记录Id,{}", payment.getId());
} else {
@@ -1108,7 +1111,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
shopRechargeService.recharge(payment.getShopId(), payment.getSourceId(), payment.getRelatedId(),
BigDecimal.valueOf(czgCallBackDto.getAmount()).divide(BigDecimal.valueOf(100), 2, RoundingMode.DOWN), payment.getId(), payment.getPayType(), bizEnum);
//充值并支付 ↓
OrderInfo orderInfo = getOne(new QueryWrapper()
orderInfo = getOne(new QueryWrapper()
.eq(OrderInfo::getPayOrderId, payment.getId())
.eq(OrderInfo::getPayType, PayEnums.VIP_PAY.getValue()));
if (orderInfo != null) {
@@ -1131,12 +1134,13 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
TableValueConstant.MemberExpFlow.Type.RECHARGE,
BigDecimal.valueOf(czgCallBackDto.getAmount()).divide(BigDecimal.valueOf(100), 2, RoundingMode.DOWN), null, orderInfo.getId());
if (TransactionSynchronizationManager.isSynchronizationActive()) {
OrderInfo finalOrderInfo = orderInfo;
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
// 事务成功提交后执行消息发送
String printParam = orderInfo.getId() + "_" + (!"after-pay".equals(orderInfo.getPayMode()) ? 1 : 0) + "_1";
rabbitPublisher.sendOrderPrintMsg(printParam, orderInfo.getIsPrint() == 1);
String printParam = finalOrderInfo.getId() + "_" + (!"after-pay".equals(finalOrderInfo.getPayMode()) ? 1 : 0) + "_1";
rabbitPublisher.sendOrderPrintMsg(printParam, finalOrderInfo.getIsPrint() == 1);
// log.info("订单{}事务提交后,发送打印消息", orderId);
}
});
@@ -1148,6 +1152,16 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
}
}
}
}
// 分销奖励
if (shopUser != null) {
distributionUserService.distribute(payment.getId(), payment.getOrderNo(), payment.getAmount(), shopUser.getUserId(), payment.getShopId(), "recharge");
}
if (orderInfo != null) {
distributionUserService.distribute(payment.getId(), payment.getOrderNo(), payment.getAmount(), orderInfo.getUserId(), payment.getShopId(), "recharge");
}
} else if ("memberPay".equals(payment.getPayType())) {
ShopUser shopUser = shopUserService.getById(payment.getSourceId());