Commit 6ae40a60 authored by wujipeng's avatar wujipeng
Browse files

优化

parent 257c8d2d
...@@ -57,7 +57,10 @@ public class User { ...@@ -57,7 +57,10 @@ public class User {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime; private Date updateTime;
/** 管理端账户状态;认证服务在首次登录时负责把PENDING推进为STUDYING。 */ /**
* 管理端账户业务状态。
* PENDING 只能在学员成功提交完整基础信息后推进为 STUDYING,登录认证不得修改该字段。
*/
@TableField("account_status") @TableField("account_status")
private String accountStatus; private String accountStatus;
......
...@@ -71,9 +71,11 @@ public class UserServiceImpl extends ServiceImpl<USerMapper, User> implements Us ...@@ -71,9 +71,11 @@ public class UserServiceImpl extends ServiceImpl<USerMapper, User> implements Us
user.setLoginFailNum(0); user.setLoginFailNum(0);
user.setLoginDate(new Date()); user.setLoginDate(new Date());
user.setFirstLoginFlag(false); user.setFirstLoginFlag(false);
if ("PENDING".equalsIgnoreCase(user.getAccountStatus())) { /*
user.setAccountStatus("STUDYING"); * 登录只更新认证相关信息,不能推进学员的业务状态。
} * PENDING 表示学员尚未完成基础信息,必须一直保留到学员成功提交完整资料;
* 该提交动作由业务服务原子地把状态更新为 STUDYING,避免仅登录一次就绕过资料完善页。
*/
updateById(user); updateById(user);
recordLogin(user.getId(), loginName, ipAddress, device, "SUCCESS", null); recordLogin(user.getId(), loginName, ipAddress, device, "SUCCESS", null);
......
package com.platform.auth.service.impl;
import com.platform.auth.mapper.USerMapper;
import com.platform.auth.model.dto.UserDTO;
import com.platform.auth.model.entity.User;
import com.platform.auth.model.vo.UserVO;
import com.platform.auth.service.UserRoleService;
import com.platform.auth.utils.JwtTokenUtil;
import com.platform.auth.utils.PasswordUtil;
import com.platform.model.resp.Result;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** 验证登录认证不会提前推进由基础信息提交控制的学员业务状态。 */
class UserServiceImplTest {
private UserServiceImpl service;
private USerMapper userMapper;
@BeforeEach
void setUp() {
service = new UserServiceImpl();
userMapper = mock(USerMapper.class);
PasswordUtil passwordUtil = mock(PasswordUtil.class);
JwtTokenUtil jwtTokenUtil = mock(JwtTokenUtil.class);
UserRoleService userRoleService = mock(UserRoleService.class);
// ServiceImpl 的持久化方法依赖父类 baseMapper,其余登录依赖均通过反射注入隔离为单元测试。
ReflectionTestUtils.setField(service, "baseMapper", userMapper);
ReflectionTestUtils.setField(service, "passwordUtil", passwordUtil);
ReflectionTestUtils.setField(service, "jwtTokenUtil", jwtTokenUtil);
ReflectionTestUtils.setField(service, "userRoleService", userRoleService);
when(passwordUtil.matches(anyString(), anyString())).thenReturn(true);
when(jwtTokenUtil.generateToken(anyString())).thenReturn("token");
when(userRoleService.queryUserRoleList(anyString())).thenReturn(Collections.emptyList());
}
@Test
void loginShouldKeepPendingStatusUntilStudentProfileIsSubmitted() {
User pendingStudent = new User();
pendingStudent.setId("student-1");
pendingStudent.setUsername("student-1");
pendingStudent.setPhoneNumber("13800000000");
pendingStudent.setPassword("encoded-password");
pendingStudent.setFirstLoginFlag(true);
pendingStudent.setAccountStatus("PENDING");
pendingStudent.setDelFlag(false);
when(userMapper.selectOne(any())).thenReturn(pendingStudent);
when(userMapper.updateById(any(User.class))).thenReturn(1);
when(userMapper.insertLoginLog(anyString(), anyString(), anyString(), anyString(),
anyString(), any())).thenReturn(1);
UserDTO request = new UserDTO();
request.setPhoneNumber("13800000000");
request.setPassword("plain-password");
Result<UserVO> result = service.login(request, "127.0.0.1", "unit-test");
/*
* 登录仍可清除首次登录标记并写入登录时间,但账户业务状态必须保持 PENDING;
* 后续只有完整资料提交接口可以将其更新为 STUDYING。
*/
assertEquals("PENDING", pendingStudent.getAccountStatus());
assertTrue(result.getData().getFirstLogin());
verify(userMapper).updateById(pendingStudent);
}
}
...@@ -46,6 +46,9 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio ...@@ -46,6 +46,9 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio
private static final int MAX_EXPORT = 500; private static final int MAX_EXPORT = 500;
private static final Pattern ROW_END_PATTERN = Pattern.compile("\\\\row(?![a-zA-Z])"); private static final Pattern ROW_END_PATTERN = Pattern.compile("\\\\row(?![a-zA-Z])");
private static final Pattern CELL_END_PATTERN = Pattern.compile("\\\\cell(?![a-zA-Z])"); private static final Pattern CELL_END_PATTERN = Pattern.compile("\\\\cell(?![a-zA-Z])");
/** 新版模板把出生日期拆成“年、月、日”三个值格,兼容数据库常见日期分隔符。 */
private static final Pattern BIRTHDAY_PATTERN = Pattern.compile(
"^(\\d{4})[-/.年](\\d{1,2})[-/.月](\\d{1,2})日?$");
@Resource private StudentCertificationMapper mapper; @Resource private StudentCertificationMapper mapper;
@Override @Override
...@@ -184,7 +187,8 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio ...@@ -184,7 +187,8 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio
/** 模板结构变化时立即失败,避免姓名、身份证等字段被写入错误单元格。 */ /** 模板结构变化时立即失败,避免姓名、身份证等字段被写入错误单元格。 */
private void validateTemplate(String template, List<RtfRow> rows) { private void validateTemplate(String template, List<RtfRow> rows) {
int[] expectedCells = {15, 9, 8, 8, 18, 10, 10, 5, 5, 5, 5, 7, 8, 3, 3, 8, // 新版原始DOC的出生日期行有11格,身份证行改为一个完整号码格,因此分别是11格和4格。
int[] expectedCells = {15, 11, 8, 8, 4, 10, 10, 5, 5, 5, 5, 7, 8, 3, 3, 8,
8, 6, 9, 3, 8, 8, 2, 2, 2, 2, 2, 19, 16, 1, 1, 1, 1}; 8, 6, 9, 3, 8, 8, 2, 2, 2, 2, 2, 19, 16, 1, 1, 1, 1};
if (!template.startsWith("{\\rtf1") || rows.size() != expectedCells.length) { if (!template.startsWith("{\\rtf1") || rows.size() != expectedCells.length) {
throw new BusinessException("B2模板结构已变化,请重新确认字段映射"); throw new BusinessException("B2模板结构已变化,请重新确认字段映射");
...@@ -201,7 +205,7 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio ...@@ -201,7 +205,7 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio
putSubmitDate(edits, rows, profile.getSubmitTime()); putSubmitDate(edits, rows, profile.getSubmitTime());
put(edits, rows, 0, 14, profile.getClassId(), true); put(edits, rows, 0, 14, profile.getClassId(), true);
put(edits, rows, 1, 2, profile.getName(), true); put(edits, rows, 1, 2, profile.getName(), true);
put(edits, rows, 1, 4, profile.getBirthday(), true); putBirthday(edits, rows, profile.getBirthday());
put(edits, rows, 2, 2, profile.getGender(), true); put(edits, rows, 2, 2, profile.getGender(), true);
// 户口性质、人员类别位于标签的下一行,未选项必须清空模板原有空格或示例勾。 // 户口性质、人员类别位于标签的下一行,未选项必须清空模板原有空格或示例勾。
...@@ -245,29 +249,38 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio ...@@ -245,29 +249,38 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio
"五级", "四级", "三级", "二级", "一级", "专项职业能力"); "五级", "四级", "三级", "二级", "一级", "专项职业能力");
} }
/** /** 新版模板提供一个横向合并的身份证号码格,直接写入完整号码,避免拆格导致缺位或换行。 */
* 原政府模板为早期15位身份证预留了15格。对于现行18位号码,把多出的前三位均匀放入
* 前三个格(每格两位),剩余格仍保持一格一位,既不丢数据,也不改变模板列宽。
*/
private void putIdCard(List<RtfEdit> edits, List<RtfRow> rows, String value) { private void putIdCard(List<RtfEdit> edits, List<RtfRow> rows, String value) {
String idCard = trim(value); String idCard = trim(value);
List<String> parts = new ArrayList<>(); // 9磅字号可以让18位号码在新版合并单元格中保持单行,同时仍便于打印阅读。
if (idCard != null) { putWithFontSize(edits, rows, 4, 2, idCard, 18);
int capacity = 15; }
int doubleCellCount = Math.max(0, idCard.length() - capacity);
int offset = 0; /**
for (int i = 0; i < capacity; i++) { * 新版模板的出生日期值位于第2行的第4、6、8号单元格,三个相邻单元格分别固定显示
int width = i < doubleCellCount ? 2 : 1; * “年、月、日”标签。正常日期拆分填入;无法识别的历史值仍写入年份格,保证数据不丢失。
int end = Math.min(idCard.length(), offset + width); */
parts.add(offset < idCard.length() ? idCard.substring(offset, end) : ""); private void putBirthday(List<RtfEdit> edits, List<RtfRow> rows, String birthday) {
offset = end; String normalized = trim(birthday);
} if (normalized == null) {
if (offset < idCard.length()) throw new BusinessException("身份证号超过B2模板可显示容量"); // 无生日时清空三个动态值格,避免模板中的示例日期残留。
put(edits, rows, 1, 4, "", true);
put(edits, rows, 1, 6, "", true);
put(edits, rows, 1, 8, "", true);
return;
} }
for (int i = 0; i < 15; i++) {
// 身份证格宽按旧15位号码设计,统一使用7磅字体可让18位号码的双字符格保持单行。 Matcher matcher = BIRTHDAY_PATTERN.matcher(normalized);
putWithFontSize(edits, rows, 4, i + 2, if (matcher.matches()) {
i < parts.size() ? parts.get(i) : "", 14); // putWithFontSize自身会清空单元格,不能再叠加同坐标清空操作,否则RTF坐标会重复收缩。
putWithFontSize(edits, rows, 1, 4, matcher.group(1), 16);
putWithFontSize(edits, rows, 1, 6, matcher.group(2), 16);
putWithFontSize(edits, rows, 1, 8, matcher.group(3), 16);
} else {
// 兼容少量非标准历史数据;使用较小字号尽量保持在首个日期值格内。
putWithFontSize(edits, rows, 1, 4, normalized, 12);
put(edits, rows, 1, 6, "", true);
put(edits, rows, 1, 8, "", true);
} }
} }
......
...@@ -6,12 +6,14 @@ import org.junit.jupiter.api.Test; ...@@ -6,12 +6,14 @@ import org.junit.jupiter.api.Test;
import javax.swing.text.DefaultStyledDocument; import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit; import javax.swing.text.rtf.RTFEditorKit;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
...@@ -31,23 +33,29 @@ class B2TemplateInspectionTest { ...@@ -31,23 +33,29 @@ class B2TemplateInspectionTest {
profile.setPreviousOccupation("污水处理工"); profile.setPreviousLevel("五级"); profile.setPreviousOccupation("污水处理工"); profile.setPreviousLevel("五级");
profile.setPreviousCertNo("CERT-001"); profile.setPreviousIssueDate("2024-01-01"); profile.setPreviousCertNo("CERT-001"); profile.setPreviousIssueDate("2024-01-01");
profile.setPreviousIssueDept("示例机构"); profile.setJobYears(5); profile.setPreviousIssueDept("示例机构"); profile.setJobYears(5);
profile.setTrainingOccupation("污水处理工"); profile.setTrainingLevel("五级"); // 故意使用与新模板示例值不同的数据,验证模板中的污水处理工和默认勾选会被清除。
profile.setTrainingCategory("企业培训"); profile.setSubsidyFlag(true); profile.setTrainingOccupation("焊工"); profile.setTrainingLevel("三级");
profile.setAssessmentOccupation("污水处理工"); profile.setAssessmentLevel("五级"); profile.setTrainingCategory("社会培训"); profile.setSubsidyFlag(false);
profile.setAssessmentOccupation("电工"); profile.setAssessmentLevel("二级");
profile.setClassId(12); profile.setSubmitTime(new Date()); profile.setClassId(12); profile.setSubmitTime(new Date());
byte[] bytes = new StudentCertificationAdminServiceImpl().createDocument(profile); byte[] bytes = new StudentCertificationAdminServiceImpl().createDocument(profile);
String rtf = new String(bytes, StandardCharsets.US_ASCII); String rtf = new String(bytes, StandardCharsets.US_ASCII);
// 原模板固定为33行复杂合并表格;行数变化意味着字段坐标需要重新人工确认。 // 新模板仍为33行,但出生日期行和身份证行分别调整为11格、4格;必须锁定完整结构。
assertTrue(rtf.startsWith("{\\rtf1")); assertTrue(rtf.startsWith("{\\rtf1"));
assertTrue(rtf.contains("\\trowd")); assertTrue(rtf.contains("\\trowd"));
assertEquals(33, countMatches(rtf, Pattern.compile("\\\\row(?![a-zA-Z])"))); assertEquals(33, countMatches(rtf, Pattern.compile("\\\\row(?![a-zA-Z])")));
assertArrayEquals(new int[]{15, 11, 8, 8, 4, 10, 10, 5, 5, 5, 5, 7, 8, 3, 3, 8,
8, 6, 9, 3, 8, 8, 2, 2, 2, 2, 2, 19, 16, 1, 1, 1, 1},
countCellsByRow(rtf));
assertTrue(rtf.contains(toRtfUnicode("张三"))); assertTrue(rtf.contains(toRtfUnicode("张三")));
assertTrue(rtf.contains("13800138000")); assertTrue(rtf.contains("13800138000"));
assertTrue(rtf.contains(toRtfUnicode("污水处理工"))); assertTrue(rtf.contains(toRtfUnicode("焊工")));
assertTrue(rtf.contains(toRtfUnicode("电工")));
assertTrue(rtf.contains(toRtfUnicode("示例水务公司"))); assertTrue(rtf.contains(toRtfUnicode("示例水务公司")));
assertTrue(rtf.contains("CERT-001")); assertTrue(rtf.contains("CERT-001"));
assertFalse(rtf.contains(toRtfGbkHex("污水处理工(污泥处理工)")));
assertFalse(rtf.contains("WordDocument")); assertFalse(rtf.contains("WordDocument"));
// 使用JDK自带RTF解析器真实回读;去除单元格间空白后,18位身份证必须完整可见。 // 使用JDK自带RTF解析器真实回读;去除单元格间空白后,18位身份证必须完整可见。
...@@ -57,7 +65,14 @@ class B2TemplateInspectionTest { ...@@ -57,7 +65,14 @@ class B2TemplateInspectionTest {
String compactText = visibleText.replaceAll("\\s+", ""); String compactText = visibleText.replaceAll("\\s+", "");
assertTrue(visibleText.contains("张三")); assertTrue(visibleText.contains("张三"));
assertTrue(compactText.contains("310101199506011234")); assertTrue(compactText.contains("310101199506011234"));
assertTrue(visibleText.contains("污水处理工")); // 静态模板文字使用GBK十六进制,JDK解析器会乱码;日期值由Unicode/ASCII动态写入,直接校验三格。
int birthdayIndex = rtf.indexOf("1995");
assertTrue(rtf.contains("\\fs16 1995"),
birthdayIndex < 0 ? "birthday missing" : rtf.substring(birthdayIndex - 24, birthdayIndex + 16));
assertTrue(rtf.contains("\\fs16 06"));
assertTrue(rtf.contains("\\fs16 01"));
assertTrue(visibleText.contains("焊工"));
assertTrue(visibleText.contains("电工"));
} }
private int countMatches(String source, Pattern pattern) { private int countMatches(String source, Pattern pattern) {
...@@ -67,6 +82,20 @@ class B2TemplateInspectionTest { ...@@ -67,6 +82,20 @@ class B2TemplateInspectionTest {
return count; return count;
} }
/** 按RTF真实行、单元格结束符统计结构,避免仅校验总行数而遗漏合并单元格变化。 */
private int[] countCellsByRow(String rtf) {
Matcher rowMatcher = Pattern.compile("\\\\row(?![a-zA-Z])").matcher(rtf);
int[] counts = new int[33];
int rowStart = 0;
int rowIndex = 0;
while (rowMatcher.find()) {
String row = rtf.substring(rowStart, rowMatcher.start());
counts[rowIndex++] = countMatches(row, Pattern.compile("\\\\cell(?![a-zA-Z])"));
rowStart = rowMatcher.end();
}
return counts;
}
/** 与生产代码相同地把中文转换为RTF Unicode控制字,避免测试依赖本机字符集。 */ /** 与生产代码相同地把中文转换为RTF Unicode控制字,避免测试依赖本机字符集。 */
private String toRtfUnicode(String value) { private String toRtfUnicode(String value) {
StringBuilder escaped = new StringBuilder(); StringBuilder escaped = new StringBuilder();
...@@ -76,4 +105,13 @@ class B2TemplateInspectionTest { ...@@ -76,4 +105,13 @@ class B2TemplateInspectionTest {
} }
return escaped.toString(); return escaped.toString();
} }
/** WPS另存的模板静态中文使用GBK十六进制字节,按同一编码构造可重复的原文断言。 */
private String toRtfGbkHex(String value) {
StringBuilder escaped = new StringBuilder();
for (byte valueByte : value.getBytes(Charset.forName("GBK"))) {
escaped.append("\\'").append(String.format("%02x", valueByte & 0xff));
}
return escaped.toString();
}
} }
...@@ -52,6 +52,20 @@ class StudentCertificationBatchExportTest { ...@@ -52,6 +52,20 @@ class StudentCertificationBatchExportTest {
assertTrue(names.get(1).contains("_2.doc")); assertTrue(names.get(1).contains("_2.doc"));
} }
@Test void singleExportShouldKeepLegacyDocDownloadContract() throws Exception {
// B2模板内部使用RTF保存版式,但对管理端仍必须维持原有DOC扩展名和Word媒体类型。
when(mapper.selectProfile("1", false)).thenReturn(profile("1", "张三", "310101199506011234"));
MockHttpServletResponse response = new MockHttpServletResponse();
service.exportOne("1", response);
assertEquals("application/msword", response.getContentType());
assertTrue(response.getHeader("Content-Disposition").contains("UTF-8''"));
assertTrue(response.getHeader("Content-Disposition").endsWith(".doc"));
assertTrue(new String(response.getContentAsByteArray(), java.nio.charset.StandardCharsets.US_ASCII)
.startsWith("{\\rtf1"));
}
@Test void moreThanFiveHundredSelectedStudentsShouldBeRejectedBeforeQuery() { @Test void moreThanFiveHundredSelectedStudentsShouldBeRejectedBeforeQuery() {
StudentCertificationExportDTO request=new StudentCertificationExportDTO(); StudentCertificationExportDTO request=new StudentCertificationExportDTO();
List<String> ids=new ArrayList<>(); for(int i=0;i<501;i++) ids.add(String.valueOf(i)); request.setStudentIds(ids); List<String> ids=new ArrayList<>(); for(int i=0;i<501;i++) ids.add(String.valueOf(i)); request.setStudentIds(ids);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment