zhoushihao
2024-10-15 668137140bca5894b8a98e08414109d8f8cca070
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
package com.mes.job;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mes.common.config.Const;
import com.mes.engineering.entity.Engineering;
import com.mes.engineering.mapper.EngineeringMapper;
import com.mes.milo.model.ReadWriteEntity;
import com.mes.milo.service.MiloService;
import com.mes.rawglassdetails.entity.RawGlassStorageDetails;
import com.mes.rawglassdetails.service.RawGlassStorageDetailsService;
import com.mes.rawglassstation.entity.RawGlassStorageStation;
import com.mes.rawglassstation.service.RawGlassStorageStationService;
import com.mes.rawglasstask.entity.RawGlassStorageTask;
import com.mes.rawglasstask.service.RawGlassStorageTaskService;
import com.mes.uppattenusage.entity.vo.UpPattenUsageVO;
import com.mes.uppattenusage.mapper.UpPattenUsageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @Author : zhoush
 * @Date: 2024/10/11 16:13
 * @Description:
 */
@Slf4j
@Component
public class RawGlassTask {
 
    @Autowired
    private RawGlassStorageStationService rawGlassStorageStationService;
    @Autowired
    private RawGlassStorageDetailsService rawGlassStorageDetailsService;
 
    @Autowired
    private RawGlassStorageTaskService rawGlassStorageTaskService;
 
    @Resource
    private EngineeringMapper engineeringMapper;
    @Resource
    private UpPattenUsageMapper upPattenUsageMapper;
 
    @Autowired
    private MiloService miloService;
 
    private static final List<String> liftingStation = Arrays.asList("1", "2");
    private static final List<String> loadGlassStation = Arrays.asList("3", "4", "5", "6");
 
    /**
     * 入库任务:吊装位有玻璃,先去工位表查询空格子,生成入库任务从吊装位到目标格子
     *
     * @throws Exception
     */
    @Scheduled(fixedDelay = 1000)
    public void warehouseTask() throws Exception {
        ReadWriteEntity entity = miloService.readFromOpcUa("rawglass.device.request");
        String value = entity.getValueString();
        if (!"1".equals(value)) {
            log.info("大车忙碌");
            return;
        }
        List<RawGlassStorageDetails> rawGlassList = rawGlassStorageDetailsService.list(new LambdaQueryWrapper<RawGlassStorageDetails>()
                .eq(RawGlassStorageDetails::getState, Const.GLASS_STATE_IN)
                .inSql(RawGlassStorageDetails::getSlotId, "select slot from raw_glass_storage_station where enable_state = 1 and slot in (1,2)"));
        if (CollectionUtil.isEmpty(rawGlassList)) {
            log.info("吊装位被禁用或没有玻璃");
            return;
        }
        //查询工位信息是否有
        List<RawGlassStorageStation> stationList = rawGlassStorageStationService.list(new LambdaQueryWrapper<RawGlassStorageStation>().notInSql(RawGlassStorageStation::getSlot, "select slot_id from raw_glass_storage_details where state = '100'")
                .eq(RawGlassStorageStation::getEnableState, Const.SLOT_ON));
        if (CollectionUtil.isEmpty(stationList)) {
            log.info("没有空的工位");
            return;
        }
        //生成进笼任务
        generateTask(rawGlassList.get(0).getSlotId(), stationList.get(0).getSlot(),
                rawGlassList.get(0).getRemainQuantity(), Const.RAW_GLASS_TASK_TYPE_IN);
        //生成工位任务,将吊装位的玻璃状态改位进笼中
    }
 
 
    /**
     * 出库任务:1、点出库,立马生成出片任务 2、点出库修改工位详情内的状态为待出库,定时任务扫描生成出库任务
     */
    @Scheduled(fixedDelay = 1000)
    public void outboundTask() throws Exception {
        ReadWriteEntity entity = miloService.readFromOpcUa("rawglass.device.request");
        String value = entity.getValueString();
        if (!"2".equals(value)) {
            log.info("大车忙碌");
            return;
        }
        List<RawGlassStorageDetails> rawGlassList = rawGlassStorageDetailsService.list(new LambdaQueryWrapper<RawGlassStorageDetails>()
                .eq(RawGlassStorageDetails::getState, Const.GLASS_STATE_OUT_ING)
                .inSql(RawGlassStorageDetails::getSlotId, "select slot from raw_glass_storage_station where enable_state = 1 and slot not in (1,2)"));
        if (CollectionUtil.isEmpty(rawGlassList)) {
            log.info("系统没有需要出库的原片信息");
            return;
        }
        List<Integer> emptyLeftingList = rawGlassStorageDetailsService.listBySlotState(liftingStation, Arrays.asList(Const.GLASS_STATE_IN));
        if (CollectionUtil.isEmpty(emptyLeftingList)) {
            log.info("吊装位当前都有原片,结束出片任务");
        }
        //生成出库任务
        generateTask(rawGlassList.get(0).getSlotId(), emptyLeftingList.get(0),
                rawGlassList.get(0).getRemainQuantity(), Const.RAW_GLASS_TASK_TYPE_OUT);
    }
 
