1
zhang
2025-07-03 13ea8b334572c2423abb8d156fd8428f8d074172
zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainZkdService.java
@@ -2,17 +2,22 @@
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.*;
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;
@@ -74,8 +79,8 @@
    private TravelService travelService;
    @Autowired
    private SegmentService segmentService;
    @Autowired
    private TrafficService trafficService;
    //    @Autowired
//    private TrafficService trafficService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
@@ -149,20 +154,33 @@
                allocateTask.setEnd(endCode);
                allocateTask.setType("1");
                allocateTask.setPriority(task.getPriority());
                allocateTasks.add(allocateTask);
            }
            allocateTasks.add(allocateTask);
            String post = HttpUtils.post("http://localhost:8080/zy-acs-manager/api/v1/zkd/allocateTask", JSON.toJSONString(allocateTasks));
            List<AllocateTaskResponse> allocateTaskResponses = JSON.parseArray(post, AllocateTaskResponse.class);
            for (AllocateTaskResponse allocateTaskResponse : allocateTaskResponses) {
                Task task = taskService.getById(allocateTaskResponse.getTaskId());
                task.setAgvId(Long.parseLong(allocateTaskResponse.getAgvId()));
                task.setTaskSts(TaskStsType.WAITING.val());
                task.setIoTime(now);
                task.setUpdateTime(now);
                if (!taskService.updateById(task)) {
                    throw new BusinessException("seqNum: " + task.getSeqNum() + " failed to update");
            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();
@@ -175,37 +193,53 @@
     * todo: {@link com.zy.acs.manager.core.HandlerController#controlAgv(String, HandlerPublishParam)}
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void buildMajorTask(Long agvId, Navigation navigation) {
    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("任务组保存失败");
            }
//            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
            int segSerial = 0;
            List<Segment> segmentList = new ArrayList<>();
            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++;
                //segSerial++;
                Task task = taskService.getById(Long.parseLong(codeDTO.getTaskId()));
                Segment segment = new Segment();
                segment.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
                segment.setTravelId(travel.getId());
                segment.setAgvId(agvId);
                segment.setUuid(navigation.getSegId());
                segment.setTaskId(task.getId());
                segment.setSerial(segSerial);
                Code cacheByData = codeService.getCacheByData(codeDTO.getCode());
                Code cacheByData = codeService.getCacheById(Long.parseLong(codeDTO.getCode()));
                if (cacheByData == null) {
                    throw new BusinessException(codeDTO.getCode() + "点位不存在");
                }
                segment.setEndNode(cacheByData.getId());
                segment.setPosType(codeDTO.getPosType());
                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());
                segmentList.add(segment);
                segment.setPath((Cools.isEmpty(segment.getPath()) ? "" : segment.getPath()) + cacheByData.getData() + ",");
                task.setTaskSts(TaskStsType.ASSIGN.val());
                task.setStartTime(new Date());
@@ -214,20 +248,17 @@
                    throw new BusinessException(task.getUuid() + "任务更新失败");
                }
            }
            for (int i = 0; i < segmentList.size(); i++) {
                Segment segment = segmentList.get(i);
                if (i == 0) {
                    segment.setState(SegmentStateType.WAITING.toString());
                }
                if (!segmentService.save(segment)) {
                    throw new BusinessException("任务组保存失败");
                }
            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;
    }