seatonwan9
2025-08-28 1cda9be49d77c83bdab4cfea7e3558fd4064bdb1
src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java
@@ -42,7 +42,7 @@
    @Override
    public PageResult<PointsFlow> getPersonalPointsFlowPage(PointsFlowQueryDTO queryDTO) {
        if (queryDTO.getUserId() == null) {
        if (!StringUtils.hasText(queryDTO.getUserId())) {
            throw new BusinessException("用户ID不能为空");
        }
@@ -123,68 +123,118 @@
            throw new BusinessException("参数不能为空");
        }
        Long userId = addPointsFlowDTO.getUserId();
        Long unitId = addPointsFlowDTO.getUnitId();
        String userId = addPointsFlowDTO.getUserId();
        String providerId = addPointsFlowDTO.getProviderId();
        String unitId = addPointsFlowDTO.getUnitId();
        Integer ruleType = addPointsFlowDTO.getRuleType();
        String category = addPointsFlowDTO.getCategory();
        String ruleNameCode = addPointsFlowDTO.getRuleNameCode();
        Integer count = addPointsFlowDTO.getCount() != null ? addPointsFlowDTO.getCount() : 1;
        // 根据ruleType、ruleNameCode、category查询生效时间最新的积分规则
        PointsRule pointsRule = getLatestEffectiveRule(ruleType, ruleNameCode, category);
        if (pointsRule == null) {
            throw new BusinessException("积分规则不存在或未启用: ruleType=" + ruleType + ", ruleNameCode=" + ruleNameCode + ", category=" + category);
        boolean applyUser = StringUtils.hasText(userId) ;
        boolean applyProvider = StringUtils.hasText(providerId) ;;
        if (!applyUser && !applyProvider) {
            throw new BusinessException("userId 与 providerId 不能同时为空");
        }
        // 验证规则类型是否匹配
        if (!ruleType.equals(pointsRule.getRuleType())) {
            throw new BusinessException("规则类型不匹配,期望: " + pointsRule.getRuleType() + ",实际: " + ruleType);
        // 分别获取用户侧/提供者侧最新生效规则(按 points_winner 过滤:1用户、0提供者)
        PointsRule userRule = null;
        PointsRule providerRule = null;
        if (applyUser) {
            userRule = getLatestEffectiveRule(ruleType, ruleNameCode, category, 1);
            if (userRule == null) {
                throw new BusinessException("未找到用户侧生效规则: ruleType=" + ruleType + ", ruleNameCode=" + ruleNameCode + ", category=" + category + ", points_winner=1");
            }
            if (!ruleType.equals(userRule.getRuleType())) {
                throw new BusinessException("用户侧规则类型不匹配,期望: " + userRule.getRuleType() + ",实际: " + ruleType);
            }
        }
        if (applyProvider) {
            providerRule = getLatestEffectiveRule(ruleType, ruleNameCode, category, 0);
            if (providerRule == null) {
                throw new BusinessException("未找到提供者侧生效规则: ruleType=" + ruleType + ", ruleNameCode=" + ruleNameCode + ", category=" + category + ", points_winner=0");
            }
            if (!ruleType.equals(providerRule.getRuleType())) {
                throw new BusinessException("提供者侧规则类型不匹配,期望: " + providerRule.getRuleType() + ",实际: " + ruleType);
            }
        }
        // 计算积分值
        Integer basePoints = pointsRule.getPointsValue() != null ? pointsRule.getPointsValue() : 0;
        Integer totalPoints = basePoints * count;
        // 如果是消费类型,积分为负数
        if (ruleType == RuleTypeEnum.CONSUME.getCode()) { // 1表示消费类型
            totalPoints = -totalPoints;
        // 计算两侧积分变化(各自按规则计算)
        int userPointsChange = 0;
        int providerPointsChange = 0;
        if (applyUser) {
            int base = userRule.getPointsValue() != null ? userRule.getPointsValue() : 0;
            int amount = base * count;
            userPointsChange = (ruleType == RuleTypeEnum.CONSUME.getCode()) ? -Math.abs(amount) : Math.abs(amount);
        }
        if (applyProvider) {
            int base = providerRule.getPointsValue() != null ? providerRule.getPointsValue() : 0;
            int amount = base * count;
            providerPointsChange = (ruleType == RuleTypeEnum.CONSUME.getCode()) ? -Math.abs(amount) : Math.abs(amount);
        }
        // 检查每日积分上限
        if (pointsRule.getIsLimit() != null && pointsRule.getIsLimit() == 0) { // 0表示有每日上限
            checkDailyLimitByRule(userId, unitId, pointsRule, totalPoints);
        // 每日上限校验(分别校验)
        if (applyUser && userRule.getIsLimit() != null && userRule.getIsLimit() == 0) {
            checkDailyLimitByRule(userId, unitId, userRule, userPointsChange);
        }
        if (applyProvider && providerRule.getIsLimit() != null && providerRule.getIsLimit() == 0) {
            checkDailyLimitByRule(providerId, providerId, providerRule, providerPointsChange);
        }
        // 如果是扣积分操作,先检查余额是否足够
        if (totalPoints < 0) {
            checkBalanceSufficient(userId, unitId, Math.abs(totalPoints));
        // 余额校验(仅在扣减时)
        if (applyUser && userPointsChange < 0) {
            checkBalanceSufficient(userId, unitId, Math.abs(userPointsChange));
        }
        if (applyProvider && providerPointsChange < 0) {
            checkBalanceSufficient(providerId, providerId, Math.abs(providerPointsChange));
        }
        // 创建积分流水记录
        // 生成用户侧流水并更新账户
        if (applyUser && userPointsChange != 0) {
        PointsFlow pointsFlow = new PointsFlow();
        pointsFlow.setUserId(userId);
        pointsFlow.setUnitId(unitId);
        pointsFlow.setDataType(ruleType);
        pointsFlow.setDataCategory(addPointsFlowDTO.getCategory());
        pointsFlow.setPoints(totalPoints);
        pointsFlow.setName(addPointsFlowDTO.getDescription() != null ? addPointsFlowDTO.getDescription() : pointsRule.getRuleDescription());
            pointsFlow.setPoints(userPointsChange);
            pointsFlow.setName(addPointsFlowDTO.getDescription() != null ? addPointsFlowDTO.getDescription() : userRule.getRuleDescription());
        pointsFlow.setFlowTime(LocalDateTime.now());
        pointsFlow.setRlueId(pointsRule.getId());
            pointsFlow.setRlueId(userRule.getId());
        boolean saved = save(pointsFlow);
        if (!saved) {
            throw new BusinessException("保存积分流水失败");
        }
        // 更新用户积分账户
        updateUserPointsByRule(userId, unitId, totalPoints);
            updateUserPointsByRule(userId, unitId, userPointsChange);
        }
        // 生成提供者侧流水并更新账户
        if (applyProvider && providerPointsChange != 0) {
            PointsFlow providerFlow = new PointsFlow();
            providerFlow.setUserId(providerId);
            providerFlow.setUnitId(providerId);
            providerFlow.setDataType(ruleType);
            providerFlow.setDataCategory(addPointsFlowDTO.getCategory());
            providerFlow.setPoints(providerPointsChange);
            providerFlow.setName(addPointsFlowDTO.getDescription() != null ? addPointsFlowDTO.getDescription() : providerRule.getRuleDescription());
            providerFlow.setFlowTime(LocalDateTime.now());
            providerFlow.setRlueId(providerRule.getId());
            boolean providerSaved = save(providerFlow);
            if (!providerSaved) {
                throw new BusinessException("保存提供者积分流水失败");
            }
            updateProviderUnitPoints(providerId, providerPointsChange);
        }
        return true;
    }
    @Override
    public UserPoints getUserPointsTotal(Long userId) {
        if (userId == null) {
    public UserPoints getUserPointsTotal(String userId) {
        if (!StringUtils.hasText(userId)) {
            throw new BusinessException("用户ID不能为null");
        }
        
@@ -272,9 +322,25 @@
    }
    /**
     * 根据ruleType、ruleNameCode、category、points_winner查询生效时间最新的积分规则
     */
    private PointsRule getLatestEffectiveRule(Integer ruleType, String ruleNameCode, String category, Integer pointsWinner) {
        QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", 0)
               .eq("is_enabled", 0)
               .eq("rule_type", ruleType)
               .eq("rule_name_code", ruleNameCode)
               .eq("data_category", category)
               .eq(pointsWinner != null, "points_winner", pointsWinner)
               .orderByDesc("created_at")
               .last("LIMIT 1");
        return pointsRuleService.getOne(wrapper);
    }
    /**
     * 检查每日积分上限(基于规则)
     */
    private void checkDailyLimitByRule(Long userId, Long unitId, PointsRule pointsRule, Integer currentPoints) {
    private void checkDailyLimitByRule(String userId, String unitId, PointsRule pointsRule, Integer currentPoints) {
        // 获取今日开始和结束时间
        LocalDate today = LocalDate.now();
        LocalDateTime startOfDay = today.atStartOfDay();
@@ -315,7 +381,7 @@
    /**
     * 检查积分余额是否足够
     */
    private void checkBalanceSufficient(Long userId, Long unitId, Integer requiredPoints) {
    private void checkBalanceSufficient(String userId, String unitId, Integer requiredPoints) {
        // 检查个人积分余额
        QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>();
        userWrapper.eq("deleted", 0)
@@ -371,7 +437,7 @@
    /**
     * 更新用户积分
     */
    private void updateUserPoints(Long userId, Long unitId, Integer pointsValue) {
    private void updateUserPoints(String userId, String unitId, Integer pointsValue) {
        // 更新个人积分
        QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>();
        userWrapper.eq("deleted", 0)
@@ -412,7 +478,7 @@
    /**
     * 根据规则更新用户积分账户
     */
    private void updateUserPointsByRule(Long userId, Long unitId, Integer pointsValue) {
    private void updateUserPointsByRule(String userId, String unitId, Integer pointsValue) {
        // 更新个人积分账户
        QueryWrapper<UserPoints> userWrapper = new QueryWrapper<>();
        userWrapper.eq("deleted", 0)
@@ -499,4 +565,46 @@
            userPointsMapper.updateById(unitPoints);
        }
    }
    /**
     * 仅更新提供者(单位)积分账户
     */
    private void updateProviderUnitPoints(String providerUnitId, Integer pointsValue) {
        QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>();
        unitWrapper.eq("deleted", 0)
                  .eq("unit_id", providerUnitId);
        UserPoints unitPoints = userPointsMapper.selectOne(unitWrapper);
        if (unitPoints == null) {
            if (pointsValue < 0) {
                throw new BusinessException("提供者积分余额不足,无法扣除积分");
            }
            unitPoints = new UserPoints();
            unitPoints.setUserId(null);
            unitPoints.setUnitId(providerUnitId);
            unitPoints.setBalance(pointsValue);
            unitPoints.setTotalEarned(pointsValue > 0 ? pointsValue : 0);
            unitPoints.setTotalConsumed(pointsValue < 0 ? Math.abs(pointsValue) : 0);
            userPointsMapper.insert(unitPoints);
        } else {
            if (pointsValue < 0 && unitPoints.getBalance() + pointsValue < 0) {
                throw new BusinessException("提供者积分余额不足,当前余额: " + unitPoints.getBalance() + ",需要扣除: " + Math.abs(pointsValue));
            }
            unitPoints.setBalance(unitPoints.getBalance() + pointsValue);
            if (pointsValue > 0) {
                unitPoints.setTotalEarned(unitPoints.getTotalEarned() != null ?
                        unitPoints.getTotalEarned() + pointsValue : pointsValue);
            }
            if (pointsValue < 0) {
                unitPoints.setTotalConsumed(unitPoints.getTotalConsumed() != null ?
                        unitPoints.getTotalConsumed() + Math.abs(pointsValue) : Math.abs(pointsValue));
            }
            unitPoints.setUpdateTime(LocalDateTime.now());
            userPointsMapper.updateById(unitPoints);
        }
    }
}