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
package com.mes.hollow.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mes.common.config.Const;
import com.mes.glassinfo.entity.GlassInfo;
import com.mes.glassinfo.service.GlassInfoService;
import com.mes.hollow.entity.HollowBigStorageCage;
import com.mes.hollow.entity.HollowBigStorageCageDetails;
import com.mes.hollow.entity.HollowGlassOutRelationInfo;
import com.mes.hollow.entity.HollowGlassRelationInfo;
import com.mes.hollow.entity.dto.FlowCardGlassInfoDTO;
import com.mes.hollow.entity.dto.HollowBigStorageDTO;
import com.mes.hollow.entity.dto.HollowGlassDetailsDTO;
import com.mes.hollow.entity.dto.LackDetailsDTO;
import com.mes.hollow.entity.vo.HollowBigStorageDetailsQueryVO;
import com.mes.hollow.mapper.HollowGlassRelationInfoMapper;
import com.mes.hollow.service.HollowBigStorageCageDetailsService;
import com.mes.hollow.service.HollowBigStorageCageService;
import com.mes.hollow.service.HollowGlassOutRelationInfoService;
import com.mes.hollow.service.HollowGlassRelationInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * (HollowGlassRelationInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-11-23 15:59:30
 */
@Service
@Slf4j
public class HollowGlassRelationInfoServiceImpl extends ServiceImpl<HollowGlassRelationInfoMapper, HollowGlassRelationInfo> implements HollowGlassRelationInfoService {
 
    @Resource
    GlassInfoService glassInfoService;
    @Resource
    HollowBigStorageCageService hollowBigStorageCageService;
    @Resource
    HollowGlassRelationInfoService hollowGlassRelationInfoService;
    @Resource
    HollowGlassOutRelationInfoService hollowGlassOutRelationInfoService;
    @Resource
    HollowBigStorageCageDetailsService hollowBigStorageCageDetailsService;
    @Value("${mes.slotWidth}")
    private Integer slotWidth;
    @Value("${mes.glassGap}")
    private Integer glassGap;
 
    @Value("${mes.outCarMaxSize}")
    private Integer outCarMaxSize;
 
    @Override
    public HollowBigStorageDTO queryHollowTargetSlot(String flowCardId, double width, double height, int totalLayer, int layer) {
        //按照玻璃信息获取关系表中对应的大理片笼格子号
        HollowGlassRelationInfo relationInfoOne = hollowGlassRelationInfoService.getOne(new LambdaQueryWrapper<HollowGlassRelationInfo>()
                .eq(HollowGlassRelationInfo::getFlowCardId, flowCardId)
                .eq(HollowGlassRelationInfo::getWidth, width)
                .eq(HollowGlassRelationInfo::getHeight, height)
                .eq(HollowGlassRelationInfo::getTotalLayer, totalLayer)
                .eq(HollowGlassRelationInfo::getLayer, layer)
                .eq(HollowGlassRelationInfo::getState, Const.HOLLOW_RELATION_NEW)
                .orderByAsc(HollowGlassRelationInfo::getHollowSequence)
                .last("limit 1")
        );
        if (relationInfoOne == null) {
            //查看mes是否有对应的流程卡信息
            int count = hollowGlassRelationInfoService.count(new LambdaQueryWrapper<HollowGlassRelationInfo>()
                    .eq(HollowGlassRelationInfo::getFlowCardId, flowCardId)
                    .eq(HollowGlassRelationInfo::getLayer, layer));
            if (count == 0) {
                generateHollowGlassInfo(flowCardId, totalLayer, layer);
            } else {
                //比较关系表及中空理片笼详情表的流程卡数据,处理脏数据:将不在笼内的流程卡匹配数据职位空
                this.baseMapper.clearDirtyFlowCardData(flowCardId, layer);
            }
            //理片笼关系表中没有对应的数据,查看理片笼虚拟位置表是否有本工程下的所有玻璃虚拟信息
            //虚拟位置表没有本工程下的所有玻璃虚拟信息,按照玻璃id生成本工程下所有玻璃的虚拟信息
            relationInfoOne = this.getOne(new LambdaQueryWrapper<HollowGlassRelationInfo>()
                    .eq(HollowGlassRelationInfo::getFlowCardId, flowCardId)
                    .eq(HollowGlassRelationInfo::getWidth, width)
                    .eq(HollowGlassRelationInfo::getHeight, height)
                    .eq(HollowGlassRelationInfo::getTotalLayer, totalLayer)
                    .eq(HollowGlassRelationInfo::getLayer, layer)
                    .eq(HollowGlassRelationInfo::getState, Const.HOLLOW_RELATION_NEW)
                    .orderByAsc(HollowGlassRelationInfo::getHollowSequence)
                    .last("limit 1")
            );
        }
        //详情表内获取本组是否已经有玻璃在笼子内(0表示提前占用)
        int taskCount = hollowGlassOutRelationInfoService.count(new LambdaQueryWrapper<HollowGlassOutRelationInfo>()
                .eq(HollowGlassOutRelationInfo::getFlowCardId, flowCardId));
        HollowBigStorageCage storageCage = null;
        //如果不存在则选择笼内未用的新格子
        if (taskCount > 0) {
            storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper<HollowBigStorageCage>()
                    .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth)
                    .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness())
                    .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness())
                    .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1"));
            HollowBigStorageDTO storageDTO = new HollowBigStorageDTO();
            BeanUtils.copyProperties(storageCage, storageDTO);
            BeanUtils.copyProperties(relationInfoOne, storageDTO);
            return storageDTO;
        }       //详情表内获取本组是否已经有玻璃在笼子内(0表示提前占用)
        List<HollowBigStorageCageDetails> hollowDetailsList = hollowBigStorageCageDetailsService.list(new LambdaQueryWrapper<HollowBigStorageCageDetails>()
                .eq(HollowBigStorageCageDetails::getFlowCardId, relationInfoOne.getFlowCardId())
                .eq(HollowBigStorageCageDetails::getTotalLayer, totalLayer)
                .eq(HollowBigStorageCageDetails::getLayer, layer)
                .eq(HollowBigStorageCageDetails::getVirtualSlot, relationInfoOne.getVirtualSlot())
                .in(HollowBigStorageCageDetails::getState, Const.GLASS_STATE_IN_ALL_ZERO));
        //如果不存在则选择笼内未用的新格子
        if (CollectionUtil.isEmpty(hollowDetailsList)) {
            storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper<HollowBigStorageCage>()
                    .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth)
                    .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness())
                    .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness())
                    .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1"));
            HollowBigStorageDTO storageDTO = new HollowBigStorageDTO();
            BeanUtils.copyProperties(storageCage, storageDTO);
            BeanUtils.copyProperties(relationInfoOne, storageDTO);
            return storageDTO;
        }
        //获取当前组在笼子内的格子号(按照组内序号-1计算 如果没有则新开一格)
        HollowGlassRelationInfo relationInfoBefore = hollowGlassRelationInfoService.getOne(new LambdaQueryWrapper<HollowGlassRelationInfo>()
                .eq(HollowGlassRelationInfo::getFlowCardId, relationInfoOne.getFlowCardId())
                .eq(HollowGlassRelationInfo::getTotalLayer, relationInfoOne.getTotalLayer())
                .eq(HollowGlassRelationInfo::getLayer, relationInfoOne.getLayer())
                .eq(HollowGlassRelationInfo::getVirtualSlot, relationInfoOne.getVirtualSlot())
                .eq(HollowGlassRelationInfo::getSlotSequence, relationInfoOne.getSlotSequence() - 1));
        if (null == relationInfoBefore) {
            storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper<HollowBigStorageCage>()
                    .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth)
                    .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness())
                    .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness())
                    .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1"));
        } else {
            //获取详情表组内前一片玻璃的笼内详情数据
            HollowBigStorageCageDetails beforeGlass = hollowBigStorageCageDetailsService.getOne(new LambdaQueryWrapper<HollowBigStorageCageDetails>()
                    .in(HollowBigStorageCageDetails::getState, Const.GLASS_STATE_IN_ALL_ZERO)
                    .eq(HollowBigStorageCageDetails::getEngineerId, relationInfoBefore.getEngineerId())
                    .eq(HollowBigStorageCageDetails::getTemperingLayoutId, relationInfoBefore.getTemperingLayoutId())
                    .eq(HollowBigStorageCageDetails::getTemperingFeedSequence, relationInfoBefore.getTemperingFeedSequence())
            );
            //改组在笼内有玻璃但是前一块玻璃没有:原因 玻璃破损处理
            if (null == beforeGlass) {
                storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper<HollowBigStorageCage>()
                        .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth)
                        .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness())
                        .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness())
                        .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1"));
            } else {
                //取出每组玻璃占用笼子的格子并计算格子内的最大组序号,避免玻璃顺序错乱
                List<HollowBigStorageCageDetails> hollowSequenceList = hollowBigStorageCageDetailsService
                        .querySlotMaxSequence(beforeGlass.getFlowCardId(), beforeGlass.getTotalLayer(), beforeGlass.getLayer(), beforeGlass.getVirtualSlot());
                for (HollowBigStorageCageDetails hollowBigStorageCageDetail : hollowSequenceList) {
                    if (relationInfoOne.getSlotSequence() - 1 == hollowBigStorageCageDetail.getSequence()) {
                        storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper<HollowBigStorageCage>()
                                .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getSlot, beforeGlass.getSlot()));
                        break;
                    }
                }
                if (storageCage == null) {
                    storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper<HollowBigStorageCage>()
                            .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth)
                            .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness())
                            .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness())
                            .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1"));
                }
            }
        }
        Assert.isTrue(null != storageCage, "没有空余的笼子存放玻璃");
        HollowBigStorageDTO storageDTO = new HollowBigStorageDTO();
        BeanUtils.copyProperties(storageCage, storageDTO);
        BeanUtils.copyProperties(relationInfoOne, storageDTO);
        return storageDTO;
    }
 
    @Override
    public void generateHollowGlassInfo(String flowCardId, int totalLayer, int layer) {
 
        GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>().eq(GlassInfo::getFlowCardId, flowCardId)
                .eq(GlassInfo::getLayer, layer).last("limit 1"));
        if (null == glassInfo) {
            log.info("当前流程卡信息为导入mes系统流程卡:{},层数{}", flowCardId, layer);
            return;
        }
        //按照流程卡获取本流程卡最后一层或第一次的玻璃数据
        List<HollowGlassDetailsDTO> glassDetailsDTOS = this.baseMapper.queryFlowCardIdMaxLayerGlassInfo(flowCardId, totalLayer);
        if (CollectionUtil.isEmpty(glassDetailsDTOS)) {
            log.info("当前流程卡最外层数据未找到,请在erp确认数据无误,流程卡:{},总层数{}", flowCardId, totalLayer);
            return;
        }
        if (totalLayer != layer) {
            glassDetailsDTOS = this.baseMapper.queryFlowCardIdLayerGlassInfo(flowCardId, totalLayer, layer);
        }
        if (CollectionUtil.isEmpty(glassDetailsDTOS)) {
            log.info("当前流程卡最外层数据未找到,请在erp确认数据无误,流程卡:{},总层数{},层数{}", flowCardId, totalLayer, layer);
            return;
        }
        ArrayList<HollowGlassDetailsDTO> tempGlassList = new ArrayList<>();
        int hollowSequence = 1;
        for (HollowGlassDetailsDTO item : glassDetailsDTOS) {
            for (int i = 0; i < item.getQuantity(); i++) {
                HollowGlassDetailsDTO dto = new HollowGlassDetailsDTO();
                BeanUtils.copyProperties(item, dto);
                dto.setHollowSequence(hollowSequence++);
                tempGlassList.add(dto);
            }
        }
        //方式一:将玻璃按流程卡、尺寸、版图、版序 依次生成虚拟格子信息,格子一直往后累加
