云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/certificate/HrmCertificateServiceImpl.java
@@ -1,22 +1,32 @@
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.module.hrm.enums.ErrorCodeConstants.HRM_CERTIFICATE_NOT_EXISTS;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.HRM_EMPLOYEE_NOT_EXISTS;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
/**
 * HRM 证书资质 Service 实现类
@@ -31,6 +41,8 @@
    private HrmCertificateMapper certificateMapper;
    @Resource
    private HrmEmployeeService employeeService;
    @Resource
    private HrmEmployeeMapper employeeMapper;
    @Override
    public Long createCertificate(HrmCertificateSaveReqVO createReqVO) {
@@ -80,4 +92,58 @@
        }
    }
    @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;
    }
}