liu
2 天以前 104db2c4423867a7fa4f41343213f45985ae3110
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
package cn.iocoder.yudao.module.mes.service.pd.document;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.mes.controller.admin.pd.document.vo.MesPdDocumentPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pd.document.vo.MesPdDocumentSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.document.MesPdDocumentDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pd.document.MesPdDocumentMapper;
import cn.iocoder.yudao.module.mes.service.pd.project.MesPdProjectService;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
import cn.iocoder.yudao.module.system.api.storage.dto.StorageBlobRespDTO;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDateTime;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_DOCUMENT_NOT_EXISTS;
 
/**
 * MES 设计资料 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesPdDocumentServiceImpl implements MesPdDocumentService {
 
    @Resource
    private MesPdDocumentMapper documentMapper;
 
    @Resource
    @Lazy
    private MesPdProjectService projectService;
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
 
    @Override
    public Long createDocument(MesPdDocumentSaveReqVO createReqVO) {
        // 校验设计项目存在
        projectService.validateProjectExists(createReqVO.getProjectId());
        // 插入
        MesPdDocumentDO document = BeanUtils.toBean(createReqVO, MesPdDocumentDO.class);
        // 补充上传人、上传时间
        if (document.getUploadUserId() == null) {
            document.setUploadUserId(SecurityFrameworkUtils.getLoginUserId());
        }
        if (document.getUploadTime() == null) {
            document.setUploadTime(LocalDateTime.now());
        }
        documentMapper.insert(document);
        storageAttachmentApi.bindAttachments("file", "mes_pd_document",
                document.getId(), createReqVO.getBlobIds());
        // 未填写文件名时,从上传附件自动补全
        fillFileNameIfAbsent(document);
        return document.getId();
    }
 
    @Override
    public void updateDocument(MesPdDocumentSaveReqVO updateReqVO) {
        // 校验存在
        MesPdDocumentDO existing = validateDocumentExists(updateReqVO.getId());
        // 更新
        MesPdDocumentDO updateObj = BeanUtils.toBean(updateReqVO, MesPdDocumentDO.class);
        // 保留原上传人、上传时间;历史数据为空时补齐
        if (updateObj.getUploadUserId() == null) {
            updateObj.setUploadUserId(existing.getUploadUserId() != null
                    ? existing.getUploadUserId() : SecurityFrameworkUtils.getLoginUserId());
        }
        if (updateObj.getUploadTime() == null) {
            updateObj.setUploadTime(existing.getUploadTime() != null
                    ? existing.getUploadTime() : LocalDateTime.now());
        }
        documentMapper.updateById(updateObj);
        storageAttachmentApi.updateAttachments("file", "mes_pd_document", updateReqVO.getId(), updateReqVO.getBlobIds());
        // 未填写文件名时,从上传附件自动补全
        fillFileNameIfAbsent(updateObj);
    }
 
    @Override
    public void deleteDocument(Long id) {
        // 校验存在
        validateDocumentExists(id);
        // 删除附件
        storageAttachmentApi.deleteAttachmentsByRecord("mes_pd_document", id);
        // 删除
        documentMapper.deleteById(id);
    }
 
    @Override
    public MesPdDocumentDO getDocument(Long id) {
        return documentMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesPdDocumentDO> getDocumentPage(MesPdDocumentPageReqVO pageReqVO) {
        return documentMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<MesPdDocumentDO> getDocumentListByProjectId(Long projectId) {
        return documentMapper.selectListByProjectId(projectId);
    }
 
    @Override
    public void deleteByProjectId(Long projectId) {
        List<MesPdDocumentDO> documents = documentMapper.selectListByProjectId(projectId);
        for (MesPdDocumentDO document : documents) {
            storageAttachmentApi.deleteAttachmentsByRecord("mes_pd_document", document.getId());
        }
        documentMapper.deleteByProjectId(projectId);
    }
 
    private MesPdDocumentDO validateDocumentExists(Long id) {
        MesPdDocumentDO document = documentMapper.selectById(id);
        if (document == null) {
            throw exception(PD_DOCUMENT_NOT_EXISTS);
        }
        return document;
    }
 
    /**
     * 未填写文件名时,从上传附件自动补全文件名
     *
     * @param document 设计资料
     */
    private void fillFileNameIfAbsent(MesPdDocumentDO document) {
        if (StrUtil.isNotEmpty(document.getFileName())) {
            return;
        }
        List<StorageBlobRespDTO> attachments = storageAttachmentApi.listAttachments("mes_pd_document", document.getId());
        if (CollUtil.isNotEmpty(attachments)) {
            document.setFileName(attachments.get(0).getName());
            documentMapper.updateById(document);
        }
    }
}