Merge remote-tracking branch 'origin/test' into test

This commit is contained in:
wangw 2025-10-30 09:37:57 +08:00
commit 26ef4f20e7
12 changed files with 345 additions and 34 deletions

View File

@ -12,6 +12,18 @@ public class AAMarketTasks {
//生日有礼奖励发放
@Resource
private BirthdayGiftTask birthdayGiftTask;
@Resource
private DistributionTask distributionTask;
// 分销延时发放
//每天0时0分0秒 执行
@Scheduled(fixedRate = 30000)
public void distributionTask() {
distributionTask.deliver();
}
//每天0时0分0秒 执行
@Scheduled(cron = "0 0 8 * * ?")
public void birthdayGiftTask() {

View File

@ -0,0 +1,58 @@
package com.czg.task;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.ShopUserService;
import com.czg.market.entity.MkDistributionDeliver;
import com.czg.market.service.MkBirthdayGiftService;
import com.czg.market.service.MkDistributionDeliverService;
import com.czg.market.service.MkDistributionUserService;
import com.czg.order.entity.OrderInfo;
import com.czg.order.service.OrderInfoService;
import com.czg.utils.FunUtils;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
/**
* 分销定时任务
*
* @author ww
* @description
*/
@Slf4j
@Component
public class DistributionTask {
@Resource
private MkDistributionDeliverService distributionDeliverService;
@Resource
private MkDistributionUserService distributionUserService;
@DubboReference
private OrderInfoService orderInfoService;
/**
* 生日有礼奖励发放
* AAMarketTasks 统一调用位置
*/
// @Scheduled(cron = "0 0 0 * * ?")
public void deliver() {
distributionDeliverService.list(new QueryWrapper().eq(MkDistributionDeliver::getStatus, "pending")).forEach(item -> {
FunUtils.safeRunVoid(() -> {
log.info("开始处理延时分账, orderNo: {}, 类型: {}", 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);
});
});
}
}

View File

@ -0,0 +1,78 @@
package com.czg.market.dto;
import java.io.Serializable;
import java.time.LocalDateTime;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.experimental.Accessors;
import java.io.Serial;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 分账延时发放表 实体类
*
* @author ww
* @since 2025-10-30
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MkDistributionDeliverDTO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private Long id;
/**
* 订单id
*/
private Long sourceId;
/**
* 订单编号
*/
private String orderNo;
/**
* 店铺id
*/
private Long shopId;
/**
* order订单recharge充值
*/
private String type;
/**
* 创建时间
*/
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
/**
* 修改时间
*/
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
/**
* 应该发放时间
*/
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime shouldDeliverTime;
/**
* pending待发放 success成功发放 fail发放失败
*/
private String ststua;
/**
* 失败原因
*/
private String msg;
}

View File

@ -0,0 +1,85 @@
package com.czg.market.entity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.io.Serial;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
/**
* 分账延时发放表 实体类
*
* @author ww
* @since 2025-10-30
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("mk_distribution_deliver")
@Accessors(chain = true)
public class MkDistributionDeliver implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id(keyType = KeyType.Auto)
private Long id;
/**
* 订单id
*/
private Long sourceId;
/**
* 订单编号
*/
private String orderNo;
/**
* 店铺id
*/
private Long shopId;
/**
* order订单recharge充值
*/
private String type;
/**
* 创建时间
*/
@Column(onInsertValue = "now()")
private LocalDateTime createTime;
/**
* 修改时间
*/
@Column(onInsertValue = "now()", onUpdateValue = "now()")
private LocalDateTime updateTime;
/**
* 应该发放时间
*/
private LocalDateTime shouldDeliverTime;
/**
* pending待发放 success成功发放 fail发放失败
*/
private String status;
/**
* 失败原因
*/
private String msg;
}

View File

@ -109,7 +109,7 @@ public class MkDistributionFlow implements Serializable {
private Long sourceShopUserId;
private String nickName;
private Long sourceDistributionUserId;
private LocalDateTime deliverTime;
private BigDecimal commission;
private BigDecimal parentCommission;
}

View File

@ -0,0 +1,14 @@
package com.czg.market.service;
import com.mybatisflex.core.service.IService;
import com.czg.market.entity.MkDistributionDeliver;
/**
* 分账延时发放表 服务层
*
* @author ww
* @since 2025-10-30
*/
public interface MkDistributionDeliverService extends IService<MkDistributionDeliver> {
}

View File

@ -123,7 +123,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);
void distribute(Long sourceId, String orderNo, BigDecimal amount, Long userId, Long shopId, String type, boolean isTask);
void updateIncome(BigDecimal pendingIncome, BigDecimal receivedIncome, BigDecimal withdrawIncome, Long id, Long userId, Long shopId, Integer isOne);

View File

@ -0,0 +1,14 @@
package com.czg.service.market.mapper;
import com.mybatisflex.core.BaseMapper;
import com.czg.market.entity.MkDistributionDeliver;
/**
* 分账延时发放表 映射层
*
* @author ww
* @since 2025-10-30
*/
public interface MkDistributionDeliverMapper extends BaseMapper<MkDistributionDeliver> {
}

View File

@ -0,0 +1,18 @@
package com.czg.service.market.service.impl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkDistributionDeliver;
import com.czg.market.service.MkDistributionDeliverService;
import com.czg.service.market.mapper.MkDistributionDeliverMapper;
import org.springframework.stereotype.Service;
/**
* 分账延时发放表 服务层实现
*
* @author ww
* @since 2025-10-30
*/
@Service
public class MkDistributionDeliverServiceImpl extends ServiceImpl<MkDistributionDeliverMapper, MkDistributionDeliver> implements MkDistributionDeliverService{
}

View File

@ -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

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czg.service.market.mapper.MkDistributionDeliverMapper">
</mapper>

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");
distributionUserService.distribute(orderInfo.getId(), payment.getOrderNo(), payment.getAmount(), orderInfo.getUserId(), orderInfo.getShopId(), "order",false);
} else if ("memberIn".equals(payment.getPayType()) || "free".equals(payment.getPayType())) {
boolean isFree = "free".equals(payment.getPayType());
ShopUser shopUser = shopUserService.getById(payment.getSourceId());
@ -1153,12 +1153,12 @@ 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");
}
// 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());
memberConfigService.joinMember(payment.getShopId(), shopUser.getUserId(), payment.getRelatedId());