1
zhang
2025-07-03 13ea8b334572c2423abb8d156fd8428f8d074172
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
package com.zy.acs.manager.core.service;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.SnowflakeIdWorker;
import com.zy.acs.manager.common.domain.param.HandlerPublishParam;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.manager.core.domain.TaskPosDto;
import com.zy.acs.manager.core.service.astart.MapDataDispatcher;
import com.zy.acs.manager.core.third.zkd.HttpUtils;
import com.zy.acs.manager.core.third.zkd.dto.AllocateTask;
import com.zy.acs.manager.core.third.zkd.dto.AllocateTaskResponse;
import com.zy.acs.manager.core.third.zkd.dto.Navigation;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.BusStsType;
import com.zy.acs.manager.manager.enums.SegmentStateType;
import com.zy.acs.manager.manager.enums.TaskStsType;
import com.zy.acs.manager.manager.enums.TaskTypeType;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.manager.utils.ActionSorter;
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * Created by vincent on 2023/6/14
 */
@Slf4j
@Service("zkdMainService")
public class MainZkdService extends BaseMainService {
 
    @Autowired
    private BusService busService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ActionService actionService;
    @Autowired
    private StaService staService;
    @Autowired
    private LocService locService;
    @Autowired
    private AgvService agvService;
    @Autowired
    private AgvDetailService agvDetailService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private ValidService validService;
    @Autowired
    private AllocateService allocateService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private MapService mapService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private CodeGapService codeGapService;
    @Autowired
    private AgvCmdService agvCmdService;
    @Autowired
    private FuncStaService funcStaService;
    @Autowired
    private MapDataDispatcher mapDataDispatcher;
    @Autowired
    private TravelService travelService;
    @Autowired
    private SegmentService segmentService;
    //    @Autowired
//    private TrafficService trafficService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
    private LaneService laneService;
    @Autowired
    private ActionSorter actionSorter;
 
 
    /**
     * 任务分配给车辆 ( 车辆此时是空闲且静止的 )
     */
    @Transactional
    public synchronized void allocateTask(Bus bus) {
        try {
            Date now = new Date();
            List<Task> taskList = taskService.list(new LambdaQueryWrapper<Task>()
                    .eq(Task::getBusId, bus.getId())
                    .eq(Task::getTaskSts, TaskStsType.INIT.val())
                    .orderByDesc(Task::getPriority)
            );
 
            if (Cools.isEmpty(taskList)) {
                bus.setBusSts(BusStsType.PROGRESS.val());
                bus.setUpdateTime(now);
                if (!busService.updateById(bus)) {
                    log.error("Bus [{}] failed to Update !!!", bus.getUuid());
                }
                return;
            }
            List<Long> taskIds = taskList.stream().map(Task::getId).distinct().collect(Collectors.toList());
            List<AllocateTask> allocateTasks = new ArrayList<>();
            AllocateTask allocateTask = null;
            String startCode = null;
            String endCode = null;
            Loc oriLoc = null;
            Loc destLoc = null;
            Sta oriSta = null;
            Sta destSta = null;
            for (Task task : taskList) {
                allocateTask = new AllocateTask();
                switch (Objects.requireNonNull(TaskTypeType.get(task.getTaskTypeEl()))) {
                    case LOC_TO_LOC:
                        oriLoc = locService.getById(task.getOriLoc());
                        destLoc = locService.getById(task.getDestLoc());
                        startCode = codeService.getCacheById(oriLoc.getCode()).getData();
                        endCode = codeService.getCacheById(destLoc.getCode()).getData();
                        break;
                    case LOC_TO_STA:
                        oriLoc = locService.getById(task.getOriLoc());
                        destSta = staService.getById(task.getDestSta());
                        startCode = codeService.getCacheById(oriLoc.getCode()).getData();
                        endCode = codeService.getCacheById(destSta.getCode()).getData();
                        break;
                    case STA_TO_LOC:
                        oriSta = staService.getById(task.getOriSta());
                        destLoc = locService.getById(task.getDestLoc());
                        startCode = codeService.getCacheById(oriSta.getCode()).getData();
                        endCode = codeService.getCacheById(destLoc.getCode()).getData();
                        break;
                    case STA_TO_STA:
                        oriSta = staService.getById(task.getOriSta());
                        destSta = staService.getById(task.getDestSta());
                        startCode = codeService.getCacheById(oriSta.getCode()).getData();
                        endCode = codeService.getCacheById(destSta.getCode()).getData();
                        break;
                    default:
                        throw new BusinessException(task.getSeqNum() + "任务类型错误");
                }
                allocateTask.setTaskId(task.getId() + "");
                allocateTask.setStart(startCode);
                allocateTask.setEnd(endCode);
                allocateTask.setType("1");
                allocateTask.setPriority(task.getPriority());
                allocateTasks.add(allocateTask);
            }
            log.info("allocateTask:{}", JSON.toJSONString(allocateTasks));
            String post = HttpUtils.post("http://10.10.10.239:8002/open/task/send/v1", JSON.toJSONString(allocateTasks));
            if (post == null) {
                return;
            }
            Object data = JSON.parseObject(post).get("data");
            if (data != null) {
                List<AllocateTaskResponse> allocateTaskResponses = JSON.parseArray(data.toString(), AllocateTaskResponse.class);
                if (Cools.isEmpty(allocateTaskResponses)) {
                    log.error("allocateTaskResponses is null,{}", post);
                    throw new BusinessException("任务下发失败");
                }
                for (AllocateTaskResponse allocateTaskResponse : allocateTaskResponses) {
                    Task task = taskService.getById(allocateTaskResponse.getTaskId());
                    task.setAgvId(agvService.getAgvId(allocateTaskResponse.getAgvId()));
                    task.setBack(allocateTaskResponse.getLev_id());
                    task.setTaskSts(TaskStsType.WAITING.val());
                    task.setIoTime(now);
                    task.setUpdateTime(now);
                    if (!taskService.updateById(task)) {
                        throw new BusinessException("seqNum: " + task.getSeqNum() + " failed to update");
                    }
                }
            }
 
        } catch (Exception e) {
            log.error("mainService.allocateTaskByZkd", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
 
 
    /**
     * 解析取放货集合任务,进行最优的排列组合顺序 ( 车辆此时是空闲且静止的 )
     * todo: {@link com.zy.acs.manager.core.HandlerController#controlAgv(String, HandlerPublishParam)}
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean buildMajorTask(Long agvId, Navigation navigation) {
        try {
            // generate travel
//            Travel travel = new Travel();
//            travel.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
//            travel.setTravelId(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
//            travel.setAgvId(agvId);
//            travel.setTaskContent(JSON.toJSONString(navigation));
//            //travel.setTaskIds(JSON.toJSONString(taskList.stream().map(Task::getId).collect(Collectors.toList())));
//            travel.setState(TravelStateType.RUNNING.toString());
//            if (!travelService.save(travel)) {
//                throw new BusinessException("任务组保存失败");
//            }
            // generate segment
            Segment segment = segmentService.getOne(new QueryWrapper<Segment>().eq("uuid", navigation.getSegId()));
            if (segment == null) {
                segment = new Segment();
            }
            int segSerial = 1;
            //segment.setTravelId(travel.getId());
            segment.setAgvId(agvId);
 
            segment.setSerial(segSerial);
            for (Navigation.CodeDTO codeDTO : navigation.getCodeList()) {
                //segSerial++;
                Task task = taskService.getById(Long.parseLong(codeDTO.getTaskId()));
                segment.setUuid(navigation.getSegId());
                segment.setTaskId(task.getId());
                Code cacheByData = codeService.getCacheById(Long.parseLong(codeDTO.getCode()));
                if (cacheByData == null) {
                    throw new BusinessException(codeDTO.getCode() + "点位不存在");
                }
                segment.setEndNode(cacheByData.getId());
                if (codeDTO.getPosType() != null) {
                    if (task.getTaskType().equals(TaskTypeType.LOC_TO_STA.val())) {
                        segment.setPosType(codeDTO.getPosType().equals("1") ? TaskPosDto.PosType.ORI_LOC.toString() : TaskPosDto.PosType.DEST_STA.toString());
                    } else if (task.getTaskType().equals(TaskTypeType.STA_TO_LOC.val())) {
                        segment.setPosType(codeDTO.getPosType().equals("1") ? TaskPosDto.PosType.ORI_STA.toString() : TaskPosDto.PosType.DEST_LOC.toString());
                    } else if (task.getTaskType().equals(TaskTypeType.STA_TO_STA.val())) {
                        segment.setPosType(codeDTO.getPosType().equals("1") ? TaskPosDto.PosType.ORI_STA.toString() : TaskPosDto.PosType.DEST_STA.toString());
                    } else if (task.getTaskType().equals(TaskTypeType.LOC_TO_LOC.val())) {
                        segment.setPosType(codeDTO.getPosType().equals("1") ? TaskPosDto.PosType.ORI_LOC.toString() : TaskPosDto.PosType.DEST_LOC.toString());
                    }
                }
                segment.setBackpack(codeDTO.getLev());
                segment.setState(SegmentStateType.INIT.toString());
                segment.setPath((Cools.isEmpty(segment.getPath()) ? "" : segment.getPath()) + cacheByData.getData() + ",");
 
                task.setTaskSts(TaskStsType.ASSIGN.val());
                task.setStartTime(new Date());
                task.setUpdateTime(new Date());
                if (!taskService.updateById(task)) {
                    throw new BusinessException(task.getUuid() + "任务更新失败");
                }
            }
 
            segment.setState(SegmentStateType.WAITING.toString());
            if (!segmentService.save(segment)) {
                throw new BusinessException("任务组保存失败");
            }
 
        } catch (Exception e) {
            log.error("mainService.buildMajorTask[task]", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return true;
    }
 
 
}