#
vincentlu
13 小时以前 55f9c93b292feca5148fdbbf391b159578e70bd9
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
package com.zy.acs.manager.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.exception.CoolException;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.manager.core.service.MapService;
import com.zy.acs.manager.manager.controller.result.MissionVo;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.*;
import com.zy.acs.manager.manager.service.*;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Created by vincent on 9/27/2024
 */
@Service("missionService")
public class MissionServiceImpl implements MissionService {
 
    @Autowired
    private SegmentService segmentService;
    @Autowired
    private ActionService actionService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private AgvDetailService agvDetailService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private BusService busService;
    @Autowired
    private SqlSession sqlSession;
 
    @Override
    public List<MissionVo> getList(Long agvId, String groupNo) {
        List<MissionVo> result = new ArrayList<>();
        List<String> runningGroupNos = segmentService.getGroupNo(SegmentStateType.RUNNING, agvId, groupNo);
        for (String runningGroupNo : runningGroupNos) {
            MissionVo vo = generateVo(runningGroupNo);
            if (null != vo) {
                result.add(vo);
            }
        }
 
        return result;
    }
 
    @Override
    public MissionVo generateVo(String groupNo) {
        if (Cools.isEmpty(groupNo)) {
            return null;
        }
        // segment -------------------------------
        List<Segment> list = segmentService.list(new LambdaQueryWrapper<Segment>().eq(Segment::getGroupId, groupNo).orderByAsc(Segment::getSerial));
        if (Cools.isEmpty(list)) {
            return null;
        }
        Segment segment = list.get(0);
 
        // agv -------------------------------
        AgvDetail agvDetail = agvDetailService.selectByAgvId(segment.getAgvId());
        Long recentCode = agvDetail.getRecentCode();
        String currCode = null;
        if (null != recentCode) {
            Code cacheById = codeService.getCacheById(recentCode);
            if (cacheById != null) {
                currCode = cacheById.getData();
            } else {
                throw new CoolException(segment.getAgvId() + "小车还在删除的点位中");
            }
 
        }
 
        // action -------------------------------
        List<Action> actionList = actionService.list(new LambdaQueryWrapper<Action>()
                .eq(Action::getGroupId, groupNo).ne(Action::getActionSts, ActionStsType.EXPIRED.val()).orderByDesc(Action::getPriority));
        if (Cools.isEmpty(actionList)) {
            return null;
        }
 
        MissionVo vo = new MissionVo();
        vo.setGroupNo(groupNo);
        vo.setId(segment.getId());
        vo.setPosType(segment.getPosType());
        vo.setAgv(segment.getAgvId$());
        vo.setTaskNos(list.stream().map(Segment::getTaskId$).collect(Collectors.toList()));
        vo.setBackpack(segment.getBackpack());
        vo.setDestCode(segment.getEndNode$());
        vo.setCurrCode(currCode);
        vo.setProgress(calcProgress(currCode, actionList));
        return vo;
    }
 
    @Override
    public MissionVo generateVoMoreInfo(String groupNo) {
        MissionVo vo = generateVo(groupNo);
        if (null == vo) {
            return vo;
        }
        List<Segment> list = segmentService.list(new LambdaQueryWrapper<Segment>().eq(Segment::getGroupId, groupNo).orderByAsc(Segment::getSerial));
        if (Cools.isEmpty(list)) {
            return null;
        }
        List<Action> actionList = actionService.list(new LambdaQueryWrapper<Action>()
                .eq(Action::getGroupId, groupNo).ne(Action::getActionSts, ActionStsType.EXPIRED.val()).orderByDesc(Action::getPriority));
        if (Cools.isEmpty(actionList)) {
            return null;
        }
 
        Segment segment = list.get(0);
        Action action = actionList.get(0);
        Task task = taskService.getById(segment.getTaskId());
//        vo.setSendTime(action.getStartTime());
        vo.setBusNo(task.getBusId$());
        vo.setActionCount(actionList.size());
        vo.setTaskIds(list.stream().map(Segment::getTaskId).collect(Collectors.toList()));
        vo.setCodeList(actionList.stream().map(Action::getCode).distinct().collect(Collectors.toList()));
        vo.setActionIds(actionList.stream().map(Action::getId).collect(Collectors.toList()));
        return vo;
    }
 
