p-honggang.li
2025-08-18 1b1248f500a49f11f12c3cf9b469c300430b4514
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.webmanage.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.webmanage.common.BusinessException;
import com.webmanage.dto.PointsRuleDTO;
 
import com.webmanage.emun.RuleTypeEnum;
import com.webmanage.entity.Points;
import com.webmanage.entity.PointsRule;
import com.webmanage.mapper.PointsRuleMapper;
import com.webmanage.service.PointsRuleService;
import com.webmanage.service.PointsService;
 
import com.webmanage.vo.PointsRuleResultVO;
import com.webmanage.vo.PointsRuleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.beans.Transient;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 积分规则Service实现类
 */
@Slf4j
@Service
public class PointsRuleServiceImpl extends ServiceImpl<PointsRuleMapper, PointsRule> implements PointsRuleService {
    
    @Autowired
    private PointsService pointsService;
 
 
    @Override
    public PointsRuleResultVO getPointsRuleList(Long ruleId) {
 
        QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
 
        wrapper.eq("deleted", 0).eq("is_enabled",0);
 
        if(ruleId != null){
        wrapper.eq("points_id", ruleId);
        }
 
        List<PointsRule> list = list(wrapper);
        PointsRuleResultVO pointsRuleResultVO = new PointsRuleResultVO();
        if (!CollectionUtil.isEmpty(list)) {
            Map<Integer, List<PointsRule>> collect = list.stream().collect(Collectors.groupingBy(PointsRule::getRuleType));
 
            Map<String, List<PointsRule>> getRules = collect.get(RuleTypeEnum.GET.getCode()).stream().collect(Collectors.groupingBy(PointsRule::getCategory));
            Map<String, List<PointsRule>> consumeRules = collect.get(RuleTypeEnum.CONSUME.getCode()).stream().collect(Collectors.groupingBy(PointsRule::getCategory));
            List<PointsRuleVO> getPointsRuleVOList = new ArrayList<>();
            List<PointsRuleVO> consumePointsRuleVOList = new ArrayList<>();
            getRules.forEach((k,v)->{
                PointsRuleVO pointsRuleVO = new PointsRuleVO();
                pointsRuleVO.setRlueSort(v.get(0).getRuleOrder());
                pointsRuleVO.setCategory(k);
                pointsRuleVO.setPointsRules(v);
                getPointsRuleVOList.add(pointsRuleVO);
 
            });
            consumeRules.forEach((k,v)->{
                PointsRuleVO pointsRuleVO = new PointsRuleVO();
                pointsRuleVO.setRlueSort(v.get(0).getRuleOrder());
                pointsRuleVO.setCategory(k);
                pointsRuleVO.setPointsRules(v);
                consumePointsRuleVOList.add(pointsRuleVO);
 
            });
            getPointsRuleVOList.sort(Comparator.comparing(PointsRuleVO::getRlueSort));
            consumePointsRuleVOList.sort(Comparator.comparing(PointsRuleVO::getRlueSort));
            pointsRuleResultVO.setGetPointsRuleList(getPointsRuleVOList);
            pointsRuleResultVO.setConsumePointsRuleList(consumePointsRuleVOList);
        }
 
        return pointsRuleResultVO;
    }
 
    @Override
    public boolean addPointsRule(PointsRuleDTO pointsRuleDTO) {
        // 验证规则名称是否重复
        QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
        wrapper.eq("rule_name", pointsRuleDTO.getRuleName())
               .eq("deleted", 0);
        
        if (count(wrapper) > 0) {
            throw new BusinessException("规则名称已存在");
        }
        
        PointsRule pointsRule = new PointsRule();
        BeanUtils.copyProperties(pointsRuleDTO, pointsRule);
        
        return save(pointsRule);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePointsRule(List<PointsRuleDTO> pointsRuleDTO) {
        if (CollectionUtils.isEmpty(pointsRuleDTO)){
            return false;
        }
 
        // 查询规则名称
         Points pointsOld = pointsService.query().eq("id", pointsRuleDTO.get(0).getPointsId())
                 .eq("deleted", 0).one();
        // 查询当天是否有版本更新记录
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY-MM-DD HH:mm:ss");
        LocalDateTime nowDatetime = LocalDateTime.now();
        LocalDateTime localDateTimeStart = nowDatetime.toLocalDate().atStartOfDay();
        LocalDateTime localDateTimeEnd = nowDatetime.toLocalDate().atStartOfDay();
        // dateTimeFormatter.format(localDateTimeStart), dateTimeFormatter.format(localDateTimeEnd)
        Long counts = pointsService.query().between("created_at", localDateTimeStart, localDateTimeEnd).count();
        // 创建新的积分主表记录
        Points points = new Points();
        points.setPointsName(pointsOld.getPointsName());
        points.setEffectiveStart(LocalDateTime.now());
        points.setModifierId(pointsOld.getModifierId()); // 这里应该从上下文中获取当前用户ID
        points.setModifierName("admin"); // 这里应该从上下文中获取当前用户名
        points.setVersion(counts>0 ? pointsOld.getVersion() + 0.1f: 1.0f);
        points.setStatus(0);
        pointsService.save(points);
        
        // 创建积分规则记录
        for (PointsRuleDTO ruleDTO : pointsRuleDTO) {
            PointsRule pointsRule = new PointsRule();
            BeanUtils.copyProperties(ruleDTO,pointsRule);
            pointsRule.setPointsId(points.getId());
            pointsRule.setUpdatedAt(LocalDateTime.now());
            save(pointsRule);
        }
 
        return true;
    }
 
    @Override
    public boolean deletePointsRule(Long id) {
        if (id == null) {
            throw new BusinessException("规则ID不能为空");
        }
        
        PointsRule pointsRule = getById(id);
        if (pointsRule == null) {
            throw new BusinessException("积分规则不存在");
        }
        
        return removeById(id);
    }
 
    @Override
    public boolean togglePointsRuleStatus(Long id, Boolean isEnabled) {
        if (id == null) {
            throw new BusinessException("规则ID不能为空");
        }
        
        PointsRule pointsRule = getById(id);
        if (pointsRule == null) {
            throw new BusinessException("积分规则不存在");
        }
        
        pointsRule.setIsEnabled(isEnabled ? 0: 1);
        pointsRule.setUpdatedAt(LocalDateTime.now());
        
        return updateById(pointsRule);
    }
 
    @Override
    public List<PointsRule> getEnabledRulesByType(String ruleType) {
        QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", 0)
               .eq("is_enabled", true)
               .eq("rule_type", ruleType)
               .orderByAsc("priority", "created_at");
        
        return list(wrapper);
    }
 
    @Override
    public PointsRule getRuleByTriggerCondition(String triggerCondition) {
        if (!StringUtils.hasText(triggerCondition)) {
            return null;
        }
        
        QueryWrapper<PointsRule> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", 0)
               .eq("is_enabled", true)
               .eq("trigger_condition", triggerCondition)
               .orderByAsc("priority")
               .last("LIMIT 1");
        
        return getOne(wrapper);
    }
}