lyh
2 天以前 82c1b82a6d46620a5ae493353c37d40da691f716
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package org.jeecg.modules.dnc.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.dnc.constant.DocAttributionTypeEnum;
import org.jeecg.modules.dnc.dto.ComponentHierarchy;
import org.jeecg.modules.dnc.dto.ProcessTraceChain;
import org.jeecg.modules.dnc.entity.*;
import org.jeecg.modules.dnc.mapper.*;
import org.jeecg.modules.dnc.service.IPermissionStreamNewService;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
 
@Service
public class FullHierarchyTraceService {
    @Autowired
    private ProductMixMapper productMixMapper;
    @Autowired
    private ProductInfoMapper productMapper;
    @Autowired
    private ComponentInfoMapper componentMapper;
    @Autowired
    private PartsInfoMapper partsMapper;
    @Autowired
    private ProcessSpecVersionMapper psvMapper;
    @Autowired
    private ProcessStreamMapper processMapper;
    @Autowired
    private WorkStepMapper workStepMapper;
    @Autowired
    private DeviceTypeMapper deviceTypeMapper;
    @Autowired
    private DeviceManagementMapper deviceManagementMapper;
    @Autowired
    private DocInfoMapper docInfoMapper;
    @Autowired
    private DocFileMapper docFileMapper;
    @Autowired
    private CutterMapper cutterMapper;
    @Autowired
    private GuideCardBatchMapper guideCardBatchMapper;
    @Autowired
    private IPermissionStreamNewService permissionStreamNewService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IMdcProductionService mdcProductionService;
 
    /**
     * 产品封装
     * @param docRelative
     * @return
     */
    public ProcessTraceChain traceFromProduct(DocRelative docRelative) {
        ProcessTraceChain chain = initChainWithDocInfo(docRelative);
        traceProductChain(chain, docRelative.getAttributionId());
        List<ProductMix> productMixList=buildFullTreePath(chain);
        chain.setTreePath(productMixList);
        chain.setPermissionStreamNewList(buildFullTreePathPermission(productMixList));
        return chain;
    }
 
    /**
     * 部件封装
     * @param docRelative
     * @return
     */
    public ProcessTraceChain traceFromComponent(DocRelative docRelative) {
        ProcessTraceChain chain = initChainWithDocInfo(docRelative);
        traceComponentChain(chain, docRelative.getAttributionId());
        List<ProductMix> productMixList=buildFullTreePath(chain);
        chain.setTreePath(productMixList);
        chain.setPermissionStreamNewList(buildFullTreePathPermission(productMixList));
        return chain;
    }
 
    /**
     * 零件封装
     * @param docRelative
     * @return
     */
    public ProcessTraceChain traceFromParts(DocRelative docRelative) {
        ProcessTraceChain chain = initChainWithDocInfo(docRelative);
        tracePartsChain(chain, docRelative.getAttributionId());
        List<ProductMix> productMixList=buildFullTreePath(chain);
        chain.setTreePath(productMixList);
        chain.setPermissionStreamNewList(buildFullTreePathPermission(productMixList));
        return chain;
    }
 
    /**
     * 工艺规程版本封装
     * @param docRelative
     * @return
     */
    public ProcessTraceChain traceFromPsv(DocRelative docRelative) {
        ProcessTraceChain chain = initChainWithDocInfo(docRelative);
        traceProcessSpecVersionChain(chain, docRelative.getAttributionId());
        List<ProductMix> productMixList=buildFullTreePath(chain);
        chain.setTreePath(productMixList);
        chain.setPermissionStreamNewList(buildFullTreePathPermission(productMixList));
        return chain;
    }
 
    /**
     * 工序封装
     * @param docRelative
     * @return
     */
    public ProcessTraceChain traceFromProcess(DocRelative docRelative) {
        ProcessTraceChain chain = initChainWithDocInfo(docRelative);
        DeviceType deviceType = deviceTypeMapper.selectById(docRelative.getAttributionId());
        chain.setDeviceType(deviceType);
 
        if (isProcessType(deviceType)) {
            chain.setDeviceManagement(deviceManagementMapper.selectById(deviceType.getDeviceManagementId()));
            traceProcessChain(chain, deviceType.getAttributionId());
        }
 
        completeChainWithProductInfo(chain);
        List<ProductMix> productMixList=buildFullTreePath(chain);
        chain.setTreePath(productMixList);
        chain.setPermissionStreamNewList(buildFullTreePathPermission(productMixList));
        return chain;
    }
 
