fix(hrm): 员工导入日期列容错解析(支持 2015年 / 2015-01-01 / Excel序列号),修复含非标准日期时整表报系统异常
已修改3个文件
68 ■■■■■ 文件已修改
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/employee/HrmEmployeeController.java 4 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/employee/vo/HrmEmployeeImportExcelVO.java 4 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/employee/HrmEmployeeServiceImpl.java 60 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/employee/HrmEmployeeController.java
@@ -205,8 +205,8 @@
        List<HrmEmployeeImportExcelVO> list = Arrays.asList(
                HrmEmployeeImportExcelVO.builder().name("张三").gender(1).phone("13800138000")
                        .deptName("研发部").postName("部门经理,项目经理").roleNames("普通管理员")
                        .hireDate(LocalDate.of(2020, 3, 1)).employeeStatus(1)
                        .idCard("320102199001150011").birthday(LocalDate.of(1990, 1, 15))
                        .hireDate("2020-03-01").employeeStatus(1)
                        .idCard("320102199001150011").birthday("1990-01-15")
                        .nation("汉族").marriageStatus("已婚").age(36).education("本科").politicalStatus("中共党员")
                        .email("zhangsan@example.com").build()
        );
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/employee/vo/HrmEmployeeImportExcelVO.java
@@ -40,7 +40,7 @@
    private String roleNames;
    @ExcelProperty("入职日期")
    private LocalDate hireDate;
    private String hireDate;
    @ExcelProperty(value = "员工状态", converter = DictConvert.class)
    @DictFormat("hrm_employee_status")
@@ -54,7 +54,7 @@
    private String idCard;
    @ExcelProperty("出生日期")
    private LocalDate birthday;
    private String birthday;
    @ExcelProperty("民族")
    private String nation;
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/employee/HrmEmployeeServiceImpl.java
@@ -459,8 +459,9 @@
        if (StrUtil.isBlank(importEmployee.getPhone())) {
            throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "手机号");
        }
        if (importEmployee.getHireDate() == null) {
            throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "入职日期");
        LocalDate hireDate = parseImportDate(importEmployee.getHireDate());
        if (hireDate == null) {
            throw exception(EMPLOYEE_IMPORT_FIELD_REQUIRED, "入职日期(格式:2015 或 2015-01-01)");
        }
        HrmEmployeeSaveReqVO reqVO = new HrmEmployeeSaveReqVO();
        reqVO.setName(importEmployee.getName());
@@ -469,11 +470,11 @@
        reqVO.setDeptId(deptId);
        reqVO.setPostIds(CollUtil.isEmpty(postIds) ? null : new ArrayList<>(postIds));
        reqVO.setRoleIds(CollUtil.isEmpty(roleIds) ? null : new ArrayList<>(roleIds));
        reqVO.setHireDate(importEmployee.getHireDate());
        reqVO.setHireDate(hireDate);
        reqVO.setEmployeeStatus(importEmployee.getEmployeeStatus() != null
                ? importEmployee.getEmployeeStatus() : 1); // 默认在职
        reqVO.setEmail(importEmployee.getEmail());
        reqVO.setBirthday(importEmployee.getBirthday());
        reqVO.setBirthday(parseImportDate(importEmployee.getBirthday()));
        reqVO.setIdCard(importEmployee.getIdCard());
        reqVO.setNation(importEmployee.getNation());
        reqVO.setMarriageStatus(importEmployee.getMarriageStatus());
@@ -541,6 +542,57 @@
        return roleIds;
    }
    /**
     * 容错解析导入日期文本,支持:2015 / 2015年 / 2015-01 / 2015-01-02 / 2015/1/2 / 2015.1.2 /
     * 2015-01-02 00:00:00 / 20150102,以及 Excel 日期序列号;解析不出返回 null。
     */
    private static LocalDate parseImportDate(String raw) {
        if (StrUtil.isBlank(raw)) {
            return null;
        }
        String s = raw.trim();
        // 去掉时间部分
        int sp = s.indexOf(' ');
        if (sp > 0) {
            s = s.substring(0, sp);
        }
        // 中文年月日 / 斜杠 / 点 / 下划线 统一成 - 分隔
        s = s.replace("年", "-").replace("月", "-").replace("日", "")
                .replace("/", "-").replace(".", "-").replace("_", "-").trim();
        s = s.replaceAll("^-+|-+$", "");
        if (StrUtil.isBlank(s)) {
            return null;
        }
        try {
            if (s.matches("\\d+")) {
                int len = s.length();
                if (len == 4) { // 2015
                    return LocalDate.of(Integer.parseInt(s), 1, 1);
                }
                if (len == 5) { // Excel 日期序列号(1900 日期系统,基准 1899-12-30)
                    return LocalDate.of(1899, 12, 30).plusDays(Long.parseLong(s));
                }
                if (len == 6) { // 201506
                    return LocalDate.of(Integer.parseInt(s.substring(0, 4)), Integer.parseInt(s.substring(4, 6)), 1);
                }
                if (len == 8) { // 20150601
                    return LocalDate.of(Integer.parseInt(s.substring(0, 4)), Integer.parseInt(s.substring(4, 6)), Integer.parseInt(s.substring(6, 8)));
                }
                return null;
            }
            String[] parts = s.split("-");
            if (parts.length == 2) { // 2015-06
                return LocalDate.of(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), 1);
            }
            if (parts.length >= 3) { // 2015-06-01
                return LocalDate.of(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }
    // ========== 转正 ==========
    @Override