|  |  |  | 
|---|
|  |  |  | package com.zy.acs.manager.manager.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONArray; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.zy.acs.framework.common.BaseRes; | 
|---|
|  |  |  | import com.zy.acs.framework.common.Cools; | 
|---|
|  |  |  | import com.zy.acs.framework.common.SnowflakeIdWorker; | 
|---|
|  |  |  | import com.zy.acs.framework.exception.CoolException; | 
|---|
|  |  |  | import com.zy.acs.manager.common.exception.BusinessException; | 
|---|
|  |  |  | import com.zy.acs.manager.core.domain.Lane; | 
|---|
|  |  |  | import com.zy.acs.manager.core.service.LaneService; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.entity.*; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.enums.*; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.mapper.TaskMapper; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.CodeService; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.FuncStaService; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.LocService; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.TaskService; | 
|---|
|  |  |  | import com.zy.acs.framework.common.Cools; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.*; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | 
|---|
|  |  |  | public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private BusService busService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private CodeService codeService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocService locService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private StaService staService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private FuncStaService funcStaService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SnowflakeIdWorker snowflakeIdWorker; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LaneService laneService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void buildActionList(Task task) { | 
|---|
|  |  |  | List<String> codeList = JSONArray.parseArray(task.getPhase(), String.class); | 
|---|
|  |  |  | if (Cools.isEmpty(codeList)) { | 
|---|
|  |  |  | throw new BusinessException(task.getSeqNum() + "任务解析步骤失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (String data : codeList) { | 
|---|
|  |  |  | Code code = codeService.selectByData(data); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public synchronized String generateSeqNum() { | 
|---|
|  |  |  | return String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<Map<String, Object>> selectStatByLastSevenDays() { | 
|---|
|  |  |  | return this.baseMapper.selectStatByLastSevenDays(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public void operateTaskSts(Integer taskId, Boolean finish) { | 
|---|
|  |  |  | public Boolean complete(Long taskId, Long userId) { | 
|---|
|  |  |  | Task task = this.getById(taskId); | 
|---|
|  |  |  | if (null == task) { | 
|---|
|  |  |  | return Boolean.FALSE; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | this.maintainLocSts(task, Boolean.TRUE); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | task.setTaskSts(TaskStsType.COMPLETE.val()); | 
|---|
|  |  |  | task.setUpdateTime(new Date()); | 
|---|
|  |  |  | task.setUpdateBy(userId); | 
|---|
|  |  |  | if (!this.updateById(task)) { | 
|---|
|  |  |  | throw new CoolException(BaseRes.ERROR); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | busService.checkoutComplete(task.getBusId()); | 
|---|
|  |  |  | return Boolean.TRUE; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public Boolean cancel(Long taskId, Long userId) { | 
|---|
|  |  |  | Task task = this.getById(taskId); | 
|---|
|  |  |  | if (null == task) { | 
|---|
|  |  |  | return Boolean.FALSE; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | this.maintainLocSts(task, Boolean.FALSE); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | task.setTaskSts(TaskStsType.CANCEL.val()); | 
|---|
|  |  |  | task.setUpdateTime(new Date()); | 
|---|
|  |  |  | task.setUpdateBy(userId); | 
|---|
|  |  |  | if (!this.updateById(task)) { | 
|---|
|  |  |  | throw new CoolException(BaseRes.ERROR); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | busService.checkoutComplete(task.getBusId()); | 
|---|
|  |  |  | return Boolean.TRUE; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Lane checkoutOriginLane(Task task) { | 
|---|
|  |  |  | Long codeId = null; | 
|---|
|  |  |  | TaskTypeType typeType = TaskTypeType.get(task.getTaskTypeEl()); | 
|---|
|  |  |  | switch (Objects.requireNonNull(typeType)) { | 
|---|
|  |  |  | case LOC_TO_LOC: | 
|---|
|  |  |  | case LOC_TO_STA: | 
|---|
|  |  |  | codeId = locService.getById(task.getOriLoc()).getCode(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case STA_TO_LOC: | 
|---|
|  |  |  | case STA_TO_STA: | 
|---|
|  |  |  | codeId = staService.getById(task.getOriSta()).getCode(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (null == codeId) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return laneService.search(codeService.getCacheById(codeId).getData()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Lane checkoutDestinationLane(Task task) { | 
|---|
|  |  |  | Long codeId = null; | 
|---|
|  |  |  | TaskTypeType typeType = TaskTypeType.get(task.getTaskTypeEl()); | 
|---|
|  |  |  | switch (Objects.requireNonNull(typeType)) { | 
|---|
|  |  |  | case LOC_TO_LOC: | 
|---|
|  |  |  | case STA_TO_LOC: | 
|---|
|  |  |  | codeId = locService.getById(task.getDestLoc()).getCode(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case LOC_TO_STA: | 
|---|
|  |  |  | case STA_TO_STA: | 
|---|
|  |  |  | codeId = staService.getById(task.getDestSta()).getCode(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (null == codeId) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return laneService.search(codeService.getCacheById(codeId).getData()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<Task> findRunningTasksByLaneHash(String laneHash) { | 
|---|
|  |  |  | if (Cools.isEmpty(laneHash)) { | 
|---|
|  |  |  | return new ArrayList<>(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return this.list(new LambdaQueryWrapper<Task>() | 
|---|
|  |  |  | .in(Task::getTaskSts, TaskStsType.WAITING.val(), TaskStsType.ASSIGN.val(), TaskStsType.PROGRESS.val()) | 
|---|
|  |  |  | .and(i -> { | 
|---|
|  |  |  | i.eq(Task::getOriLaneHash, laneHash).or().eq(Task::getDestLaneHash, laneHash); | 
|---|
|  |  |  | }) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<Task> findTransportTasksByAgv(Long agvId) { | 
|---|
|  |  |  | if (null == agvId) { | 
|---|
|  |  |  | return new ArrayList<>(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<Task>().eq(Task::getAgvId, agvId); | 
|---|
|  |  |  | wrapper.in(Task::getTaskSts, TaskStsType.WAITING.val(), TaskStsType.ASSIGN.val(), TaskStsType.PROGRESS.val()); | 
|---|
|  |  |  | wrapper.notIn(Task::getTaskType, TaskTypeType.MOVE.val(), TaskTypeType.TO_CHARGE.val(), TaskTypeType.TO_STANDBY.val()); | 
|---|
|  |  |  | return this.list(wrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Integer findTransportTasksCountByAgv(Long agvId) { | 
|---|
|  |  |  | LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<Task>().eq(Task::getAgvId, agvId); | 
|---|
|  |  |  | wrapper.in(Task::getTaskSts, TaskStsType.WAITING.val(), TaskStsType.ASSIGN.val(), TaskStsType.PROGRESS.val()); | 
|---|
|  |  |  | wrapper.notIn(Task::getTaskType, TaskTypeType.MOVE.val(), TaskTypeType.TO_CHARGE.val(), TaskTypeType.TO_STANDBY.val()); | 
|---|
|  |  |  | return this.count(wrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Task findLatestTask(Long agvId, TaskStsType taskSts) { | 
|---|
|  |  |  | LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<Task>() | 
|---|
|  |  |  | .orderByDesc(Task::getCreateTime) | 
|---|
|  |  |  | .last("limit 0, 1"); | 
|---|
|  |  |  | if (null != agvId) { | 
|---|
|  |  |  | wrapper.eq(Task::getAgvId, agvId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (null != taskSts) { | 
|---|
|  |  |  | wrapper.eq(Task::getTaskSts, taskSts.val()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return this.list(wrapper).stream().findFirst().orElse(null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public void maintainLocSts(Task task, Boolean complete) { | 
|---|
|  |  |  | Loc oriLoc = null; Loc destLoc = null; | 
|---|
|  |  |  | Sta oriSta = null; Sta destSta = null; | 
|---|
|  |  |  | Date now = new Date(); | 
|---|
|  |  |  | 
|---|
|  |  |  | oriLoc = locService.getById(task.getOriLoc()); | 
|---|
|  |  |  | destLoc = locService.getById(task.getDestLoc()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | oriLoc.setLocSts(finish?LocStsType.IDLE.val():LocStsType.STOCK.val()); | 
|---|
|  |  |  | oriLoc.setLocSts(complete?LocStsType.IDLE.val():LocStsType.STOCK.val()); | 
|---|
|  |  |  | oriLoc.setUpdateTime(now); | 
|---|
|  |  |  | if (!locService.updateById(oriLoc)) { | 
|---|
|  |  |  | throw new BusinessException("Loc [" + task.getOriLoc$() + "] 库位修改状态失败 !!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | destLoc.setLocSts(finish?LocStsType.STOCK.val():LocStsType.IDLE.val()); | 
|---|
|  |  |  | destLoc.setLocSts(complete?LocStsType.STOCK.val():LocStsType.IDLE.val()); | 
|---|
|  |  |  | destLoc.setUpdateTime(now); | 
|---|
|  |  |  | if (!locService.updateById(destLoc)) { | 
|---|
|  |  |  | throw new BusinessException("Loc [" + task.getDestLoc$() + "] 库位修改状态失败 !!!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case LOC_TO_STA: | 
|---|
|  |  |  | oriLoc = locService.getById(task.getOriLoc()); | 
|---|
|  |  |  | oriLoc.setLocSts(finish?LocStsType.IDLE.val():LocStsType.STOCK.val()); | 
|---|
|  |  |  | oriLoc.setLocSts(complete?LocStsType.IDLE.val():LocStsType.STOCK.val()); | 
|---|
|  |  |  | oriLoc.setUpdateTime(now); | 
|---|
|  |  |  | if (!locService.updateById(oriLoc)) { | 
|---|
|  |  |  | throw new BusinessException("Loc [" + task.getOriLoc$() + "] 库位修改状态失败 !!!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case STA_TO_LOC: | 
|---|
|  |  |  | destLoc = locService.getById(task.getDestLoc()); | 
|---|
|  |  |  | destLoc.setLocSts(finish?LocStsType.STOCK.val():LocStsType.IDLE.val()); | 
|---|
|  |  |  | destLoc.setLocSts(complete?LocStsType.STOCK.val():LocStsType.IDLE.val()); | 
|---|
|  |  |  | destLoc.setUpdateTime(now); | 
|---|
|  |  |  | if (!locService.updateById(destLoc)) { | 
|---|
|  |  |  | throw new BusinessException("Loc [" + task.getDestLoc$() + "] 库位修改状态失败 !!!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | case TO_CHARGE: | 
|---|
|  |  |  | case TO_STANDBY: | 
|---|
|  |  |  | FuncSta funcSta = funcStaService.getByCodeAndType(task.getDestCode(), FuncStaType.query(typeType).toString()); | 
|---|
|  |  |  | if (!finish) { | 
|---|
|  |  |  | if (!complete) { | 
|---|
|  |  |  | funcSta.setState(FuncStaStateType.IDLE.toString()); | 
|---|
|  |  |  | funcSta.setUpdateTime(now); | 
|---|
|  |  |  | if (!funcStaService.updateById(funcSta)) { | 
|---|
|  |  |  | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | task.setTaskSts(TaskStsType.COMPLETE.val()); | 
|---|
|  |  |  | task.setUpdateTime(now); | 
|---|
|  |  |  | if (!this.updateById(task)) { | 
|---|
|  |  |  | throw new BusinessException(task.getSeqNum() + "任务更新状态失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<Map<String, Object>> selectStatByLastSevenDays() { | 
|---|
|  |  |  | return this.baseMapper.selectStatByLastSevenDays(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|