skyouc
2025-01-04 3c52f39678034ce21c1158a01b4885e3afde4443
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
package com.zy.asrs.wms.asrs.timer;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.asrs.entity.*;
import com.zy.asrs.wms.asrs.entity.enums.LocStsType;
import com.zy.asrs.wms.asrs.entity.enums.TaskStsType;
import com.zy.asrs.wms.asrs.service.*;
import io.jsonwebtoken.lang.Collections;
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.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Slf4j
@Component
public class TaskTimer {
 
    @Autowired
    private TaskService taskService;
 
    @Autowired
    private TaskDetlService taskDetlService;
 
    @Autowired
    private TaskDetlFieldService taskDetlFieldService;
 
    @Autowired
    private LocService locService;
 
    @Autowired
    private LocDetlService locDetlService;
 
    @Autowired
    private LocDetlFieldService locDetlFieldService;
 
    @Autowired
    private WaitPakinService waitPakinService;
 
    @Autowired
    private WaitPakinLogService waitPakinLogService;
 
    @Autowired
    private TaskDetlLogService taskDetlLogService;
 
    @Autowired
    private TaskDetlFieldLogService taskDetlFieldLogService;
 
    @Autowired
    private OrderService orderService;
 
    @Autowired
    private OrderDetlService orderDetlService;
 
    @Autowired
    private CacheSiteService cacheSiteService;
 
 
    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional(rollbackFor = Exception.class)
    public void inExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
            //获取入库完成任务
            List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskSts, TaskStsType.COMPLETE_IN.id));
            if (list.isEmpty()) {
                return;
            }
 
            for (Task task : list) {
                //同步数据
                switch (task.getTaskType().intValue()) {
                    case 1://入库
                        executeTask1(task);
                        break;
                    case 11://库位移转
                        executeTask11(task);
                        break;
                    case 53://拣料再入库
                        executeTask53(task);
                        break;
                    default:
                        throw new CoolException("未知任务类型");
                }
 
                task.setTaskSts(TaskStsType.UPDATED_IN.id);//100.库存更新完成
                task.setUpdateTime(new Date());
                if (!taskService.updateById(task)) {
                    throw new CoolException("库存更新失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
    }
 
 
    /**
     * 1. 定时拉取需要执行回库任务列表数据,
     * 2. 删除原始库位明细,原始库位状态置成O.空库
     * 3.
     */
    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional(rollbackFor = Exception.class)
    public void rollbackStock() {
        //获取需要执行回库的任务,更新库存信息
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskType, 53).eq(Task::getTaskSts, TaskStsType.WCS_CONVEYOR_START.id));
        if (tasks.isEmpty()) {
            return;
        }
        tasks.forEach(task -> {
            //删除原始库位明细,状态置为O.空库
            executeTask103(task);
        });
        //todo 需添加一个任务状态或临时状态,否则会一直轮循,程序报错(库位状态不处于R.出库预约)
    }
 
 
    @Scheduled(cron = "0/10 * * * * ? ")
    @Transactional(rollbackFor = Exception.class)
    public void outExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
            //获取出库完成任务
            List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskSts, TaskStsType.COMPLETE_OUT.id));
            if (list.isEmpty()) {
                return;
            }
            for (Task task : list) {
                //同步数据
                switch (task.getTaskType().intValue()) {
                    case 101://出库xx
                        executeTask101(task);
                        break;
                    case 103://拣料
                        executeTask103(task);
                        break;
                    default:
                        throw new CoolException("未知任务类型");
                }
                task.setTaskSts(TaskStsType.UPDATED_OUT.id);//200.库存更新完成
                if (!taskService.updateById(task)) {
                    throw new CoolException("库存更新失败");
                } else {
                    //CacheSite 释放已被占用的通道 //fixme 播种功能完成后,需要将这里注释掉
//                    List<TaskDetl> detls = taskDetlService.list(new LambdaQueryWrapper<TaskDetl>().eq(TaskDetl::getTaskId, task.getId()));
//                    if (!Collections.isEmpty(detls)) {
//                        List<Long> waveIds = detls.stream().map(TaskDetl::getWaveId).collect(Collectors.toList());
//                        List<Order> orders = orderService.list(new LambdaQueryWrapper<Order>().in(Order::getWaveId, waveIds));
//                        List<Long> orderIds = orders.stream().map(Order::getId).collect(Collectors.toList());
//                        cacheSiteService.update(new LambdaUpdateWrapper<CacheSite>()
//                                .in(CacheSite::getOrderId, orderIds)
//                                .set(CacheSite::getSiteStatus, 0)
//                                .set(CacheSite::getOrderId, null)
//                                .set(CacheSite::getOrderNo, null));
//                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
    }
 
    //入库
    private void executeTask1(Task task) {
        Long hostId = task.getHostId();
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getTargetLoc()).eq(Loc::getHostId, hostId));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
 
        if (loc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于S.入库预约");
        }
 
        loc.setLocStsId(LocStsType.F.val());
        loc.setUpdateTime(new Date());
        loc.setBarcode(task.getBarcode());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
 
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
 
        //添加库存明细
        for (TaskDetl taskDetl : taskDetls) {
            LocDetl locDetl = new LocDetl();
            locDetl.setLocId(loc.getId());
            locDetl.setLocNo(loc.getLocNo());
            locDetl.setMatId(taskDetl.getMatId());
            locDetl.setMatnr(taskDetl.getMat$().getMatnr());
            locDetl.setOrderNo(taskDetl.getOrderNo());
            locDetl.setBatch(taskDetl.getBatch());
            locDetl.setAnfme(taskDetl.getAnfme());
            locDetl.setHostId(hostId);
            if (!locDetlService.save(locDetl)) {
                throw new CoolException("插入库存明细失败");
            }
            //fixme 暂时关闭HOSTID,后续打开机构ID
            //添加库存明细扩展字段
//            List<TaskDetlField> detlFields = taskDetlFieldService.list(new LambdaQueryWrapper<TaskDetlField>().eq(TaskDetlField::getDetlId, taskDetl.getId()).eq(TaskDetlField::getHostId, hostId));
            List<TaskDetlField> detlFields = taskDetlFieldService.list(new LambdaQueryWrapper<TaskDetlField>().eq(TaskDetlField::getDetlId, taskDetl.getId()));
            for (TaskDetlField detlField : detlFields) {
                LocDetlField locDetlField = new LocDetlField();
                locDetlField.setDetlId(locDetl.getId());
                locDetlField.setFieldId(detlField.getFieldId());
                locDetlField.setName(detlField.getName());
                locDetlField.setValue(detlField.getValue());
                locDetlField.setHostId(hostId);
                if (!locDetlFieldService.save(locDetlField)) {
                    throw new CoolException("插入明细扩展字段失败");
                }
            }
        }
        //fixme 暂时关闭HOSTID,后续打开机构ID
        //组托通知档转历史档
