diff --git a/cash-api/market-server/src/main/java/com/czg/task/DistributionTask.java b/cash-api/market-server/src/main/java/com/czg/task/DistributionTask.java new file mode 100644 index 00000000..d1ba61e0 --- /dev/null +++ b/cash-api/market-server/src/main/java/com/czg/task/DistributionTask.java @@ -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); + }); + }); + } + + +} diff --git a/cash-common/cash-common-service/src/main/java/com/czg/market/dto/MkDistributionDeliverDTO.java b/cash-common/cash-common-service/src/main/java/com/czg/market/dto/MkDistributionDeliverDTO.java new file mode 100644 index 00000000..82b85dfc --- /dev/null +++ b/cash-common/cash-common-service/src/main/java/com/czg/market/dto/MkDistributionDeliverDTO.java @@ -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; + +} diff --git a/cash-common/cash-common-service/src/main/java/com/czg/market/entity/MkDistributionDeliver.java b/cash-common/cash-common-service/src/main/java/com/czg/market/entity/MkDistributionDeliver.java new file mode 100644 index 00000000..b06e3aef --- /dev/null +++ b/cash-common/cash-common-service/src/main/java/com/czg/market/entity/MkDistributionDeliver.java @@ -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; + +} diff --git a/cash-common/cash-common-service/src/main/java/com/czg/market/entity/MkDistributionFlow.java b/cash-common/cash-common-service/src/main/java/com/czg/market/entity/MkDistributionFlow.java index e5a3bccf..0a44abb7 100644 --- a/cash-common/cash-common-service/src/main/java/com/czg/market/entity/MkDistributionFlow.java +++ b/cash-common/cash-common-service/src/main/java/com/czg/market/entity/MkDistributionFlow.java @@ -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; } diff --git a/cash-common/cash-common-service/src/main/java/com/czg/market/service/MkDistributionDeliverService.java b/cash-common/cash-common-service/src/main/java/com/czg/market/service/MkDistributionDeliverService.java new file mode 100644 index 00000000..3409ac97 --- /dev/null +++ b/cash-common/cash-common-service/src/main/java/com/czg/market/service/MkDistributionDeliverService.java @@ -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 { + +} diff --git a/cash-common/cash-common-service/src/main/java/com/czg/market/service/MkDistributionUserService.java b/cash-common/cash-common-service/src/main/java/com/czg/market/service/MkDistributionUserService.java index 3e2bcd18..9a085b0b 100644 --- a/cash-common/cash-common-service/src/main/java/com/czg/market/service/MkDistributionUserService.java +++ b/cash-common/cash-common-service/src/main/java/com/czg/market/service/MkDistributionUserService.java @@ -123,7 +123,7 @@ public interface MkDistributionUserService extends IService * @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); diff --git a/cash-service/market-service/src/main/java/com/czg/service/market/mapper/MkDistributionDeliverMapper.java b/cash-service/market-service/src/main/java/com/czg/service/market/mapper/MkDistributionDeliverMapper.java new file mode 100644 index 00000000..6a12ed70 --- /dev/null +++ b/cash-service/market-service/src/main/java/com/czg/service/market/mapper/MkDistributionDeliverMapper.java @@ -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 { + +} diff --git a/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/MkDistributionDeliverServiceImpl.java b/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/MkDistributionDeliverServiceImpl.java new file mode 100644 index 00000000..e162db92 --- /dev/null +++ b/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/MkDistributionDeliverServiceImpl.java @@ -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 implements MkDistributionDeliverService{ + +} diff --git a/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/MkDistributionUserServiceImpl.java b/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/MkDistributionUserServiceImpl.java index e7b665c5..9da38f4c 100644 --- a/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/MkDistributionUserServiceImpl.java +++ b/cash-service/market-service/src/main/java/com/czg/service/market/service/impl/MkDistributionUserServiceImpl.java @@ -84,6 +84,8 @@ public class MkDistributionUserServiceImpl extends ServiceImpl 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 { 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 + + + + diff --git a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoServiceImpl.java b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoServiceImpl.java index c0496d8b..df6085d9 100644 --- a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoServiceImpl.java +++ b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoServiceImpl.java @@ -1066,7 +1066,7 @@ public class OrderInfoServiceImpl extends ServiceImpl