自动化立体仓库 - WMS系统
pang.jiabao
2024-10-12 a4dcb36d4c24a8b0505e4e331df6af582b27654a
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package com.zy.asrs.task.handler;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.mapper.*;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.WorkService;
import com.zy.asrs.service.WrkDetlService;
import com.zy.asrs.utils.Utils;
import com.zy.common.constant.MesConstant;
import com.zy.common.properties.SlaveProperties;
import com.zy.common.utils.HttpHandler;
import com.zy.system.entity.Config;
import com.zy.system.mapper.ConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author pang.jiabao
 * @description 冠鸿江铜定时任务实现
 * @createDate 2024/7/3 9:09
 */
@Slf4j
@Service
public class GhjtHandler {
 
    @Resource
    private OrderMapper orderMapper;
 
    @Autowired
    private ApiLogService apiLogService;
 
    @Resource
    private WrkMastMapper wrkMastMapper;
 
    @Resource
    private WrkDetlService wrkDetlService;
 
    @Resource
    private OrderDetlMapper orderDetlMapper;
 
    @Resource
    private LocDetlMapper locDetlMapper;
 
    @Resource
    private LocMastMapper locMastMapper;
 
    @Resource
    private WorkService workService;
 
    @Resource
    private ConfigMapper configMapper;
 
    @Autowired
    private SlaveProperties slaveProperties;
 
