李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
package com.chinaztt.mes.production.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chinaztt.mes.basic.mapper.WorkstationMapper;
import com.chinaztt.mes.production.dto.DutyRecordDTO;
import com.chinaztt.mes.production.dto.OperationTaskDTO;
import com.chinaztt.mes.production.dto.ProductOutputDTO;
import com.chinaztt.mes.production.dto.TraceDTO;
import com.chinaztt.mes.production.entity.OperationTask;
import com.chinaztt.mes.production.entity.ProductMain;
import com.chinaztt.mes.production.entity.ProductOutput;
import com.chinaztt.mes.production.entity.ProductOutputStaff;
import com.chinaztt.mes.production.mapper.*;
import com.chinaztt.mes.production.service.TraceService;
import com.chinaztt.mes.production.vo.DiagramLink;
import com.chinaztt.mes.production.vo.DiagramNode;
import com.chinaztt.mes.quality.dto.ApplyPartDTO;
import com.chinaztt.mes.quality.dto.ReportSampleDTO;
import com.chinaztt.mes.quality.mapper.ApplyPartMapper;
import com.chinaztt.mes.quality.mapper.ReportSampleItemMapper;
import com.chinaztt.mes.quality.mapper.ReportSampleMapper;
import com.chinaztt.mes.technology.mapper.OperationMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
import static com.chinaztt.mes.quality.entity.Apply.OUTPUT_APPLY;
 
 
/**
 * @author zhangxy
 */
@Service
@AllArgsConstructor
public class TraceServiceImpl implements TraceService {
 
    /**
     * 追溯递归深度
     */
    private static final int DEPTH_LIMIT = 10;
 
    private TraceMapper traceMapper;
 
    private OperationMapper operationMapper;
 
    private WorkstationMapper workstationMapper;
 
    private ProductMainMapper productMainMapper;
 
    private OperationTaskMapper operationTaskMapper;
 
    private ProductOutputMapper productOutputMapper;
 
    private ReportSampleItemMapper reportSampleItemMapper;
 
    private ReportSampleMapper reportSampleMapper;
 
    private DutyRecordMapper dutyRecordMapper;
 
    private ProductOutputStaffMapper productOutputStaffMapper;
 
    private ApplyPartMapper applyPartMapper;
 
    @Override
    public List<TraceDTO> queryListByNo(String no) {
        return traceMapper.queryListByNo(no);
    }
 
    /**
     * 反向 从后往前
     */
    private static final String POSITE = "posite";
 
    /**
     * 正向 从前往后
     */
    private static final String NEGATE = "negate";
 
    /**
     * @param systemNo
     * @param traceType posite  反向  negate正向
     * @return
     */
    @Override
    public JSONObject diagram(String systemNo, String traceType) {
        int i = 0;
        JSONObject result = new JSONObject();
        Set<DiagramLink> links = new HashSet<>();
        Set<DiagramNode> nodes = new HashSet<>();
        if (POSITE.equals(traceType)) {
            traceFromBack(systemNo, nodes, links, i);
        }
        if (NEGATE.equals(traceType)) {
            traceToBack(systemNo, nodes, links, i);
        }
        result.put("nodeDataArray", nodes);
        result.put("linkDataArray", links);
        return result;
    }
 
    /**
     * 反向追溯
     *
     * @param outputSystemNo
     * @param nodes
     * @param links
     * @param i
     */
    private void traceFromBack(String outputSystemNo, Set<DiagramNode> nodes, Set<DiagramLink> links, int i) {
        //查询产出
        List<TraceDTO> outList = traceMapper.getProductOutByNo(outputSystemNo);
        if (CollectionUtil.isEmpty(outList)) {
            return;
        }
        TraceDTO output = outList.get(0);
        DiagramNode self = new DiagramNode();
        self.setKey(output.getSystemNo());       //系统号
        self.setQuantity(output.getQuantity());  //零件数量
        self.setBatchNo(output.getBatchNo());    //系統號碼
        self.setText(output.getPartName());      //零件名字
        self.setIfsBatchNo(output.getIfsBatchNo());      //ifs批次号
        self.setUnit(output.getUnit()); //单位
        nodes.add(self);
        buildDiagramNodesFromBack(output.getSystemNo(), nodes, links, 0);
    }
 
    /**
     * 反向追溯
     *
     * @param outputSystemNo
     * @param nodes
     * @param links
     * @param i
     */
    private void buildDiagramNodesFromBack(String outputSystemNo, Set<DiagramNode> nodes, Set<DiagramLink> links, int i) {
        //判断递归深度
        if (i > DEPTH_LIMIT) {
            return;
        }
        List<TraceDTO> inList = traceMapper.getProductInByOutSystemNo(outputSystemNo);
        for (TraceDTO input : inList) {
            String from = input.getSystemNo();
            String to = outputSystemNo;
            DiagramNode node = new DiagramNode();
            node.setKey(from);
            node.setBatchNo(input.getBatchNo());
            node.setQuantity(input.getQuantity());
            node.setText(input.getPartName());
            node.setIfsBatchNo(input.getIfsBatchNo());
            node.setUnit(input.getUnit());
            nodes.add(node);
            DiagramLink link = new DiagramLink();
            link.setFrom(from);
            link.setTo(to);
            links.add(link);
            buildDiagramNodesFromBack(input.getSystemNo(), nodes, links, i + 1);
        }
    }
 
