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 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 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 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; } }