seatonwan9
2025-08-24 5fd8f535ef44ef055d91673740491b9c9177aa89
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package com.webmanage.controller;
 
import com.webmanage.common.PageResult;
import com.webmanage.common.Result;
import com.webmanage.dto.*;
import com.webmanage.entity.Points;
import com.webmanage.entity.PointsRule;
import com.webmanage.entity.UserPoints;
import com.webmanage.service.PointsFlowService;
import com.webmanage.service.PointsRuleService;
import com.webmanage.service.PointsService;
import com.webmanage.vo.PointsRuleResultVO;
import com.webmanage.vo.PointsRuleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import com.webmanage.entity.PointsFlow;
 
/**
 * 积分管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/points")
@Api(tags = "积分管理")
@Validated
public class PointsController {
 
    @Resource
    private PointsRuleService pointsRuleService;
 
    @Resource
    private PointsFlowService pointsFlowService;
 
    @Resource
    private PointsService pointsService;
 
    // ==================== 积分主表管理 ====================
 
    @PostMapping("/main/page")
    @ApiOperation("分页查询积分主表")
    public Result<PageResult<Points>> getPointsMainPage(@Valid @RequestBody PointsMainQueryDTO queryDTO) {
        try {
            return Result.success(pointsService.getPointsMainPage(queryDTO));
        } catch (Exception e) {
            log.error("查询积分主表失败", e);
            return Result.error("查询积分主表失败:" + e.getMessage());
        }
    }
 
 
    // ==================== 积分规则管理 ====================
 
    @PostMapping("/rule/list")
    @ApiOperation("根据规则主表ID查询积分规则")
    public Result<PointsRuleResultVO> getPointsRuleListById(
                                          @RequestParam(required = true) @NotNull Long ruleId) {
        try {
            return Result.success(pointsRuleService.getPointsRuleList(ruleId));
        } catch (Exception e) {
            log.error("查询积分规则失败", e);
            return Result.error("查询积分规则失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/rule/add")
    @ApiOperation("新增积分规则")
    public Result<Object> addPointsRule(@Valid @RequestBody PointsRuleDTO pointsRuleDTO) {
        try {
            boolean result = pointsRuleService.addPointsRule(pointsRuleDTO);
            if (result) {
                return Result.success("新增积分规则成功");
            } else {
                return Result.error("新增积分规则失败");
            }
        } catch (Exception e) {
            log.error("新增积分规则失败", e);
            return Result.error("新增积分规则失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/rule/update")
    @ApiOperation("修改积分规则")
    public Result<Object> updatePointsRule(@Valid @RequestBody List<PointsRuleDTO> pointsRuleDTO) {
        try {
            boolean result = pointsRuleService.updatePointsRule(pointsRuleDTO);
            if (result) {
                return Result.success("修改积分规则成功");
            } else {
                return Result.error("修改积分规则失败");
            }
        } catch (Exception e) {
            log.error("修改积分规则失败", e);
            return Result.error("修改积分规则失败:" + e.getMessage());
        }
    }
 
    @DeleteMapping("/rule/{id}")
    @ApiOperation("删除积分规则")
    public Result<Object> deletePointsRule(@ApiParam("规则ID") @PathVariable Long id) {
        try {
            boolean result = pointsRuleService.deletePointsRule(id);
            if (result) {
                return Result.success("删除积分规则成功");
            } else {
                return Result.error("删除积分规则失败");
            }
        } catch (Exception e) {
            log.error("删除积分规则失败", e);
            return Result.error("删除积分规则失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/rule/toggle/{id}")
    @ApiOperation("启用/禁用积分规则")
    public Result<Object> togglePointsRuleStatus(@ApiParam("规则ID") @PathVariable Long id,
                                               @ApiParam("是否启用") @RequestParam @NotNull Boolean isEnabled) {
        try {
            boolean result = pointsRuleService.togglePointsRuleStatus(id, isEnabled);
            if (result) {
                return Result.success("操作成功");
            } else {
                return Result.error("操作失败");
            }
        } catch (Exception e) {
            log.error("操作积分规则状态失败", e);
            return Result.error("操作积分规则状态失败:" + e.getMessage());
        }
    }
 
    @GetMapping("/rule/type/{ruleType}")
    @ApiOperation("根据规则类型查询启用的规则")
    public Result<Object> getEnabledRulesByType(@ApiParam("规则类型") @PathVariable String ruleType) {
        try {
            List<PointsRule> rules = pointsRuleService.getEnabledRulesByType(ruleType);
            return Result.success(rules);
        } catch (Exception e) {
            log.error("查询积分规则失败", e);
            return Result.error("查询积分规则失败:" + e.getMessage());
        }
    }
 
    // ==================== 积分流水查询 ====================
 
    @PostMapping("/flow/personal/page")
    @ApiOperation("分页查询个人积分流水")
    public Result<Object> getPersonalPointsFlowPage(@Valid @RequestBody PointsFlowQueryDTO queryDTO) {
        try {
            return Result.success(pointsFlowService.getPersonalPointsFlowPage(queryDTO));
        } catch (Exception e) {
            log.error("查询个人积分流水失败", e);
            return Result.error("查询个人积分流水失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/flow/unit/page")
    @ApiOperation("分页查询单位积分流水")
    public Result<Object> getUnitPointsFlowPage(@Valid @RequestBody PointsFlowQueryDTO queryDTO) {
        try {
            return Result.success(pointsFlowService.getUnitPointsFlowPage(queryDTO));
        } catch (Exception e) {
            log.error("查询单位积分流水失败", e);
            return Result.error("查询单位积分流水失败:" + e.getMessage());
        }
    }
 
    @GetMapping("/flow/user/{userId}")
    @ApiOperation("根据用户ID查询积分流水")
    public Result<Object> getPointsFlowByUserId(@ApiParam("用户ID") @PathVariable Long userId) {
        try {
            List<PointsFlow> flows = pointsFlowService.getPointsFlowByUserId(userId);
            return Result.success(flows);
        } catch (Exception e) {
            log.error("查询用户积分流水失败", e);
            return Result.error("查询用户积分流水失败:" + e.getMessage());
        }
    }
 
    @GetMapping("/flow/unit/{unitId}")
    @ApiOperation("根据单位ID查询积分流水")
    public Result<Object> getPointsFlowByUnitId(@ApiParam("单位ID") @PathVariable Long unitId) {
        try {
            List<PointsFlow> flows = pointsFlowService.getPointsFlowByUnitId(unitId);
            return Result.success(flows);
        } catch (Exception e) {
            log.error("查询单位积分流水失败", e);
            return Result.error("查询单位积分流水失败:" + e.getMessage());
        }
    }
 
    // ==================== 新增积分流水 ====================
 
    @PostMapping("/flow/add")
    @ApiOperation("新增积分流水(根据规则自动计算)")
    public Result<Object> addPointsFlow(@Valid @RequestBody AddPointsFlowDTO addPointsFlowDTO) {
        try {
            boolean result = pointsFlowService.addPointsFlowByRule(addPointsFlowDTO);
            if (result) {
                return Result.success("新增积分流水成功");
            } else {
                return Result.error("新增积分流水失败");
            }
        } catch (Exception e) {
            log.error("新增积分流水失败", e);
            return Result.error("新增积分流水失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/flow/add/personal")
    @ApiOperation("新增个人积分流水")
    public Result<Object> addPersonalPointsFlow(@Valid @RequestBody AddPointsFlowDTO addPointsFlowDTO) {
        try {
            boolean result = pointsFlowService.addPointsFlowByRule(addPointsFlowDTO);
            if (result) {
                return Result.success("新增个人积分流水成功");
            } else {
                return Result.error("新增个人积分流水失败");
            }
        } catch (Exception e) {
            log.error("新增个人积分流水失败", e);
            return Result.error("新增个人积分流水失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/flow/add/unit")
    @ApiOperation("新增单位积分流水")
    public Result<Object> addUnitPointsFlow(@Valid @RequestBody AddPointsFlowDTO addPointsFlowDTO) {
        try {
            boolean result = pointsFlowService.addPointsFlowByRule(addPointsFlowDTO);
            if (result) {
                return Result.success("新增单位积分流水成功");
            } else {
                return Result.error("新增单位积分流水失败");
            }
        } catch (Exception e) {
            log.error("新增单位积分流水失败", e);
            return Result.error("新增单位积分流水失败:" + e.getMessage());
        }
    }
 
    // ==================== 积分统计 ====================
 
    @GetMapping("/total/user/{userId}")
    @ApiOperation("获取用户积分统计")
    public Result<UserPoints> getUserPointsTotal(@ApiParam("用户ID") @PathVariable Long userId) {
        try {
            UserPoints userPoints = pointsFlowService.getUserPointsTotal(userId);
            return Result.success(userPoints);
        } catch (Exception e) {
            log.error("获取用户积分统计失败", e);
            return Result.error("获取用户积分统计失败:" + e.getMessage());
        }
    }
 
    @GetMapping("/total/unit/{unitId}")
    @ApiOperation("获取单位积分统计")
    public Result<UserPoints> getUnitPointsTotal(@ApiParam("单位ID") @PathVariable Long unitId) {
        try {
            UserPoints total = pointsFlowService.getUnitPointsTotal(unitId);
            return Result.success(total);
        } catch (Exception e) {
            log.error("获取单位积分统计失败", e);
            return Result.error("获取单位积分统计失败:" + e.getMessage());
        }
    }
 
    // ==================== 积分流水数据类目 ====================
 
    @GetMapping("/flow/categories")
    @ApiOperation("获取积分流水数据类目列表")
    public Result<Object> getPointsFlowCategories() {
        try {
            List<String> categories = pointsFlowService.getPointsFlowCategories();
            return Result.success(categories);
        } catch (Exception e) {
            log.error("获取积分流水数据类目失败", e);
            return Result.error("获取积分流水数据类目失败:" + e.getMessage());
        }
    }
}