ZengTao
2025-01-08 29f942cefe9d9f4272332eec0276f2c5a60d9d9e
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
package com.mes.downglassinfo.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mes.common.config.Const;
import com.mes.downglassinfo.entity.DownGlassInfo;
import com.mes.downglassinfo.entity.DownGlassTask;
import com.mes.downglassinfo.entity.request.DownGlassInfoRequest;
import com.mes.downglassinfo.mapper.DownGlassInfoMapper;
import com.mes.downglassinfo.service.DownGlassInfoService;
import com.mes.downglassinfo.service.DownGlassTaskService;
import com.mes.downstorage.entity.DownStorageCageDetails;
import com.mes.downstorage.service.DownStorageCageDetailsService;
import com.mes.downworkstation.entity.DownWorkstation;
import com.mes.downworkstation.entity.dto.DownGlassInfoDTO;
import com.mes.downworkstation.service.DownWorkstationService;
import com.mes.glassinfo.entity.GlassInfo;
import com.mes.job.DownLoadCacheGlassTask;
import com.mes.pp.service.FlowCardService;
import com.mes.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class DownGlassInfoServiceImpl extends ServiceImpl<DownGlassInfoMapper, DownGlassInfo> implements DownGlassInfoService {
 
    @Autowired
    DownLoadCacheGlassTask downLoadCacheGlassTask;
 
    @Autowired
    DownStorageCageDetailsService downStorageCageDetailsService;
 
 
    @Autowired
    DownWorkstationService downWorkstationService;
 
    @Autowired
    RedisUtil redisUtil;
 
    @Autowired
    FlowCardService flowCardService;
 
    @Resource
    DownGlassTaskService downGlassTaskService;
 
    /**
     * 根据流程卡号查询最大序号
     */
    @Override
    public Integer getMaxSequenceByFlowCardId(String flowCardId, int layer) {
        LambdaQueryWrapper<DownGlassInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(DownGlassInfo::getFlowCardId, flowCardId)
                .eq(DownGlassInfo::getLayer, layer)
                .select(DownGlassInfo::getSequence)
                .orderByDesc(DownGlassInfo::getSequence)
                .last("LIMIT 1");
 
        DownGlassInfo downGlassInfo = baseMapper.selectOne(lambdaQueryWrapper);
        return downGlassInfo != null ? downGlassInfo.getSequence() : 0;
    }
 
    @Override
    public void insertDownGlassInfo(DownGlassInfo downGlassInfo) {
        baseMapper.insert(downGlassInfo);
    }
 
    @Override
    public DownGlassInfoDTO queryDownGlassMaxLayer(String flowCardId) {
        return baseMapper.queryDownGlassMaxLayer(flowCardId);
    }
 
    @Override
    public Integer queryMaxSequence(String flowCardId, int layer) {
        return baseMapper.queryMaxSequence(flowCardId, layer);
    }
 
    @Override
    public List<DownGlassInfoDTO> queryWorkStationIsIn(List<Integer> workList, Boolean isDownload) {
        //按照要求获取已落架/未落架的玻璃信息
        return baseMapper.queryWorkStationIsIn(workList, isDownload);
    }
 
    @Override
    public List<DownGlassInfoDTO> queryWorkStationFlowCard(List<Integer> workList) {
        //获取已绑定流程卡的架子未落玻璃的数据信息
        return baseMapper.queryWorkStationFlowCard(workList);
    }
 
    @Override
    public boolean generateOutGlassTask(String glassId) {
        //更新卧式理片笼内玻璃状态
        downStorageCageDetailsService.update(new LambdaUpdateWrapper<DownStorageCageDetails>()
                .set(DownStorageCageDetails::getState, Const.GLASS_STATE_OUT).eq(DownStorageCageDetails::getGlassId, glassId));
        DownStorageCageDetails details = downStorageCageDetailsService.getOne(new LambdaQueryWrapper<DownStorageCageDetails>()
                .eq(DownStorageCageDetails::getGlassId, glassId));
        //生成下片信息
        DownGlassInfo downGlassInfo = new DownGlassInfo();
        BeanUtils.copyProperties(details, downGlassInfo);
        //获取当前流程卡最大片序
        downGlassInfo.setSequence(this.getMaxSequenceByFlowCardId(details.getFlowCardId(), details.getLayer()) + 1);
        downGlassInfo.setWorkStationId(Const.G13_WORK_STATION);
        this.save(downGlassInfo);
        //生成任务信息 并向plc发送出片任务
        GlassInfo glassInfo = new GlassInfo();
        BeanUtils.copyProperties(details, glassInfo);
        return downLoadCacheGlassTask.initDownGlassTask(glassInfo, details.getSlot(), Const.G13_WORK_STATION, 2);
    }
 
    @Override
    public String setDownGlassInfoRequest(DownGlassInfoRequest request) {
        if (request == null) {
            redisUtil.deleteObject("downGlassRequest");
        } else {
            redisUtil.setCacheObject("downGlassRequest", request);
        }
        return "success";
    }
 
    @Override
    public List<DownWorkstation> queryWorkStationIsFull() {
 
        //查询可以落架的玻璃信息且已绑定流程卡的工位信息
//        List<DownWorkstation> list = downWorkstationService.list(new LambdaQueryWrapper<DownWorkstation>().eq(DownWorkstation::getEnableState, Const.SLOT_ON)
//                .isNotNull(DownWorkstation::getFlowCardId).ne(DownWorkstation::getFlowCardId, ""));
//        List<DownWorkstation> workstationFull = new ArrayList<>();
//        if (CollectionUtils.isNotEmpty(list)) {
//            //都有玻璃的工位信息(包括绑定流程卡)
//            List<Integer> workstationList = list.stream().map(DownWorkstation::getWorkstationId).collect(Collectors.toList());
//            //按照符合条件的工位获取未落架的流程卡玻璃,如果返回为空,则表明所有都已落架完成
//            List<DownGlassInfoDTO> downGlassInfoDTOList = queryWorkStationIsIn(workstationList, Boolean.FALSE);
//            //获取工位上的未满的流程卡及层数
//            List<String> flowCardIdList = downGlassInfoDTOList.stream().map(item -> item.getFlowCardId() + ":" + item.getLayer()).collect(Collectors.toList());
//            //可以落架的玻璃信息且已绑定流程卡的所有的工位 - 玻璃未满流程卡及层数的工位  =  已满工位
//            workstationFull = list.stream().filter(item -> !flowCardIdList.contains(item.getFlowCardId() + ":" + item.getLayer())).collect(Collectors.toList());
//            //是否需要将已满的工位置为不可落架
////            if (CollectionUtils.isNotEmpty(workstationFull)) {
////                List<Integer> workstationIds = workstationFull.stream().map(DownWorkstation::getWorkstationId).collect(Collectors.toList());
////                downWorkstationService.update(new LambdaUpdateWrapper<DownWorkstation>().set(DownWorkstation::getEnableState, Const.SLOT_OFF)
////                        .in(DownWorkstation::getWorkstationId, workstationIds));
////            }
//        }
        List<DownWorkstation> workstationFull = downWorkstationService.list(new LambdaQueryWrapper<DownWorkstation>()
                .eq(DownWorkstation::getEnableState, Const.SLOT_ON)
                .isNotNull(DownWorkstation::getFlowCardId)
                .ne(DownWorkstation::getFlowCardId, "")
                .apply("total_quantity = racks_number + other_number"));
        return workstationFull;
    }
 
    @Override
    public List<Map<String, List<Map<String, Object>>>> downGlassPrint(DownGlassInfo downGlassInfo) {
        log.info("打印参数:{}", downGlassInfo);
        QueryWrapper<DownGlassInfo> queryWrapper = Wrappers.query();
        queryWrapper.eq("flow_card_id", downGlassInfo.getFlowCardId())
                .orderByAsc("id");
        int printLayer = 1;
        if (downGlassInfo.getLayer() != 0) {
            queryWrapper.select("flow_card_id", "layer", "width", "height", "filmsid", "thickness", "glass_type", "COUNT(*) AS quantity");
            queryWrapper.groupBy("flow_card_id", "layer", "width", "height", "filmsid", "thickness", "glass_type", "id");
            queryWrapper.eq("layer", downGlassInfo.getLayer());
            printLayer = downGlassInfo.getLayer();
        } else {
            queryWrapper.select("flow_card_id", "width", "height", "filmsid", "thickness", "glass_type", "COUNT(*) AS quantity");
            queryWrapper.groupBy("flow_card_id", "width", "height", "filmsid", "thickness", "glass_type", "id");
        }
        List<Map<String, Object>> resultList = baseMapper.selectMaps(queryWrapper);
 
        log.info("MES落架数据:{}", resultList);
        List<Map<String, Object>> projectInfo = flowCardService.selectProject(downGlassInfo.getFlowCardId(), printLayer);
        log.info("流程卡头部数据:{}", projectInfo);
        List<Map<String, Object>> flowCardInfo = flowCardService.selectFlowCard(downGlassInfo.getFlowCardId(), downGlassInfo.getLayer());
        log.info("流程卡玻璃数据:{}", flowCardInfo);
        for (Map<String, Object> row : flowCardInfo) {
            int order_number = (int) row.get("order_number");
 
            int quantity1 = 0;
            for (Map<String, Object> row1 : resultList) {
                int glass_type = (int) row1.get("glass_type");
                if (downGlassInfo.getLayer() != 0) {
                    int layer = (int) row1.get("layer");
                    int technology_number = (int) row.get("technology_number");
                    if (order_number == glass_type && technology_number == layer) {
                        quantity1 += 1;
                    }
                } else {
                    if (order_number == glass_type) {
                        quantity1 += 1;
                    }
                }
            }
            row.put("quantity1", quantity1);
        }
        log.info("流程卡玻璃数据2:{}", flowCardInfo);
        List<Map<String, List<Map<String, Object>>>> listMap = new ArrayList<>();
        Map<String, List<Map<String, Object>>> result = new HashMap<>();
        result.put("detail", projectInfo);
        result.put("detailList", flowCardInfo);
        for (Map<String, Object> row : projectInfo) {
            String order_number = (String) row.get("process");
            String[] processes = order_number.split("->");
            List<Map<String, Object>> processList = new ArrayList<>();
            for (int i = 0; i < processes.length; i++) {
                Map<String, Object> processMap = new JSONObject();
                processMap.put("process", processes[i]);
                processMap.put("id", i);
                processList.add(processMap);
            }
            result.put("processList", processList);
        }
        listMap.add(result);
        return listMap;
    }
 
 
    @Override
    public List<Map<String, Object>> downGlassLabelPrint(DownGlassInfo downGlassInfo) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (downGlassInfo.getFlowCardId() == null) {
            List<DownGlassTask> downGlassTasks = downGlassTaskService.list(
                    new LambdaQueryWrapper<DownGlassTask>()
                            .eq(DownGlassTask::getEndCell, 7)
                            .eq(DownGlassTask::getTaskType, 3)
                            .lt(DownGlassTask::getTaskStatus, 3)
            );
            List<String> glassIds = downGlassTasks.stream().map(DownGlassTask::getGlassId).collect(Collectors.toList());
            resultList = baseMapper.downGlassLabelPrint(null, null, glassIds);
        } else {
            resultList = baseMapper.downGlassLabelPrint(downGlassInfo.getFlowCardId(), downGlassInfo.getLayer(), null);
        }
        if (CollectionUtils.isNotEmpty(resultList)) {
            List<String> flowCardIds = resultList.stream()
                    .map(map -> (String) map.get("flow_card_id"))
                    .distinct()
                    .collect(Collectors.toList());
            log.info("MES落架数据:{}", resultList);
            //获取ERP标签所需的信息
            List<Map<String, Object>> labelInfo;
            if (downGlassInfo.getFlowCardId() != null) {
                flowCardIds.add(downGlassInfo.getFlowCardId());
                labelInfo = flowCardService.selectLabel(flowCardIds, 1);
            } else {
                labelInfo = flowCardService.selectLabel(flowCardIds, 1);
            }
            log.info("标签数据:{}", labelInfo);
 
            for (Map<String, Object> row : resultList) {
                int glass_type = (int) row.get("glass_type");
                int layer = (int) row.get("layer");
                String flowCardId = (String) row.get("flow_card_id");
                for (Map<String, Object> row1 : labelInfo) {
                    int orderNumber = (int) row1.get("orderNumber");
                    int technologyNumber = (int) row1.get("technologyNumber");
                    String processId = (String) row1.get("process_id");
//                    if (downGlassInfo.getLayer() != 0) {
                        if (orderNumber == glass_type  && processId.equals(flowCardId)) {
 
                            String order_id = (String) row1.get("order_id");
                            String project = (String) row1.get("project");
                            Long customer_id = (Long) row1.get("customer_id");
                            BigDecimal width = (BigDecimal) row1.get("width");
                            BigDecimal height = (BigDecimal) row1.get("height");
                            Long quantity = (Long) row1.get("quantity");
                            String glass_child = (String) row1.get("glass_child");
                            String process = (String) row1.get("process");
                            String customer_name = (String) row1.get("customer_name");
                            String processing_note = (String) row1.get("processing_note");
                            String other_colunmns = (String) row1.get("other_colunmns");
                            String building_number = (String) row1.get("building_number");
                            String bend_radius = (String) row1.get("bend_radius");
 
                            row.put("orderNumber", orderNumber);
                            row.put("technology_number", technologyNumber);
                            row.put("order_id", order_id);
                            row.put("project", project);
                            row.put("customer_id", customer_id);
                            row.put("width", width);
                            row.put("height", height);
                            row.put("quantity", quantity);
                            row.put("glass_child", glass_child);
                            row.put("process", process);
                            row.put("customer_name", customer_name);
                            row.put("processing_note", processing_note);
                            row.put("other_colunmns", other_colunmns);
                            row.put("building_number", building_number);
                            row.put("bend_radius", bend_radius);
                        }
//                    } else {
//                        if (orderNumber == glass_type) {
//
//                            String order_id = (String) row1.get("order_id");
//                            String project = (String) row1.get("project");
//                            Long customer_id = (Long) row1.get("customer_id");
//                            BigDecimal width = (BigDecimal) row1.get("width");
//                            BigDecimal height = (BigDecimal) row1.get("height");
//                            Long quantity = (Long) row1.get("quantity");
//                            String glass_child = (String) row1.get("glass_child");
//                            String process = (String) row1.get("process");
//                            String customer_name = (String) row1.get("customer_name");
//                            String processing_note = (String) row1.get("processing_note");
//                            String other_colunmns = (String) row1.get("other_colunmns");
//                            String building_number = (String) row1.get("building_number");
//                            String bend_radius = (String) row1.get("bend_radius");
//
//                            row.put("orderNumber", orderNumber);
//                            row.put("technology_number", technologyNumber);
//                            row.put("order_id", order_id);
//                            row.put("project", project);
//                            row.put("customer_id", customer_id);
//                            row.put("width", width);
//                            row.put("height", height);
//                            row.put("quantity", quantity);
//                            row.put("glass_child", glass_child);
//                            row.put("process", process);
//                            row.put("customer_name", customer_name);
//                            row.put("processing_note", processing_note);
//                            row.put("other_colunmns", other_colunmns);
//                            row.put("building_number", building_number);
//                            row.put("bend_radius", bend_radius);
//                        }
//                    }
                }
            }
        }
        return resultList;
    }
 
}