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 taskList = taskService.list(new LambdaQueryWrapper() .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 taskIds = taskList.stream().map(Task::getId).distinct().collect(Collectors.toList()); List 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 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().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; } }