云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
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
package cn.iocoder.yudao.module.hrm.service.certificate;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
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.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.dal.mysql.certificate.HrmCertificateMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.employee.HrmEmployeeMapper;
import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
 
/**
 * HRM 证书资质 Service 实现类
 *
 * @author xiaoyi
 */
@Service
@Validated
public class HrmCertificateServiceImpl implements HrmCertificateService {
 
    @Resource
    private HrmCertificateMapper certificateMapper;
    @Resource
    private HrmEmployeeService employeeService;
    @Resource
    private HrmEmployeeMapper employeeMapper;
 
    @Override
    public Long createCertificate(HrmCertificateSaveReqVO createReqVO) {
        validateEmployee(createReqVO.getEmployeeId());
        HrmCertificateDO certificate = BeanUtils.toBean(createReqVO, HrmCertificateDO.class);
        certificateMapper.insert(certificate);
        return certificate.getId();
    }
 
    @Override
    public void updateCertificate(HrmCertificateSaveReqVO updateReqVO) {
        validateCertificateExists(updateReqVO.getId());
        validateEmployee(updateReqVO.getEmployeeId());
        certificateMapper.updateById(BeanUtils.toBean(updateReqVO, HrmCertificateDO.class));
    }
 
    @Override
    public void deleteCertificate(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        for (Long id : ids) {
            validateCertificateExists(id);
            certificateMapper.deleteById(id);
        }
    }
 
    @Override
    public HrmCertificateDO getCertificate(Long id) {
        return certificateMapper.selectById(id);
    }
 
    @Override
    public PageResult<HrmCertificateDO> getCertificatePage(HrmCertificatePageReqVO pageReqVO) {
        return certificateMapper.selectPage(pageReqVO);
    }
 
    private void validateEmployee(Long employeeId) {
        if (employeeService.getEmployee(employeeId) == null) {
            throw exception(HRM_EMPLOYEE_NOT_EXISTS);
        }
    }
 
    private void validateCertificateExists(Long id) {
        if (certificateMapper.selectById(id) == null) {
            throw exception(HRM_CERTIFICATE_NOT_EXISTS);
        }
    }
 
    @Override
    public HrmCertificateImportRespVO importCertificateList(
            List<HrmCertificateImportExcelVO> importList, HrmCertificateImportReqVO importReqVO) {
        // 1. 校验非空
        importList = filterList(importList, item -> StrUtil.isNotBlank(item.getCertName()));
        if (CollUtil.isEmpty(importList)) {
            throw exception(CERTIFICATE_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 逐条处理
        HrmCertificateImportRespVO respVO = HrmCertificateImportRespVO.builder()
                .createNames(new ArrayList<>())
                .updateNames(new ArrayList<>())
                .failureNames(new LinkedHashMap<>())
                .build();
        importList.forEach(importCertificate -> {
            try {
                // 2.1 解析员工
                if (StrUtil.isBlank(importCertificate.getEmployeeName())) {
                    respVO.getFailureNames().put(importCertificate.getCertName(), "员工姓名不能为空");
                    return;
                }
                HrmEmployeeDO employee = employeeMapper.selectByName(importCertificate.getEmployeeName());
                if (employee == null) {
                    throw exception(CERTIFICATE_EMPLOYEE_NOT_EXISTS, importCertificate.getEmployeeName());
                }
                // 2.2 判断是否存在(按员工+证书编号或员工+证书名称唯一)
                HrmCertificateDO existCertificate = StrUtil.isNotBlank(importCertificate.getCertNo())
                        ? certificateMapper.selectByEmployeeIdAndCertNo(employee.getId(), importCertificate.getCertNo())
                        : certificateMapper.selectByEmployeeIdAndCertName(employee.getId(), importCertificate.getCertName());
                if (existCertificate == null) {
                    // 2.2.1 新增
                    HrmCertificateDO certificate = BeanUtils.toBean(importCertificate, HrmCertificateDO.class)
                            .setEmployeeId(employee.getId());
                    certificateMapper.insert(certificate);
                    respVO.getCreateNames().add(importCertificate.getCertName());
                    return;
                }
                // 2.2.2 已存在,判断是否允许更新
                if (!importReqVO.getUpdateSupport()) {
                    respVO.getFailureNames().put(importCertificate.getCertName(),
                            StrUtil.format(CERTIFICATE_EXISTS.getMsg(), importCertificate.getCertName()));
                    return;
                }
                HrmCertificateDO updateCertificate = BeanUtils.toBean(importCertificate, HrmCertificateDO.class)
                        .setId(existCertificate.getId()).setEmployeeId(employee.getId());
                certificateMapper.updateById(updateCertificate);
                respVO.getUpdateNames().add(importCertificate.getCertName());
            } catch (ServiceException ex) {
                respVO.getFailureNames().put(importCertificate.getCertName(), ex.getMessage());
            }
        });
        return respVO;
    }
 
}