package cn.iocoder.yudao.module.system.service.robot;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.api.robot.RequestProcessor;
import cn.iocoder.yudao.module.system.api.robot.dto.RobotStatusDTO;
import cn.iocoder.yudao.module.system.api.robot.dto.RobotStatusDataPoseDTO;
import cn.iocoder.yudao.module.system.api.robot.vo.RobotInformationVO;
import cn.iocoder.yudao.module.system.constant.robot.RobotTaskChcheConstant;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.informationmapassociation.InformationMapAssociationDO;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapDO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotInformationDO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotModelDO;
import cn.iocoder.yudao.module.system.dal.mysql.positionmap.PositionMapMapper;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotInformationMapper;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotModelMapper;
import cn.iocoder.yudao.module.system.enums.robot.CommandTypeEnum;
import cn.iocoder.yudao.module.system.enums.robot.RobotStatusEnum;
import cn.iocoder.yudao.module.system.enums.robot.RobotTaskModelEnum;
import cn.iocoder.yudao.module.system.enums.robot.information.RobotStatisticsTypeEnum;
import cn.iocoder.yudao.module.system.service.informationmapassociation.InformationMapAssociationService;
import cn.iocoder.yudao.module.system.util.redis.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 车辆信息 Service 实现类
 *
 * @author 陈宾顺
 */
@Service
@Validated
public class RobotInformationServiceImpl extends ServiceImpl<RobotInformationMapper, RobotInformationDO> implements RobotInformationService {

    @Resource
    private RobotInformationMapper informationMapper;

    @Resource
    private PositionMapMapper positionMapMapper;
    @Resource
    private RobotModelMapper modelMapper;

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private RequestProcessor processor;
    @Resource
    private InformationMapAssociationService informationMapAssociationService;
    // -- 获取所有的设备信息key
    public static final String key = RobotTaskChcheConstant.ROBOT_GET_ROBOT_INFO;

    @Override
    public Long createInformation(RobotInformationSaveReqVO createReqVO) {
        //判断mac地址是否重复
        RobotInformationDO query = new RobotInformationDO();
        query.setMacAddress(createReqVO.getMacAddress());
        List<RobotInformationDO> existRobotMac = informationMapper.queryAllByLimit(query);
        if (ObjectUtil.isNotEmpty(existRobotMac)) {
            throw exception(ROBOT_MAC_ADDRESS_EXISTS);
        }
        //判断机器人编号
        query.setMacAddress(null);
        query.setRobotNo(createReqVO.getRobotNo());
        List<RobotInformationDO> existRobotNo = informationMapper.queryAllByLimit(query);
        if (ObjectUtil.isNotEmpty(existRobotNo)) {
            throw exception(ROBOT_ROBOT_NO_EXISTS);
        }
        RobotModelDO robotModelDO = modelMapper.selectById(createReqVO.getRobotModelId());
        createReqVO.setRobotModelNumber(robotModelDO.getRobotModelNumber());
        createReqVO.setUrl(robotModelDO.getUrl());
        createReqVO.setChargeType(robotModelDO.getChargeType());
        // 插入
        RobotInformationDO information = BeanUtils.toBean(createReqVO, RobotInformationDO.class);
        informationMapper.insert(information);
        redisUtil.set(RobotTaskChcheConstant.ROBOT_GET_ROBOTNO_BY_MAC + information.getMacAddress(), information.getRobotNo());
        redisUtil.set(RobotTaskChcheConstant.ROBOT_GET_MAC_BY_NO + information.getRobotNo(), information.getMacAddress());
        // 新增AGV于地图关系表 -----
        if (CollUtil.isNotEmpty(createReqVO.getFloorAreaJson())) {
            List<InformationMapAssociationDO> list = new ArrayList<>();
            for (Long mapId : createReqVO.getFloorAreaJson()) {
                InformationMapAssociationDO item = new InformationMapAssociationDO();
                item.setRobotInformationId(information.getId());
                item.setPositionMapId(mapId);
                list.add(item);
            }
            informationMapAssociationService.saveBatch(list);
        }
        // 返回
        return information.getId();
    }

