|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+package com.sundot.airport.web.controller.exam;
|
|
|
2
|
+
|
|
|
3
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
4
|
+import cn.hutool.core.util.StrUtil;
|
|
|
5
|
+import com.sundot.airport.common.annotation.Log;
|
|
|
6
|
+import com.sundot.airport.common.core.controller.BaseController;
|
|
|
7
|
+import com.sundot.airport.common.core.domain.AjaxResult;
|
|
|
8
|
+import com.sundot.airport.common.core.domain.entity.SysUser;
|
|
|
9
|
+import com.sundot.airport.common.core.page.TableDataInfo;
|
|
|
10
|
+import com.sundot.airport.common.enums.BusinessType;
|
|
|
11
|
+import com.sundot.airport.common.exception.ServiceException;
|
|
|
12
|
+import com.sundot.airport.common.utils.poi.ExcelUtil;
|
|
|
13
|
+import com.sundot.airport.exam.domain.ExamMonthlyScore;
|
|
|
14
|
+import com.sundot.airport.exam.dto.ExamMonthlyScoreImportVO;
|
|
|
15
|
+import com.sundot.airport.exam.service.IExamMonthlyScoreService;
|
|
|
16
|
+import com.sundot.airport.system.service.ISysUserService;
|
|
|
17
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
18
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
19
|
+import org.springframework.web.bind.annotation.*;
|
|
|
20
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
21
|
+
|
|
|
22
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
23
|
+import java.util.List;
|
|
|
24
|
+
|
|
|
25
|
+/**
|
|
|
26
|
+ * 月考成绩Controller
|
|
|
27
|
+ *
|
|
|
28
|
+ * @author ruoyi
|
|
|
29
|
+ * @date 2026-05-06
|
|
|
30
|
+ */
|
|
|
31
|
+@RestController
|
|
|
32
|
+@RequestMapping("/exam/monthlyScore")
|
|
|
33
|
+public class ExamMonthlyScoreController extends BaseController {
|
|
|
34
|
+ @Autowired
|
|
|
35
|
+ private IExamMonthlyScoreService examMonthlyScoreService;
|
|
|
36
|
+
|
|
|
37
|
+ @Autowired
|
|
|
38
|
+ private ISysUserService userService;
|
|
|
39
|
+
|
|
|
40
|
+ /**
|
|
|
41
|
+ * 查询月考成绩列表
|
|
|
42
|
+ */
|
|
|
43
|
+ @PreAuthorize("@ss.hasPermi('exam:monthlyScore:list')")
|
|
|
44
|
+ @GetMapping("/list")
|
|
|
45
|
+ public TableDataInfo list(ExamMonthlyScore examMonthlyScore) {
|
|
|
46
|
+ startPage();
|
|
|
47
|
+ List<ExamMonthlyScore> list = examMonthlyScoreService.selectExamMonthlyScoreList(examMonthlyScore);
|
|
|
48
|
+ return getDataTable(list);
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ /**
|
|
|
52
|
+ * 导出月考成绩列表
|
|
|
53
|
+ */
|
|
|
54
|
+ @PreAuthorize("@ss.hasPermi('exam:monthlyScore:export')")
|
|
|
55
|
+ @Log(title = "月考成绩", businessType = BusinessType.EXPORT)
|
|
|
56
|
+ @PostMapping("/export")
|
|
|
57
|
+ public void export(HttpServletResponse response, ExamMonthlyScore examMonthlyScore) {
|
|
|
58
|
+ List<ExamMonthlyScore> list = examMonthlyScoreService.selectExamMonthlyScoreList(examMonthlyScore);
|
|
|
59
|
+ ExcelUtil<ExamMonthlyScore> util = new ExcelUtil<ExamMonthlyScore>(ExamMonthlyScore.class);
|
|
|
60
|
+ util.exportExcel(response, list, "月考成绩数据");
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
|
63
|
+ /**
|
|
|
64
|
+ * 获取月考成绩详细信息
|
|
|
65
|
+ */
|
|
|
66
|
+ @PreAuthorize("@ss.hasPermi('exam:monthlyScore:query')")
|
|
|
67
|
+ @GetMapping(value = "/{id}")
|
|
|
68
|
+ public AjaxResult getInfo(@PathVariable("id") Long id) {
|
|
|
69
|
+ return success(examMonthlyScoreService.selectExamMonthlyScoreById(id));
|
|
|
70
|
+ }
|
|
|
71
|
+
|
|
|
72
|
+ /**
|
|
|
73
|
+ * 新增月考成绩
|
|
|
74
|
+ */
|
|
|
75
|
+ @PreAuthorize("@ss.hasPermi('exam:monthlyScore:add')")
|
|
|
76
|
+ @Log(title = "月考成绩", businessType = BusinessType.INSERT)
|
|
|
77
|
+ @PostMapping
|
|
|
78
|
+ public AjaxResult add(@RequestBody ExamMonthlyScore examMonthlyScore) {
|
|
|
79
|
+ return toAjax(examMonthlyScoreService.insertExamMonthlyScore(examMonthlyScore));
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ /**
|
|
|
83
|
+ * 修改月考成绩
|
|
|
84
|
+ */
|
|
|
85
|
+ @PreAuthorize("@ss.hasPermi('exam:monthlyScore:edit')")
|
|
|
86
|
+ @Log(title = "月考成绩", businessType = BusinessType.UPDATE)
|
|
|
87
|
+ @PutMapping
|
|
|
88
|
+ public AjaxResult edit(@RequestBody ExamMonthlyScore examMonthlyScore) {
|
|
|
89
|
+ return toAjax(examMonthlyScoreService.updateExamMonthlyScore(examMonthlyScore));
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ /**
|
|
|
93
|
+ * 删除月考成绩
|
|
|
94
|
+ */
|
|
|
95
|
+ @PreAuthorize("@ss.hasPermi('exam:monthlyScore:remove')")
|
|
|
96
|
+ @Log(title = "月考成绩", businessType = BusinessType.DELETE)
|
|
|
97
|
+ @DeleteMapping("/{ids}")
|
|
|
98
|
+ public AjaxResult remove(@PathVariable Long[] ids) {
|
|
|
99
|
+ return toAjax(examMonthlyScoreService.deleteExamMonthlyScoreByIds(ids));
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ /**
|
|
|
103
|
+ * 下载导入模板
|
|
|
104
|
+ */
|
|
|
105
|
+ @PostMapping("/importTemplate")
|
|
|
106
|
+ public void importTemplate(HttpServletResponse response) {
|
|
|
107
|
+ ExcelUtil<ExamMonthlyScoreImportVO> util = new ExcelUtil<ExamMonthlyScoreImportVO>(ExamMonthlyScoreImportVO.class);
|
|
|
108
|
+ util.importTemplateExcel(response, "月考成绩数据");
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ /**
|
|
|
112
|
+ * 导入月考成绩数据
|
|
|
113
|
+ */
|
|
|
114
|
+ @Log(title = "月考成绩", businessType = BusinessType.IMPORT)
|
|
|
115
|
+ @PreAuthorize("@ss.hasPermi('exam:monthlyScore:import')")
|
|
|
116
|
+ @PostMapping("/importData")
|
|
|
117
|
+ public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
|
|
|
118
|
+ ExcelUtil<ExamMonthlyScoreImportVO> util = new ExcelUtil<ExamMonthlyScoreImportVO>(ExamMonthlyScoreImportVO.class);
|
|
|
119
|
+ List<ExamMonthlyScoreImportVO> list = util.importExcel(file.getInputStream());
|
|
|
120
|
+ if (CollectionUtil.isEmpty(list)) {
|
|
|
121
|
+ throw new ServiceException("导入月考成绩数据不能为空");
|
|
|
122
|
+ }
|
|
|
123
|
+ String message = importData(list, updateSupport);
|
|
|
124
|
+ return success(message);
|
|
|
125
|
+ }
|
|
|
126
|
+
|
|
|
127
|
+ /**
|
|
|
128
|
+ * 导入数据处理
|
|
|
129
|
+ */
|
|
|
130
|
+ private String importData(List<ExamMonthlyScoreImportVO> list, boolean updateSupport) {
|
|
|
131
|
+ if (CollectionUtil.isEmpty(list)) {
|
|
|
132
|
+ throw new ServiceException("导入月考成绩数据不能为空!");
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ int successNum = 0;
|
|
|
136
|
+ int failureNum = 0;
|
|
|
137
|
+ StringBuilder successMsg = new StringBuilder();
|
|
|
138
|
+ StringBuilder failureMsg = new StringBuilder();
|
|
|
139
|
+
|
|
|
140
|
+ // 1. 预处理:提取所有学生姓名,批量查询用户
|
|
|
141
|
+ List<String> studentNames = list.stream()
|
|
|
142
|
+ .map(ExamMonthlyScoreImportVO::getStudentName)
|
|
|
143
|
+ .filter(StrUtil::isNotBlank)
|
|
|
144
|
+ .distinct()
|
|
|
145
|
+ .collect(java.util.stream.Collectors.toList());
|
|
|
146
|
+
|
|
|
147
|
+ // 批量查询用户(同时按用户名和昵称查询)
|
|
|
148
|
+ java.util.Map<String, SysUser> userMap = new java.util.HashMap<>();
|
|
|
149
|
+ if (CollectionUtil.isNotEmpty(studentNames)) {
|
|
|
150
|
+ List<SysUser> users = userService.selectUsersByNames(studentNames);
|
|
|
151
|
+ for (SysUser user : users) {
|
|
|
152
|
+ // 优先用用户名做key,如果没有则用昵称
|
|
|
153
|
+ if (StrUtil.isNotBlank(user.getUserName())) {
|
|
|
154
|
+ userMap.put(user.getUserName(), user);
|
|
|
155
|
+ }
|
|
|
156
|
+ if (StrUtil.isNotBlank(user.getNickName())) {
|
|
|
157
|
+ userMap.put(user.getNickName(), user);
|
|
|
158
|
+ }
|
|
|
159
|
+ }
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+ List<Long> userIds = userMap.values().stream()
|
|
|
163
|
+ .map(SysUser::getUserId)
|
|
|
164
|
+ .distinct()
|
|
|
165
|
+ .collect(java.util.stream.Collectors.toList());
|
|
|
166
|
+
|
|
|
167
|
+ java.util.Map<String, ExamMonthlyScore> existScoreMap = new java.util.HashMap<>();
|
|
|
168
|
+ if (CollectionUtil.isNotEmpty(userIds)) {
|
|
|
169
|
+ // 批量查询这些用户的月考成绩
|
|
|
170
|
+ List<ExamMonthlyScore> allScores = examMonthlyScoreService.selectByUserIds(userIds);
|
|
|
171
|
+ for (ExamMonthlyScore score : allScores) {
|
|
|
172
|
+ String key = score.getUserId() + "_" + score.getYearMonth();
|
|
|
173
|
+ existScoreMap.put(key, score);
|
|
|
174
|
+ }
|
|
|
175
|
+ }
|
|
|
176
|
+
|
|
|
177
|
+ List<ExamMonthlyScore> insertList = new java.util.ArrayList<>();
|
|
|
178
|
+ List<ExamMonthlyScore> updateList = new java.util.ArrayList<>();
|
|
|
179
|
+
|
|
|
180
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
181
|
+ ExamMonthlyScoreImportVO vo = list.get(i);
|
|
|
182
|
+ int rowNum = i + 2;
|
|
|
183
|
+
|
|
|
184
|
+ try {
|
|
|
185
|
+ // 验证必填字段
|
|
|
186
|
+ if (StrUtil.isBlank(vo.getStudentName())) {
|
|
|
187
|
+ failureNum++;
|
|
|
188
|
+ failureMsg.append("<br/>").append(failureNum).append("、第").append(rowNum).append("行:学生姓名不能为空");
|
|
|
189
|
+ continue;
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ if (StrUtil.isBlank(vo.getYearMonth())) {
|
|
|
193
|
+ failureNum++;
|
|
|
194
|
+ failureMsg.append("<br/>").append(failureNum).append("、第").append(rowNum).append("行:年月不能为空");
|
|
|
195
|
+ continue;
|
|
|
196
|
+ }
|
|
|
197
|
+
|
|
|
198
|
+ // 从缓存中获取用户
|
|
|
199
|
+ SysUser user = userMap.get(vo.getStudentName());
|
|
|
200
|
+ if (user == null) {
|
|
|
201
|
+ failureNum++;
|
|
|
202
|
+ failureMsg.append("<br/>").append(failureNum).append("、第").append(rowNum).append("行:用户【").append(vo.getStudentName()).append("】不存在");
|
|
|
203
|
+ continue;
|
|
|
204
|
+ }
|
|
|
205
|
+
|
|
|
206
|
+ // 转换年月格式
|
|
|
207
|
+ String yearMonth = convertYearMonth(vo.getYearMonth());
|
|
|
208
|
+
|
|
|
209
|
+ // 从缓存中检查是否已存在
|
|
|
210
|
+ String key = user.getUserId() + "_" + yearMonth;
|
|
|
211
|
+ ExamMonthlyScore existScore = existScoreMap.get(key);
|
|
|
212
|
+
|
|
|
213
|
+ ExamMonthlyScore score = new ExamMonthlyScore();
|
|
|
214
|
+ score.setUserId(user.getUserId());
|
|
|
215
|
+ score.setStudentName(vo.getStudentName());
|
|
|
216
|
+ score.setYearMonth(yearMonth);
|
|
|
217
|
+ score.setTeam(vo.getTeam());
|
|
|
218
|
+ score.setJobPosition(vo.getJobPosition());
|
|
|
219
|
+ score.setComprehensiveScore(vo.getComprehensiveScore());
|
|
|
220
|
+ score.setComprehensiveTime(vo.getComprehensiveTime());
|
|
|
221
|
+ score.setSpecialScore(vo.getSpecialScore());
|
|
|
222
|
+ score.setSpecialTime(vo.getSpecialTime());
|
|
|
223
|
+
|
|
|
224
|
+ if (existScore != null) {
|
|
|
225
|
+ // 已存在
|
|
|
226
|
+ if (updateSupport) {
|
|
|
227
|
+ score.setId(existScore.getId());
|
|
|
228
|
+ score.setUpdateBy(getLoginUser().getUsername());
|
|
|
229
|
+ updateList.add(score);
|
|
|
230
|
+ successNum++;
|
|
|
231
|
+ successMsg.append("<br/>").append(successNum).append("、用户【").append(vo.getStudentName()).append("】").append(yearMonth).append(" 月考成绩更新成功");
|
|
|
232
|
+ } else {
|
|
|
233
|
+ failureNum++;
|
|
|
234
|
+ failureMsg.append("<br/>").append(failureNum).append("、第").append(rowNum).append("行:用户【").append(vo.getStudentName()).append("】").append(yearMonth).append(" 月考成绩已存在");
|
|
|
235
|
+ }
|
|
|
236
|
+ } else {
|
|
|
237
|
+ // 新增
|
|
|
238
|
+ score.setCreateBy(getLoginUser().getUsername());
|
|
|
239
|
+ insertList.add(score);
|
|
|
240
|
+ successNum++;
|
|
|
241
|
+ successMsg.append("<br/>").append(successNum).append("、用户【").append(vo.getStudentName()).append("】").append(yearMonth).append(" 月考成绩导入成功");
|
|
|
242
|
+ }
|
|
|
243
|
+ } catch (Exception e) {
|
|
|
244
|
+ failureNum++;
|
|
|
245
|
+ String msg = "<br/>" + failureNum + "、第" + rowNum + "行:导入失败:";
|
|
|
246
|
+ failureMsg.append(msg).append(e.getMessage());
|
|
|
247
|
+ }
|
|
|
248
|
+ }
|
|
|
249
|
+
|
|
|
250
|
+ // 4. 批量插入和更新
|
|
|
251
|
+ if (CollectionUtil.isNotEmpty(insertList)) {
|
|
|
252
|
+ for (ExamMonthlyScore score : insertList) {
|
|
|
253
|
+ examMonthlyScoreService.insertExamMonthlyScore(score);
|
|
|
254
|
+ }
|
|
|
255
|
+ }
|
|
|
256
|
+ if (CollectionUtil.isNotEmpty(updateList)) {
|
|
|
257
|
+ for (ExamMonthlyScore score : updateList) {
|
|
|
258
|
+ examMonthlyScoreService.updateExamMonthlyScore(score);
|
|
|
259
|
+ }
|
|
|
260
|
+ }
|
|
|
261
|
+
|
|
|
262
|
+ if (failureNum > 0) {
|
|
|
263
|
+ failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
|
|
|
264
|
+ throw new ServiceException(failureMsg.toString());
|
|
|
265
|
+ } else {
|
|
|
266
|
+ successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
|
|
|
267
|
+ }
|
|
|
268
|
+ return successMsg.toString();
|
|
|
269
|
+ }
|
|
|
270
|
+
|
|
|
271
|
+ /**
|
|
|
272
|
+ * 转换年月格式
|
|
|
273
|
+ */
|
|
|
274
|
+ private String convertYearMonth(String yearMonth) {
|
|
|
275
|
+ if (StrUtil.isBlank(yearMonth)) {
|
|
|
276
|
+ return yearMonth;
|
|
|
277
|
+ }
|
|
|
278
|
+
|
|
|
279
|
+ yearMonth = yearMonth.trim();
|
|
|
280
|
+
|
|
|
281
|
+ if (yearMonth.contains("年") && yearMonth.contains("月")) {
|
|
|
282
|
+ yearMonth = yearMonth.replace("年", "-").replace("月", "");
|
|
|
283
|
+ }
|
|
|
284
|
+
|
|
|
285
|
+ if (yearMonth.contains(".")) {
|
|
|
286
|
+ yearMonth = yearMonth.replace(".", "-");
|
|
|
287
|
+ }
|
|
|
288
|
+
|
|
|
289
|
+ String[] parts = yearMonth.split("-");
|
|
|
290
|
+ if (parts.length == 2) {
|
|
|
291
|
+ String month = parts[1];
|
|
|
292
|
+ if (month.length() == 1) {
|
|
|
293
|
+ month = "0" + month;
|
|
|
294
|
+ }
|
|
|
295
|
+ yearMonth = parts[0] + "-" + month;
|
|
|
296
|
+ }
|
|
|
297
|
+
|
|
|
298
|
+ return yearMonth;
|
|
|
299
|
+ }
|
|
|
300
|
+}
|