xiaoyi
8 天以前 481d8165f26fc7e37f0e87f6444ac0340bb563a3
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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 jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
 
@Service
@Validated
public class SrmSupplierCertificateServiceImpl implements SrmSupplierCertificateService {
 
    /**
     * 资质证书附件的业务记录类型,对应 {@link cn.iocoder.yudao.module.system.enums.storage.StorageRecordTypeEnum#SRM_SUPPLIER_CERTIFICATE}
     */
    public static final String CERTIFICATE_RECORD_TYPE = "srm_supplier_certificate";
 
    @Resource
    private SrmSupplierCertificateMapper certificateMapper;
 
    @Resource
    private SrmSupplierMapper supplierMapper;
 
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCertificate(SrmSupplierCertificateSaveReqVO createReqVO) {
        SrmSupplierCertificateDO certificate = BeanUtils.toBean(createReqVO, SrmSupplierCertificateDO.class);
        updateStatus(certificate);
        certificateMapper.insert(certificate);
        // 绑定证书附件
        if (CollUtil.isNotEmpty(createReqVO.getBlobIds())) {
            storageAttachmentApi.bindAttachments("file", CERTIFICATE_RECORD_TYPE, certificate.getId(), createReqVO.getBlobIds());
        }
        return certificate.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCertificate(SrmSupplierCertificateSaveReqVO updateReqVO) {
        validateCertificateExists(updateReqVO.getId());
        SrmSupplierCertificateDO updateObj = BeanUtils.toBean(updateReqVO, SrmSupplierCertificateDO.class);
        updateStatus(updateObj);
        certificateMapper.updateById(updateObj);
        // 增量更新证书附件
        storageAttachmentApi.updateAttachments("file", CERTIFICATE_RECORD_TYPE, updateReqVO.getId(), updateReqVO.getBlobIds());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCertificate(Long id) {
        validateCertificateExists(id);
        // 删除证书附件
        storageAttachmentApi.deleteAttachmentsByRecord(CERTIFICATE_RECORD_TYPE, id);
        certificateMapper.deleteById(id);
    }
 
    @Override
    public SrmSupplierCertificateDO getCertificate(Long id) {
        return certificateMapper.selectById(id);
    }
 
    @Override
    public PageResult<SrmSupplierCertificateDO> getCertificatePage(SrmSupplierCertificatePageReqVO pageReqVO) {
        return certificateMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<SrmSupplierCertificateDO> getCertificateListBySupplierId(Long supplierId) {
        return certificateMapper.selectListBySupplierId(supplierId);
    }
 
    @Override
    public List<SrmSupplierCertificateDO> getExpiringCertificates() {
        LocalDate now = LocalDate.now();
        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);
        }
    }
 
    private void updateStatus(SrmSupplierCertificateDO certificate) {
        if (certificate.getValidEndDate() != null) {
            LocalDate now = LocalDate.now();
            if (certificate.getValidEndDate().isBefore(now)) {
                certificate.setStatus(1); // 已过期
            } else if (certificate.getValidEndDate().isBefore(now.plusDays(30))) {
                certificate.setStatus(2); // 即将过期
            } else {
                certificate.setStatus(0); // 有效
            }
        }
    }
 
}