gaoluyang
2026-10-16 7d6825d2344db398b7cd142fea419242db033c54
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
package cn.iocoder.yudao.module.hrm.controller.admin.certificate;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hrm.controller.admin.certificate.vo.HrmCertificateImportExcelVO;
import cn.iocoder.yudao.module.hrm.controller.admin.certificate.vo.HrmCertificateImportReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.certificate.vo.HrmCertificateImportRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.certificate.vo.HrmCertificatePageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.certificate.vo.HrmCertificateRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.certificate.vo.HrmCertificateSaveReqVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.certificate.HrmCertificateDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
import cn.iocoder.yudao.module.hrm.service.certificate.HrmCertificateService;
import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService;
import cn.iocoder.yudao.module.hrm.service.remind.HrmExpiryRemindService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.io.IOException;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
 
@Tag(name = "管理后台 - HRM 证书资质")
@RestController
@RequestMapping("/hrm/certificate")
@Validated
public class HrmCertificateController {
 
    @Resource
    private HrmCertificateService certificateService;
    @Resource
    private HrmEmployeeService employeeService;
    @Resource
    private HrmExpiryRemindService expiryRemindService;
 
    @PostMapping("/create")
    @Operation(summary = "创建证书资质")
    @PreAuthorize("@ss.hasPermission('hrm:certificate:create')")
    public CommonResult<Long> createCertificate(@Valid @RequestBody HrmCertificateSaveReqVO createReqVO) {
        return success(certificateService.createCertificate(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新证书资质")
    @PreAuthorize("@ss.hasPermission('hrm:certificate:update')")
    public CommonResult<Boolean> updateCertificate(@Valid @RequestBody HrmCertificateSaveReqVO updateReqVO) {
        certificateService.updateCertificate(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除证书资质")
    @Parameter(name = "ids", description = "编号数组", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:certificate:delete')")
    public CommonResult<Boolean> deleteCertificate(@RequestParam("ids") List<Long> ids) {
        certificateService.deleteCertificate(ids);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得证书资质")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:certificate:query')")
    public CommonResult<HrmCertificateRespVO> getCertificate(@RequestParam("id") Long id) {
        HrmCertificateDO certificate = certificateService.getCertificate(id);
        return success(certificate != null ? buildRespVO(certificate) : null);
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得证书资质分页")
    @PreAuthorize("@ss.hasPermission('hrm:certificate:query')")
    public CommonResult<PageResult<HrmCertificateRespVO>> getCertificatePage(@Valid HrmCertificatePageReqVO pageReqVO) {
        // 懒触发:有人打开证书列表时顺带补扫即将到期证书并发送站内信提醒(best-effort)
        try {
            expiryRemindService.sendCertificateExpireRemind();
        } catch (Exception ignored) {
        }
        PageResult<HrmCertificateDO> pageResult = certificateService.getCertificatePage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }
        Map<Long, HrmEmployeeDO> employeeMap = employeeService.getEmployeeList(
                convertSet(pageResult.getList(), HrmCertificateDO::getEmployeeId)).stream()
                .collect(java.util.stream.Collectors.toMap(HrmEmployeeDO::getId, e -> e, (a, b) -> a));
        return success(BeanUtils.toBean(pageResult, HrmCertificateRespVO.class,
                vo -> buildRespVOItem(vo, employeeMap.get(vo.getEmployeeId()))));
    }
 
    private HrmCertificateRespVO buildRespVO(HrmCertificateDO certificate) {
        HrmCertificateRespVO vo = BeanUtils.toBean(certificate, HrmCertificateRespVO.class);
        HrmEmployeeDO employee = employeeService.getEmployee(certificate.getEmployeeId());
        buildRespVOItem(vo, employee);
        return vo;
    }
 
    private void buildRespVOItem(HrmCertificateRespVO vo, HrmEmployeeDO employee) {
        if (employee != null) {
            vo.setEmployeeName(employee.getName());
        }
        vo.setCertTypeName(convertCertTypeName(vo.getCertType()));
        vo.setExpireStatus(calcExpireStatus(vo.getExpireDate()));
    }
 
    private String convertCertTypeName(Integer certType) {
        return switch (certType == null ? 0 : certType) {
            case 1 -> "特种作业证书";
            case 2 -> "从业资质";
            case 3 -> "技能等级证书";
            case 4 -> "安全培训合格证";
            default -> "其他";
        };
    }
 
    /**
     * 到期状态:0=有效,1=即将到期(30天内),2=已过期
     */
    private Integer calcExpireStatus(LocalDate expireDate) {
        if (expireDate == null) {
            return 0;
        }
        LocalDate today = LocalDate.now();
        if (expireDate.isBefore(today)) {
            return 2;
        }
        if (expireDate.isBefore(today.plusDays(30))) {
            return 1;
        }
        return 0;
    }
 
    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入证书资质模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<HrmCertificateImportExcelVO> list = Arrays.asList(
                HrmCertificateImportExcelVO.builder().employeeName("张三").certType(1).certName("低压电工作业证")
                        .certNo("T20260001").issueOrg("应急管理局").issueDate(LocalDate.of(2026, 1, 1))
                        .expireDate(LocalDate.of(2029, 1, 1)).remark("").build()
        );
        // 输出
        ExcelUtils.write(response, "证书资质导入模板.xls", "证书资质列表", HrmCertificateImportExcelVO.class, list);
    }
 
    @PostMapping("/import")
    @Operation(summary = "导入证书资质")
    @PreAuthorize("@ss.hasPermission('hrm:certificate:import')")
    public CommonResult<HrmCertificateImportRespVO> importExcel(@Valid HrmCertificateImportReqVO importReqVO)
            throws Exception {
        List<HrmCertificateImportExcelVO> list = ExcelUtils.read(importReqVO.getFile(), HrmCertificateImportExcelVO.class);
        return success(certificateService.importCertificateList(list, importReqVO));
    }
 
}