//        flowCardIdMap.forEach((e, v) -> {
//            int remainWidth = slotWidth;
//            int slotNumber = 1;
//            for (GlassInfo item : v) {
//                int maxLength = (int) Math.max(item.getWidth(), item.getHeight());
//                if (remainWidth > maxLength) {
//                    remainWidth = remainWidth - maxLength - glassGap;
//                } else {
//                    slotNumber = slotNumber + 1;
//                    remainWidth = slotWidth - maxLength - glassGap;
//                }
//                HollowGlassInfo hollow = new HollowGlassInfo();
//                BeanUtils.copyProperties(item, hollow);
//                hollow.setSlot(slotNumber);
//                hollowGlassInfoList.add(hollow);
//            }
//        });
//        this.saveBatch(hollowGlassInfoList);
        //获取中空大理片笼的所有空闲格子
//        List<HollowBigStorageCage> hollowSlotList = HollowBigStorageCageService.list(new LambdaQueryWrapper<HollowBigStorageCage>()
//                .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth));
        //方式二:将玻璃按流程卡、尺寸、版图、版序 ,优先将格子全部补全后 依次计算后面的格子号
        List<HollowGlassRelationInfo> relationInfoList = new ArrayList();
        List<List<HollowGlassRelationInfo>> tempHollowList = new ArrayList<>();
        int slotNumber = 1;
        for (HollowGlassDetailsDTO item : tempGlassList) {
            boolean flag = false;
            for (List<HollowGlassRelationInfo> temp : tempHollowList) {
                int sum = 0;
                for (HollowGlassRelationInfo i : temp) {
                    sum = sum + (int) Math.max(i.getHeight(), i.getWidth()) + glassGap;
                }
                if (sum + (int) Math.max(item.getHeight(), item.getWidth()) <= slotWidth && temp.size() < outCarMaxSize) {
                    HollowGlassRelationInfo hollow = new HollowGlassRelationInfo();
                    BeanUtils.copyProperties(item, hollow);
                    hollow.setSlotSequence(temp.size() + 1);
                    hollow.setTotalLayer(totalLayer);
                    hollow.setVirtualSlot(temp.get(0).getVirtualSlot());
                    hollow.setFilmsId(glassInfo.getFilmsid());
                    hollow.setThickness(glassInfo.getThickness());
                    hollow.setState(Const.HOLLOW_RELATION_NEW);
                    temp.add(hollow);
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                List<HollowGlassRelationInfo> newList = new ArrayList<>();
                HollowGlassRelationInfo hollow = new HollowGlassRelationInfo();
                BeanUtils.copyProperties(item, hollow);
                hollow.setSlotSequence(1);
                hollow.setTotalLayer(totalLayer);
                hollow.setVirtualSlot(slotNumber++);
                hollow.setFilmsId(glassInfo.getFilmsid());
                hollow.setThickness(glassInfo.getThickness());
                hollow.setState(Const.HOLLOW_RELATION_NEW);
                newList.add(hollow);
                tempHollowList.add(newList);
            }
        }
        for (List<HollowGlassRelationInfo> item : tempHollowList) {
            relationInfoList.addAll(item);
        }
        log.info("分配完毕");
        this.saveBatch(relationInfoList);
    }
 
    @Override
    public Map<String, List<FlowCardGlassInfoDTO>> queryHollowAllFlowCard(HollowBigStorageDetailsQueryVO query) {
        List<HollowBigStorageCageDetails> detailsList = hollowBigStorageCageDetailsService.list(new LambdaQueryWrapper<HollowBigStorageCageDetails>()
                .eq(HollowBigStorageCageDetails::getState, Const.GLASS_STATE_IN)
                .like(StringUtils.isNotBlank(query.getFilmsId()), HollowBigStorageCageDetails::getFilmsId, query.getFilmsId())
                .like(StringUtils.isNotBlank(query.getFlowCardId()), HollowBigStorageCageDetails::getFlowCardId, query.getFlowCardId())
                .eq(query.getThickness() != 0, HollowBigStorageCageDetails::getThickness, query.getThickness())
                .orderByAsc(HollowBigStorageCageDetails::getFlowCardId)
        );
        if (CollectionUtil.isEmpty(detailsList)) {
            log.info("笼内无玻璃");
            return new HashMap<>();
        }
        Map<String, List<HollowBigStorageCageDetails>> listMap = detailsList.stream().collect(Collectors.groupingBy(HollowBigStorageCageDetails::getFlowCardId));
        List<FlowCardGlassInfoDTO> dtos = new ArrayList<>();
        listMap.forEach((e, v) -> {
            HollowBigStorageCageDetails cageDetails = v.get(0);
            dtos.addAll(hollowBigStorageCageDetailsService.hollowIsAll(e, cageDetails.getTotalLayer(), Boolean.FALSE));
        });
        return dtos.stream().collect(Collectors.groupingBy(FlowCardGlassInfoDTO::getFlowCardId));
    }
 
    @Override
    public Map<Integer, List<LackDetailsDTO>> queryLackByFlowCard(String flowCardId) {
        List<LackDetailsDTO> lackDetailsList = this.baseMapper.queryLackByFlowCard(flowCardId);
        Map<Integer, List<LackDetailsDTO>> listMap = lackDetailsList.stream().collect(Collectors.groupingBy(LackDetailsDTO::getLayer));
        return listMap;
    }
 
    @Override
    public int queryLayerByFlowCardId(String flowCardId) {
        return baseMapper.queryLayerByFlowCardId(flowCardId);
    }
    //    @Override
//    public List<LackDetailsDTO> queryLackByFlowCard(String flowCardId) {
//        List<LackDetailsDTO> lackDetailsList = this.baseMapper.queryLackByFlowCard(flowCardId);
//        return lackDetailsList;
//    }
 
}