#
luxiaotao1123
2024-10-28 70e8f76e19575de4b21e5c0ff225af7a1849fec6
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
package com.zy.acs.manager.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.acs.framework.common.BaseRes;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.SnowflakeIdWorker;
import com.zy.acs.framework.exception.CoolException;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.manager.core.domain.Lane;
import com.zy.acs.manager.core.service.LaneService;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.*;
import com.zy.acs.manager.manager.mapper.TaskMapper;
import com.zy.acs.manager.manager.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
@Slf4j
@Service("taskService")
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {
 
    @Autowired
    private CodeService codeService;
    @Autowired
    private LocService locService;
    @Autowired
    private StaService staService;
    @Autowired
    private FuncStaService funcStaService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private LaneService laneService;
 
    @Override
    public synchronized String generateSeqNum() {
        return String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19);
    }
 
    @Override
    public Task pick(TaskStsType taskStsType) {
        List<Task> list = this.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskSts, taskStsType.val()).orderByDesc(Task::getPriority));
        return !Cools.isEmpty(list)?list.get(0):null;
    }
 
    @Override
    public Task selectByUuid(String uuid) {
        return this.getOne(new LambdaQueryWrapper<Task>().eq(Task::getUuid, uuid));
    }
 
    @Override
    public List<Task> selectBySts(TaskStsType taskStsType) {
        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Task::getTaskSts, taskStsType.val());
        return this.list(wrapper);
    }
 
    @Override
    public List<Task> selectInSts(Long agvId, TaskStsType... taskStsTypes) {
        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>();
        List<Long> params = new ArrayList<>();
        for (TaskStsType type : taskStsTypes) {
            params.add(type.val());
        }
        wrapper.in(Task::getTaskSts, params);
        if (null != agvId) {
            wrapper.eq(Task::getAgvId, agvId);
        }
        return this.list(wrapper);
    }
 
    @Override
    public List<Map<String, Object>> selectStatByLastSevenDays() {
        return this.baseMapper.selectStatByLastSevenDays();
    }
 
    @Override
    @Transactional
    public Boolean complete(Long taskId, Long userId) {
        Task task = this.getById(taskId);
        if (null == task) {
            return Boolean.FALSE;
        }
        this.maintainLocSts(task, Boolean.TRUE);
 
        task.setTaskSts(TaskStsType.COMPLETE.val());
        task.setUpdateTime(new Date());
        task.setUpdateBy(userId);
        if (!this.updateById(task)) {
            throw new CoolException(BaseRes.ERROR);
        }
        return Boolean.TRUE;
    }
 
    @Override
    @Transactional
    public Boolean cancel(Long taskId, Long userId) {
        Task task = this.getById(taskId);
        if (null == task) {
            return Boolean.FALSE;
        }
        this.maintainLocSts(task, Boolean.FALSE);
 
        task.setTaskSts(TaskStsType.CANCEL.val());
        task.setUpdateTime(new Date());
        task.setUpdateBy(userId);
        if (!this.updateById(task)) {
            throw new CoolException(BaseRes.ERROR);
        }
        return Boolean.TRUE;
    }
 
    @Override
    public Lane checkoutOriginLane(Task task) {
        Long codeId = null;
        TaskTypeType typeType = TaskTypeType.get(task.getTaskTypeEl());
        switch (Objects.requireNonNull(typeType)) {
            case LOC_TO_LOC:
            case LOC_TO_STA:
                codeId = locService.getById(task.getOriLoc()).getCode();
                break;
            case STA_TO_LOC:
            case STA_TO_STA:
                codeId = staService.getById(task.getOriSta()).getCode();
                break;
            default:
                break;
        }
 
        if (null == codeId) {
            return null;
        }
        return laneService.search(codeService.getById(codeId).getData());
    }
 
    @Override
    public Lane checkoutDestinationLane(Task task) {
        Long codeId = null;
        TaskTypeType typeType = TaskTypeType.get(task.getTaskTypeEl());
        switch (Objects.requireNonNull(typeType)) {
            case LOC_TO_LOC:
            case STA_TO_LOC:
                codeId = locService.getById(task.getDestLoc()).getCode();
                break;
            case LOC_TO_STA:
            case STA_TO_STA:
                codeId = staService.getById(task.getDestSta()).getCode();
                break;
            default:
                break;
        }
 
        if (null == codeId) {
            return null;
        }
        return laneService.search(codeService.getById(codeId).getData());
    }
 
    @Transactional
    public void maintainLocSts(Task task, Boolean complete) {
        Loc oriLoc = null; Loc destLoc = null;
        Sta oriSta = null; Sta destSta = null;
        Date now = new Date();
        TaskTypeType typeType = TaskTypeType.get(task.getTaskTypeEl());
        switch (Objects.requireNonNull(typeType)) {
            case LOC_TO_LOC:
                oriLoc = locService.getById(task.getOriLoc());
                destLoc = locService.getById(task.getDestLoc());
 
                oriLoc.setLocSts(complete?LocStsType.IDLE.val():LocStsType.STOCK.val());
                oriLoc.setUpdateTime(now);
                if (!locService.updateById(oriLoc)) {
                    throw new BusinessException("Loc [" + task.getOriLoc$() + "] 库位修改状态失败 !!!");
                }
 
                destLoc.setLocSts(complete?LocStsType.STOCK.val():LocStsType.IDLE.val());
                destLoc.setUpdateTime(now);
                if (!locService.updateById(destLoc)) {
                    throw new BusinessException("Loc [" + task.getDestLoc$() + "] 库位修改状态失败 !!!");
                }
                break;
            case LOC_TO_STA:
                oriLoc = locService.getById(task.getOriLoc());
                oriLoc.setLocSts(complete?LocStsType.IDLE.val():LocStsType.STOCK.val());
                oriLoc.setUpdateTime(now);
                if (!locService.updateById(oriLoc)) {
                    throw new BusinessException("Loc [" + task.getOriLoc$() + "] 库位修改状态失败 !!!");
                }
                break;
            case STA_TO_LOC:
                destLoc = locService.getById(task.getDestLoc());
                destLoc.setLocSts(complete?LocStsType.STOCK.val():LocStsType.IDLE.val());
                destLoc.setUpdateTime(now);
                if (!locService.updateById(destLoc)) {
                    throw new BusinessException("Loc [" + task.getDestLoc$() + "] 库位修改状态失败 !!!");
                }
                break;
            case STA_TO_STA:
                break;
            case TO_CHARGE:
            case TO_STANDBY:
                FuncSta funcSta = funcStaService.getByCodeAndType(task.getDestCode(), FuncStaType.query(typeType).toString());
                if (!complete) {
                    funcSta.setState(FuncStaStateType.IDLE.toString());
                    funcSta.setUpdateTime(now);
                    if (!funcStaService.updateById(funcSta)) {
                        throw new BusinessException("FuncSta [" + funcSta.getName() + "] 更新状态失败 !!!");
                    }
                }
                break;
            default:
                break;
        }
    }
 
}