| | |
| | | package cn.iocoder.yudao.module.srm.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.collection.CollectionUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.certificate.vo.SrmSupplierCertificateImportExcelVO; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.certificate.vo.SrmSupplierCertificateImportReqVO; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.certificate.vo.SrmSupplierCertificateImportRespVO; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.certificate.vo.SrmSupplierCertificatePageReqVO; |
| | | import cn.iocoder.yudao.module.srm.controller.admin.certificate.vo.SrmSupplierCertificateSaveReqVO; |
| | | import cn.iocoder.yudao.module.srm.dal.dataobject.SrmSupplierCertificateDO; |
| | | import cn.iocoder.yudao.module.srm.dal.dataobject.SrmSupplierDO; |
| | | import cn.iocoder.yudao.module.srm.dal.mysql.SrmSupplierCertificateMapper; |
| | | import cn.iocoder.yudao.module.srm.dal.mysql.SrmSupplierMapper; |
| | | import cn.iocoder.yudao.module.srm.enums.ErrorCodeConstants; |
| | | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; |
| | | import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | |
| | | |
| | | @Resource |
| | | private SrmSupplierCertificateMapper certificateMapper; |
| | | |
| | | @Resource |
| | | private SrmSupplierMapper supplierMapper; |
| | | |
| | | @Resource |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | |
| | | return certificateMapper.selectExpiringList(now, now.plusDays(30)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public SrmSupplierCertificateImportRespVO importCertificateList(List<SrmSupplierCertificateImportExcelVO> importCertificates, |
| | | SrmSupplierCertificateImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importCertificates = CollectionUtils.filterList(importCertificates, |
| | | item -> StrUtil.isNotBlank(item.getCertificateName())); |
| | | if (CollUtil.isEmpty(importCertificates)) { |
| | | throw ServiceExceptionUtil.exception(ErrorCodeConstants.CERTIFICATE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐条处理 |
| | | SrmSupplierCertificateImportRespVO respVO = SrmSupplierCertificateImportRespVO.builder() |
| | | .createCertificateNames(new ArrayList<>()) |
| | | .updateCertificateNames(new ArrayList<>()) |
| | | .failureCertificateNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importCertificates.forEach(importCertificate -> { |
| | | try { |
| | | // 2.1 解析供应商 |
| | | if (StrUtil.isBlank(importCertificate.getSupplierName())) { |
| | | throw ServiceExceptionUtil.exception(ErrorCodeConstants.CERTIFICATE_IMPORT_SUPPLIER_NAME_REQUIRED); |
| | | } |
| | | SrmSupplierDO supplier = supplierMapper.selectByName(importCertificate.getSupplierName()); |
| | | if (supplier == null) { |
| | | throw ServiceExceptionUtil.exception(ErrorCodeConstants.CERTIFICATE_IMPORT_SUPPLIER_NOT_EXISTS, |
| | | importCertificate.getSupplierName()); |
| | | } |
| | | // 2.2 判断是否存在 |
| | | SrmSupplierCertificateDO existCertificate = certificateMapper.selectBySupplierIdAndCertificateName( |
| | | supplier.getId(), importCertificate.getCertificateName()); |
| | | if (existCertificate == null) { |
| | | // 2.2.1 新增 |
| | | SrmSupplierCertificateDO certificate = BeanUtils.toBean(importCertificate, SrmSupplierCertificateDO.class) |
| | | .setSupplierId(supplier.getId()); |
| | | updateStatus(certificate); |
| | | certificateMapper.insert(certificate); |
| | | respVO.getCreateCertificateNames().add(importCertificate.getCertificateName()); |
| | | return; |
| | | } |
| | | // 2.2.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureCertificateNames().put(importCertificate.getCertificateName(), |
| | | StrUtil.format(ErrorCodeConstants.CERTIFICATE_IMPORT_NAME_EXISTS.getMsg(), importCertificate.getCertificateName())); |
| | | return; |
| | | } |
| | | SrmSupplierCertificateDO updateCertificate = BeanUtils.toBean(importCertificate, SrmSupplierCertificateDO.class) |
| | | .setId(existCertificate.getId()).setSupplierId(supplier.getId()); |
| | | updateStatus(updateCertificate); |
| | | certificateMapper.updateById(updateCertificate); |
| | | respVO.getUpdateCertificateNames().add(importCertificate.getCertificateName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureCertificateNames().put(importCertificate.getCertificateName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | private void validateCertificateExists(Long id) { |
| | | if (certificateMapper.selectById(id) == null) { |
| | | throw ServiceExceptionUtil.exception(ErrorCodeConstants.CERTIFICATE_NOT_EXISTS); |