    @Override
    public void updateInformation(RobotInformationSaveReqVO updateReqVO) {
        // 校验存在
        validateInformationExists(updateReqVO.getId());
        RobotInformationDO robotInformationDO = informationMapper.selectById(updateReqVO.getId());
        if (robotInformationDO == null) {
            throw exception(ROBOT_INFORMATION_NOT_EXISTS);
        }

        //校验MAC地址
        RobotInformationDO query = new RobotInformationDO();
        query.setMacAddress(updateReqVO.getMacAddress());
        List<RobotInformationDO> existRobot = informationMapper.queryAllByLimit(query);
        boolean exist = existRobot.stream().anyMatch(v -> (v.getMacAddress().equals(updateReqVO.getMacAddress())
                && !v.getId().equals(updateReqVO.getId())));
        if (exist) {
            throw exception(ROBOT_MAC_ADDRESS_EXISTS);
        }
        //校验机器人编号
        query.setMacAddress(null);
        query.setRobotNo(updateReqVO.getRobotNo());
        List<RobotInformationDO> existRobotNo = informationMapper.queryAllByLimit(query);
        boolean robotNoExist = existRobotNo.stream().anyMatch(v -> (v.getRobotNo().equals(updateReqVO.getRobotNo())
                && !v.getId().equals(updateReqVO.getId())));
        if (robotNoExist) {
            throw exception(ROBOT_ROBOT_NO_EXISTS);
        }
        // 更新
        RobotInformationDO updateObj = BeanUtils.toBean(updateReqVO, RobotInformationDO.class);
        informationMapper.updateById(updateObj);
        redisUtil.set(RobotTaskChcheConstant.ROBOT_GET_ROBOTNO_BY_MAC + updateObj.getMacAddress(), updateObj.getRobotNo());
        redisUtil.set(RobotTaskChcheConstant.ROBOT_GET_MAC_BY_NO + updateObj.getRobotNo(), updateObj.getMacAddress());
        // 更新AGV于地图关系表 -----
        List<List<Long>> list = CollectionUtils.compareLists(
                (CollUtil.isEmpty(robotInformationDO.getFloorAreaJson()) ? Collections.emptyList() : new ArrayList<>(robotInformationDO.getFloorAreaJson())),
                (CollUtil.isEmpty(updateReqVO.getFloorAreaJson()) ? Collections.emptyList() : new ArrayList<>(updateReqVO.getFloorAreaJson())),
                ObjectUtil::equal);
        informationMapAssociationService.saveOrUpdateOrDel(updateReqVO.getId(), list);
    }

    @Override
    public void deleteInformation(Long id) {
        // 校验存在
        validateInformationExists(id);
        // 删除
        informationMapper.deleteById(id);
    }

