10 小时以前 db46314b9c56256a31c9114bd45d9bd63368cbd8
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
package cn.iocoder.yudao.module.srm.service.certificate;
 
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
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.mysql.SrmSupplierCertificateMapper;
import cn.iocoder.yudao.module.srm.enums.ErrorCodeConstants;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDate;
import java.util.List;
 
@Service
@Validated
public class SrmSupplierCertificateServiceImpl implements SrmSupplierCertificateService {
 
    @Resource
    private SrmSupplierCertificateMapper certificateMapper;
 
    @Override
    public Long createCertificate(SrmSupplierCertificateSaveReqVO createReqVO) {
        SrmSupplierCertificateDO certificate = BeanUtils.toBean(createReqVO, SrmSupplierCertificateDO.class);
        updateStatus(certificate);
        certificateMapper.insert(certificate);
        return certificate.getId();
    }
 
    @Override
    public void updateCertificate(SrmSupplierCertificateSaveReqVO updateReqVO) {
        validateCertificateExists(updateReqVO.getId());
        SrmSupplierCertificateDO updateObj = BeanUtils.toBean(updateReqVO, SrmSupplierCertificateDO.class);
        updateStatus(updateObj);
        certificateMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteCertificate(Long id) {
        validateCertificateExists(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));
    }
 
    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); // 有效
            }
        }
    }
 
}