运营端充值 分账修改

This commit is contained in:
张松 2025-10-30 10:23:19 +08:00
parent cb5f006c22
commit 96ecb86d42
6 changed files with 66 additions and 46 deletions

View File

@ -1,13 +1,22 @@
package com.czg.task;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import com.czg.account.entity.ShopInfo;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.ShopInfoService;
import com.czg.account.service.ShopUserService;
import com.czg.constant.TableValueConstant;
import com.czg.market.entity.MkDistributionDeliver;
import com.czg.market.entity.MkDistributionFlow;
import com.czg.market.service.MkBirthdayGiftService;
import com.czg.market.service.MkDistributionDeliverService;
import com.czg.market.service.MkDistributionFlowService;
import com.czg.market.service.MkDistributionUserService;
import com.czg.order.entity.OrderInfo;
import com.czg.order.service.OrderInfoService;
import com.czg.service.market.enums.OrderStatusEnums;
import com.czg.utils.FunUtils;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
@ -28,9 +37,13 @@ public class DistributionTask {
private MkDistributionDeliverService distributionDeliverService;
@Resource
private MkDistributionUserService distributionUserService;
@Resource
private MkDistributionFlowService distributionFlowService;
@DubboReference
private OrderInfoService orderInfoService;
@DubboReference
private ShopInfoService shopInfoService;
/**
* 生日有礼奖励发放
@ -38,18 +51,24 @@ public class DistributionTask {
*/
// @Scheduled(cron = "0 0 0 * * ?")
public void deliver() {
distributionDeliverService.list(new QueryWrapper().eq(MkDistributionDeliver::getStatus, "pending")).forEach(item -> {
distributionFlowService.list(new QueryWrapper().ge(MkDistributionFlow::getDeliverTime, DateUtil.date())
.eq(MkDistributionFlow::getStatus, TableValueConstant.DistributionFlow.Status.PENDING.getCode())).forEach(item -> {
FunUtils.safeRunVoid(() -> {
log.info("开始处理延时分账, orderNo: {}, 类型: {}", item.getOrderNo(), item.getType());
log.info("开始处理延时分账, id: {}, orderNo: {}, 类型: {}", item.getId(), item.getOrderNo(), item.getType());
OrderInfo orderInfo = orderInfoService.getOne(new QueryWrapper().eq(OrderInfo::getOrderNo, item.getOrderNo()));
if (orderInfo == null) {
log.warn("订单不存在, 订单号: {}", item.getOrderNo());
return;
}
distributionUserService.distribute(orderInfo.getId(), orderInfo.getOrderNo(), orderInfo.getPayAmount(), orderInfo.getUserId(), orderInfo.getShopId(), "order", true);
item.setStatus("success");
distributionDeliverService.updateById(item);
if (CollUtil.newArrayList(OrderStatusEnums.REFUND.getCode(), OrderStatusEnums.REFUNDING.getCode(), OrderStatusEnums.PART_REFUND.getCode()).contains(orderInfo.getStatus())) {
log.warn("订单已退款, 订单号: {}", item.getOrderNo());
item.setStatus(TableValueConstant.DistributionFlow.Status.REFUND.getCode());
}else {
item.setStatus(TableValueConstant.DistributionFlow.Status.SUCCESS.getCode());
distributionUserService.updateShopInfoAmount(orderInfo.getShopId(), item.getRewardAmount(), orderInfo.getId());
}
distributionFlowService.updateById(item);
});
});
}

View File

@ -112,4 +112,5 @@ public class MkDistributionFlow implements Serializable {
private BigDecimal commission;
private BigDecimal parentCommission;
private LocalDateTime deliverTime;
}

View File

@ -114,6 +114,8 @@ public interface MkDistributionUserService extends IService<MkDistributionUser>
*/
void rechargeCallBack(Long shopId, BigDecimal amount, Long paymentId);
BigDecimal updateShopInfoAmount(Long shopId, BigDecimal changeAmount, Long sourceId);
/**
* 发放分销奖励
*
@ -123,7 +125,7 @@ public interface MkDistributionUserService extends IService<MkDistributionUser>
* @param userId 用户id
* @param shopId 店铺id
*/
void distribute(Long sourceId, String orderNo, BigDecimal amount, Long userId, Long shopId, String type, boolean isTask);
void distribute(Long sourceId, String orderNo, BigDecimal amount, Long userId, Long shopId, String type);
void updateIncome(BigDecimal pendingIncome, BigDecimal receivedIncome, BigDecimal withdrawIncome, Long id, Long userId, Long shopId, Integer isOne);

View File

@ -85,6 +85,7 @@ public interface TableValueConstant {
enum Status {
PENDING("pending", "待入账"),
SUCCESS("success", "已入账"),
REFUND("REFUND", "已退款"),
FAIL("fail", "失败");
private final String code;
private final String msg;

View File

@ -554,7 +554,7 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
distributionAmountFlowService.save(new MkDistributionAmountFlow()
.setType(TableValueConstant.DistributionAmountFlow.Type.SELF_RECHARGE.getCode())
.setMainShopId(mainShopId).setShopId(shopId).setAmount(finalAmount).setChangeAmount(amount).setSourceId(paymentId)
.setRemark("自助充值").setOpAccount(StpKit.USER.getAccount()));
.setRemark("自助充值"));
}
@ -610,56 +610,52 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
// }
rewardAmount = amount.multiply(finalCommission.divide(BigDecimal.valueOf(100), RoundingMode.DOWN));
boolean flag = true;
BigDecimal finalAmount = BigDecimal.ZERO;
try {
finalAmount = shopInfoService.updateAmount(currentDistributionUser.getShopId(), rewardAmount.negate());
} catch (Exception e) {
flag = false;
}
ShopUser shopUser = shopUserService.getById(currentDistributionUser.getId());
// 延时发放时间
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())
.setNickName(shopUser.getNickName()).setSourceShopUserId(orderSourceShopUser.getId()).setSourceNickName(orderSourceShopUser.getNickName())
.setCommission(finalCommission).setParentCommission(parentLevel.getLevelOneCommission())
.setCommission(finalCommission).setParentCommission(parentLevel != null ? parentLevel.getLevelOneCommission() : null)
.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())
.setSourceId(sourceId).setAmount(amount).setType(type)
.setRewardAmount(rewardAmount).setBillNo(IdUtil.simpleUUID());
distributionFlowService.save(mkDistributionFlow);
updateIncome(!flag ? rewardAmount : BigDecimal.ZERO,
flag ? rewardAmount : BigDecimal.ZERO, BigDecimal.ZERO, currentDistributionUser.getId(), shopUser.getUserId(), shopUser.getMainShopId(), currentLevel == 1 ? 1 : 2);
if (delayTime == null) {
mkDistributionFlow.setStatus(TableValueConstant.DistributionFlow.Status.SUCCESS.getCode());
try {
updateShopInfoAmount(currentDistributionUser.getShopId(), rewardAmount, mkDistributionFlow.getId());
updateIncome(BigDecimal.ZERO, rewardAmount, BigDecimal.ZERO, currentDistributionUser.getId(), shopUser.getUserId(), shopUser.getMainShopId(), currentLevel == 1 ? 1 : 2);
} catch (Exception e) {
mkDistributionFlow.setStatus(TableValueConstant.DistributionFlow.Status.PENDING.getCode());
distributionFlowService.updateById(mkDistributionFlow);
}
}else {
updateIncome(rewardAmount, BigDecimal.ZERO, BigDecimal.ZERO, currentDistributionUser.getId(), shopUser.getUserId(), shopUser.getMainShopId(), currentLevel == 1 ? 1 : 2);
if (flag) {
distributionAmountFlowService.save(new MkDistributionAmountFlow()
.setType(TableValueConstant.DistributionAmountFlow.Type.SUB.getCode())
.setShopId(config.getShopId()).setAmount(finalAmount).setChangeAmount(rewardAmount).setSourceId(mkDistributionFlow.getId())
.setRemark("分账扣除"));
}
}
@Override
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");
public BigDecimal updateShopInfoAmount(Long shopId, BigDecimal changeAmount, Long sourceId) {
BigDecimal finalAmount = shopInfoService.updateAmount(shopId, changeAmount.negate());
distributionAmountFlowService.save(new MkDistributionAmountFlow()
.setType(TableValueConstant.DistributionAmountFlow.Type.SUB.getCode())
.setShopId(shopId).setAmount(finalAmount).setChangeAmount(changeAmount).setSourceId(sourceId)
.setRemark("分账扣除"));
return finalAmount;
}
@Override
public void distribute(Long sourceId, String orderNo, BigDecimal amount, Long sourceUserId, Long shopId, String type) {
MkDistributionDeliver deliver = new MkDistributionDeliver().setSourceId(sourceId).setOrderNo(orderNo).setShopId(shopId).setType(type).setStatus("success");
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) {
@ -670,15 +666,16 @@ public class MkDistributionUserServiceImpl extends ServiceImpl<MkDistributionUse
MkDistributionUser distributionUser = getById(sourceShopUserInfo.getDistributionUserId());
deepReward(sourceShopUserInfo, null, config, distributionUser, amount, sourceId, type, orderNo, 1);
});
if (!isTask) {
distributionDeliverService.save(deliver);
}
distributionDeliverService.save(deliver);
}
@Override
public void updateIncome(BigDecimal pendingIncome, BigDecimal receivedIncome, BigDecimal withdrawIncome, Long id, Long userId, Long mainShopId, Integer isOne) {
shopUserService.updateOneOrTwoAmount(userId, mainShopId, receivedIncome == null ? pendingIncome : receivedIncome, isOne);
// 用户收益信息
shopUserService.updateOneOrTwoAmount(userId, mainShopId, receivedIncome, isOne);
// 用户可提现余额
userInfoService.updateDistributionAmount(userId, receivedIncome);
//分销员余额
boolean flag = mapper.updateIncome(pendingIncome, receivedIncome, withdrawIncome, id);
if (!flag) {
throw new CzgException("操作失败");

View File

@ -1066,7 +1066,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
// 分销员升级等级
distributionUserService.costUpgradeLevelBefore(orderInfo.getUserId(), orderInfo.getShopId());
// 分销奖励
distributionUserService.distribute(orderInfo.getId(), payment.getOrderNo(), payment.getAmount(), orderInfo.getUserId(), orderInfo.getShopId(), "order",false);
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());