gaoluyang
4 天以前 d39e4f7d839167c16425086dfa2f8747bccfdff9
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
package cn.iocoder.yudao.module.mes.service.pro.workorder;
 
import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemTechnicalFileRespVO;
import cn.iocoder.yudao.module.mdm.service.item.MdmItemTechnicalFileService;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.workorder.MesProWorkOrderTechnicalFileDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.workorder.MesProWorkOrderTechnicalFileMapper;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
 
@Service
public class MesProWorkOrderTechnicalFileService {
    private static final String RECORD_TYPE = "mes_work_order_technical_file";
    @Resource private MdmItemTechnicalFileService itemTechnicalFileService;
    @Resource private MesProWorkOrderTechnicalFileMapper mapper;
    @Resource private StorageAttachmentApi storageApi;
 
    /** 调用方在工单事务中执行,避免清单与工单创建结果不一致。 */
    public void snapshot(Long orderId, Long itemId) {
        var files = itemTechnicalFileService.getTechnicalFileList(itemId);
        for (var file : files) {
            MesProWorkOrderTechnicalFileDO row = new MesProWorkOrderTechnicalFileDO();
            row.setWorkOrderId(orderId);
            row.setSourceFileId(file.getId());
            row.setBlobId(file.getBlobId());
            row.setVersionNo(file.getVersionNo());
            row.setFileName(file.getFileName());
            mapper.insert(row);
        }
        if (!files.isEmpty()) storageApi.bindAttachments("file", RECORD_TYPE, orderId,
                files.stream().map(MdmItemTechnicalFileRespVO::getBlobId).distinct().toList());
    }
 
    public void replace(Long orderId, Long itemId) {
        // 不调用 deleteAttachmentsByRecord:该接口会删除共享的底层文件。
        mapper.delete(MesProWorkOrderTechnicalFileDO::getWorkOrderId, orderId);
        snapshot(orderId, itemId);
    }
 
    public List<MdmItemTechnicalFileRespVO> list(Long orderId) {
        var blobs = storageApi.listAttachments(RECORD_TYPE, orderId);
        var byId = blobs == null ? Map.<Long, cn.iocoder.yudao.module.system.api.storage.dto.StorageBlobRespDTO>of()
                : blobs.stream().collect(Collectors.toMap(b -> b.getId(), Function.identity(), (a, b) -> a));
        return mapper.selectByWorkOrderId(orderId).stream().map(row -> {
            MdmItemTechnicalFileRespVO result = new MdmItemTechnicalFileRespVO();
            result.setId(row.getSourceFileId()); result.setBlobId(row.getBlobId());
            result.setVersionNo(row.getVersionNo()); result.setFileName(row.getFileName());
            var blob = byId.get(row.getBlobId());
            if (blob != null) { result.setPreviewURL(blob.getPreviewURL()); result.setDownloadURL(blob.getDownloadURL()); }
            return result;
        }).toList();
    }
}