| package com.zy.acs.manager.manager.service.impl; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
| import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
| import com.zy.acs.common.constant.RedisConstant; | 
| import com.zy.acs.common.enums.AgvStatusType; | 
| import com.zy.acs.common.utils.RedisSupport; | 
| import com.zy.acs.framework.common.Cools; | 
| import com.zy.acs.manager.common.domain.BaseParam; | 
| import com.zy.acs.manager.common.domain.PageParam; | 
| import com.zy.acs.manager.common.domain.PageResult; | 
| import com.zy.acs.manager.core.cache.CoreCache; | 
| import com.zy.acs.manager.core.domain.VehicleDto; | 
| import com.zy.acs.manager.manager.controller.result.AgvResult; | 
| import com.zy.acs.manager.manager.entity.Agv; | 
| import com.zy.acs.manager.manager.entity.AgvDetail; | 
| import com.zy.acs.manager.manager.entity.AgvModel; | 
| import com.zy.acs.manager.manager.entity.Segment; | 
| import com.zy.acs.manager.manager.enums.SegmentStateType; | 
| import com.zy.acs.manager.manager.mapper.AgvMapper; | 
| import com.zy.acs.manager.manager.service.*; | 
| 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 javax.annotation.PostConstruct; | 
| import java.util.ArrayList; | 
| import java.util.HashMap; | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * 字典数据Service实现 | 
|  * | 
|  * @author vincent | 
|  * @since 2020-03-14 11:29:04 | 
|  */ | 
| @Slf4j | 
| @Service | 
| public class AgvServiceImpl extends ServiceImpl<AgvMapper, Agv> implements AgvService { | 
|   | 
|     private final RedisSupport redis = RedisSupport.defaultRedisSupport; | 
|   | 
|     private static final Map<String, Long> AGV_ID_MAP = new HashMap<>(); | 
|   | 
|     private static final Map<Long, String> AGV_NO_MAP = new HashMap<>(); | 
|   | 
|     @Autowired | 
|     private AgvDetailService agvDetailService; | 
|     @Autowired | 
|     private AgvModelService agvModelService; | 
|     @Autowired | 
|     private ConfigService configService; | 
|     @Autowired | 
|     private CodeService codeService; | 
|     @Autowired | 
|     private SegmentService segmentService; | 
|     @Autowired | 
|     private TaskService taskService; | 
|   | 
|     @PostConstruct | 
|     public void init() { | 
|         for (Agv agv : this.list()) { | 
|             AGV_ID_MAP.put(agv.getUuid(), agv.getId()); | 
|             AGV_NO_MAP.put(agv.getId(), agv.getUuid()); | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public PageResult<AgvResult> pageRel(PageParam<Agv, BaseParam> pageParam) { | 
|         return new PageResult<>(this.baseMapper.selectPageRel(pageParam, pageParam.checkoutMap()), pageParam.getTotal()); | 
|     } | 
|   | 
|     @Override | 
|     public Agv selectByUuid(String uuid) { | 
|         return this.getOne(new LambdaQueryWrapper<Agv>().eq(Agv::getUuid, uuid)); | 
|     } | 
|   | 
|     @Override | 
|     public Long getAgvId(String uuid) { | 
|         if (Cools.isEmpty(uuid)) { | 
|             Agv agv = this.selectByUuid(uuid); | 
|             if (agv != null) { | 
|                 AGV_ID_MAP.put(uuid, agv.getId()); | 
|             } else { | 
|                 return null; | 
|             } | 
|         } | 
|         return AGV_ID_MAP.get(uuid); | 
|     } | 
|   | 
|     @Override | 
|     public String getAgvNo(Long agvId) { | 
|         if (Cools.isEmpty(agvId)) { | 
|             Agv agv = this.getById(agvId); | 
|             if (agv != null) { | 
|                 AGV_NO_MAP.put(agvId, agv.getUuid()); | 
|             } else { | 
|                 return null; | 
|             } | 
|         } | 
|         return AGV_NO_MAP.get(agvId); | 
|     } | 
|   | 
|   | 
|     @Override | 
|     public Boolean judgeEnable(Long agvId) { | 
|         return this.judgeEnable(agvId, false); | 
|     } | 
|   | 
|     @Override | 
|     public Boolean judgeEnable(Long agvId, Boolean isLowBattery) { | 
|         String agvNo = this.getAgvNo(agvId); | 
|         AgvModel agvModel = agvModelService.getByAgvId(agvId); | 
|         if (0 < segmentService.count(new LambdaQueryWrapper<Segment>() | 
|                         .eq(Segment::getAgvId, agvId) | 
|                         .eq(Segment::getState, SegmentStateType.RUNNING.toString()) | 
|         )) { | 
|             log.warn("[{}]号Agv正在忙碌 - segment......", agvNo); | 
|             return false; | 
|         } | 
|         if (!this.judgeOnline(agvId)) { | 
|             log.warn("[{}]号Agv不是在线状态......", agvNo); | 
|             return false; | 
|         } | 
|         AgvDetail agvDetail = agvDetailService.selectMajorByAgvId(agvId); | 
|         if (null == agvDetail) { | 
|             log.warn("[{}]号Agv未收到状态包......", agvNo); | 
|             return false; | 
|         } | 
|         if (agvDetail.getVol() == 0) { | 
|             return false; | 
|         } | 
|         if (!agvDetail.getAgvStatus().equals(AgvStatusType.IDLE) && !agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) { | 
|             log.warn("[{}]号Agv不是空闲状态......", agvNo); | 
|             return false; | 
|         } | 
|         if (agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) { | 
|             if (agvDetail.getVol() < agvModel.getQuaBattery()) { | 
|                 return false; | 
|             } | 
|         } | 
|         if (!agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) { | 
|             if (agvDetail.getPos() != 1) { | 
|                 log.warn("[{}]号Agv当前不在定位......", agvNo); | 
|                 return false; | 
|             } | 
|             if (null == agvDetail.getRecentCode()) { | 
|                 log.warn("[{}]号Agv当前不在定位......", agvNo); | 
|                 return false; | 
|             } | 
|         } | 
|         // with battery | 
|         if (isLowBattery) { | 
|             Agv agv = this.getById(agvId); | 
|             if (agvDetailService.isPowerLoss(agv, agvDetail, agvModel)) { | 
|                 log.warn("[{}]号Agv电量不足......", agvNo); | 
|                 return false; | 
|             } | 
|         } | 
|   | 
|         return true; | 
|     } | 
|   | 
|     @Override | 
|     public Boolean judgeOnline(Long agvId) { | 
|         Agv agv = this.getById(agvId); | 
|         return redis.getObject(RedisConstant.AGV_ONLINE_FLAG, agv.getUuid()) != null; | 
|     } | 
|   | 
|     @Override | 
|     public List<VehicleDto> getVehicleDtoList(List<String> noIncluded) { | 
|         List<VehicleDto> res = new ArrayList<>(); | 
|         List<Agv> list = this.list(new LambdaQueryWrapper<Agv>()); | 
|         for (Agv agv : list) { | 
|             if (!Cools.isEmpty(noIncluded)) { | 
|                 if (noIncluded.contains(agv.getUuid())) { | 
|                     continue; | 
|                 } | 
|             } | 
|             AgvDetail detail = agvDetailService.selectByAgvId(agv.getId()); | 
|             VehicleDto dto = new VehicleDto(); | 
|             dto.setVehicle(agv.getUuid()); | 
|             dto.setPosCode(codeService.getCacheById(detail.getRecentCode()).getData()); | 
|             res.add(dto); | 
|         } | 
|         return res; | 
|     } | 
|   | 
|     @Override | 
|     public Integer getBackpack(Agv agv) { | 
| //        if (null != agv.getStage()) { | 
| //            return agv.getStage(); | 
| //        } | 
|         AgvModel agvModel = agvModelService.getByAgvId(agv.getId()); | 
|         assert null != agvModel; | 
|         return agvModel.getBackpack(); | 
|     } | 
|   | 
|     @Override | 
|     public Agv findByPosition(Long codeId) { | 
|         List<Agv> agvList = this.list(new LambdaQueryWrapper<Agv>() | 
| //                .eq(Agv::getStatus, StatusType.ENABLE.val) | 
|         ); | 
|         for (Agv agv : agvList) { | 
|             AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId()); | 
|             if (null == agvDetail) { | 
|                 continue; | 
|             } | 
|             if (null == agvDetail.getRecentCode()) { | 
|                 continue; | 
|             } | 
|             if (agvDetail.getRecentCode().equals(codeId)) { | 
|                 return agv; | 
|             } | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     @Override | 
|     public int getBackpackRemainingCapacity(Long agvId) { | 
|         Integer usedSlots = CoreCache.AGV_BACKPACK_USED_CACHE.get(agvId); | 
|         if (null == usedSlots) { | 
|             usedSlots = taskService.findTransportTasksCountByAgv(agvId); | 
|         } | 
|   | 
|         AgvModel agvModel = agvModelService.getByAgvId(agvId); | 
|         return Math.max(agvModel.getBackpack() - usedSlots, 0); // if less than zero, then return zero | 
|     } | 
|   | 
| } |