    private void validateInformationExists(Long id) {
        if (informationMapper.selectById(id) == null) {
            throw exception(ROBOT_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public RobotInformationRespVO getInformation(Long id) {
        RobotInformationDO robotInformationDO = informationMapper.selectById(id);
        RobotInformationRespVO bean = BeanUtils.toBean(robotInformationDO, RobotInformationRespVO.class);
        setPositionMapList(bean);
        String pose2dKey = RobotTaskChcheConstant.ROBOT_INFORMATION_POSE_BAT_SOC + bean.getMacAddress();
        Object object = redisUtil.get(pose2dKey);
        RobotStatusDataPoseDTO robotStatusDataPoseDTO = JSONUtil.toBean((String) object, RobotStatusDataPoseDTO.class);

        if (ObjectUtil.isNotEmpty(object) && ObjectUtil.isNotEmpty(robotStatusDataPoseDTO)) {
            bean.setElectricity(robotStatusDataPoseDTO.getBat_soc());
            bean.setFloor(robotStatusDataPoseDTO.getFloor());
            bean.setArea(robotStatusDataPoseDTO.getArea());
        }
        return bean;
    }

    public void setPositionMapList(RobotInformationRespVO bean) {
        Set<Long> ids = bean.getFloorAreaJson();
        if (ObjectUtil.isNotEmpty(ids)) {
            List<PositionMapDO> list = positionMapMapper.selectByIds(ids);
            List<RobotPositionMapVO> targetList = BeanUtil.copyToList(list, RobotPositionMapVO.class);
            bean.setPositionMapList(targetList);
        }
    }

    @Override
    public PageResult<RobotInformationPageRespVO> getInformationPage(RobotInformationPageReqVO pageReqVO) {
        PageResult<RobotInformationDO> pageResult = informationMapper.selectPage(pageReqVO);

        PageResult<RobotInformationPageRespVO> dataPage = new PageResult<>();
        dataPage.setTotal(pageResult.getTotal());
        List<RobotInformationDO> list = pageResult.getList();
        List<RobotInformationPageRespVO> targetList = BeanUtil.copyToList(list, RobotInformationPageRespVO.class);

        for (RobotInformationPageRespVO v : targetList) {
            setMsgAndRobotStatus(v,pageReqVO.getRobotStatisticsType());
        }

        if (ObjectUtil.isNotEmpty(pageReqVO.getRobotStatisticsType())) {
            List<RobotInformationPageRespVO> resultList = new ArrayList<>();
            for (RobotInformationPageRespVO v : targetList) {
                if (RobotStatisticsTypeEnum.STANDBY.getType().equals(pageReqVO.getRobotStatisticsType())
                        && v.getRobotTaskStatus().equals(0)) {
                    resultList.add(v);
                    continue;
                }

                if (RobotStatisticsTypeEnum.INTASK.getType().equals(pageReqVO.getRobotStatisticsType())
                        && v.getRobotTaskStatus().equals(1)) {
                    resultList.add(v);
                    continue;
                }

                if (RobotStatisticsTypeEnum.CHARGE.getType().equals(pageReqVO.getRobotStatisticsType())
                        && v.getRobotTaskStatus().equals(2)) {
                    resultList.add(v);
                    continue;
                }

                if (RobotStatisticsTypeEnum.DOLOCK.getType().equals(pageReqVO.getRobotStatisticsType())
                        && v.getRobotTaskModel().equals(0)) {
                    resultList.add(v);
                    continue;
                }

                if (RobotStatisticsTypeEnum.OFFLINE.getType().equals(pageReqVO.getRobotStatisticsType())
                        && v.getOnlineStatus().equals(0)) {
                    resultList.add(v);
                    continue;
                }

                if (RobotStatisticsTypeEnum.FAULT.getType().equals(pageReqVO.getRobotStatisticsType())
                        && v.getRobotEssenceStatus().equals(2)) {
                    resultList.add(v);
                }
            }
            dataPage.setList(resultList);
        } else {
            dataPage.setList(targetList);
        }

        return dataPage;
    }

    /**
     * 设置状态和信息
     *
     * @param v
     */
    private void setMsgAndRobotStatus(RobotInformationPageRespVO v,String robotStatisticsType) {
        String pose2dKey = RobotTaskChcheConstant.ROBOT_INFORMATION_POSE_BAT_SOC + v.getMacAddress();
        Object object = redisUtil.get(pose2dKey);
        RobotStatusDataPoseDTO robotStatusDataPoseDTO = JSONUtil.toBean((String) object, RobotStatusDataPoseDTO.class);

        String robotDoingActionKey = RobotTaskChcheConstant.ROBOT_QUERY_DOING_ACTION + v.getMacAddress();
        Object action = redisUtil.get(robotDoingActionKey);

        if (ObjectUtil.isNotEmpty(object) && ObjectUtil.isNotEmpty(robotStatusDataPoseDTO)) {
            v.setElectricity(robotStatusDataPoseDTO.getBat_soc());
            v.setFloor(robotStatusDataPoseDTO.getFloor());
            v.setArea(robotStatusDataPoseDTO.getArea());
            v.setOnlineStatus(1);
        }

        v.setRobotEssenceStatus(v.getRobotTaskModel());
        //设置异常
        String errorLevelKey = RobotTaskChcheConstant.ROBOT_ERROR_LEVEL + v.getMacAddress();
        Object errorLevel = redisUtil.get(errorLevelKey);
        if (ObjectUtil.isNotEmpty(errorLevel)) {
            v.setRobotEssenceStatus(2);
        }

        if (ObjectUtil.isNotEmpty(errorLevel)) {
            v.setRobotCodeLevel(Integer.valueOf(errorLevel.toString()).intValue());
        }

        String errorMsgKey = RobotTaskChcheConstant.ROBOT_ERROR_MSG + v.getMacAddress();
        Object errorMsg = redisUtil.get(errorMsgKey);
        if (ObjectUtil.isNotEmpty(errorMsg)) {
            v.setRobotCodeMsg(errorMsg.toString());
        }

        if (RobotStatusEnum.STAND_BY.getType().equals(v.getRobotStatus())) {
            //待命中
            v.setRobotTaskStatus(0);
            v.setMsg("车辆正在待命中");
        } else if (RobotStatusEnum.CHARGE.getType().equals(v.getRobotStatus())) {
            //充电中
            v.setRobotTaskStatus(2);
            v.setMsg("车辆正在充电");
        } else {
            //任务中
            v.setRobotTaskStatus(1);
            if (RobotStatusEnum.DOING.getType().equals(v.getRobotStatus()) && ObjectUtil.isNotEmpty(action)) {
                v.setRobotStatus(1);
                v.setMsg(String.valueOf(action));
            }
        }

    }

    /**
     * 统计车辆待命/任务中/离线
     *
     * @return
     */
    @Override
    public RobotInformationStatisticsVO statisticsInformation() {
        RobotInformationStatisticsVO info = new RobotInformationStatisticsVO();

        RobotInformationDO query = new RobotInformationDO();
        List<RobotInformationDO> existRobot = informationMapper.queryAllByLimit(query);
        if (ObjectUtil.isEmpty(existRobot)) {
            return info;
        }
        Integer standby = 0;
        Integer inTask = 0;
        Integer doLock = 0;
        Integer offline = 0;
        Integer charge = 0;
        Integer fault = 0;
        //0：待命、1：任务中、2：锁定、3：离线、4:充电中、5:故障
        for (RobotInformationDO v : existRobot) {
            String pose2dKey = RobotTaskChcheConstant.ROBOT_INFORMATION_POSE_BAT_SOC + v.getMacAddress();
            Object object = redisUtil.get(pose2dKey);

            if (ObjectUtil.isEmpty(object)) {
                //离线
                offline++;
            }

            if (RobotTaskModelEnum.REJECTION.getType().equals(v.getRobotTaskModel())) {
                //锁定
                doLock++;
            }
            //设置异常
            String errorLevelKey = RobotTaskChcheConstant.ROBOT_ERROR_LEVEL + v.getMacAddress();
            Object errorLevel = redisUtil.get(errorLevelKey);
            if (ObjectUtil.isNotEmpty(errorLevel) && Integer.valueOf(errorLevel.toString()).intValue() >= 3) {
                fault++;
            }

            if (RobotStatusEnum.STAND_BY.getType().equals(v.getRobotStatus())) {
                //待命中
                standby++;
            } else if (RobotStatusEnum.CHARGE.getType().equals(v.getRobotStatus())) {
                //充电中
                charge++;
            } else {
                //任务中
                inTask++;
            }
        }
        info.setStandby(standby);
        info.setInTask(inTask);
        info.setDoLock(doLock);
        info.setOffline(offline);
        info.setCharge(charge);
        info.setFault(fault);
        info.setTotal(existRobot.size());

        return info;
    }

    @Override
    public List<RobotInfoStatusVO> getAGVListStatusInfo() {
        List<RobotInfoStatusVO> list = new ArrayList<>();
        // -- 先从缓存中 获取AGV状态信息 - 如果没有的话 - 再从数据库中获取 - 并且缓存到redis中
        List<RobotInformationDO> robotInformationDO = informationMapper.selectList();
        if (robotInformationDO == null) {
            return list;
        }
        for (RobotInformationDO informationDO : robotInformationDO) {
            RobotInfoStatusVO vo = new RobotInfoStatusVO();
            //0：待命、1：任务中、2：锁定、3：离线、4:充电中、5:故障
            String pose2dKey = RobotTaskChcheConstant.ROBOT_INFORMATION_POSE_BAT_SOC + informationDO.getMacAddress();
            Object object = redisUtil.get(pose2dKey);
            if (ObjectUtil.isEmpty(object)) {
                //离线
                vo.setOffline(1);
            }
            if (RobotTaskModelEnum.REJECTION.getType().equals(informationDO.getRobotTaskModel())) {
                //锁定
                vo.setDoLock(1);
            }
            //设置异常
            String errorLevelKey = RobotTaskChcheConstant.ROBOT_ERROR_LEVEL + informationDO.getMacAddress();
            Object errorLevel = redisUtil.get(errorLevelKey);
            if (ObjectUtil.isNotEmpty(errorLevel) && Integer.parseInt(errorLevel.toString()) >= 3) {
                vo.setFault(1);
            }
            if (RobotStatusEnum.STAND_BY.getType().equals(informationDO.getRobotStatus())) {
                //待命中
                vo.setStandby(1);
            } else if (RobotStatusEnum.CHARGE.getType().equals(informationDO.getRobotStatus())) {
                //充电中
                vo.setCharge(1);
            } else {
                //任务中
                vo.setInTask(1);
            }
            list.add(vo);
        }
        return list;
    }

    /**
     * @return
     */
    @Override
    public List<RobotInformationDO> getCanUseRobot() {
        RobotInformationDO query = new RobotInformationDO();
        query.setRobotTaskModel(RobotTaskModelEnum.NORMAL.getType());
        return informationMapper.queryAllByLimit(query);
    }

    @Override
    public List<RobotInformationDO> getAllRobot() {
        RobotInformationDO query = new RobotInformationDO();
        return informationMapper.queryAllByLimit(query);
    }

    /**
     * 根据mac查询机器人编号
     *
     * @param mac
     * @return
     */
    @Override
    public String getRobotNoByMac(String mac) {
        if (ObjectUtil.isEmpty(mac)) {
            return "";
        }
        String robotNo = (String) redisUtil.get(RobotTaskChcheConstant.ROBOT_GET_ROBOTNO_BY_MAC + mac);

        if (ObjectUtil.isNotEmpty(robotNo)) {
            return robotNo;
        }

        RobotInformationDO robotInformationDO = informationMapper.selectOne(new LambdaQueryWrapper<RobotInformationDO>()
                .eq(RobotInformationDO::getMacAddress, mac)
                .last("limit 1"));
        if (ObjectUtil.isNotEmpty(robotInformationDO)) {
            redisUtil.set(RobotTaskChcheConstant.ROBOT_GET_ROBOTNO_BY_MAC + mac, robotInformationDO.getRobotNo());
            return robotInformationDO.getRobotNo();
        }
        return "";
    }

    @Override
    public void test(RobotStatusDTO robotStatusDataDTO) {
        TenantContextHolder.setTenantId(1L);
        if (ObjectUtil.isEmpty(robotStatusDataDTO) || ObjectUtil.isEmpty(robotStatusDataDTO.getMac())) {
            return;
        }
        // -- 通过mac 地址获取车辆信息 - (并且加入到缓存中)
        Map<String, RobotInformationVO> robotInformationVOS = this.getAllRobotByRedis();
        RobotInformationVO robotInformationVO = robotInformationVOS.get(robotStatusDataDTO.getMac());
        if (robotInformationVO == null) {
            robotInformationVO = this.getRobotByRedis(robotStatusDataDTO.getMac());
        }

        RobotStatusDataPoseDTO robotStatusDataPoseDTO = new RobotStatusDataPoseDTO();
        robotStatusDataPoseDTO.setX(robotStatusDataDTO.getData().getPose2d().getX());
        robotStatusDataPoseDTO.setY(robotStatusDataDTO.getData().getPose2d().getY());
        robotStatusDataPoseDTO.setYaw(robotStatusDataDTO.getData().getPose2d().getYaw());
        robotStatusDataPoseDTO.setFloor(robotStatusDataDTO.getData().getFloor_zone().getFloor());
        robotStatusDataPoseDTO.setArea(robotStatusDataDTO.getData().getFloor_zone().getArea());
        robotStatusDataPoseDTO.setBat_soc(robotStatusDataDTO.getData().getPose2d().getBat_soc());

        robotInformationVO.setPose2d(robotStatusDataPoseDTO);

        // 模拟请求
        processor.handleRequest(robotStatusDataDTO.getData().getFloor_zone().getFloor() + "_" + robotStatusDataDTO.getData().getFloor_zone().getArea(),
                robotStatusDataDTO.getMac(), JSONUtil.toJsonStr(robotInformationVO));

    }

    @Override
    public Map<String, RobotInformationVO> getAllRobotByRedis() {
        Object o = redisUtil.get(key);
        if (ObjectUtil.isNotEmpty(o)) {
            // 化作列表
            List<RobotInformationVO> list = JSONUtil.toList(o.toString(), RobotInformationVO.class);
            return list.stream().collect(Collectors.toMap(RobotInformationVO::getMacAddress, Function.identity()));
        } else {
            List<RobotInformationDO> allRobotInfoList = this.list();
            List<RobotInformationVO> list = BeanUtil.copyToList(allRobotInfoList, RobotInformationVO.class);
            redisUtil.set(key, JSONUtil.toJsonStr(list));
            return list.stream().collect(Collectors.toMap(RobotInformationVO::getMacAddress, Function.identity()));
        }
    }

    @Override
    public RobotInformationVO getRobotByRedis(String macAddress) {
        // todo -- 如果说有一个设备的mac 一直都不在库里 会出现缓存穿透的问题 - 后续如果出现这种情况的话 - 考虑将null值也存入到缓存中
        RobotInformationDO item = this.getOne(new LambdaQueryWrapper<RobotInformationDO>().eq(RobotInformationDO::getMacAddress, macAddress));
        // -- 将获取到的存入到redis中
        if (ObjectUtil.isNotEmpty(item)) {
            RobotInformationVO vo = BeanUtil.toBean(item, RobotInformationVO.class);
            List<RobotInformationVO> list = new ArrayList<>();
            Object o = redisUtil.get(key);
            if (ObjectUtil.isNotEmpty(o)) {
                list = JSONUtil.toList(o.toString(), RobotInformationVO.class);
            }
            list.add(vo);
            redisUtil.set(key, JSONUtil.toJsonStr(list));
            return vo;
        }
        return new RobotInformationVO();
    }

    @Override
    public String getMacByRobotNo(String robotNo) {
        if (ObjectUtil.isEmpty(robotNo)) {
            return "";
        }
        String mac = (String) redisUtil.get(RobotTaskChcheConstant.ROBOT_GET_MAC_BY_NO + robotNo);

        if (ObjectUtil.isNotEmpty(mac)) {
            return mac;
        }

        RobotInformationDO robotInformationDO = informationMapper.selectOne(new LambdaQueryWrapper<RobotInformationDO>()
                .eq(RobotInformationDO::getRobotNo, robotNo)
                .last("limit 1"));
        if (ObjectUtil.isNotEmpty(robotInformationDO)) {
            redisUtil.set(RobotTaskChcheConstant.ROBOT_GET_MAC_BY_NO + robotNo, robotInformationDO.getMacAddress());
            return robotInformationDO.getMacAddress();
        }
        return "";
    }

    @Override
    public List<RobotInformationRespVO> getAllRobotAndErrorMsg() {
        RobotInformationDO query = new RobotInformationDO();
        List<RobotInformationDO> robotInformationDOS = informationMapper.queryAllByLimit(query);
        if (ObjectUtil.isEmpty(robotInformationDOS)) {
            return new ArrayList<>();
        }

        List<RobotInformationRespVO> list = BeanUtils.toBean(robotInformationDOS, RobotInformationRespVO.class);
        for (RobotInformationRespVO v : list) {
            String errorMsgKey = RobotTaskChcheConstant.ROBOT_ERROR_MSG + v.getMacAddress();
            Object errorMsg = redisUtil.get(errorMsgKey);
            if (ObjectUtil.isNotEmpty(errorMsg)) {
                v.setErrorMsg(String.valueOf(errorMsg));
            }
        }

        return list;
    }

    @Override
    public List<RobotInformationDO> getListByMapId(Long mapId) {
        return informationMapper.getListByMapId(mapId);
    }
}
