xiaoyi
3 天以前 abf1c0a35d85d38239ff5d2ed746a4e4b797c9d1
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
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.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 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.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.time.LocalDate;
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;
 
    @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) {
        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;
    }
 
}