#
Junjie
2024-08-01 d64a3ba24419ed4706b8f1b5108200a26356946a
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
package com.zy.asrs.wms.asrs.timer;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.service.*;
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.Date;
import java.util.List;
 
@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;
 
    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    public void inExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
            //获取入库完成任务
            List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskSts, 99));
            if (list.isEmpty()) {
                return;
            }
 
            for (Task task : list) {
                //同步数据
                Long hostId = task.getHostId();
                switch (task.getTaskType().intValue()) {
                    case 1://入库
                        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("插入库存明细失败");
                            }
 
                            //添加库存明细扩展字段
                            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.setHostId(hostId);
                                if (!locDetlFieldService.save(locDetlField)) {
                                    throw new CoolException("插入明细扩展字段失败");
                                }
                            }
                        }
 
                        //组托通知档转历史档
                        WaitPakin waitPakin = waitPakinService.getOne(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, task.getBarcode()).eq(WaitPakin::getHostId, hostId));
                        if (waitPakin == null) {
                            throw new CoolException("组托通知档不存在");
                        }
                        WaitPakinLog waitPakinLog = new WaitPakinLog();
                        waitPakinLog.sync(waitPakin);
                        if (!waitPakinLogService.save(waitPakinLog)) {
                            throw new CoolException("组托通知档转历史档失败");
                        }
 
                        //删除组托通知档
                        waitPakinService.remove(new LambdaQueryWrapper<WaitPakin>().eq(WaitPakin::getBarcode, task.getBarcode()).eq(WaitPakin::getHostId, hostId));
 
                        break;
                }
 
                task.setTaskSts(100L);//100.库存更新完成
                task.setUpdateTime(new Date());
                if (!taskService.updateById(task)) {
                    throw new CoolException("库存更新失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
    }
 
    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    public void outExecute() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
 
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
    }
 
}