liyong
5 天以前 9b386ddaa6c8892d346d1e09b8bea20cb9000f58
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package cn.iocoder.yudao.module.mes.service.pd.project;
 
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.mes.controller.admin.pd.project.vo.MesPdProjectPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pd.project.vo.MesPdProjectSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.document.MesPdDocumentDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.documentaudit.MesPdDocumentAuditDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.archive.MesPdArchiveDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.project.MesPdProjectDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pd.project.MesPdProjectMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.pd.documentaudit.MesPdDocumentAuditMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.pd.archive.MesPdArchiveMapper;
import cn.iocoder.yudao.module.mes.enums.md.autocode.MesMdAutoCodeRuleCodeEnum;
import cn.iocoder.yudao.module.mes.enums.pd.MesPdProjectStatusEnum;
import cn.iocoder.yudao.module.mes.service.md.autocode.MesMdAutoCodeRecordService;
import cn.iocoder.yudao.module.mes.service.pd.document.MesPdDocumentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 设计项目 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MesPdProjectServiceImpl implements MesPdProjectService {
 
    @Resource
    private MesPdProjectMapper projectMapper;
 
    @Resource
    @Lazy
    private MesPdDocumentService documentService;
 
    @Resource
    private MesPdDocumentAuditMapper documentAuditMapper;
 
    @Resource
    private MesPdArchiveMapper archiveMapper;
 
    @Resource
    @Lazy
    private MesMdAutoCodeRecordService autoCodeRecordService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createProject(MesPdProjectSaveReqVO createReqVO) {
        // 校验任务编码唯一
        validateTaskCodeUnique(null, createReqVO.getTaskCode());
        // 插入
        MesPdProjectDO project = MesPdProjectDO.builder()
                .taskCode(createReqVO.getTaskCode())
                .taskName(createReqVO.getTaskName())
                .productCode(createReqVO.getProductCode())
                .productName(createReqVO.getProductName())
                .version(createReqVO.getVersion())
                .status(MesPdProjectStatusEnum.INITIATE.getStatus())
                .chiefDesigner(createReqVO.getChiefDesigner())
                .reviewer(createReqVO.getReviewer())
                .planFinishTime(createReqVO.getPlanFinishTime())
                .remark(createReqVO.getRemark())
                .build();
        projectMapper.insert(project);
        return project.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProject(MesPdProjectSaveReqVO updateReqVO) {
        // 校验存在
        MesPdProjectDO project = validateProjectExists(updateReqVO.getId());
        // 校验状态为待发布
        validateProjectInitiate(project);
        // 校验任务编码唯一
        validateTaskCodeUnique(updateReqVO.getId(), updateReqVO.getTaskCode());
        // 更新
        MesPdProjectDO updateObj = MesPdProjectDO.builder()
                .id(updateReqVO.getId())
                .taskCode(updateReqVO.getTaskCode())
                .taskName(updateReqVO.getTaskName())
                .productCode(updateReqVO.getProductCode())
                .productName(updateReqVO.getProductName())
                .version(updateReqVO.getVersion())
                .chiefDesigner(updateReqVO.getChiefDesigner())
                .reviewer(updateReqVO.getReviewer())
                .planFinishTime(updateReqVO.getPlanFinishTime())
                .remark(updateReqVO.getRemark())
                .build();
        projectMapper.updateById(updateObj);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProject(Long id) {
        // 校验存在
        MesPdProjectDO project = validateProjectExists(id);
        // 校验状态为待发布
        validateProjectInitiate(project);
        // 删除资料子表
        documentService.deleteByProjectId(id);
        // 删除
        projectMapper.deleteById(id);
    }
 
    @Override
    public MesPdProjectDO validateProjectExists(Long id) {
        MesPdProjectDO project = projectMapper.selectById(id);
        if (project == null) {
            throw exception(PD_PROJECT_NOT_EXISTS);
        }
        return project;
    }
 
    @Override
    public MesPdProjectDO getProject(Long id) {
        return projectMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesPdProjectDO> getProjectPage(MesPdProjectPageReqVO pageReqVO) {
        return projectMapper.selectPage(pageReqVO);
    }
 
    @Override
    public void publishProject(Long id) {
        // 校验存在
        MesPdProjectDO project = validateProjectExists(id);
        // 校验状态为待发布
        if (!MesPdProjectStatusEnum.INITIATE.getStatus().equals(project.getStatus())) {
            throw exception(PD_PROJECT_NOT_INITIATE_FOR_PUBLISH);
        }
        // 更新状态
        projectMapper.updateById(MesPdProjectDO.builder()
                .id(id)
                .status(MesPdProjectStatusEnum.PUBLISH.getStatus())
                .build());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uploadProject(Long id, Long userId) {
        // 校验存在
        MesPdProjectDO project = validateProjectExists(id);
        // 校验状态为待上传
        if (!MesPdProjectStatusEnum.PUBLISH.getStatus().equals(project.getStatus())) {
            throw exception(PD_PROJECT_NOT_PUBLISH_FOR_UPLOAD);
        }
        // 更新项目状态为待审核
        projectMapper.updateById(MesPdProjectDO.builder()
                .id(id)
                .status(MesPdProjectStatusEnum.UPLOAD.getStatus())
                .build());
        // 自动为该项目下所有资料创建审核记录
        List<MesPdDocumentDO> documents = documentService.getDocumentListByProjectId(id);
        for (MesPdDocumentDO document : documents) {
            MesPdDocumentAuditDO audit = MesPdDocumentAuditDO.builder()
                    .auditNo(generateAuditNo())
                    .documentId(document.getId())
                    .documentName(document.getDocumentName())
                    .documentVersion(document.getDocumentVersion())
                    .reviewer(project.getReviewer())
                    .auditStatus(0) // 待审核
                    .build();
            documentAuditMapper.insert(audit);
        }
    }
 
    @Override
    public void auditProject(Long id) {
        // 校验存在
        MesPdProjectDO project = validateProjectExists(id);
        // 校验状态为待审核
        if (!MesPdProjectStatusEnum.UPLOAD.getStatus().equals(project.getStatus())) {
            throw exception(PD_PROJECT_NOT_UPLOAD_FOR_AUDIT);
        }
        // 校验该项目下所有资料审核记录均已审核(不存在待审核的记录)
        List<MesPdDocumentDO> documents = documentService.getDocumentListByProjectId(id);
        List<Long> documentIds = documents.stream().map(MesPdDocumentDO::getId).toList();
        if (documentAuditMapper.existsPendingAuditByDocumentIds(documentIds)) {
            throw exception(PD_PROJECT_AUDIT_NOT_FINISHED);
        }
        // 更新状态
        projectMapper.updateById(MesPdProjectDO.builder()
                .id(id)
                .status(MesPdProjectStatusEnum.AUDIT.getStatus())
                .build());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void archiveProject(Long id, Long userId) {
        // 校验存在
        MesPdProjectDO project = validateProjectExists(id);
        // 校验状态为待归档
        if (!MesPdProjectStatusEnum.AUDIT.getStatus().equals(project.getStatus())) {
            throw exception(PD_PROJECT_NOT_AUDIT_FOR_ARCHIVE);
        }
        // 更新项目状态为已归档
        projectMapper.updateById(MesPdProjectDO.builder()
                .id(id)
                .status(MesPdProjectStatusEnum.ARCHIVE.getStatus())
                .build());
        // 自动创建归档台账记录
        MesPdArchiveDO archive = MesPdArchiveDO.builder()
                .archiveCode(generateArchiveCode())
                .projectId(id)
                .archiver(userId)
                .archiveStatus(1) // 已归档
                .archiveTime(LocalDateTime.now())
                .build();
        archiveMapper.insert(archive);
    }
 
    @Override
    public Map<Long, MesPdProjectDO> getProjectMap(Collection<Long> ids) {
        List<MesPdProjectDO> list = projectMapper.selectBatchIds(ids);
        return convertMap(list, MesPdProjectDO::getId);
    }
 
    @Override
    public List<MesPdProjectDO> getProjectByProductCode(String productCode) {
        return projectMapper.selectList(new QueryWrapper<MesPdProjectDO>().lambda().eq(MesPdProjectDO::getProductCode,productCode).eq(MesPdProjectDO::getStatus, MesPdProjectStatusEnum.ARCHIVE.getStatus()));
    }
 
    private void validateProjectInitiate(MesPdProjectDO project) {
        if (!MesPdProjectStatusEnum.INITIATE.getStatus().equals(project.getStatus())) {
            throw exception(PD_PROJECT_NOT_INITIATE);
        }
    }
 
    private void validateTaskCodeUnique(Long id, String taskCode) {
        MesPdProjectDO existing = projectMapper.selectByTaskCode(taskCode);
        if (existing == null) {
            return;
        }
        // 如果 id 为空,说明是新增,编码重复
        if (id == null) {
            throw exception(PD_PROJECT_CODE_DUPLICATE);
        }
        // 如果 id 不为空,说明是修改,编码重复且不是同一条记录
        if (!existing.getId().equals(id)) {
            throw exception(PD_PROJECT_CODE_DUPLICATE);
        }
    }
 
    /**
     * 生成审核单号(使用编码规则)
     */
    private String generateAuditNo() {
        return autoCodeRecordService.generateAutoCode(MesMdAutoCodeRuleCodeEnum.PD_DOCUMENT_AUDIT_CODE.getCode());
    }
 
    /**
     * 生成归档编号(使用编码规则)
     */
    private String generateArchiveCode() {
        return autoCodeRecordService.generateAutoCode(MesMdAutoCodeRuleCodeEnum.PD_ARCHIVE_CODE.getCode());
    }
 
}