package com.zy.acs.manager.core.service; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.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.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); String post = HttpUtils.post("http://localhost:8080/zy-acs-manager/api/v1/zkd/allocateTask", JSON.toJSONString(allocateTasks)); List 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"); } } } 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 void 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 int segSerial = 0; List segmentList = new ArrayList<>(); for (Navigation.CodeDTO codeDTO : navigation.getCodeList()) { 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.setTaskId(task.getId()); segment.setSerial(segSerial); Code cacheByData = codeService.getCacheByData(codeDTO.getCode()); segment.setEndNode(cacheByData.getId()); segment.setPosType(codeDTO.getPosType()); segment.setBackpack(codeDTO.getLev()); segment.setState(SegmentStateType.INIT.toString()); segmentList.add(segment); task.setTaskSts(TaskStsType.ASSIGN.val()); task.setStartTime(new Date()); task.setUpdateTime(new Date()); if (!taskService.updateById(task)) { 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("任务组保存失败"); } } } catch (Exception e) { log.error("mainService.buildMajorTask[task]", e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } } }