|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.zy.acs.framework.common.Cools; | 
|---|
|  |  |  | import com.zy.acs.manager.common.exception.BusinessException; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.controller.result.MissionVo; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.entity.Segment; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.entity.*; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.enums.ActionStsType; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.enums.BusStsType; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.enums.SegmentStateType; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.MissionService; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.SegmentService; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.enums.TaskStsType; | 
|---|
|  |  |  | 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.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Created by vincent on 9/27/2024 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @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() { | 
|---|
|  |  |  | public List<MissionVo> getList(Long agvId, String groupNo) { | 
|---|
|  |  |  | List<MissionVo> result = new ArrayList<>(); | 
|---|
|  |  |  | List<String> runningGroupNos = segmentService.getGroupNo(SegmentStateType.RUNNING); | 
|---|
|  |  |  | for (String groupNo : runningGroupNos) { | 
|---|
|  |  |  | MissionVo vo = generate(groupNo); | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public MissionVo generate(String groupNo) { | 
|---|
|  |  |  | List<Segment> list = segmentService.list(new LambdaQueryWrapper<Segment>().eq(Segment::getGroupId, groupNo)); | 
|---|
|  |  |  | @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) { | 
|---|
|  |  |  | currCode = codeService.getById(recentCode).getData(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Long> actionIds = actionList.stream().map(Action::getId).collect(Collectors.toList()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String actionGroupId = actionService.getById(actionIds.get(0)).getGroupId(); | 
|---|
|  |  |  | actionService.updateStsByGroupId(actionGroupId, ActionStsType.EXPIRED.val()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Action> newActionList = new ArrayList<>(); | 
|---|
|  |  |  | Date now = new Date(); | 
|---|
|  |  |  | 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.setPriority(i); | 
|---|
|  |  |  | if (!actionService.save(action)) { | 
|---|
|  |  |  | throw new BusinessException(action.getName() + " Action Update 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|