    @Transactional
    public void startCkrwPushGwcs(WrkMast wrkMast) {
 
        // 获取请求头
        Map<String, Object> headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
 
        // 下发给gwcs要走的路径标识
        int descFlag = getDescToGwcs(wrkMast);
 
        // 构造请求体
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("workNo", wrkMast.getWrkNo());
        jsonObject.put("staNo", wrkMast.getIoType() == 3 ? 3013 :wrkMast.getStaNo());
        jsonObject.put("barcode", wrkMast.getBarcode());
        jsonObject.put("sourceStaNo", wrkMast.getIoType() == 3 ? wrkMast.getStaNo() : wrkMast.getSourceStaNo());
        jsonObject.put("descFlag", descFlag); // 101出库时用,0只有一条路径,1理货贴标路径,2贴标打带路径
        String body = jsonObject.toJSONString();
 
        boolean success = false;
        String response = "";
        try {
            response = new HttpHandler.Builder()
                    .setUri(MesConstant.GWCS_IP_PORT)
                    .setPath(MesConstant.GWCS_DCKK_URL)
                    .setHeaders(headers)
                    .setJson(body)
                    .build()
                    .doPost();
            if (!Cools.isEmpty(response)) {
                wrkMast.setWrkSts(2L); // 更新为设备上走
                if (wrkMast.getIoType() == 110) { // 空托盘出库直接完成任务
                    wrkMast.setWrkSts(14L);
                } else if (wrkMast.getIoType() == 3) {
                    // 修改工作主档状态
                    wrkMast.setWrkSts(15L);
                    wrkMast.setModiTime(new Date());
                } else if(wrkMast.getIoType() == 12) { // 跨巷道转移
                    wrkMast.setWrkSts(1L); // 状态改为1.生成入库id
                    wrkMast.setModiTime(new Date());
                }
                wrkMastMapper.updateById(wrkMast);
                success = true;
            } else {
                log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL + MesConstant.PAKIN_URL, body, response);
                throw new CoolException("下发出库任务给GWCS(从出库码头到出库口)失败");
            }
        } catch (Exception e) {
            log.error("下发出库任务给GWCS(从出库码头到出库口)异常,工作号:{},{}", wrkMast.getWrkNo(), e.getMessage());
        } finally {
            try {
                // 保存接口日志
                apiLogService.save(
                        "从出库码头到出库口",
                        MesConstant.URL + MesConstant.PAKIN_URL,
                        null,
                        "127.0.0.1",
                        jsonObject.toJSONString(),
                        response,
                        success
                );
            } catch (Exception e) {
                log.error("接口日志保存异常", e);
            }
        }
    }
 
    /**
     * 全板出库到3077或3106的任务,判断出库要走的路径
     * @param wrkMast 工作主档
     * @return 1.直接出库,只有一条路径 2.理货贴标出库 3.贴标打带出库 4.贴标出库
     */
    private int getDescToGwcs(WrkMast wrkMast) {
        // todo 两条路线怎么选
        int flag = 1;
        List<WrkDetl> wrkDetls = wrkDetlService.selectList(new EntityWrapper<WrkDetl>().eq("wrk_no", wrkMast.getWrkNo()));
        List<String> collect = wrkDetls.stream().map(WrkDetl::getBrand).distinct().collect(Collectors.toList());
 
        if (wrkMast.getIoType() == 101 && (wrkMast.getStaNo() == 3077 || wrkMast.getStaNo() == 3106)) {
            // 有几个木箱
            if (collect.size() == 1) { // 一箱
                if (wrkDetls.size() == 1) { // 一卷去贴标
                     flag = 4;
                } else { // 多卷直接出
 
                }
            } else if(collect.size() == 2) { // 两箱去贴标打带
 
                // 两箱肯定都是单卷情况 同型号(木箱型号-管芯类型-实测宽幅-生箔厚度-分切下料时间)去贴标,打带,不同型号贴标出库
                WrkDetl wrkDetl1 = wrkDetls.get(0);
                WrkDetl wrkDetl2 = wrkDetls.get(1);
                if (wrkDetl1.getColor().equals(wrkDetl2.getColor())&&
                wrkDetl1.getSku().equals(wrkDetl2.getSku())&&
                wrkDetl1.getManu().equals(wrkDetl2.getManu())&&
                wrkDetl1.getItemNum().equals(wrkDetl2.getItemNum())) {
                    flag = 3;
                } else {
                    flag = 4;
                }
            }
        } else if (wrkMast.getIoType() == 103 && (wrkMast.getStaNo() == 3077 || wrkMast.getStaNo() == 3106)) { // 两箱出一箱,需桁架理货
            // 都要先去理货
            flag = 2;
//            if (wrkDetls.size() == 1) { // 一卷贴标出库
//                flag = 4;
//            } else { // 多卷直接出库
//
//            }
        }
        return flag;
    }
 
    /**
     * 自动备货处理
     */
    @Transactional
    public void autoStockUpHandler(List<String> list,int columnNum) {
 
        // 根据包装组号获取所在库位
        List<LocDetl> locDetls = locDetlMapper.selectLocNoByGroupNo(list);
        if (locDetls.isEmpty()) {
            return;
        }
        // 相同则合并,一个库位两个包装组号
        Map<String,List<String>> map = new HashMap<>();
        for(LocDetl locDetl : locDetls) {
            List<String> brand = map.get(locDetl.getLocNo());
            if (brand == null) {
                map.put(locDetl.getLocNo(),new ArrayList<String>(){{add(locDetl.getBrand());}});
            } else {
                brand.add(locDetl.getBrand());
                map.put(locDetl.getLocNo(),brand);
            }
        }
 
        // 遍历堆垛机并且判断是否存在任务
        for (int i = 1; i <= 6; i++) {
            // 要备货的库位
            String sourceLocNo = null;
            // 备货目标库位
            String staLocNo = null;
            Integer wrkCount = wrkMastMapper.selectCount(new EntityWrapper<WrkMast>().eq("crn_no", i));
            if(wrkCount > 0) {
                log.warn("{}号堆垛机已存在任务",i);
                continue;
            }
            // 根据堆垛机号查询到对应的深库位和浅库位  深库位4*n-3和4*n 浅库位4*n-2和4*n-1
            int s1 = 4*i-3;
            int s2 = 4*i;
            int q1 = 4 * i - 2;
            int q2 = 4 * i -1;
 
            // 根据堆垛机号获取一个浅库位
            for(String key : map.keySet()) {
                int row = Integer.parseInt(key.substring(0, 2));
                if (row == q1 || row == q2) {
                    sourceLocNo = key;
                    break;
                }
            }
            // 浅库位没有则找一个深库位
            if (sourceLocNo == null) {
                for(String key : map.keySet()) {
                    int row = Integer.parseInt(key.substring(0, 2));
                    if (row == s1 || row == s2) {
                        sourceLocNo = key;
                        break;
                    }
                }
            }
            // 没有找到源库位
            if (sourceLocNo == null) {
                log.warn("没有找到源库位,堆垛机:{}",i);
                continue;
            }
            // 寻找一个备货的目标库位,先深后浅
            List<LocMast> locMasts1 = locMastMapper.selectList(new EntityWrapper<LocMast>().eq("loc_sts", "O").eq("crn_no", i).in("row1", s1, s2)
                    .le("bay1", columnNum));
            if (locMasts1.isEmpty()) {
                // 深库位为空了,取浅库位
                List<LocMast> locMasts2 = locMastMapper.selectList(new EntityWrapper<LocMast>().eq("loc_sts", "O").eq("crn_no", i).in("row1", q1, q2)
                        .le("bay1", columnNum));
                if (!locMasts2.isEmpty()) {
                    staLocNo = locMasts2.get(0).getLocNo();
                }
            } else {
                staLocNo = locMasts1.get(0).getLocNo();
            }
 
            if(staLocNo == null) {
                log.warn("{}号堆垛机备货区满了",i);
                continue;
            }
 
            // 备货的源库位,目标库位都获取到了,生成移库任务
            workService.locMove(sourceLocNo,staLocNo,29L);
 
            // 订单明细改成备货中,在任务完成时候改成备货完成,并判断整个订单是否完成
            orderDetlMapper.updateOrderDetlStatusByPackageNo(map.get(sourceLocNo), 1);
 
            // 移除已生成备货库位
            map.remove(sourceLocNo);
        }
 
    }
 
    @Transactional
    public void autoMoveLoc(List<OrderDetl> orderDetlList) {
 
        // 判断是否已经有执行的移库任务
        Integer count = wrkMastMapper.selectCount(new EntityWrapper<WrkMast>().in("io_type", 11, 12));
        if (count > 0) {
            return;
        }
 
        OrderDetl detl = null; // 要移库的明细
        String staLoc = null; // 移库目标库位
 
        // 从待移库位列表中先浅后深获取一个待移库位
        Optional<OrderDetl> any = orderDetlList.stream().filter(orderDetl -> Utils.isShallowLoc(slaveProperties,orderDetl.getSpecs())).findAny();
 
        if (any.isPresent()){
            detl = any.get();
        }
        // 剩下的应该都是深库位,获取第一个
        if (detl == null) {
            detl = orderDetlList.get(0);
            // 对应浅库位有货,在堆垛机出库的时候会检测到,在那里生成移库任务
        }
 
        // 获取备货区配置
        Config config = configMapper.selectConfigByCode("auto_stock_up");
        if (config == null) {
            return;
        }
        // 备货取是前几列
        int bay1 = Integer.parseInt(config.getValue());
        // 指定的目标库位
        String model = detl.getModel();
 
        // 指定目标库位
        if (!Cools.isEmpty(model)) {
            // 目标库位是深库位
            if (Utils.isDeepLoc(slaveProperties,model)) {
                // 目标库位
                LocMast locMast2 = locMastMapper.selectById(model);
                if (locMast2 == null || !locMast2.getLocSts().equals("O")) {
                    log.error("指定的目标库位【{}】状不为空", model);
                    return;
                }
                // 获取到对应浅库位
                String shallowLoc = Utils.getShallowLoc(slaveProperties, model);
                LocMast locMast = locMastMapper.selectById(shallowLoc);
                // 浅库位有货
                if (locMast.getLocSts().equals("F") || locMast.getLocSts().equals("D")) {
                    log.error("选择的深库位【{}】,但是浅库位【{}】有货", model, locMast.getLocNo());
                    return;
                }
                // 避开备货区
                if (Utils.getBay(model) <= bay1) {
                    log.error("指定的目标库位【{}】在备货区,不能转移", model);
                    return;
                }
            }
            // 深库位检测通过,或者是浅库位
            staLoc = model;
        } else if ( detl.getBeBatch() != null) { // 指定目标巷道
            // 目标巷道
            Integer beBatch = detl.getBeBatch();
            // 从巷道里面先深后浅取一个空库位,不要占用备货区
            List<LocMast> locMasts = locMastMapper.selectList(new EntityWrapper<LocMast>().eq("crn_no", beBatch).eq("loc_sts", "O").gt("bay1", bay1));
            if (locMasts.isEmpty()) {
                log.error("指定巷道【{}】没有能移库的空库位了",beBatch);
                return;
            }
            // 先过滤出深库位
            Optional<LocMast> a = locMasts.stream().filter(locMast -> Utils.isDeepLoc(slaveProperties,locMast.getLocNo())).findAny();
            if (a.isPresent()){
                staLoc = a.get().getLocNo();
                // 获取到对应浅库位
                String shallowLoc = Utils.getShallowLoc(slaveProperties, staLoc);
                LocMast locMast = locMastMapper.selectById(shallowLoc);
                // 浅库位有货
                if (locMast.getLocSts().equals("F") || locMast.getLocSts().equals("D")) {
                    log.error("指定的目标巷道深库位【{}】,但是浅库位【{}】有货",staLoc,locMast.getLocNo());
                    return;
                }
            }
            // 深库位没有则取浅库位
            if (staLoc == null) {
                staLoc = locMasts.get(0).getLocNo();
            }
        } else {
            log.error("目标库位或目标巷道有误:{}",detl);
            return;
        }
 
        // 生成跨巷道移库任务
        workService.autoLocMove(detl.getOrderNo(),detl.getSpecs(),staLoc,29L);
 
        // 更新单据明细移库状态为移库中
        detl.setDanger(1);
        if (detl.getBeBatch() != null) {
            detl.setModel(staLoc); // 补充目标库位
        }
        orderDetlMapper.updateById(detl);
        // 更新单据状态为2处理中
        orderMapper.updatePendingSettleByOrderNo(detl.getOrderNo(),2L);
    }
 
}