    /**
     * 正向追溯
     *
     * @param inputSystemNo 投入的Sysno
     * @param nodes
     * @param links
     * @param i
     */
    private void traceToBack(String inputSystemNo, Set<DiagramNode> nodes, Set<DiagramLink> links, int i) {
        //判断递归深度
        if (i > DEPTH_LIMIT) {
            return;
        }
        List<TraceDTO> inList = traceMapper.getProductInByInSystemNo(inputSystemNo);
        for (TraceDTO input : inList) {
            String from = input.getSystemNo() + "_" + input.getMainId();
            DiagramNode node = new DiagramNode();
            node.setKey(from);
            node.setBatchNo(input.getBatchNo());
            node.setQuantity(input.getQuantity());
            node.setText(input.getPartName());
            node.setIfsBatchNo(input.getIfsBatchNo());
            node.setUnit(input.getUnit());
            nodes.add(node);
            List<TraceDTO> outList = traceMapper.getProductOutByMainId(input.getMainId());
            for (TraceDTO out : outList) {
                String to = out.getSystemNo() + "_" + input.getMainId();
                DiagramNode node1 = new DiagramNode();
                node1.setKey(to);
                node1.setBatchNo(out.getBatchNo());
                node1.setQuantity(out.getQuantity());
                node1.setText(out.getPartName());
                node1.setIfsBatchNo(out.getIfsBatchNo());
                node1.setUnit(out.getUnit());
                nodes.add(node1);
                DiagramLink link = new DiagramLink();
                link.setFrom(from);
                link.setTo(to);
                links.add(link);
                traceToBack(out.getSystemNo(), nodes, links, i + 1);
            }
        }
    }
 
 
    @Override
    public JSONObject nodeDetail(String systemNo) {
        JSONObject result = new JSONObject();
        result.put("operationTask", null);
        result.put("productOut", null);
        result.put("sampleItems", null);
        result.put("dutyRecordId", null);
        ProductOutput pout = productOutputMapper.selectOne(Wrappers.<ProductOutput>lambdaQuery().eq(ProductOutput::getSystemNo, systemNo));
        if (pout != null) {
            List<ProductOutputStaff> staffRecord = productOutputStaffMapper.selectList(Wrappers.<ProductOutputStaff>lambdaQuery().eq(ProductOutputStaff::getProductOutId, pout.getId()));
            if (CollectionUtil.isNotEmpty(staffRecord)) {
                result.put("dutyRecordId", staffRecord.get(0).getDutyRecordId());
            }
            ProductMain main = productMainMapper.selectById(pout.getProductMainId());
            QueryWrapper wrapper = Wrappers.<OperationTask>query();
            wrapper.eq("oot.id", main.getOperationTaskId());
            List<OperationTask> tasks = operationTaskMapper.getOperationTask(wrapper, false, null, null, null, null,
                    null,true);
            if (CollectionUtil.isNotEmpty(tasks)) {
                result.put("operationTask", tasks.get(0));
            }
            List<ProductOutputDTO> poutDtos = productOutputMapper.getProductOutPutById(pout.getId());
            if (CollectionUtil.isNotEmpty(poutDtos)) {
                OperationTaskDTO operationTaskDTO = operationTaskMapper.getOperationTaskDTO(tasks.get(0).getId());
                DutyRecordDTO dutyRecordDTO = dutyRecordMapper.getDtoById(staffRecord.get(0).getDutyRecordId());
                ProductOutputDTO productOutputDTO = poutDtos.get(0);
                productOutputDTO.setWorkstationName(workstationMapper.selectById(main.getWorkstationId()).getName());
                productOutputDTO.setOperationName(operationTaskDTO.getOperationName());
                if(dutyRecordDTO!=null) {
                    productOutputDTO.setCrewName(dutyRecordDTO.getCrewName());
                }
                result.put("productOut", productOutputDTO);
            }
 
            List<ReportSampleDTO> sampleDTOList = reportSampleMapper.getBySystemNoAndReportType(systemNo);
            if (CollectionUtil.isNotEmpty(sampleDTOList)) {
                List<Long> idList = sampleDTOList.stream().map(a -> a.getId()).collect(Collectors.toList());
                result.put("sampleItems", reportSampleItemMapper.getSampleItemsByList(idList));
            } else {
                //产出不存在样品检测
                //查看该产出是否有产出尾检检测汇报,若无,则没有检测项,若有,则取该检测汇报中某个样品的检测信息
                List<ApplyPartDTO> applyPartDtos = applyPartMapper.getApplyPartDtoBySysNoAndAppType(systemNo);
                if (applyPartDtos != null && applyPartDtos.size() > 0) {
                    //取集合中的第一个,实际数据库中的最后一个
                    ApplyPartDTO applyPartDto = applyPartDtos.get(0);
                    //根据检测汇报id,查询样品,若存在多个样品,取最后一个样品
                    ReportSampleDTO reportSampleDTO = reportSampleMapper.getSampleById(applyPartDto.getReportId());
                    if (reportSampleDTO != null) {
                        List<Long> idList =new ArrayList<>();
                        idList.add(reportSampleDTO.getId());
                        result.put("sampleItems", reportSampleItemMapper.getSampleItemsByList(idList));
                    }
                }
            }
        }
        return result;
    }
}