//        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, task.getBarcode()).eq(WaitPakin::getHostId, hostId));
        List<WaitPakin> waitPakins = waitPakinService.list(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, task.getBarcode()));
        if (waitPakins.isEmpty()) {
            throw new CoolException("组托通知档不存在");
        }
        for (WaitPakin waitPakin : waitPakins) {
            WaitPakinLog waitPakinLog = new WaitPakinLog();
            waitPakinLog.sync(waitPakin);
            waitPakinLog.setId(null);
            if (!waitPakinLogService.save(waitPakinLog)) {
                throw new CoolException("组托通知档转历史档失败");
            }
 
            //删除组托通知档
            waitPakinService.removeById(waitPakin.getId());
        }
    }
 
    //库位移转
    private void executeTask11(Task task) {
        Long hostId = task.getHostId();
 
        Loc originLoc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()).eq(Loc::getHostId, hostId));
        if (originLoc == null) {
            throw new CoolException("源库位不存在");
        }
 
        if (originLoc.getLocStsId() != LocStsType.R.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
 
        Loc targetLoc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getTargetLoc()).eq(Loc::getHostId, hostId));
        if (targetLoc == null) {
            throw new CoolException("目标库位不存在");
        }
 
        if (targetLoc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于S.入库预约");
        }
 
        originLoc.setLocStsId(LocStsType.O.val());
        originLoc.setUpdateTime(new Date());
        originLoc.setBarcode("");
        if (!locService.updateById(originLoc)) {
            throw new CoolException("库位状态更新失败");
        }
 
        targetLoc.setLocStsId(LocStsType.F.val());
        targetLoc.setUpdateTime(new Date());
        targetLoc.setBarcode(task.getBarcode());
        if (!locService.updateById(targetLoc)) {
            throw new CoolException("库位状态更新失败");
        }
 
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
 
        //添加库存明细
        for (TaskDetl taskDetl : taskDetls) {
            LocDetl locDetl = new LocDetl();
            locDetl.setLocId(targetLoc.getId());
            locDetl.setLocNo(targetLoc.getLocNo());
            locDetl.setMatId(taskDetl.getMatId());
            locDetl.setMatnr(taskDetl.getMat$().getMatnr());
            locDetl.setOrderNo(taskDetl.getOrderNo());
            locDetl.setBatch(taskDetl.getBatch());
            locDetl.setAnfme(taskDetl.getAnfme());
            locDetl.setHostId(hostId);
            if (!locDetlService.save(locDetl)) {
                throw new CoolException("插入库存明细失败");
            }
 
            //添加库存明细扩展字段
            List<TaskDetlField> detlFields = taskDetlFieldService.list(new LambdaQueryWrapper<TaskDetlField>().eq(TaskDetlField::getDetlId, taskDetl.getId()).eq(TaskDetlField::getHostId, hostId));
            for (TaskDetlField detlField : detlFields) {
                LocDetlField locDetlField = new LocDetlField();
                locDetlField.setDetlId(locDetl.getId());
                locDetlField.setFieldId(detlField.getFieldId());
                locDetlField.setName(detlField.getName());
                locDetlField.setValue(detlField.getValue());
                locDetlField.setHostId(hostId);
                if (!locDetlFieldService.save(locDetlField)) {
                    throw new CoolException("插入明细扩展字段失败");
                }
            }
        }
 
        List<LocDetl> locDetls = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, originLoc.getId()));
        for (LocDetl locDetl : locDetls) {
            boolean remove = locDetlFieldService.remove(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()));
            boolean result = locDetlService.removeById(locDetl.getId());
            if (!result) {
                throw new CoolException("清除明细失败");
            }
        }
 
    }
 
    /**
     * 拣料再入库,根据任务目标库位,生成新库存信息
     * @param task
     */
    //拣料再入库
    private void executeTask53(Task task) {
        Long hostId = task.getHostId();
        //fixme 将任务当前
//        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getTargetLoc()).eq(Loc::getHostId, hostId));
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getTargetLoc()));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
 
        if (loc.getLocStsId() != LocStsType.S.val()) {
            throw new CoolException("库位状态不处于S.入库预约");
        }
 
        loc.setLocStsId(LocStsType.F.val());
        loc.setUpdateTime(new Date());
        loc.setBarcode(task.getBarcode());
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
 
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
 
        //添加库存明细
        for (TaskDetl taskDetl : taskDetls) {
            double anfme = taskDetl.getStock() - taskDetl.getAnfme();
            if (anfme <= 0) {
                continue;
            }
 
            LocDetl locDetl = new LocDetl();
            locDetl.setLocId(loc.getId());
            locDetl.setLocNo(loc.getLocNo());
            locDetl.setMatId(taskDetl.getMatId());
            locDetl.setMatnr(taskDetl.getMat$().getMatnr());
            locDetl.setOrderNo(taskDetl.getOrderNo());
            locDetl.setBatch(taskDetl.getBatch());
            locDetl.setAnfme(anfme);
            locDetl.setHostId(hostId);
            if (!locDetlService.save(locDetl)) {
                throw new CoolException("插入库存明细失败");
            }
 
            //添加库存明细扩展字段
            //fixme 注释
//            List<TaskDetlField> detlFields = taskDetlFieldService.list(new LambdaQueryWrapper<TaskDetlField>().eq(TaskDetlField::getDetlId, taskDetl.getId()).eq(TaskDetlField::getHostId, hostId));
            List<TaskDetlField> detlFields = taskDetlFieldService.list(new LambdaQueryWrapper<TaskDetlField>().eq(TaskDetlField::getDetlId, taskDetl.getId()));
            for (TaskDetlField detlField : detlFields) {
                LocDetlField locDetlField = new LocDetlField();
                locDetlField.setDetlId(locDetl.getId());
                locDetlField.setFieldId(detlField.getFieldId());
                locDetlField.setName(detlField.getName());
                locDetlField.setValue(detlField.getValue());
                locDetlField.setHostId(hostId);
                if (!locDetlFieldService.save(locDetlField)) {
                    throw new CoolException("插入明细扩展字段失败");
                }
            }
        }
    }
 
    //出库
    private void executeTask101(Task task) {
        Long hostId = task.getHostId();
//        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()).eq(Loc::getHostId, hostId));
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.R.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
 
        loc.setLocStsId(LocStsType.O.val());
        loc.setBarcode("");
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
        List<LocDetl> detlList = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, loc.getId()).eq(LocDetl::getHostId, hostId));
        //删除库存明细
        for (LocDetl locDetl : detlList) {
            if (!locDetlService.removeById(locDetl)) {
                throw new CoolException("删除库存明细失败");
            }
            List<LocDetlField> detlFields = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()).eq(LocDetlField::getHostId, hostId));
            for (LocDetlField detlField : detlFields) {
                if (!locDetlFieldService.removeById(detlField)) {
                    throw new CoolException("删除明细扩展字段失败");
                }
            }
        }
    }
 
    //拣料出库
    private void executeTask103(Task task) {
        Long hostId = task.getHostId();
        //FIXME 暂时注释HOSTID筛选条件
//        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()).eq(Loc::getHostId, hostId));
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, task.getOriginLoc()));
        if (loc == null) {
            throw new CoolException("库位不存在");
        }
        if (loc.getLocStsId() != LocStsType.R.val()) {
            throw new CoolException("库位状态不处于R.出库预约");
        }
        List<TaskDetl> taskDetls = taskDetlService.getTaskDetlByTaskId(task.getId());
        if (taskDetls.isEmpty()) {
            throw new CoolException("任务明细不存在");
        }
 
        loc.setLocStsId(LocStsType.O.val());
        loc.setBarcode("");
        if (!locService.updateById(loc)) {
            throw new CoolException("库位状态更新失败");
        }
 
        List<LocDetl> detlList = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, loc.getId()).eq(LocDetl::getHostId, hostId));
        //删除库存明细
        for (LocDetl locDetl : detlList) {
            if (!locDetlService.removeById(locDetl)) {
                throw new CoolException("删除库存明细失败");
            }
            List<LocDetlField> detlFields = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()).eq(LocDetlField::getHostId, hostId));
            for (LocDetlField detlField : detlFields) {
                if (!locDetlFieldService.removeById(detlField)) {
                    throw new CoolException("删除明细扩展字段失败");
                }
            }
        }
    }
 
}