hyingbo
6 天以前 cc0e9036de6e922e8fe254fef01d8de9996024b7
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
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.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;
 
    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);
        return chain;
    }
 
    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);
        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()));
                    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 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;
    }
 
}