自动化立体仓库 - WMS系统
#
zwl
7 小时以前 44b6b79dd5dee0ebbd2d11b08abbc2be275bed58
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package com.zy.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.api.controller.params.ReceviceTaskParams;
import com.zy.api.controller.params.WorkTaskParams;
import com.zy.api.service.WcsApiService;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.constant.MesConstant;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class WcsApiServiceImpl implements WcsApiService {
 
    @Autowired
    private BasDeviceService basDeviceService;
    @Autowired
    private LocAroundBindService locAroundBindService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private WorkService workService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private MatService matService;
    @Autowired
    private LocDetlService locDetlService;
    @Value("${wcs.address.URL}")
    private String wcs_address;
 
    @Value("${wcs.address.createOutTask}")
    private String getWcs_address;
 
    @Value("${wcs.address.createInTask}")
    private String createInTask;
 
    @Value("${wcs.address.createLocMoveTask}")
    private String createLocMoveTask;
    @Autowired
    private CommonService commonService;
 
    /**
     * 通知WCS锁定库位,及禁止当前库位的一切操作
     *
     * @param params
     * @return com.core.common.R
     * @author Ryan
     * @date 2026/1/10 11:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R lockLocs(ReceviceTaskParams params) {
        BasDevice basDevice = basDeviceService.selectOne(new EntityWrapper<BasDevice>()
                .eq("status", 1)
                .eq("dev_no", params.getDevNo()));
        if (Objects.isNull(basDevice)) {
            throw new CoolException("机台信息不存在或已禁用!!");
        }
        List<LocAroundBind> binds = locAroundBindService.selectList(new EntityWrapper<LocAroundBind>().eq("dev_no", basDevice.getDevNo()));
        if (Objects.isNull(binds) || binds.isEmpty()) {
            throw new CoolException("机台未绑定工作站台!!");
        }
        Set<String> locs = binds.stream().map(LocAroundBind::getBlocNo).collect(Collectors.toSet());
 
        reportLockLocs(locs, "lock");
 
        return R.ok("上报成功!!");
    }
 
    /**
     * 余料回库 (搬运余料回库)
     *
     * @param params
     * @return com.core.common.R
     * @author Ryan
     * @date 2026/1/10 13:19
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R backLocs(String params) {
//        if (Objects.isNull(params.getWrkNo())) {
//            throw new CoolException("工作号不能为空!!");
//        }
//        String wrkCode = params.getTaskNo();
//       if (wrkCode.contains("-1")) {
//           throw new CoolException("配对任务编码错误,请检查后重新上传!!");
//       }
        WrkMast mast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("barcode", params));
        if (Objects.isNull(mast)) {
            throw new CoolException("任务不存在!!");
        }
//       if (!mast.getWrkSts().equals(103L)) {
//           throw new CoolException("当前任务并非余料出库任务!!");
//       }
//       if (params.getIsSuplus() == 1) {
//           mast.setIsSuplus(1);
//       }
//
//       if (!wrkMastService.updateById(mast)) {
//           throw new CoolException("任务状态更新失败!!");
//       }
        workService.backLocOperation(mast.getWrkNo() + "", mast.getAppeUser());
 
        return R.ok("接收成功,执行回库中...");
    }
 
    /**
     * 下发任务至WCS
     *
     * @param params
     * @return com.core.common.R
     * @author Ryan
     * @date 2026/1/10 13:58
     */
    @Override
    public R pubWrkToWcs(WorkTaskParams params) {
        if (Objects.isNull(params.getTaskNo())) {
            return R.error("任务号不能为空!!");
        }
        if (Objects.isNull(params.getBarcode())) {
            return R.error("托盘码不能为空!!");
        }
        if (Objects.isNull(params.getLocNo())) {
            return R.error("目标库位不能为空!!");
        }
        String url = createInTask;
        if (!Objects.isNull(params.getType()) && params.getType().equals("out")) {
            url = getWcs_address;
        }else if (!Objects.isNull(params.getType()) && params.getType().equals("move")) {
            url = createLocMoveTask;
        }
        String response;
        R r = R.ok();
        try {
            log.info("下发搬运任务给wcs="+JSON.toJSONString(params));
            response = new HttpHandler.Builder()
                    .setUri(wcs_address)
                    .setPath(url)
                    .setJson(JSON.toJSONString(params))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            log.info("下发任务给wcs的返回值="+response);
            Integer code = jsonObject.getInteger("code");
 
            if (code==200) {
                WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", params.getTaskNo()));
                if (!Objects.isNull(wrkMast)) {
                    if (wrkMast.getIoType()==1 || wrkMast.getIoType()==10) {
                        wrkMast.setWrkSts(2L);
                        wrkMast.setModiTime(new Date());
                        wrkMastService.updateById(wrkMast);
                    }else if(wrkMast.getIoType()==2){
                        wrkMast.setWrkSts(2L);
                        wrkMast.setModiTime(new Date());
                        wrkMastService.updateById(wrkMast);
                        String s = Utils.convertLocFormat(params.getLocNo());
                        LocAroundBind bind = locAroundBindService.selectOne(new EntityWrapper<LocAroundBind>()
                                .eq("b_loc_no", s));
                        if (!Objects.isNull(bind)) {
                            bind.setFreeze(1);
                            locAroundBindService.updateById(bind);
                            //冻结相邻库位
                            Integer[] aroundIds = freezeLocAround(bind.getOrderNo());
                            if (aroundIds != null) {
                                for (Integer id : aroundIds) {
                                    if (id != null && id > 0) {
                                        LocAroundBind aroundBind = locAroundBindService.selectOne(
                                                new EntityWrapper<LocAroundBind>()
                                                        .eq("dev_id", bind.getDevId())
                                                        .eq("order_no", id));
                                        if (aroundBind != null) {
                                            aroundBind.setFreeze(1);
                                            locAroundBindService.updateById(aroundBind);
                                        }
                                    }
                                }
                            }
                        }
                    }else if (wrkMast.getIoType()==101){
                        wrkMast.setWrkSts(12L);
                        wrkMast.setModiTime(new Date());
                        wrkMastService.updateById(wrkMast);
                        //出库为机台工位时,冻结相对有的库位,及相邻库位
                        String s = Utils.convertLocFormat(params.getLocNo());
                        LocAroundBind bind = locAroundBindService.selectOne(new EntityWrapper<LocAroundBind>()
                                .eq("b_loc_no", s));
                        if (!Objects.isNull(bind)&&Cools.isEmpty(wrkMast.getStaNo())) {
                            bind.setFreeze(1);
                            locAroundBindService.updateById(bind);
                            //冻结相邻库位
                            Integer[] aroundIds = freezeLocAround(bind.getOrderNo());
                            if (aroundIds != null) {
                                for (Integer id : aroundIds) {
                                    if (id != null && id > 0) {
                                        LocAroundBind aroundBind = locAroundBindService.selectOne(
                                                new EntityWrapper<LocAroundBind>()
                                                        .eq("dev_id", bind.getDevId())
                                                        .eq("order_no", id));
                                        if (aroundBind != null) {
                                            aroundBind.setFreeze(1);
                                            locAroundBindService.updateById(aroundBind);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //TODO 上报是否成功
            }else {
                r =R.error();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return r;
    }
 
 
    /**
     * 上报锁定/释放库位信息
     *
     * @param locs
     * @author Ryan
     * @date 2026/1/10 12:50
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reportLockLocs(Set<String> locs, String type) {
        String url = MesConstant.LOCK_LOCS_URL;
        if (!Objects.isNull(type)) {
            if (type.equals("release")) {
                url = MesConstant.RELEASE_LOCS_URL;
            }
        }
        String response;
        try {
            response = new HttpHandler.Builder()
                    .setUri(MesConstant.URL)
                    .setPath(url)
                    .setJson(JSON.toJSONString(locs))
                    .build()
                    .doPost();
            R result = JSON.parseObject(response, R.class);
 
            if (result.get("code").equals("200")) {
                //TODO 上报是否成功
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
 
    }
 
    /**
     * 堆垛机执行状态上报
     *
     * @param params
     * @return com.core.common.R
     * @author Ryan
     * @date 2026/1/10 16:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R receviceTaskFromWcs(ReceviceTaskParams params) {
        log.info("wcs任务反馈="+JSON.toJSONString(params));
        if (Objects.isNull(params.getSuperTaskNo())) {
            throw new CoolException("WMS任务号不能为空!!");
        }
        if (Objects.isNull(params.getNotifyType())) {
            throw new CoolException("动作类型不能为空!!");
        }
        WrkMast mast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", params.getSuperTaskNo()));
        if (Objects.isNull(mast)) {
            throw new CoolException("任务档不存在!!");
        }
 
 
        if (params.getNotifyType().equals("task")) {
            //任务
            if (params.getMsgType().equals("task_complete")) {
                //称重
                if ((mast.getIoType()==1||mast.getIoType()==10)&&mast.getWrkSts() == 2 && Cools.isEmpty(mast.getStaNo())) {
                    Double weight = 0.0;
                    if (!Cools.isEmpty(params.getData())){
                        JSONObject jsonObject = JSONObject.parseObject(params.getData());
                        weight=jsonObject.getDouble("weight");
                    }
                    if (mast.getIoType() == 10) {
                        //空板入库任务档
                        LocDetl locDetl = locDetlService.selectOne(new EntityWrapper<LocDetl>()
                                .eq("zpallet", mast.getBarcode())
                                .eq("loc_no", mast.getSourceLocNo()));
                        if (Objects.isNull(locDetl)) {
                            throw new CoolException("明细数据不存在!!");
                        }
                        Mat matnr = matService.selectOne(new EntityWrapper<Mat>().eq("matnr", locDetl.getMatnr()));
                        if (Objects.isNull(matnr)) {
                            throw new CoolException("物料基础信息不存在!!");
                        }
                        if (!Cools.isEmpty(weight)) {
                            Double val = Math.round((weight - matnr.getSafeQty()) * 10000) / 10000.0;
                            if (val.compareTo(0.0) > 0) {
                                WrkDetl wrkDetl = new WrkDetl();
                                BeanUtils.copyProperties(locDetl, wrkDetl);
                                wrkDetl.setWrkNo(mast.getWrkNo());
                                wrkDetl.setStockQty(locDetl.getAnfme());
                                wrkDetl.setIoTime(new Date());
                                wrkDetl.setWeight(weight);
                                wrkDetl.setIoTime(mast.getIoTime());
                                //余料长度
                                wrkDetl.setAnfme(val * matnr.getVolume());
                                if (!wrkDetlService.insert(wrkDetl)){
                                    throw new CoolException("无法新增明细!!");
                                }
                                mast.setIoType(1);
                                mast.setIsSuplus(1);
                                mast.setOveMk("Y");
                                if (!wrkMastService.updateById(mast)) {
                                    throw new CoolException("任务档修改失败!!");
                                }
                                // 更新原出库单
 
                                if (!Cools.isEmpty(mast.getWrkCode())) {
                                    String wrkNo = mast.getWrkCode();
                                    WrkMast orgWrk = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", wrkNo));
                                    if (Objects.isNull(orgWrk)) {
                                        throw new CoolException("数据错误,主任务档不存在或已删除!!");
                                    }
                                    orgWrk.setOveMk("Y");
                                    if (!wrkMastService.updateById(orgWrk)) {
                                        throw new CoolException("任务档修改失败!!");
                                    }
                                    WrkDetl detl = wrkDetlService.selectOne(new EntityWrapper<WrkDetl>().eq("wrk_no", wrkNo));
                                    if (Objects.isNull(detl)) {
                                        throw new CoolException("子任务档明细不存在!!");
                                    }
//                                    //库存减去称重长度
//                                    Double v = Math.round((wrkDetl.getStockQty() - realQty) * 10000) / 10000.0;
//                                    //原出为数量与真实数量互换,保持一致性
//                                    detl.setAnfme(v);
//                                    detl.setWeight(weight);
                                    if (!wrkDetlService.update(detl, new EntityWrapper<WrkDetl>().eq("wrk_no", wrkNo).eq("matnr", detl.getMatnr()).eq("barcode", detl.getBarcode()))) {
                                        throw new CoolException("原任务档明细修改失败!!");
                                    }
                                } else {
                                    throw new CoolException("任务号截取失败,请检查主任务档任档wrkCode字段");
                                }
                            }
                        }
                    } else {
                        WrkDetl wrkDetl = wrkDetlService.selectOne(new EntityWrapper<WrkDetl>().eq("wrk_no", mast.getWrkNo()));
                        if (Objects.isNull(wrkDetl)) {
                            throw new CoolException("数据错误,任务明细不存在!!");
                        }
                        Mat matnr = matService.selectOne(new EntityWrapper<Mat>().eq("matnr", wrkDetl.getMatnr()));
                        if (Objects.isNull(matnr)) {
                            throw new CoolException("物料基础信息不存在!!");
                        }
                        Double realQty = Math.round((wrkDetl.getStockQty() - wrkDetl.getAnfme()) * 10000) / 10000.0;
                        if (!Cools.isEmpty(weight)) {
                            Double val = Math.round((weight - 2.35) * 10000) / 10000.0;
                            //称重后,计算出真实长度
                            realQty = val * matnr.getVolume();
                            if (realQty<matnr.getSafeQty()){
                                wrkDetlService.delete(new EntityWrapper<WrkDetl>().eq("wrk_no", wrkDetl.getWrkNo()));
                                mast.setIoType(10);
                                mast.setEmptyMk("Y");
                            }else{
                                wrkDetl.setAnfme(realQty);
                                wrkDetl.setWeight(weight);
                                if (!wrkDetlService.update(wrkDetl, new EntityWrapper<WrkDetl>().eq("wrk_no", mast.getWrkNo()).eq("matnr", wrkDetl.getMatnr()).eq("barcode", wrkDetl.getBarcode()))) {
                                    throw new CoolException("任务档明细修改失败!!");
                                }
                            }
 
                        }
 
 
                        mast.setOveMk("Y");
                        if (!wrkMastService.updateById(mast)) {
                            throw new CoolException("任务档修改失败!!");
                        }
 
                        // 更新原出库单
//                        int indexOf = mast.getWrkCode().indexOf("-1");
                        if (!Cools.isEmpty(mast.getWrkCode())) {
                            String wrkNo = mast.getWrkCode();
                            WrkMast orgWrk = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", wrkNo));
                            if (Objects.isNull(orgWrk)) {
                                throw new CoolException("数据错误,主任务档不存在或已删除!!");
                            }
                            orgWrk.setOveMk("Y");
                            if (!wrkMastService.updateById(orgWrk)) {
                                throw new CoolException("任务档修改失败!!");
                            }
                            WrkDetl detl = wrkDetlService.selectOne(new EntityWrapper<WrkDetl>().eq("wrk_no", wrkNo));
                            if (Objects.isNull(detl)) {
                                throw new CoolException("子任务档明细不存在!!");
                            }
                            //库存减去称重长度
                            Double v = Math.round((wrkDetl.getStockQty() - realQty) * 10000) / 10000.0;
                            //原出为数量与真实数量互换,保持一致性
                            detl.setAnfme(v);
                            detl.setWeight(weight);
                            if (!wrkDetlService.update(detl, new EntityWrapper<WrkDetl>().eq("wrk_no", wrkNo).eq("matnr", detl.getMatnr()).eq("barcode", detl.getBarcode()))) {
                                throw new CoolException("原任务档明细修改失败!!");
                            }
                        } else {
                            throw new CoolException("任务号截取失败,请检查主任务档任档wrkCode字段");
                        }
                    }
                }
 
                if (!Objects.isNull(mast.getWrkCode())) {
                    if (mast.getOveMk().equals("N") && !Cools.isEmpty(mast.getIsSuplus())) {
                        throw new CoolException("任务未上报重量,无法完结任务!!");
                    }
                }
                if (mast.getIoType() == 1 || mast.getIoType() == 2 ||mast.getIoType() == 10) {
                    mast.setWrkSts(4L);
                } else if ((mast.getIoType() == 101||mast.getIoType()==110) && mast.getWrkSts()<14) {
                    mast.setWrkSts(14L);
                    if(Cools.isEmpty(mast.getStaNo())){
                        mast.setOveMk("Y");
                    }
                }
                if (!wrkMastService.updateById(mast)) {
                    throw new CoolException("任务状态修改失败!!");
                }
            } else if (params.getMsgType().equals("task_cancel")) {
                //todo 取消任务
            }else if (params.getMsgType().equals("task_arrive")) {
                //到达目的地
                //如果出库任务是跨区则需要生成新的入库任务入库
                if(!Cools.isEmpty(mast.getLocNo())){
                    mast.setOnlineYn("N");//等待生成跨区入库任务
                }
                mast.setWrkSts(14L);
                if(Cools.isEmpty(mast.getStaNo())){
                    mast.setOveMk("Y");
                }
                mast.setModiTime(new Date());
                if (!wrkMastService.updateById(mast)) {
                    throw new CoolException("任务状态修改失败!!");
                }
            }
        } else if (params.getNotifyType().equals("weight")) {
 
        }
        return R.ok();
    }
    //输入库位自动冻结算出相邻库位序号
    @Override
    public Integer[] freezeLocAround(Integer locId) {
        Integer[] locAround = new Integer[3];
        if (Objects.isNull(locId)) {
            throw new CoolException("库位ID不能为空!!");
        }else if (locId==1){
            locAround[0] = 2;
            locAround[1] = 3;
            locAround[2] = 5;
        }else if (locId==2){
            locAround[0] = 1;
            locAround[1] = 6;
            locAround[2] = 0;
        }else if (locId==3){
            locAround[0] = 1;
            locAround[1] = 4;
            locAround[2] = 0;
        }else if (locId==4){
            locAround[0] = 5;
            locAround[1] = 3;
            locAround[2] = 0;
        }else if (locId==5){
            locAround[0] = 4;
            locAround[1] = 1;
            locAround[2] = 6;
        }else if (locId==6){
            locAround[0] = 2;
            locAround[1] = 5;
            locAround[2] = 0;
        }
        return locAround;
    }
}