|
|
@@ -1,11 +1,15 @@
|
|
1
|
1
|
package com.sundot.airport.ledger.service.impl;
|
|
2
|
2
|
|
|
3
|
3
|
import java.util.ArrayList;
|
|
|
4
|
+import java.util.Calendar;
|
|
|
5
|
+import java.util.Date;
|
|
4
|
6
|
import java.util.HashMap;
|
|
5
|
7
|
import java.util.List;
|
|
6
|
8
|
import java.util.Map;
|
|
7
|
9
|
import java.util.stream.Collectors;
|
|
8
|
10
|
|
|
|
11
|
+import org.slf4j.Logger;
|
|
|
12
|
+import org.slf4j.LoggerFactory;
|
|
9
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
10
|
14
|
import org.springframework.stereotype.Service;
|
|
11
|
15
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
@@ -15,6 +19,7 @@ import com.sundot.airport.ledger.mapper.LedgerCertificateInfoMapper;
|
|
15
|
19
|
import com.sundot.airport.ledger.mapper.LedgerCertificateReviewRecordMapper;
|
|
16
|
20
|
import com.sundot.airport.ledger.domain.LedgerCertificateInfo;
|
|
17
|
21
|
import com.sundot.airport.ledger.domain.LedgerCertificateReviewRecord;
|
|
|
22
|
+import com.sundot.airport.ledger.enums.CertificateStatusEnum;
|
|
18
|
23
|
import com.sundot.airport.ledger.service.ILedgerCertificateInfoService;
|
|
19
|
24
|
import com.sundot.airport.system.service.ISysUserService;
|
|
20
|
25
|
|
|
|
@@ -26,6 +31,8 @@ import com.sundot.airport.system.service.ISysUserService;
|
|
26
|
31
|
*/
|
|
27
|
32
|
@Service
|
|
28
|
33
|
public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificateInfoService {
|
|
|
34
|
+ private static final Logger log = LoggerFactory.getLogger(LedgerLedgerCertificateInfoServiceImpl.class);
|
|
|
35
|
+
|
|
29
|
36
|
@Autowired
|
|
30
|
37
|
private LedgerCertificateInfoMapper ledgerCertificateInfoMapper;
|
|
31
|
38
|
|
|
|
@@ -73,11 +80,20 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
73
|
80
|
@Transactional
|
|
74
|
81
|
public int insertCertificateInfo(LedgerCertificateInfo ledgerCertificateInfo) {
|
|
75
|
82
|
ledgerCertificateInfo.setCreateTime(DateUtils.getNowDate());
|
|
|
83
|
+
|
|
|
84
|
+ // 计算到期信息
|
|
|
85
|
+ calculateExpiryInfo(ledgerCertificateInfo);
|
|
|
86
|
+
|
|
76
|
87
|
int rows = ledgerCertificateInfoMapper.insertCertificateInfo(ledgerCertificateInfo);
|
|
77
|
88
|
|
|
78
|
89
|
// 保存复审记录
|
|
79
|
90
|
saveReviewRecords(ledgerCertificateInfo.getId(), ledgerCertificateInfo.getReviewRecords());
|
|
80
|
91
|
|
|
|
92
|
+ // 同步用户资质等级
|
|
|
93
|
+ if (ledgerCertificateInfo.getUserId() != null) {
|
|
|
94
|
+ recalculateUserQualification(ledgerCertificateInfo.getUserId());
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
81
|
97
|
return rows;
|
|
82
|
98
|
}
|
|
83
|
99
|
|
|
|
@@ -91,12 +107,21 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
91
|
107
|
@Transactional
|
|
92
|
108
|
public int updateCertificateInfo(LedgerCertificateInfo ledgerCertificateInfo) {
|
|
93
|
109
|
ledgerCertificateInfo.setUpdateTime(DateUtils.getNowDate());
|
|
|
110
|
+
|
|
|
111
|
+ // 计算到期信息
|
|
|
112
|
+ calculateExpiryInfo(ledgerCertificateInfo);
|
|
|
113
|
+
|
|
94
|
114
|
int rows = ledgerCertificateInfoMapper.updateCertificateInfo(ledgerCertificateInfo);
|
|
95
|
115
|
|
|
96
|
116
|
// 先删除旧的复审记录,再保存新的
|
|
97
|
117
|
ledgerCertificateReviewRecordMapper.deleteCertificateReviewRecordByCertId(ledgerCertificateInfo.getId());
|
|
98
|
118
|
saveReviewRecords(ledgerCertificateInfo.getId(), ledgerCertificateInfo.getReviewRecords());
|
|
99
|
119
|
|
|
|
120
|
+ // 同步用户资质等级
|
|
|
121
|
+ if (ledgerCertificateInfo.getUserId() != null) {
|
|
|
122
|
+ recalculateUserQualification(ledgerCertificateInfo.getUserId());
|
|
|
123
|
+ }
|
|
|
124
|
+
|
|
100
|
125
|
return rows;
|
|
101
|
126
|
}
|
|
102
|
127
|
|
|
|
@@ -109,11 +134,23 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
109
|
134
|
@Override
|
|
110
|
135
|
@Transactional
|
|
111
|
136
|
public int deleteCertificateInfoByIds(Long[] ids) {
|
|
112
|
|
- // 删除复审记录
|
|
|
137
|
+ // 收集需要刷新资质的userId
|
|
|
138
|
+ List<Long> userIds = new ArrayList<>();
|
|
113
|
139
|
for (Long id : ids) {
|
|
|
140
|
+ LedgerCertificateInfo cert = ledgerCertificateInfoMapper.selectCertificateInfoById(id);
|
|
|
141
|
+ if (cert != null && cert.getUserId() != null) {
|
|
|
142
|
+ userIds.add(cert.getUserId());
|
|
|
143
|
+ }
|
|
114
|
144
|
ledgerCertificateReviewRecordMapper.deleteCertificateReviewRecordByCertId(id);
|
|
115
|
145
|
}
|
|
116
|
|
- return ledgerCertificateInfoMapper.deleteCertificateInfoByIds(ids);
|
|
|
146
|
+ int rows = ledgerCertificateInfoMapper.deleteCertificateInfoByIds(ids);
|
|
|
147
|
+
|
|
|
148
|
+ // 同步用户资质等级
|
|
|
149
|
+ for (Long userId : userIds) {
|
|
|
150
|
+ recalculateUserQualification(userId);
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ return rows;
|
|
117
|
154
|
}
|
|
118
|
155
|
|
|
119
|
156
|
/**
|
|
|
@@ -125,9 +162,17 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
125
|
162
|
@Override
|
|
126
|
163
|
@Transactional
|
|
127
|
164
|
public int deleteCertificateInfoById(Long id) {
|
|
|
165
|
+ LedgerCertificateInfo cert = ledgerCertificateInfoMapper.selectCertificateInfoById(id);
|
|
128
|
166
|
// 删除复审记录
|
|
129
|
167
|
ledgerCertificateReviewRecordMapper.deleteCertificateReviewRecordByCertId(id);
|
|
130
|
|
- return ledgerCertificateInfoMapper.deleteCertificateInfoById(id);
|
|
|
168
|
+ int rows = ledgerCertificateInfoMapper.deleteCertificateInfoById(id);
|
|
|
169
|
+
|
|
|
170
|
+ // 同步用户资质等级
|
|
|
171
|
+ if (cert != null && cert.getUserId() != null) {
|
|
|
172
|
+ recalculateUserQualification(cert.getUserId());
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ return rows;
|
|
131
|
176
|
}
|
|
132
|
177
|
|
|
133
|
178
|
/**
|
|
|
@@ -145,11 +190,6 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
145
|
190
|
throw new RuntimeException("导入证书数据不能为空!");
|
|
146
|
191
|
}
|
|
147
|
192
|
|
|
148
|
|
- int successNum = 0;
|
|
149
|
|
- int failureNum = 0;
|
|
150
|
|
- StringBuilder successMsg = new StringBuilder();
|
|
151
|
|
- StringBuilder failureMsg = new StringBuilder();
|
|
152
|
|
-
|
|
153
|
193
|
// 第一步: 收集所有姓名,批量查询用户信息
|
|
154
|
194
|
List<String> personNames = certificateList.stream()
|
|
155
|
195
|
.map(LedgerCertificateInfo::getPersonName)
|
|
|
@@ -163,7 +203,6 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
163
|
203
|
// 构建姓名到用户的映射 Map<姓名, SysUser>
|
|
164
|
204
|
Map<String, SysUser> userMap = new HashMap<>();
|
|
165
|
205
|
for (SysUser user : users) {
|
|
166
|
|
- // 优先使用 nickName(用户名称),如果没有则使用 userName(登录名称)
|
|
167
|
206
|
String key = user.getNickName() != null && !user.getNickName().isEmpty()
|
|
168
|
207
|
? user.getNickName() : user.getUserName();
|
|
169
|
208
|
userMap.put(key, user);
|
|
|
@@ -177,7 +216,6 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
177
|
216
|
}
|
|
178
|
217
|
}
|
|
179
|
218
|
|
|
180
|
|
- // 如果有未找到的用户,报错提醒
|
|
181
|
219
|
if (!notFoundNames.isEmpty()) {
|
|
182
|
220
|
throw new RuntimeException("以下人员未在系统中找到:" + String.join("、", notFoundNames));
|
|
183
|
221
|
}
|
|
|
@@ -191,77 +229,102 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
191
|
229
|
}
|
|
192
|
230
|
}
|
|
193
|
231
|
|
|
194
|
|
- // 第四步: 批量查询已存在的证书信息(根据 userId 和 certNo)
|
|
|
232
|
+ // 第四步: 批量查询已存在的证书
|
|
|
233
|
+ List<LedgerCertificateInfo> queryKeys = certificateList.stream()
|
|
|
234
|
+ .filter(c -> c.getUserId() != null && c.getCertLevel() != null)
|
|
|
235
|
+ .collect(Collectors.toList());
|
|
|
236
|
+
|
|
195
|
237
|
Map<String, LedgerCertificateInfo> existingCertMap = new HashMap<>();
|
|
196
|
|
- for (LedgerCertificateInfo certificate : certificateList) {
|
|
197
|
|
- if (certificate.getUserId() != null && certificate.getCertNo() != null) {
|
|
198
|
|
- LedgerCertificateInfo existCert = ledgerCertificateInfoMapper.selectCertificateInfoByUserIdAndCertNo(
|
|
199
|
|
- certificate.getUserId(), certificate.getCertNo());
|
|
200
|
|
- if (existCert != null) {
|
|
201
|
|
- String key = certificate.getUserId() + "_" + certificate.getCertNo();
|
|
202
|
|
- existingCertMap.put(key, existCert);
|
|
203
|
|
- }
|
|
|
238
|
+ if (!queryKeys.isEmpty()) {
|
|
|
239
|
+ List<LedgerCertificateInfo> existingCerts = ledgerCertificateInfoMapper.selectExistingCertificates(queryKeys);
|
|
|
240
|
+ for (LedgerCertificateInfo cert : existingCerts) {
|
|
|
241
|
+ String mapKey = cert.getUserId() + "_" + cert.getCertLevel();
|
|
|
242
|
+ existingCertMap.put(mapKey, cert);
|
|
204
|
243
|
}
|
|
205
|
244
|
}
|
|
206
|
245
|
|
|
207
|
|
- // 第五步: 处理每个证书的导入(新增或更新)
|
|
|
246
|
+ // 第五步: 分类为新增和更新列表,计算到期信息
|
|
|
247
|
+ List<LedgerCertificateInfo> toInsert = new ArrayList<>();
|
|
|
248
|
+ List<LedgerCertificateInfo> toUpdate = new ArrayList<>();
|
|
|
249
|
+ List<Long> affectedUserIds = new ArrayList<>();
|
|
|
250
|
+
|
|
208
|
251
|
for (LedgerCertificateInfo certificate : certificateList) {
|
|
209
|
|
- try {
|
|
210
|
|
- String key = certificate.getUserId() + "_" + certificate.getCertNo();
|
|
211
|
|
- LedgerCertificateInfo existCert = existingCertMap.get(key);
|
|
212
|
|
-
|
|
213
|
|
- if (existCert != null) {
|
|
214
|
|
- // 存在则更新
|
|
215
|
|
- if (isUpdateSupport) {
|
|
216
|
|
- certificate.setId(existCert.getId());
|
|
217
|
|
- certificate.setUpdateBy(operName);
|
|
218
|
|
- ledgerCertificateInfoMapper.updateCertificateInfo(certificate);
|
|
219
|
|
-
|
|
220
|
|
- // 更新复审记录
|
|
221
|
|
- ledgerCertificateReviewRecordMapper.deleteCertificateReviewRecordByCertId(existCert.getId());
|
|
222
|
|
- saveReviewRecords(existCert.getId(), certificate.getReviewRecords());
|
|
223
|
|
-
|
|
224
|
|
- successNum++;
|
|
225
|
|
- successMsg.append("<br/>").append(successNum).append("、")
|
|
226
|
|
- .append(certificate.getPersonName()).append(" 的证书编号 ")
|
|
227
|
|
- .append(certificate.getCertNo()).append(" 更新成功");
|
|
228
|
|
- } else {
|
|
229
|
|
- failureNum++;
|
|
230
|
|
- failureMsg.append("<br/>").append(failureNum).append("、")
|
|
231
|
|
- .append(certificate.getPersonName()).append(" 的证书编号 ")
|
|
232
|
|
- .append(certificate.getCertNo()).append(" 已存在");
|
|
233
|
|
- }
|
|
234
|
|
- } else {
|
|
235
|
|
- // 不存在则新增
|
|
236
|
|
- certificate.setCreateBy(operName);
|
|
237
|
|
- ledgerCertificateInfoMapper.insertCertificateInfo(certificate);
|
|
238
|
|
-
|
|
239
|
|
- // 保存复审记录
|
|
240
|
|
- saveReviewRecords(certificate.getId(), certificate.getReviewRecords());
|
|
241
|
|
-
|
|
242
|
|
- successNum++;
|
|
243
|
|
- successMsg.append("<br/>").append(successNum).append("、")
|
|
244
|
|
- .append(certificate.getPersonName()).append(" 的证书编号 ")
|
|
245
|
|
- .append(certificate.getCertNo()).append(" 导入成功");
|
|
|
252
|
+ String key = certificate.getUserId() + "_" + certificate.getCertLevel();
|
|
|
253
|
+ LedgerCertificateInfo existCert = existingCertMap.get(key);
|
|
|
254
|
+
|
|
|
255
|
+ // 计算到期信息
|
|
|
256
|
+ calculateExpiryInfo(certificate);
|
|
|
257
|
+
|
|
|
258
|
+ if (existCert != null) {
|
|
|
259
|
+ certificate.setId(existCert.getId());
|
|
|
260
|
+ certificate.setUpdateBy(operName);
|
|
|
261
|
+ toUpdate.add(certificate);
|
|
|
262
|
+ } else {
|
|
|
263
|
+ certificate.setCreateBy(operName);
|
|
|
264
|
+ toInsert.add(certificate);
|
|
|
265
|
+ }
|
|
|
266
|
+
|
|
|
267
|
+ if (!affectedUserIds.contains(certificate.getUserId())) {
|
|
|
268
|
+ affectedUserIds.add(certificate.getUserId());
|
|
|
269
|
+ }
|
|
|
270
|
+ }
|
|
|
271
|
+
|
|
|
272
|
+ // 第六步: 批量新增
|
|
|
273
|
+ if (!toInsert.isEmpty()) {
|
|
|
274
|
+ ledgerCertificateInfoMapper.batchInsertCertificateInfo(toInsert);
|
|
|
275
|
+ }
|
|
|
276
|
+
|
|
|
277
|
+ // 第七步: 批量更新
|
|
|
278
|
+ if (!toUpdate.isEmpty()) {
|
|
|
279
|
+ ledgerCertificateInfoMapper.batchUpdateCertificateInfo(toUpdate);
|
|
|
280
|
+ }
|
|
|
281
|
+
|
|
|
282
|
+ // 第八步: 批量处理复审记录
|
|
|
283
|
+ // 批量删除需要更新的证书的复审记录
|
|
|
284
|
+ List<Long> updateCertIds = toUpdate.stream()
|
|
|
285
|
+ .map(LedgerCertificateInfo::getId)
|
|
|
286
|
+ .collect(Collectors.toList());
|
|
|
287
|
+ if (!updateCertIds.isEmpty()) {
|
|
|
288
|
+ ledgerCertificateReviewRecordMapper.deleteReviewRecordsByCertIds(updateCertIds);
|
|
|
289
|
+ }
|
|
|
290
|
+
|
|
|
291
|
+ // 批量插入所有复审记录
|
|
|
292
|
+ List<LedgerCertificateReviewRecord> allReviewRecords = new ArrayList<>();
|
|
|
293
|
+ for (LedgerCertificateInfo cert : toUpdate) {
|
|
|
294
|
+ if (cert.getReviewRecords() != null) {
|
|
|
295
|
+ for (int i = 0; i < cert.getReviewRecords().size(); i++) {
|
|
|
296
|
+ LedgerCertificateReviewRecord record = cert.getReviewRecords().get(i);
|
|
|
297
|
+ record.setCertId(cert.getId());
|
|
|
298
|
+ record.setReviewSeq(i + 1);
|
|
|
299
|
+ record.setCreateTime(DateUtils.getNowDate());
|
|
|
300
|
+ allReviewRecords.add(record);
|
|
|
301
|
+ }
|
|
|
302
|
+ }
|
|
|
303
|
+ }
|
|
|
304
|
+ for (LedgerCertificateInfo cert : toInsert) {
|
|
|
305
|
+ if (cert.getReviewRecords() != null) {
|
|
|
306
|
+ for (int i = 0; i < cert.getReviewRecords().size(); i++) {
|
|
|
307
|
+ LedgerCertificateReviewRecord record = cert.getReviewRecords().get(i);
|
|
|
308
|
+ record.setCertId(cert.getId());
|
|
|
309
|
+ record.setReviewSeq(i + 1);
|
|
|
310
|
+ record.setCreateTime(DateUtils.getNowDate());
|
|
|
311
|
+ allReviewRecords.add(record);
|
|
246
|
312
|
}
|
|
247
|
|
-
|
|
248
|
|
- // 第六步: 同步更新用户表的证书等级(5<4<3<2<1,数字越小等级越高)
|
|
249
|
|
- syncUserQualificationLevel(certificate.getUserId(), certificate.getCertLevel());
|
|
250
|
|
-
|
|
251
|
|
- } catch (Exception e) {
|
|
252
|
|
- failureNum++;
|
|
253
|
|
- String msg = "<br/>" + failureNum + "、" + certificate.getPersonName() + " 的证书编号 "
|
|
254
|
|
- + certificate.getCertNo() + " 导入失败:";
|
|
255
|
|
- failureMsg.append(msg + e.getMessage());
|
|
256
|
313
|
}
|
|
257
|
314
|
}
|
|
|
315
|
+ if (!allReviewRecords.isEmpty()) {
|
|
|
316
|
+ ledgerCertificateReviewRecordMapper.batchInsertReviewRecords(allReviewRecords);
|
|
|
317
|
+ }
|
|
258
|
318
|
|
|
259
|
|
- if (failureNum > 0) {
|
|
260
|
|
- failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
|
|
261
|
|
- throw new RuntimeException(failureMsg.toString());
|
|
262
|
|
- } else {
|
|
263
|
|
- successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
|
|
|
319
|
+ // 第九步: 刷新所有受影响用户的资质等级
|
|
|
320
|
+ for (Long userId : affectedUserIds) {
|
|
|
321
|
+ recalculateUserQualification(userId);
|
|
264
|
322
|
}
|
|
|
323
|
+
|
|
|
324
|
+ // 构建返回消息
|
|
|
325
|
+ int successNum = toInsert.size() + toUpdate.size();
|
|
|
326
|
+ StringBuilder successMsg = new StringBuilder();
|
|
|
327
|
+ successMsg.insert(0, "数据已全部导入成功!共 " + successNum + " 条");
|
|
265
|
328
|
return successMsg.toString();
|
|
266
|
329
|
}
|
|
267
|
330
|
|
|
|
@@ -276,7 +339,7 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
276
|
339
|
for (int i = 0; i < reviewRecords.size(); i++) {
|
|
277
|
340
|
LedgerCertificateReviewRecord record = reviewRecords.get(i);
|
|
278
|
341
|
record.setCertId(certId);
|
|
279
|
|
- record.setReviewSeq(i + 1); // 设置复审次序
|
|
|
342
|
+ record.setReviewSeq(i + 1);
|
|
280
|
343
|
record.setCreateTime(DateUtils.getNowDate());
|
|
281
|
344
|
ledgerCertificateReviewRecordMapper.insertCertificateReviewRecord(record);
|
|
282
|
345
|
}
|
|
|
@@ -284,46 +347,228 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
284
|
347
|
}
|
|
285
|
348
|
|
|
286
|
349
|
/**
|
|
287
|
|
- * 同步更新用户表的证书等级
|
|
288
|
|
- * 等级规则: 5<4<3<2<1 (数字越小等级越高)
|
|
289
|
|
- * 如果出现更高等级,就更新用户表
|
|
|
350
|
+ * 计算证书到期信息(到期日期、到期天数、状态)
|
|
|
351
|
+ * 到期日期 = 最后复审时间 + 服务期月 - 1天,如果复审时间为空则取发证时间
|
|
|
352
|
+ * 到期天数 = 到期日期 - 当前时间
|
|
|
353
|
+ * 状态: >180天=正常, >0且<=180=复训, <=0=到期
|
|
290
|
354
|
*
|
|
291
|
|
- * @param userId 用户ID
|
|
292
|
|
- * @param newCertLevel 新证书等级
|
|
|
355
|
+ * @param certificate 证书信息
|
|
293
|
356
|
*/
|
|
294
|
|
- private void syncUserQualificationLevel(Long userId, String newCertLevel) {
|
|
295
|
|
- if (userId == null || newCertLevel == null || newCertLevel.trim().isEmpty()) {
|
|
|
357
|
+ @Override
|
|
|
358
|
+ public void calculateExpiryInfo(LedgerCertificateInfo certificate) {
|
|
|
359
|
+ if (certificate == null) return;
|
|
|
360
|
+
|
|
|
361
|
+ Date baseDate = calculateExpiryBaseDate(certificate);
|
|
|
362
|
+ if (baseDate == null || certificate.getServiceMonths() == null) {
|
|
296
|
363
|
return;
|
|
297
|
364
|
}
|
|
298
|
365
|
|
|
|
366
|
+ // 计算到期日期: baseDate + serviceMonths - 1天
|
|
|
367
|
+ Date expiryDate = calculateExpiryDate(baseDate, certificate.getServiceMonths());
|
|
|
368
|
+ certificate.setExpiryDate(expiryDate);
|
|
|
369
|
+
|
|
|
370
|
+ // 计算到期天数
|
|
|
371
|
+ int expireDays = calculateExpireDays(expiryDate);
|
|
|
372
|
+ certificate.setExpireDays(expireDays);
|
|
|
373
|
+
|
|
|
374
|
+ // 计算状态
|
|
|
375
|
+ String status = calculateStatus(expireDays);
|
|
|
376
|
+ certificate.setStatus(status);
|
|
|
377
|
+ }
|
|
|
378
|
+
|
|
|
379
|
+ /**
|
|
|
380
|
+ * 计算到期基准日期:取最后复审时间,如果为空则取发证时间
|
|
|
381
|
+ */
|
|
|
382
|
+ private Date calculateExpiryBaseDate(LedgerCertificateInfo certificate) {
|
|
|
383
|
+ // 优先从传入的复审记录中查找(用于新增/导入场景)
|
|
|
384
|
+ List<LedgerCertificateReviewRecord> reviewRecords = certificate.getReviewRecords();
|
|
|
385
|
+
|
|
|
386
|
+ // 如果没有传入复审记录,从数据库查询
|
|
|
387
|
+ if ((reviewRecords == null || reviewRecords.isEmpty()) && certificate.getId() != null) {
|
|
|
388
|
+ reviewRecords = ledgerCertificateReviewRecordMapper.selectCertificateReviewRecordByCertId(certificate.getId());
|
|
|
389
|
+ }
|
|
|
390
|
+
|
|
|
391
|
+ if (reviewRecords != null && !reviewRecords.isEmpty()) {
|
|
|
392
|
+ // 找到最新的复审时间(列表最后一个,按 reviewSeq 升序)
|
|
|
393
|
+ LedgerCertificateReviewRecord latestRecord = reviewRecords.get(reviewRecords.size() - 1);
|
|
|
394
|
+ if (latestRecord.getReviewTime() != null) {
|
|
|
395
|
+ return latestRecord.getReviewTime();
|
|
|
396
|
+ }
|
|
|
397
|
+ }
|
|
|
398
|
+
|
|
|
399
|
+ // 如果没有复审记录或复审时间为空,取发证时间
|
|
|
400
|
+ return certificate.getIssueDate();
|
|
|
401
|
+ }
|
|
|
402
|
+
|
|
|
403
|
+ /**
|
|
|
404
|
+ * 计算到期日期: baseDate + months 月 - 1天
|
|
|
405
|
+ */
|
|
|
406
|
+ private Date calculateExpiryDate(Date baseDate, int months) {
|
|
|
407
|
+ Calendar cal = Calendar.getInstance();
|
|
|
408
|
+ cal.setTime(baseDate);
|
|
|
409
|
+ cal.add(Calendar.MONTH, months);
|
|
|
410
|
+ cal.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
411
|
+ return cal.getTime();
|
|
|
412
|
+ }
|
|
|
413
|
+
|
|
|
414
|
+ /**
|
|
|
415
|
+ * 计算到期天数: expiryDate - 当前日期
|
|
|
416
|
+ */
|
|
|
417
|
+ private int calculateExpireDays(Date expiryDate) {
|
|
|
418
|
+ if (expiryDate == null) return 0;
|
|
|
419
|
+ Date now = DateUtils.getNowDate();
|
|
|
420
|
+ long diff = expiryDate.getTime() - now.getTime();
|
|
|
421
|
+ return (int) (diff / (1000 * 60 * 60 * 24));
|
|
|
422
|
+ }
|
|
|
423
|
+
|
|
|
424
|
+ /**
|
|
|
425
|
+ * 计算状态: >180天=正常, >0且<=180=复训, <=0=到期
|
|
|
426
|
+ */
|
|
|
427
|
+ private String calculateStatus(int expireDays) {
|
|
|
428
|
+ return CertificateStatusEnum.calculateByExpireDays(expireDays).getDesc();
|
|
|
429
|
+ }
|
|
|
430
|
+
|
|
|
431
|
+ /**
|
|
|
432
|
+ * 重新计算用户资质等级
|
|
|
433
|
+ * 规则: 查询用户所有证书,找到最高等级(数字最小)且未到期的证书
|
|
|
434
|
+ * 如果所有证书都到期,则清空资质
|
|
|
435
|
+ *
|
|
|
436
|
+ * @param userId 用户ID
|
|
|
437
|
+ */
|
|
|
438
|
+ @Override
|
|
|
439
|
+ public void recalculateUserQualification(Long userId) {
|
|
|
440
|
+ if (userId == null) return;
|
|
|
441
|
+
|
|
299
|
442
|
try {
|
|
300
|
|
- // 查询用户当前资质等级
|
|
301
|
|
- SysUser user = sysUserService.selectUserById(userId);
|
|
302
|
|
- if (user == null) {
|
|
|
443
|
+ // 查询用户所有证书
|
|
|
444
|
+ List<LedgerCertificateInfo> allCerts = ledgerCertificateInfoMapper.selectCertificateInfoByUserId(userId);
|
|
|
445
|
+
|
|
|
446
|
+ if (allCerts == null || allCerts.isEmpty()) {
|
|
|
447
|
+ // 没有证书,清空资质
|
|
|
448
|
+ updateUserQualification(userId, null);
|
|
303
|
449
|
return;
|
|
304
|
450
|
}
|
|
305
|
451
|
|
|
306
|
|
- String currentLevel = user.getQualificationLevel();
|
|
|
452
|
+ // 重新计算每张证书的到期信息
|
|
|
453
|
+ for (LedgerCertificateInfo cert : allCerts) {
|
|
|
454
|
+ // 对于已有id的证书,查询复审记录来计算到期信息
|
|
|
455
|
+ Date baseDate = calculateExpiryBaseDate(cert);
|
|
|
456
|
+ if (baseDate != null && cert.getServiceMonths() != null) {
|
|
|
457
|
+ Date expiryDate = calculateExpiryDate(baseDate, cert.getServiceMonths());
|
|
|
458
|
+ cert.setExpiryDate(expiryDate);
|
|
|
459
|
+ int expireDays = calculateExpireDays(expiryDate);
|
|
|
460
|
+ cert.setExpireDays(expireDays);
|
|
|
461
|
+ cert.setStatus(calculateStatus(expireDays));
|
|
|
462
|
+
|
|
|
463
|
+ // 更新到期信息到数据库
|
|
|
464
|
+ ledgerCertificateInfoMapper.updateCertificateExpiryInfo(cert);
|
|
|
465
|
+ }
|
|
|
466
|
+ }
|
|
|
467
|
+
|
|
|
468
|
+ // 找到最高等级(数字最小)且未到期的证书
|
|
|
469
|
+ LedgerCertificateInfo bestValidCert = null;
|
|
|
470
|
+ int bestLevel = Integer.MAX_VALUE;
|
|
307
|
471
|
|
|
308
|
|
- // 如果用户当前没有等级,直接设置
|
|
309
|
|
- if (currentLevel == null || currentLevel.trim().isEmpty()) {
|
|
310
|
|
- updateUserQualificationLevel(userId, newCertLevel);
|
|
|
472
|
+ for (LedgerCertificateInfo cert : allCerts) {
|
|
|
473
|
+ // 使用刚计算的状态
|
|
|
474
|
+ if (!CertificateStatusEnum.EXPIRED.getDesc().equals(cert.getStatus())) {
|
|
|
475
|
+ int level = parseLevelToNumber(cert.getCertLevel());
|
|
|
476
|
+ if (level < bestLevel) {
|
|
|
477
|
+ bestLevel = level;
|
|
|
478
|
+ bestValidCert = cert;
|
|
|
479
|
+ }
|
|
|
480
|
+ }
|
|
|
481
|
+ }
|
|
|
482
|
+
|
|
|
483
|
+ if (bestValidCert != null) {
|
|
|
484
|
+ // 找到未到期证书,设置资质为最高等级
|
|
|
485
|
+ String qualificationLevel = convertCertLevelToQualificationFormat(bestValidCert.getCertLevel());
|
|
|
486
|
+ updateUserQualification(userId, qualificationLevel);
|
|
|
487
|
+ } else {
|
|
|
488
|
+ // 所有证书都到期,清空资质
|
|
|
489
|
+ updateUserQualification(userId, null);
|
|
|
490
|
+ }
|
|
|
491
|
+ } catch (Exception e) {
|
|
|
492
|
+ log.error("重新计算用户资质等级失败, userId={}", userId, e);
|
|
|
493
|
+ }
|
|
|
494
|
+ }
|
|
|
495
|
+
|
|
|
496
|
+ /**
|
|
|
497
|
+ * 每日定时更新证书到期信息
|
|
|
498
|
+ * 更新所有证书的到期日期、到期天数、状态,并同步用户资质等级
|
|
|
499
|
+ */
|
|
|
500
|
+ @Override
|
|
|
501
|
+ public void dailyUpdateExpiryInfo() {
|
|
|
502
|
+ log.info("========== 开始执行证书到期信息每日更新 ==========");
|
|
|
503
|
+
|
|
|
504
|
+ try {
|
|
|
505
|
+ // 查询所有有效证书
|
|
|
506
|
+ List<LedgerCertificateInfo> allCerts = ledgerCertificateInfoMapper.selectAllCertificateInfo();
|
|
|
507
|
+
|
|
|
508
|
+ if (allCerts == null || allCerts.isEmpty()) {
|
|
|
509
|
+ log.info("没有证书数据需要更新");
|
|
311
|
510
|
return;
|
|
312
|
511
|
}
|
|
313
|
512
|
|
|
314
|
|
- // 比较等级高低(数字越小等级越高)
|
|
315
|
|
- int currentLevelNum = parseLevelToNumber(currentLevel);
|
|
316
|
|
- int newLevelNum = parseLevelToNumber(newCertLevel);
|
|
|
513
|
+ int updatedCount = 0;
|
|
|
514
|
+ List<Long> processedUserIds = new ArrayList<>();
|
|
317
|
515
|
|
|
318
|
|
- // 如果新等级更高,则更新用户表
|
|
319
|
|
- if (newLevelNum < currentLevelNum) {
|
|
320
|
|
- updateUserQualificationLevel(userId, newCertLevel);
|
|
|
516
|
+ for (LedgerCertificateInfo cert : allCerts) {
|
|
|
517
|
+ // 计算到期基准日期
|
|
|
518
|
+ Date baseDate = calculateExpiryBaseDate(cert);
|
|
|
519
|
+ if (baseDate != null && cert.getServiceMonths() != null) {
|
|
|
520
|
+ Date expiryDate = calculateExpiryDate(baseDate, cert.getServiceMonths());
|
|
|
521
|
+ cert.setExpiryDate(expiryDate);
|
|
|
522
|
+ int expireDays = calculateExpireDays(expiryDate);
|
|
|
523
|
+ cert.setExpireDays(expireDays);
|
|
|
524
|
+ cert.setStatus(calculateStatus(expireDays));
|
|
|
525
|
+
|
|
|
526
|
+ // 更新到期信息到数据库
|
|
|
527
|
+ ledgerCertificateInfoMapper.updateCertificateExpiryInfo(cert);
|
|
|
528
|
+ updatedCount++;
|
|
|
529
|
+ }
|
|
|
530
|
+
|
|
|
531
|
+ // 收集用户ID
|
|
|
532
|
+ if (cert.getUserId() != null && !processedUserIds.contains(cert.getUserId())) {
|
|
|
533
|
+ processedUserIds.add(cert.getUserId());
|
|
|
534
|
+ }
|
|
321
|
535
|
}
|
|
|
536
|
+
|
|
|
537
|
+ log.info("已更新 {} 条证书到期信息", updatedCount);
|
|
|
538
|
+
|
|
|
539
|
+ // 统一刷新所有用户的资质等级
|
|
|
540
|
+ for (Long userId : processedUserIds) {
|
|
|
541
|
+ recalculateUserQualification(userId);
|
|
|
542
|
+ }
|
|
|
543
|
+
|
|
|
544
|
+ log.info("已刷新 {} 个用户的资质等级", processedUserIds.size());
|
|
|
545
|
+ log.info("========== 证书到期信息每日更新完成 ==========");
|
|
|
546
|
+
|
|
322
|
547
|
} catch (Exception e) {
|
|
323
|
|
- e.printStackTrace();
|
|
|
548
|
+ log.error("证书到期信息每日更新失败", e);
|
|
324
|
549
|
}
|
|
325
|
550
|
}
|
|
326
|
|
-
|
|
|
551
|
+
|
|
|
552
|
+ /**
|
|
|
553
|
+ * 将证书等级格式转换为资质等级格式
|
|
|
554
|
+ * 如: "一级" -> "LEVEL_ONE", "2" -> "LEVEL_TWO"
|
|
|
555
|
+ */
|
|
|
556
|
+ private String convertCertLevelToQualificationFormat(String certLevel) {
|
|
|
557
|
+ if (certLevel == null || certLevel.trim().isEmpty()) return null;
|
|
|
558
|
+
|
|
|
559
|
+ certLevel = certLevel.trim();
|
|
|
560
|
+
|
|
|
561
|
+ if (certLevel.contains("一") || certLevel.equals("1")) return "LEVEL_ONE";
|
|
|
562
|
+ if (certLevel.contains("二") || certLevel.equals("2")) return "LEVEL_TWO";
|
|
|
563
|
+ if (certLevel.contains("三") || certLevel.equals("3")) return "LEVEL_THREE";
|
|
|
564
|
+ if (certLevel.contains("四") || certLevel.equals("4")) return "LEVEL_FOUR";
|
|
|
565
|
+ if (certLevel.contains("五") || certLevel.equals("5")) return "LEVEL_FIVE";
|
|
|
566
|
+
|
|
|
567
|
+ if (certLevel.toUpperCase().startsWith("LEVEL_")) return certLevel.toUpperCase();
|
|
|
568
|
+
|
|
|
569
|
+ return certLevel;
|
|
|
570
|
+ }
|
|
|
571
|
+
|
|
327
|
572
|
/**
|
|
328
|
573
|
* 将等级字符串转换为数字进行比较
|
|
329
|
574
|
* 支持格式: "一级"、"LEVEL_ONE"、"1" 等
|
|
|
@@ -367,14 +612,14 @@ public class LedgerLedgerCertificateInfoServiceImpl implements ILedgerCertificat
|
|
367
|
612
|
* @param userId 用户ID
|
|
368
|
613
|
* @param qualificationLevel 资质等级
|
|
369
|
614
|
*/
|
|
370
|
|
- private void updateUserQualificationLevel(Long userId, String qualificationLevel) {
|
|
|
615
|
+ private void updateUserQualification(Long userId, String qualificationLevel) {
|
|
371
|
616
|
try {
|
|
372
|
617
|
SysUser user = new SysUser();
|
|
373
|
618
|
user.setUserId(userId);
|
|
374
|
619
|
user.setQualificationLevel(qualificationLevel);
|
|
375
|
620
|
sysUserService.updateUserProfile(user);
|
|
376
|
621
|
} catch (Exception e) {
|
|
377
|
|
- e.printStackTrace();
|
|
|
622
|
+ log.error("更新用户资质等级失败, userId={}, level={}", userId, qualificationLevel, e);
|
|
378
|
623
|
}
|
|
379
|
624
|
}
|
|
380
|
625
|
}
|