    @Override
    @Transactional
    public Boolean resend(List<Action> actionList) {
        if (Cools.isEmpty(actionList)) {
            return Boolean.FALSE;
        }
        Date now = new Date();
 
        List<Long> actionIds = actionList.stream().map(Action::getId).collect(Collectors.toList());
        Action firstAction = actionService.getById(actionIds.get(0));
        if (firstAction == null) {
            return Boolean.FALSE;
        }
 
        String actionGroupId = firstAction.getGroupId();
        actionService.updateStsByGroupId(actionGroupId, ActionStsType.EXPIRED.val());
 
        // if firstAction is not turn, insert a new turn action
        Action prependTurn = null;
        if (!firstAction.getActionType().equals(ActionTypeType.TurnCorner.val())) {
            String codeData = firstAction.getCode();
            Code code = codeService.getCacheByData(codeData);
            AgvDetail agvDetail = agvDetailService.selectMajorByAgvId(firstAction.getAgvId());
            Double lastDirection = MapService.mapToNearest(agvDetail.getAgvAngle());
 
            List<Action> fullActionList = actionService.list(new LambdaQueryWrapper<Action>()
                            .eq(Action::getGroupId, actionGroupId)
                            .select(Action::getId, Action::getActionType, Action::getPriority)
                            .orderByDesc(Action::getPriority)
            );
 
            int idx = -1;
            for (int k = 0; k < fullActionList.size(); k++) {
                if (fullActionList.get(k).getId().equals(firstAction.getId())) {
                    idx = k;
                    break;
                }
            }
 
            if (idx > 0) {
                Long turnActionId = null;
                // find out turn action
                for (int k = idx - 1; k >= 0; k--) {
                    Action a = fullActionList.get(k);
                    if (a.getActionType().equals(ActionTypeType.TurnCorner.val())) {
                        turnActionId = a.getId();
                        break;
                    }
                }
 
 
                if (null != turnActionId) {
                    Action turn = actionService.getById(turnActionId);
                    Double direction = Double.parseDouble(turn.getParams());
 
                    if (!lastDirection.equals(direction)) {
                        if (!code.getCornerBool()) {
                            throw new CoolException(agvDetail.getAgvId$() + "号小车方向错误,请推至转弯点手动调整");
                        }
                    }
 
                    // new turn action
                    prependTurn = new Action(
                            null,    // 编号
                            firstAction.getBusId(),    // 总线
                            firstAction.getTaskId(),    // 任务
                            null,    // 动作号
                            null,    // 优先级
                            ActionTypeType.TurnCorner.desc,    // 名称
                            (double) MapService.calcSpinDirection(code, lastDirection, direction).val,    // 属性值
                            code.getData(),    // 地面码
                            String.valueOf(direction),   // 动作参数
                            ActionTypeType.TurnCorner.val(),    // 动作类型
                            ActionStsType.PREPARE.val(),    // 动作进度
                            firstAction.getAgvId(),    // AGV
                            now    // 工作时间
                    );
                }
 
            }
        }
 
        List<Action> newActionList = new ArrayList<>();
        if (prependTurn != null) {
            prependTurn.setGroupId(actionGroupId);
            prependTurn.setCreateTime(now);
            prependTurn.setUpdateTime(now);
            newActionList.add(prependTurn);
        }
        for (Long actionId : actionIds) {
            sqlSession.clearCache();
            Action action = actionService.getById(actionId);
            action.setActionSts(ActionStsType.PREPARE.val());
            action.setIoTime(now);
            action.setUpdateTime(now);
            newActionList.add(action);
        }
        int i = newActionList.size();
        for (Action action : newActionList) {
            action.setId(null);
            action.setPriority(i);
            if (!actionService.save(action)) {
                throw new BusinessException(action.getName() + " Action Save Fail!");
            }
            i -= 1;
        }
 
        Set<Long> busIds = new HashSet<>();
        List<Long> taskIds = actionService.selectTaskIdsByGroupId(actionGroupId);
        for (Long taskId : taskIds) {
            Task task = taskService.getById(taskId);
            if (null != task) {
                task.setTaskSts(TaskStsType.ASSIGN.val());
                task.setUpdateTime(now);
                task.setIoTime(now);
                if (!taskService.updateById(task)) {
                    throw new BusinessException(task.getUuid() + " Task Update Fail!");
                }
                busIds.add(task.getBusId());
            }
        }
        for (Long busId : busIds) {
            Bus bus = busService.getById(busId);
            if (null != bus) {
                bus.setBusSts(BusStsType.PROGRESS.val());
                bus.setUpdateTime(now);
                if (!busService.updateById(bus)) {
                    throw new BusinessException(bus.getUuid() + " Bus Update Fail!");
                }
            }
        }
        return Boolean.TRUE;
    }
 
    public Double calcProgress(String currCode, List<Action> actionList) {
        double progress = 0D;
        if (Cools.isEmpty(actionList, currCode)) {
            return progress;
        }
 
        List<String> codeList = actionList.stream().map(Action::getCode).distinct().collect(Collectors.toList());
 
        int totalCodes = codeList.size();
        int currentIndex = codeList.indexOf(currCode);
 
        if (currentIndex >= 0) {
            progress = (currentIndex + 1) * 100.0 / totalCodes;
        }
 
        return progress;
    }
 
}