    /**
     * 原片调度:1、查询工程原片表,按照顺序将原片放入上片1号位,后续原片放上片2号位,出现尺寸替换,判断原上片位是否有玻璃,有 先出后进,无 直接进片
     */
    @Scheduled(fixedDelay = 1000)
    public void rawGlassDispatchTask() throws Exception {
        ReadWriteEntity entity = miloService.readFromOpcUa("rawglass.device.request");
        String value = entity.getValueString();
        if (!"2".equals(value)) {
            log.info("大车忙碌");
            return;
        }
        //查询当前系统正在执行的订单
        Engineering engineering = engineeringMapper.selectOne(new LambdaQueryWrapper<Engineering>().eq(Engineering::getState, Const.ENGINEERING_RUNNING).last("order by id limit 1"));
        if (null == engineering) {
            log.info("没有正在执行的工程");
            return;
        }
        //当前尺寸需要上片的数量
        List<UpPattenUsageVO> pattenUsageList = upPattenUsageMapper.queryRawGlassByEngineeringId(engineering.getEngineerId());
        if (CollectionUtils.isEmpty(pattenUsageList)) {
            log.info("正在执行的工程原片无可上片的原片信息");
            return;
        }
        Map<String, List<UpPattenUsageVO>> upListMap = pattenUsageList.stream()
                .collect(Collectors.groupingBy(UpPattenUsageVO::getGroupNumber));
        //todo:按照工程号按照工程下未完成的尺寸的顺序,当1号上片位架子上的当前尺寸玻璃少于3片且2号上片位无原片玻璃,则将去调度玻璃去2号上片位,
        //todo:当一号上片位架子上的玻璃位空或者当前尺寸用完时时,将2号(有玻璃)上片位调度到1号上片位
        //1、查询4个上片的原片详情
        List<RawGlassStorageDetails> rawGlassDetailsList = rawGlassStorageDetailsService.list(new LambdaQueryWrapper<RawGlassStorageDetails>()
                .eq(RawGlassStorageDetails::getState, Const.GLASS_STATE_IN).in(RawGlassStorageDetails::getSlotId, loadGlassStation));
        if (CollectionUtils.isEmpty(rawGlassDetailsList)) {
            //表示1上片位没有原片,直接找原片放入对应的上片位
            List<UpPattenUsageVO> upPattenUsage01VOS = upListMap.get("1");
            UpPattenUsageVO usageVO = upPattenUsage01VOS.get(0);
            RawGlassStorageDetails details = rawGlassStorageDetailsService.getOne(new LambdaQueryWrapper<RawGlassStorageDetails>()
                    .eq(RawGlassStorageDetails::getFilmsId, usageVO.getFilmsId())
                    .eq(RawGlassStorageDetails::getPatternWidth, usageVO.getWidth())
                    .eq(RawGlassStorageDetails::getPatternHeight, usageVO.getHeight())
                    .eq(RawGlassStorageDetails::getPatternThickness, usageVO.getThickness())
                    .gt(RawGlassStorageDetails::getRemainQuantity, upPattenUsage01VOS.size())
                    .orderByAsc(RawGlassStorageDetails::getRemainQuantity)
                    .last("limit 1")
            );
            generateTask(details.getSlotId(), 1, details.getRemainQuantity(), Const.RAW_GLASS_TASK_TYPE_DISPATCH);
            //结束调度任务
        }
        Map<Integer, List<RawGlassStorageDetails>> listMap = rawGlassDetailsList.stream().collect(Collectors.groupingBy(RawGlassStorageDetails::getSlotId));
        RawGlassStorageDetails rawGlass03Details = listMap.get(3).get(0);
        RawGlassStorageDetails rawGlass04Details = listMap.get(4).get(0);
//        RawGlassStorageDetails rawGlass05Details = listMap.get(5).get(0);
//        RawGlassStorageDetails rawGlass06Details = listMap.get(6).get(0);
        //todo:上片1号位2种清空方式:方式一:原片用完  方式二:当前尺寸用完
        if (null == rawGlass03Details) {
            if (null == rawGlass04Details) {
                //表示1上片位没有原片,直接找原片放入对应的上片位
                List<UpPattenUsageVO> upPattenUsage01VOS = upListMap.get("1");
                UpPattenUsageVO usageVO = upPattenUsage01VOS.get(0);
                RawGlassStorageDetails details = rawGlassStorageDetailsService.getOne(new LambdaQueryWrapper<RawGlassStorageDetails>()
                        .eq(RawGlassStorageDetails::getFilmsId, usageVO.getFilmsId())
                        .eq(RawGlassStorageDetails::getPatternWidth, usageVO.getWidth())
                        .eq(RawGlassStorageDetails::getPatternHeight, usageVO.getHeight())
                        .eq(RawGlassStorageDetails::getPatternThickness, usageVO.getThickness())
                        .gt(RawGlassStorageDetails::getRemainQuantity, upPattenUsage01VOS.size())
                        .orderByAsc(RawGlassStorageDetails::getRemainQuantity)
                        .last("limit 1")
                );
                generateTask(details.getSlotId(), 1, details.getRemainQuantity(), Const.RAW_GLASS_TASK_TYPE_DISPATCH);
                //结束调度任务
            } else {
                //将2号上片位的原片放入1号上片位
                generateTask(2, 1, rawGlass04Details.getRemainQuantity(), Const.RAW_GLASS_TASK_TYPE_DISPATCH);
                //结束调度任务
            }
        } else {
            if (null == rawGlass04Details) {
                List<UpPattenUsageVO> upPattenUsage01VOS = upListMap.get("2");
                if (CollectionUtils.isEmpty(upPattenUsage01VOS)) {
                    return;
                }
                UpPattenUsageVO usageVO = upPattenUsage01VOS.get(0);
                RawGlassStorageDetails details = rawGlassStorageDetailsService.getOne(new LambdaQueryWrapper<RawGlassStorageDetails>()
                        .eq(RawGlassStorageDetails::getFilmsId, usageVO.getFilmsId())
                        .eq(RawGlassStorageDetails::getPatternWidth, usageVO.getWidth())
                        .eq(RawGlassStorageDetails::getPatternHeight, usageVO.getHeight())
                        .eq(RawGlassStorageDetails::getPatternThickness, usageVO.getThickness())
                        .gt(RawGlassStorageDetails::getRemainQuantity, upPattenUsage01VOS.size())
                        .orderByAsc(RawGlassStorageDetails::getRemainQuantity)
                        .last("limit 1")
                );
                generateTask(details.getSlotId(), 2, details.getRemainQuantity(), Const.RAW_GLASS_TASK_TYPE_DISPATCH);
            }
        }
 
 
    }
 
 
    /**
     * 生成原片仓储任务
     *
     * @param startSlot
     * @param endSlot
     * @param patternQuantity
     * @param taskType
     * @return
     */
    private boolean generateTask(int startSlot, int endSlot, Integer patternQuantity, int taskType) {
        RawGlassStorageTask task = RawGlassStorageTask.builder()
                .originateSlot(startSlot)
                .endSlot(endSlot)
                .patternQuantity(patternQuantity)
                .taskType(taskType)
                .createTime(new Date()).build();
        return rawGlassStorageTaskService.save(task);
    }
}