    /**
     * 工步封装
     * @param docRelative
     * @return
     */
    public ProcessTraceChain traceFromWorkStep(DocRelative docRelative) {
        ProcessTraceChain chain = initChainWithDocInfo(docRelative);
        DeviceType deviceType = deviceTypeMapper.selectById(docRelative.getAttributionId());
        chain.setDeviceType(deviceType);
 
        if (isWorkSiteType(deviceType)) {
            chain.setDeviceManagement(deviceManagementMapper.selectById(deviceType.getDeviceManagementId()));
            traceWorkStepChain(chain, deviceType.getAttributionId());
        }
 
        completeChainWithProductInfo(chain);
        List<ProductMix> productMixList=buildFullTreePath(chain);
        chain.setTreePath(productMixList);
        chain.setPermissionStreamNewList(buildFullTreePathPermission(productMixList));
        return chain;
    }
 
    private ProcessTraceChain initChainWithDocInfo(DocRelative docRelative) {
        ProcessTraceChain chain = ProcessTraceChain.builder().docRelative(docRelative).build();
        Optional.ofNullable(docInfoMapper.selectById(docRelative.getDocId()))
                .ifPresent(doc -> {
                    chain.setDocInfo(doc);
                    chain.setDocFile(docFileMapper.selectById(doc.getPublishFileId()));
                    if (Objects.equals(docRelative.getAttributionType(), DocAttributionTypeEnum.PROCESS.getCode()) ||
                            Objects.equals(docRelative.getAttributionType(), DocAttributionTypeEnum.WORKSITE.getCode())) {
                        chain.setCutterList(getCuttersByDocId(doc.getDocId()));
                        getLatestGuideCardBatch(doc.getDocId()).ifPresent(chain::setGuideCardBatch);
                    }});
        return chain;
    }
 
    private List<Cutter> getCuttersByDocId(String docId) {
        return cutterMapper.selectList(new QueryWrapper<Cutter>().eq("doc_id", docId));
    }
 
    private Optional<GuideCardBatch> getLatestGuideCardBatch(String docId) {
        List<GuideCardBatch> batches = guideCardBatchMapper.selectList(
                new QueryWrapper<GuideCardBatch>()
                        .eq("doc_id", docId)
                        .orderByDesc("SUBSTRING(serial_number, LEN(serial_number)-3, 4)"));
        return CollectionUtils.isEmpty(batches) ? Optional.empty() : Optional.of(batches.get(0));
    }
 
    private boolean isProcessType(DeviceType deviceType) {
        return deviceType != null &&
                Objects.equals(deviceType.getAttributionType(), DocAttributionTypeEnum.PROCESS.getCode());
    }
 
    private boolean isWorkSiteType(DeviceType deviceType) {
        return deviceType != null &&
                Objects.equals(deviceType.getAttributionType(), DocAttributionTypeEnum.WORKSITE.getCode());
    }
 
    private void traceProductChain(ProcessTraceChain chain, String productId) {
        ProductInfo product = productMapper.selectById(productId);
        chain.setProduct(product);
    }
 
    private void traceComponentChain(ProcessTraceChain chain, String componentId) {
        chain.setComponentHierarchy(traceComponentHierarchy(componentId));
        Optional.ofNullable(chain.getComponentHierarchy())
                .map(ComponentHierarchy::getRootProduct)
                .ifPresent(chain::setProduct);
    }
 
    private void tracePartsChain(ProcessTraceChain chain, String partsId) {
        PartsInfo parts = partsMapper.selectById(partsId);
        chain.setParts(parts);
        if (parts != null) {
            if (parts.getComponentId() != null) {
                chain.setComponentHierarchy(traceComponentHierarchy(parts.getComponentId()));
                Optional.ofNullable(chain.getComponentHierarchy())
                        .map(ComponentHierarchy::getRootProduct)
                        .ifPresent(chain::setProduct);
            }
        }
    }
 
    private void traceProcessSpecVersionChain(ProcessTraceChain chain, String psvId) {
        ProcessSpecVersion psv = psvMapper.selectById(psvId);
        chain.setProcessSpec(psv);
        if (psv != null) {
            if (psv.getPartsId() != null) {
                PartsInfo parts = partsMapper.selectById(psv.getPartsId());
                chain.setParts(parts);
                if (parts != null && parts.getComponentId() != null) {
                    chain.setComponentHierarchy(traceComponentHierarchy(parts.getComponentId()));
                    Optional.ofNullable(chain.getComponentHierarchy())
                            .map(ComponentHierarchy::getRootProduct)
                            .ifPresent(chain::setProduct);
                }
            }
        }
    }
 
