分销问题

会员充值问题
This commit is contained in:
2025-12-26 10:37:32 +08:00
parent 8bde6b15f6
commit 70307e3833
7 changed files with 89 additions and 52 deletions

View File

@@ -6,6 +6,7 @@ import org.springframework.stereotype.Component;
/**
* market服务 任务总调度
*
* @author ww
*/
@Component
@@ -18,9 +19,9 @@ public class AAMarketTasks {
// 分销延时发放
@Scheduled(fixedRate = 30000)
@Scheduled(cron = "0 0 0/2 * * ? ")
public void distributionTask() {
distributionTask.deliver();
distributionTask.deliver(null);
}
@@ -29,6 +30,7 @@ public class AAMarketTasks {
public void birthdayGiftTask() {
birthdayGiftTask.deliver();
}
//会员生日弹窗提醒重置 每年1月1日
@Scheduled(cron = "0 0 0 1 1 ?")
public void birthdayGiftRemindTask() {
@@ -39,6 +41,7 @@ public class AAMarketTasks {
//优惠券 过期
@Resource
private CouponTask couponTask;
//每天每小时的30分 0秒 执行
@Scheduled(cron = "0 30 * * * ? ")
public void couponTask() {
@@ -48,6 +51,7 @@ public class AAMarketTasks {
//会员奖励发放
@Resource
private MemberTask memberTask;
//每天1点 0分 0秒 执行
@Scheduled(cron = "0 0 1 * * ? ")
public void memberTask() {
@@ -57,6 +61,7 @@ public class AAMarketTasks {
//满减活动/限时折扣 处理任务状态 定时任务
@Resource
private ActivityStatusTask activityStatusTask;
//每天0点 0分 1秒 执行
@Scheduled(cron = "1 0 0 * * ? ")
public void activityStatusTask() {
@@ -67,6 +72,7 @@ public class AAMarketTasks {
//月累计 发送条数 累计金额
@Resource
private SmsShopMoneyTask smsShopMoneyTask;
//每月1号 0点 0分 1秒 执行
@Scheduled(cron = "1 0 0 1 * ?")
public void smsShopMoneyTask() {

View File

@@ -2,10 +2,13 @@ package com.czg.task;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
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.constants.SystemConstants;
import com.czg.exception.CzgException;
import com.czg.market.entity.MkDistributionFlow;
import com.czg.market.service.MkDistributionFlowService;
import com.czg.market.service.MkDistributionUserService;
@@ -18,6 +21,7 @@ import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@@ -40,8 +44,6 @@ public class DistributionTask {
private OrderInfoService orderInfoService;
@DubboReference
private ShopInfoService shopInfoService;
@DubboReference
private ShopUserService shopUserService;
List<String> list = List.of(OrderStatusEnums.REFUND.getCode(), OrderStatusEnums.PART_REFUND.getCode());
@@ -51,44 +53,55 @@ public class DistributionTask {
*/
// @Scheduled(cron = "0 0 0 * * ?")
// @Scheduled(fixedRate = 30000)
public void deliver() {
// 1. 订单完成支付时(判断是否分销)产生流水记录。
// 2. 判断入账时间。
// 3. 如果是 0 天,再去判断商户余额是否足够。够则入账,不足则不管。
// 4. 流水增加应该入账的时间(订单产生时带入)
// 5. 定时任务 应该是一天执行一次。查询待入账状态和应入账时间小于当前时间的记录,循环处理:并且判断商户余额是否足够,余额不足忽略处理;余额足够变为已入账并扣除商户余额。
// 6. 订单产生退款时,去流水表查询该订单的流水记录,如果未入账改为已入账,并插入一条退款扣钱的流水。
// shopInfo 查余额>0
// 循环 shopId 查询 如果金额不足 终止
//
LocalDateTime localDateTime = DateUtil.date().toLocalDateTime();
distributionFlowService.list(new QueryWrapper()
.eq(MkDistributionFlow::getStatus, TableValueConstant.DistributionFlow.Status.PENDING.getCode()).le(MkDistributionFlow::getDeliverTime, localDateTime)).forEach(item -> {
FunUtils.safeRunVoid(() -> {
log.info("开始处理延时分账, id: {}, orderNo: {}, 类型: {}", item.getId(), item.getOrderNo(), item.getType());
OrderInfo orderInfo = orderInfoService.getOne(new QueryWrapper().eq(OrderInfo::getOrderNo, item.getOrderNo()));
if (orderInfo == null) {
item.setStatus(TableValueConstant.DistributionFlow.Status.FAIL.getCode());
distributionFlowService.updateById(item);
log.warn("订单不存在, 订单号: {}", item.getOrderNo());
return;
public void deliver(Long shopId) {
LocalDateTime now = LocalDateTime.now();
List<ShopInfo> shopInfos = shopInfoService.list(QueryWrapper.create()
.eq(ShopInfo::getIsDeleted, SystemConstants.OneZero.ZERO)
.isNotNull(ShopInfo::getExpireTime)
.lt(ShopInfo::getExpireTime, now)
.gt(ShopInfo::getAmount, BigDecimal.ZERO)
.eq(ShopInfo::getId, shopId)
);
if (CollUtil.isEmpty(shopInfos)) {
log.info("分销延时分账 无符合条件的店铺,无需处理分账");
return;
}
for (ShopInfo shopInfo : shopInfos) {
boolean breakCurrentShopFlow = false;
List<MkDistributionFlow> flowList = distributionFlowService.list(new QueryWrapper()
.eq(MkDistributionFlow::getShopId, shopInfo.getId())
.eq(MkDistributionFlow::getStatus, TableValueConstant.DistributionFlow.Status.PENDING.getCode())
.le(MkDistributionFlow::getDeliverTime, now)
.orderBy(MkDistributionFlow::getId, true)
);
for (MkDistributionFlow item : flowList) {
if (breakCurrentShopFlow) {
break;
}
if (list.contains(orderInfo.getStatus())) {
log.warn("订单已退款, 订单号: {}", item.getOrderNo());
distributionUserService.refund(orderInfo.getId(), orderInfo.getOrderNo());
} else {
item.setStatus(TableValueConstant.DistributionFlow.Status.SUCCESS.getCode());
ShopUser shopUser = shopUserService.getById(item.getDistributionUserId());
distributionUserService.updateShopInfoAmount(orderInfo.getShopId(), item.getRewardAmount().negate(), orderInfo.getId(), TableValueConstant.DistributionAmountFlow.Type.SUB, "分销扣减");
distributionUserService.updateIncome(item.getRewardAmount().negate(), item.getRewardAmount(), BigDecimal.ZERO,
item.getDistributionUserId(), shopUser.getUserId(), item.getShopUserId(), item.getShopId(), item.getLevel());
distributionFlowService.updateById(item);
try {
log.info("分销延时分账, id: {}, orderNo: {}, 类型: {}", item.getId(), item.getOrderNo(), item.getType());
OrderInfo orderInfo = orderInfoService.getOne(new QueryWrapper().eq(OrderInfo::getOrderNo, item.getOrderNo()));
if (orderInfo == null) {
item.setStatus(TableValueConstant.DistributionFlow.Status.FAIL.getCode());
distributionFlowService.updateById(item);
log.warn("分销延时分账。订单不存在, 订单号: {}", item.getOrderNo());
continue;
}
if (list.contains(orderInfo.getStatus())) {
log.warn("分销延时分账。订单已退款, 订单号: {}", item.getOrderNo());
distributionUserService.refund(orderInfo.getId(), orderInfo.getOrderNo());
} else {
item.setStatus(TableValueConstant.DistributionFlow.Status.SUCCESS.getCode());
distributionUserService.distributionUserAmount(item, orderInfo);
}
} catch (CzgException e) {
log.error("店铺{}:{}分销延时分账异常:{}", shopInfo.getId(), shopInfo.getShopName(), e.getMessage());
breakCurrentShopFlow = true;
} catch (Exception e) {
log.error("店铺{}:{}分销延时分账异常", shopInfo.getId(), shopInfo.getShopName(), e);
breakCurrentShopFlow = true;
}
});
});
}
}
}
}