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
package com.mes.bigstorage.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.extension.service.impl.ServiceImpl;
import com.mes.bigstorage.entity.BigStorageCage;
import com.mes.bigstorage.entity.BigStorageCageDetails;
import com.mes.bigstorage.entity.BigStorageGlassInfo;
import com.mes.bigstorage.entity.BigStorageGlassRelationInfo;
import com.mes.bigstorage.entity.dto.BigStorageDTO;
import com.mes.bigstorage.mapper.BigStorageGlassInfoMapper;
import com.mes.bigstorage.service.BigStorageCageDetailsService;
import com.mes.bigstorage.service.BigStorageCageService;
import com.mes.bigstorage.service.BigStorageGlassInfoService;
import com.mes.bigstorage.service.BigStorageGlassRelationInfoService;
import com.mes.common.config.Const;
import com.mes.common.config.ConstSysConfig;
import com.mes.glassinfo.entity.GlassInfo;
import com.mes.glassinfo.service.GlassInfoService;
import com.mes.sysconfig.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * (BigStorageGlassInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-11-24 10:15:35
 */
@Service
@Slf4j
public class BigStorageGlassInfoServiceImpl extends ServiceImpl<BigStorageGlassInfoMapper, BigStorageGlassInfo> implements BigStorageGlassInfoService {
    @Resource
    GlassInfoService glassInfoService;
    @Resource
    BigStorageCageService bigStorageCageService;
    @Resource
    BigStorageCageDetailsService bigStorageCageDetailsService;
    @Resource
    BigStorageGlassRelationInfoService bigStorageGlassRelationInfoService;
    @Resource
    SysConfigService sysConfigService;
//    @Value("${mes.slotWidth}")
//    private Integer slotWidth;
//    @Value("${mes.glassGap}")
//    private Integer glassGap;
//    @Value("${mes.outCarMaxSize}")
//    private Integer outCarMaxSize;
 
    @Override
    public BigStorageDTO queryBigStorageTargetSlot(String engineerId, Integer temperingLayoutId, Integer temperingFeedSequence) {
        //按照玻璃信息获取关系表是否有对应的关系
        BigStorageGlassRelationInfo relationInfoOne = bigStorageGlassRelationInfoService.getOne(new LambdaQueryWrapper<BigStorageGlassRelationInfo>()
                .eq(BigStorageGlassRelationInfo::getEngineerId, engineerId)
                .eq(BigStorageGlassRelationInfo::getTemperingLayoutId, temperingLayoutId)
                .eq(BigStorageGlassRelationInfo::getTemperingFeedSequence, temperingFeedSequence)
        );
        if (relationInfoOne == null) {
            //理片笼关系表中没有对应的数据,查看理片笼虚拟位置表是否有本工程下的所有玻璃虚拟信息
            BigStorageGlassInfo bigStorageGlassInfo = this.getOne(new LambdaQueryWrapper<BigStorageGlassInfo>()
                    .eq(BigStorageGlassInfo::getEngineerId, engineerId)
                    .eq(BigStorageGlassInfo::getTemperingLayoutId, temperingLayoutId)
                    .eq(BigStorageGlassInfo::getTemperingFeedSequence, temperingFeedSequence));
            if (null == bigStorageGlassInfo) {
                //虚拟位置表没有本工程下的所有玻璃虚拟信息,按照玻璃id生成本工程下所有玻璃的虚拟信息
                generateBigStorageGlassInfo(engineerId, temperingLayoutId);
                bigStorageGlassInfo = this.getOne(new LambdaQueryWrapper<BigStorageGlassInfo>()
                        .eq(BigStorageGlassInfo::getEngineerId, engineerId)
                        .eq(BigStorageGlassInfo::getTemperingLayoutId, temperingLayoutId)
                        .eq(BigStorageGlassInfo::getTemperingFeedSequence, temperingFeedSequence));
            }
            //按照虚拟位置和大理片笼现有可用格子生成关系表
            bigStorageSlotPair(bigStorageGlassInfo);
            //关系表获取实际位置
            relationInfoOne = bigStorageGlassRelationInfoService.getOne(new LambdaQueryWrapper<BigStorageGlassRelationInfo>()
                    .eq(BigStorageGlassRelationInfo::getEngineerId, engineerId)
                    .eq(BigStorageGlassRelationInfo::getTemperingLayoutId, temperingLayoutId)
                    .eq(BigStorageGlassRelationInfo::getTemperingFeedSequence, temperingFeedSequence));
        }
        Integer slotWidth = sysConfigService.queryConfigValue(ConstSysConfig.VERTICAL_SLOT_WIDTH);
        BigStorageCageDetails bigStorageCageDetails = bigStorageCageDetailsService.getOne(new LambdaQueryWrapper<BigStorageCageDetails>()
                .eq(BigStorageCageDetails::getSlot, relationInfoOne.getSlot()).in(BigStorageCageDetails::getState, Const.GLASS_STATE_IN_ALL_ZERO)
                .orderByDesc(BigStorageCageDetails::getSequence).last("limit 1"));
        if (null == bigStorageCageDetails) {
            BigStorageDTO storageDTO = new BigStorageDTO();
            storageDTO.setDeviceId(relationInfoOne.getDeviceId());
            storageDTO.setSlot(relationInfoOne.getSlot());
            storageDTO.setSlotSequence(relationInfoOne.getSlotSequence());
            storageDTO.setWidth(slotWidth);
            return storageDTO;
        }
        BigStorageGlassRelationInfo relationInfoBefore = bigStorageGlassRelationInfoService.getOne(new LambdaQueryWrapper<BigStorageGlassRelationInfo>()
                .eq(BigStorageGlassRelationInfo::getEngineerId, relationInfoOne.getEngineerId())
                .eq(BigStorageGlassRelationInfo::getTemperingLayoutId, relationInfoOne.getTemperingLayoutId())
                .eq(BigStorageGlassRelationInfo::getVirtualSlot, relationInfoOne.getVirtualSlot())
                .eq(BigStorageGlassRelationInfo::getSlotSequence, relationInfoOne.getSlotSequence() - 1));
        BigStorageCage storageCage = null;
        if (null == relationInfoBefore) {
            //表示序号没有或者  序号为1又不是第一块来的 新开一格
            storageCage = bigStorageCageService.getOne(new LambdaQueryWrapper<BigStorageCage>()
                    .eq(BigStorageCage::getEnableState, Const.SLOT_ON).eq(BigStorageCage::getRemainWidth, slotWidth)
                    .le(BigStorageCage::getMinThickness, bigStorageCageDetails.getThickness())
                    .ge(BigStorageCage::getMaxThickness, bigStorageCageDetails.getThickness())
                    .orderByAsc(BigStorageCage::getMaxThickness)
                    .orderByAsc(BigStorageCage::getSlot).last("limit 1"));
        } else {
            BigStorageCageDetails beforeGlass = bigStorageCageDetailsService.getOne(new LambdaQueryWrapper<BigStorageCageDetails>()
                    .in(BigStorageCageDetails::getState, Const.GLASS_STATE_IN_ALL_ZERO)
                    .eq(BigStorageCageDetails::getEngineerId, relationInfoBefore.getEngineerId())
                    .eq(BigStorageCageDetails::getTemperingLayoutId, relationInfoBefore.getTemperingLayoutId())
                    .eq(BigStorageCageDetails::getTemperingFeedSequence, relationInfoBefore.getTemperingFeedSequence())
            );
            if (null == beforeGlass) {
                storageCage = bigStorageCageService.getOne(new LambdaQueryWrapper<BigStorageCage>()
                        .eq(BigStorageCage::getEnableState, Const.SLOT_ON).eq(BigStorageCage::getRemainWidth, slotWidth)
                        .le(BigStorageCage::getMinThickness, bigStorageCageDetails.getThickness())
                        .ge(BigStorageCage::getMaxThickness, bigStorageCageDetails.getThickness())
                        .orderByAsc(BigStorageCage::getMaxThickness).orderByAsc(BigStorageCage::getSlot).last("limit 1"));
            } else {
                storageCage = bigStorageCageService.getOne(new LambdaQueryWrapper<BigStorageCage>()
                        .eq(BigStorageCage::getEnableState, Const.SLOT_ON).eq(BigStorageCage::getSlot, beforeGlass.getSlot()));
            }
        }
        Assert.isTrue(null != storageCage, "没有空余的笼子存放玻璃");
        BigStorageDTO storageDTO = new BigStorageDTO();
        storageDTO.setDeviceId(storageCage.getDeviceId());
        storageDTO.setSlotSequence(relationInfoOne.getSlotSequence());
        storageDTO.setSlot(storageCage.getSlot());
        storageDTO.setWidth(storageCage.getRemainWidth());
        return storageDTO;
    }
 
    @Override
    public void generateBigStorageGlassInfo(String engineerId, int temperingLayoutId) {
        List<BigStorageGlassInfo> list = this.list(new LambdaQueryWrapper<BigStorageGlassInfo>()
                .eq(BigStorageGlassInfo::getEngineerId, engineerId).eq(BigStorageGlassInfo::getTemperingLayoutId, temperingLayoutId));
        if (CollectionUtil.isNotEmpty(list)) {
            return;
        }
        Integer slotWidth = sysConfigService.queryConfigValue(ConstSysConfig.VERTICAL_SLOT_WIDTH);
        Integer glassGap = sysConfigService.queryConfigValue(ConstSysConfig.VERTICAL_GLASS_GAP);
        Integer outCarMaxSize = sysConfigService.queryConfigValue(ConstSysConfig.VERTICAL_OUT_CAR_SIZE);
//        Engineering engineering = engineeringMapper.selectOne(new LambdaQueryWrapper<Engineering>()
//                .eq(Engineering::getEngineerId, glassInfo.getEngineerId()));
        //获取玻璃的工程id,按照工程id获取工程下的所有玻璃信息
        List<GlassInfo> glassInfoList = glassInfoService.list(new LambdaQueryWrapper<GlassInfo>()
                .eq(GlassInfo::getEngineerId, engineerId)
                .orderByAsc(GlassInfo::getTemperingLayoutId).orderByAsc(GlassInfo::getTemperingFeedSequence));
        Map<Integer, List<GlassInfo>> trmperingIdMap = glassInfoList.stream().collect(Collectors.groupingBy(GlassInfo::getTemperingLayoutId));
        List<BigStorageGlassInfo> bigStorageGlassInfoList = new ArrayList<>();
        //方式一:将玻璃按版图、版序 依次生成虚拟格子信息,格子一直往后累加
        trmperingIdMap.forEach((e, v) -> {
            int remainWidth = slotWidth;
            int slotNumber = 1;
            int slotSequence = 1;
            for (GlassInfo item : v) {
                int maxLength = (int) Math.max(item.getWidth(), item.getHeight());
                if (remainWidth > maxLength && slotSequence <= outCarMaxSize) {
                    remainWidth = remainWidth - maxLength - glassGap;
                } else {
                    slotNumber = slotNumber + 1;
                    slotSequence = 1;
                    remainWidth = slotWidth - maxLength - glassGap;
                }
                BigStorageGlassInfo bigStorage = new BigStorageGlassInfo();
                BeanUtils.copyProperties(item, bigStorage);
                bigStorage.setVirtualSlot(slotNumber);
                bigStorage.setSlotSequence(slotSequence++);
                bigStorage.setId(null);
                bigStorageGlassInfoList.add(bigStorage);
            }
        });
        this.saveBatch(bigStorageGlassInfoList);
        log.info("分配完毕");
 
    }
 
    @Override
    public int bigStorageSlotPair(BigStorageGlassInfo bigStorageGlassInfo) {
        //获取所有空闲可用的格子号
        Integer slotWidth = sysConfigService.queryConfigValue(ConstSysConfig.VERTICAL_SLOT_WIDTH);
        BigStorageCage storageCage = bigStorageCageService.getOne(new LambdaQueryWrapper<BigStorageCage>()
                .eq(BigStorageCage::getEnableState, Const.SLOT_ON).eq(BigStorageCage::getRemainWidth, slotWidth)
                .le(BigStorageCage::getMinThickness, bigStorageGlassInfo.getThickness())
                .ge(BigStorageCage::getMaxThickness, bigStorageGlassInfo.getThickness())
                .orderByAsc(BigStorageCage::getMaxThickness).orderByAsc(BigStorageCage::getSlot).last("limit 1"));
        Assert.isTrue(null != storageCage, "没有空余的笼子存放玻璃");
        Integer slot = storageCage.getSlot();
        //获取该工程同一流程卡同一车的玻璃信息
        List<BigStorageGlassInfo> bigStorageGlassInfoList = this.list(new LambdaQueryWrapper<BigStorageGlassInfo>()
                .eq(BigStorageGlassInfo::getEngineerId, bigStorageGlassInfo.getEngineerId())
                .eq(BigStorageGlassInfo::getTemperingLayoutId, bigStorageGlassInfo.getTemperingLayoutId())
                .eq(BigStorageGlassInfo::getVirtualSlot, bigStorageGlassInfo.getVirtualSlot()));
        //设置关系表的实际格子号
        List<BigStorageGlassRelationInfo> relationInfoList = bigStorageGlassInfoList.stream().map(e -> {
            BigStorageGlassRelationInfo info = new BigStorageGlassRelationInfo();
            BeanUtils.copyProperties(e, info);
            info.setSlot(slot);
            info.setDeviceId(storageCage.getDeviceId());
            return info;
        }).collect(Collectors.toList());
        //保存关系表
        bigStorageGlassRelationInfoService.saveBatch(relationInfoList);
        //返回格子信息
        return slot;
    }
 
 
}