liu
7 天以前 ee982b85c9ff09fbf6a6eb7c133b1e6f5fb160ba
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
package cn.iocoder.yudao.module.srm.controller.admin.certificate;
 
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
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.*;
import cn.iocoder.yudao.module.srm.dal.dataobject.SrmSupplierDO;
import cn.iocoder.yudao.module.srm.service.certificate.SrmSupplierCertificateService;
import cn.iocoder.yudao.module.srm.service.certificate.SrmSupplierCertificateServiceImpl;
import cn.iocoder.yudao.module.srm.service.supplier.SrmSupplierService;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
 
@Tag(name = "管理后台 - SRM 供应商资质")
@RestController
@RequestMapping("/srm/supplier-certificate")
@Validated
public class SrmSupplierCertificateController {
 
    @Resource
    private SrmSupplierCertificateService certificateService;
 
    @Resource
    private SrmSupplierService supplierService;
 
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
 
    @PostMapping("/create")
    @Operation(summary = "创建资质证书")
    @PreAuthorize("@ss.hasPermission('srm:supplier-certificate:create')")
    public CommonResult<Long> createCertificate(@Valid @RequestBody SrmSupplierCertificateSaveReqVO createReqVO) {
        return success(certificateService.createCertificate(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新资质证书")
    @PreAuthorize("@ss.hasPermission('srm:supplier-certificate:update')")
    public CommonResult<Boolean> updateCertificate(@Valid @RequestBody SrmSupplierCertificateSaveReqVO updateReqVO) {
        certificateService.updateCertificate(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除资质证书")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('srm:supplier-certificate:delete')")
    public CommonResult<Boolean> deleteCertificate(@RequestParam("id") Long id) {
        certificateService.deleteCertificate(id);
        return success(true);
    }
 
    @GetMapping("/page")
    @Operation(summary = "获取资质证书分页")
    @PreAuthorize("@ss.hasPermission('srm:supplier-certificate:query')")
    public CommonResult<PageResult<SrmSupplierCertificateRespVO>> getCertificatePage(@Valid SrmSupplierCertificatePageReqVO pageReqVO) {
        var pageResult = certificateService.getCertificatePage(pageReqVO);
        var respList = BeanUtils.toBean(pageResult.getList(), SrmSupplierCertificateRespVO.class);
        enrichSupplierNames(respList);
        enrichAttachments(respList);
        return success(new PageResult<>(respList, pageResult.getTotal()));
    }
 
    @GetMapping("/list-by-supplier")
    @Operation(summary = "根据供应商ID获取资质列表")
    @PreAuthorize("@ss.hasPermission('srm:supplier-certificate:query')")
    public CommonResult<List<SrmSupplierCertificateRespVO>> getListBySupplier(@RequestParam("supplierId") Long supplierId) {
        var list = BeanUtils.toBean(certificateService.getCertificateListBySupplierId(supplierId), SrmSupplierCertificateRespVO.class);
        enrichSupplierNames(list);
        enrichAttachments(list);
        return success(list);
    }
 
    @GetMapping("/expiring-list")
    @Operation(summary = "获取即将到期的资质列表(30天内)")
    @PreAuthorize("@ss.hasPermission('srm:supplier-certificate:query')")
    public CommonResult<List<SrmSupplierCertificateRespVO>> getExpiringList() {
        var list = BeanUtils.toBean(certificateService.getExpiringCertificates(), SrmSupplierCertificateRespVO.class);
        enrichSupplierNames(list);
        return success(list);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获取资质证书详情")
    @PreAuthorize("@ss.hasPermission('srm:supplier-certificate:query')")
    public CommonResult<SrmSupplierCertificateRespVO> getCertificate(@RequestParam("id") Long id) {
        var resp = BeanUtils.toBean(certificateService.getCertificate(id), SrmSupplierCertificateRespVO.class);
        enrichSupplierName(resp);
        enrichAttachments(List.of(resp));
        return success(resp);
    }
 
    // ========== 关联数据填充 ==========
 
    private void enrichSupplierNames(List<SrmSupplierCertificateRespVO> list) {
        if (list == null || list.isEmpty()) return;
        Set<Long> supplierIds = convertSet(list, SrmSupplierCertificateRespVO::getSupplierId);
        Map<Long, SrmSupplierDO> supplierMap = supplierService.getSupplierMap(supplierIds);
        list.forEach(vo -> {
            SrmSupplierDO supplier = supplierMap.get(vo.getSupplierId());
            if (supplier != null) {
                vo.setSupplierName(supplier.getName());
            }
        });
    }
 
    private void enrichSupplierName(SrmSupplierCertificateRespVO vo) {
        if (vo == null || vo.getSupplierId() == null) return;
        SrmSupplierDO supplier = supplierService.getSupplier(vo.getSupplierId());
        if (supplier != null) {
            vo.setSupplierName(supplier.getName());
        }
    }
 
    private void enrichAttachments(List<SrmSupplierCertificateRespVO> list) {
        if (list == null || list.isEmpty()) return;
        list.forEach(vo -> vo.setAttachmentList(storageAttachmentApi.listAttachments(
                SrmSupplierCertificateServiceImpl.CERTIFICATE_RECORD_TYPE, vo.getId())));
    }
 
}