    private void traceProcessChain(ProcessTraceChain chain, String processId) {
        ProcessStream process = processMapper.selectById(processId);
        if (process == null) return;
 
        chain.setProcess(process);
        if (process.getPsvId() != null) {
            ProcessSpecVersion psv = psvMapper.selectById(process.getPsvId());
            chain.setProcessSpec(psv);
            if (psv != null && psv.getPartsId() != null) {
                PartsInfo parts = partsMapper.selectById(psv.getPartsId());
                chain.setParts(parts);
                if (parts != null && parts.getComponentId() != null) {
                    chain.setComponentHierarchy(traceComponentHierarchy(parts.getComponentId()));
                }
            }
        } else if (process.getComponentId() != null) {
            chain.setComponentHierarchy(traceComponentHierarchy(process.getComponentId()));
        }
    }
 
    private void traceWorkStepChain(ProcessTraceChain chain, String workStepId) {
        WorkStep workStep = workStepMapper.selectById(workStepId);
        if (workStep == null) return;
 
        chain.setWorkStep(workStep);
        traceProcessChain(chain, workStep.getProcessId());
    }
 
    private ComponentHierarchy traceComponentHierarchy(String componentId) {
        ComponentHierarchy hierarchy = new ComponentHierarchy();
        ComponentInfo current = componentMapper.selectById(componentId);
 
        while (current != null) {
            hierarchy.addComponentToTop(current);
 
            if (current.getParentId() == null || current.getParentId().isEmpty()) {
                Optional.ofNullable(current.getProductId())
                        .map(productMapper::selectById)
                        .ifPresent(hierarchy::setRootProduct);
                break;
            }
 
            current = componentMapper.selectById(current.getParentId());
        }
 
        return hierarchy;
    }
 
    private void completeChainWithProductInfo(ProcessTraceChain chain) {
        Optional.ofNullable(chain.getComponentHierarchy())
                .map(ComponentHierarchy::getComponents)
                .filter(components -> !components.isEmpty())
                .map(components -> components.get(0))
                .map(ComponentInfo::getProductId)
                .map(productMapper::selectById)
                .ifPresent(chain::setProduct);
    }
 
    private List<ProductMix> buildFullTreePath(ProcessTraceChain chain) {
        List<ProductMix> path = new ArrayList<>();
 
        Optional.ofNullable(chain.getProduct())
                .map(ProductInfo::getProductId)
                .map(productMixMapper::findByProductId)
                .ifPresent(path::add);
 
        Optional.ofNullable(chain.getComponentHierarchy())
                .map(ComponentHierarchy::getComponentsFromTop)
                .ifPresent(components -> components.stream()
                        .map(ComponentInfo::getComponentId)
                        .map(productMixMapper::findByComponentId)
                        .filter(Objects::nonNull)
                        .forEach(path::add));
 
        Optional.ofNullable(chain.getParts())
                .map(PartsInfo::getPartsId)
                .map(productMixMapper::findByPartsId)
                .ifPresent(path::add);
 
        Optional.ofNullable(chain.getProcessSpec())
                .map(ProcessSpecVersion::getId)
                .map(productMixMapper::findByOperationId)
                .ifPresent(path::add);
 
        Optional.ofNullable(chain.getProcess())
                .map(ProcessStream::getProcessId)
                .map(productMixMapper::findByProcessId)
                .ifPresent(path::add);
 
        Optional.ofNullable(chain.getWorkStep())
                .map(WorkStep::getId)
                .map(productMixMapper::findByWorksiteId)
                .ifPresent(path::add);
 
        return path;
    }
 
    private List<PermissionStreamNew> buildFullTreePathPermission(List<ProductMix> productMixList) {
        List<Long> ids=productMixList.stream().map(ProductMix::getId).collect(Collectors.toList());
        List<PermissionStreamNew> path = permissionStreamNewService
                .list(new QueryWrapper<PermissionStreamNew>().in("business_id",ids)
                        .eq("delete_flag",0));
        path.forEach(item->{
            if (item.getDepartId()!=null){
                MdcProduction mdcProduction=mdcProductionService.getById(item.getDepartId());
                if(mdcProduction!=null){
                    item.setDepartId(item.getDepartId());
                }
            }
            if (item.getUserId()!=null){
                item.setUserId(sysUserService.getById(item.getUserId()).getUsername());
            }
        });
        return path;
    }
}