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

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mqtt.api.common.CommonApi;
import cn.iocoder.yudao.module.system.config.poperties.ZnConfigConstant;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotInformationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskDetailAddVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotTaskSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.cycle.TaskCycleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.housearea.HouseAreaDO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselane.WareHouseLaneDO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseLocationDO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseTakePointDO;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapItemDO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotInformationDO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDetailDO;
import cn.iocoder.yudao.module.system.dal.mysql.cycle.TaskCycleMapper;
import cn.iocoder.yudao.module.system.dal.mysql.housearea.HouseAreaMapper;
import cn.iocoder.yudao.module.system.dal.mysql.houselane.WareHouseLaneMapper;
import cn.iocoder.yudao.module.system.dal.mysql.houselocation.WareHouseLocationMapper;
import cn.iocoder.yudao.module.system.dal.mysql.information.DeviceInformationMapper;
import cn.iocoder.yudao.module.system.dal.mysql.log.RobotTaskDetailActionLogMapper;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotChargeLogMapper;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotInformationMapper;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotTaskDetailMapper;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotTaskMapper;
import cn.iocoder.yudao.module.system.enums.common.ZeroOneEnum;
import cn.iocoder.yudao.module.system.enums.redis.RobotCacheLockEnum;
import cn.iocoder.yudao.module.system.enums.robot.LocationEnableEnum;
import cn.iocoder.yudao.module.system.enums.robot.LocationLockEnum;
import cn.iocoder.yudao.module.system.enums.robot.LocationUseStatusEnum;
import cn.iocoder.yudao.module.system.enums.robot.MoveAllEnum;
import cn.iocoder.yudao.module.system.enums.robot.ReleaseTakeEnum;
import cn.iocoder.yudao.module.system.enums.robot.RobotTaskTypeEnum;
import cn.iocoder.yudao.module.system.service.houselocation.WareHouseTakePointService;
import cn.iocoder.yudao.module.system.service.information.DeviceInformationService;
import cn.iocoder.yudao.module.system.service.log.RobotTaskDetailActionLogService;
import cn.iocoder.yudao.module.system.service.log.UserOperationLogService;
import cn.iocoder.yudao.module.system.service.positionmap.PositionMapItemService;
import cn.iocoder.yudao.module.system.service.robot.RobotInformationService;
import cn.iocoder.yudao.module.system.service.robot.RobotTaskDetailService;
import cn.iocoder.yudao.module.system.service.statistics.RobotWorkingHoursStatisticsService;
import cn.iocoder.yudao.module.system.util.redis.RedisUtil;
import cn.iocoder.yudao.module.system.util.redis.RedissonUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

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

@Slf4j
@Service
@Validated
public class RobotSimulationServiceImpl implements RobotSimulationService {

    @Resource
    private RobotTaskMapper taskMapper;

    @Resource
    private WareHouseLocationMapper locationMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private RobotTaskDetailMapper taskDetailMapper;

    @Resource
    private RobotTaskDetailService robotTaskDetailService;

    @Resource
    private RobotInformationMapper informationMapper;

    @Resource
    private WareHouseLaneMapper houseLaneMapper;

    @Resource
    private HouseAreaMapper houseAreaMapper;

    @Resource
    private PositionMapItemService positionMapItemService;

    @Autowired
    private ZnConfigConstant znConfigConstant;

    @Resource
    private WareHouseTakePointService houseTakePointService;

    @Override
    public void createTask(RobotTaskSaveReqVO createReqVO) {

        //设置任务号
        if (ObjectUtil.isEmpty(createReqVO.getTaskNo())) {
            String incrementByKey = redisUtil.getIncrementByKey(RobotCacheLockEnum.TASK_NO.getKey());
            createReqVO.setTaskNo(znConfigConstant.getTaskNo() + DateUtils.getYearMonthDay() + incrementByKey);
        } else {
            List<RobotTaskDO> taskDOS = taskMapper.selectList(new LambdaQueryWrapperX<RobotTaskDO>()
                    .eq(RobotTaskDO::getTaskNo, createReqVO.getTaskNo()));
            if (ObjectUtil.isNotEmpty(taskDOS)) {
                throw exception(TASK_CHECK_EXIST_NO);
            }
        }

        log.info("111111创建一个不校验的任务 :{}", createReqVO.getTaskNo());

        RobotTaskDO task = BeanUtils.toBean(createReqVO, RobotTaskDO.class);
        task.setRemainingCycleNumber(task.getCycleNumber());
        task.setStartTime(null);
        task.setEndTime(null);
        taskMapper.insert(task);

        List<Long> laneIds = new ArrayList<>();
        List<Long> areaIds = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(task.getSkuInfo()) && znConfigConstant.getTask().getCheckSkuInfo()) {
            List<WareHouseLaneDO> wareHouseLaneDOS = houseLaneMapper.selectList(new LambdaQueryWrapper<WareHouseLaneDO>()
                    .eq(WareHouseLaneDO::getLaneMsg, task.getSkuInfo()));
            if (ObjectUtil.isNotEmpty(wareHouseLaneDOS)) {
                laneIds = wareHouseLaneDOS.stream().map(WareHouseLaneDO::getId).collect(Collectors.toList());
            }
            List<HouseAreaDO> houseAreaDOS = houseAreaMapper.selectList(new LambdaQueryWrapper<HouseAreaDO>()
                    .eq(HouseAreaDO::getSkuInfo, task.getSkuInfo()));
            if (ObjectUtil.isNotEmpty(houseAreaDOS)) {
                areaIds = houseAreaDOS.stream().map(HouseAreaDO::getId).collect(Collectors.toList());
            }
        }

        //查找库位
        List<Long> locationIds = new ArrayList<>();
        if (MoveAllEnum.NO.getType().equals(createReqVO.getDoMoveAll())) {
            locationIds = setSingleLocationIdNo(createReqVO.getTaskDetailList(), task, laneIds, areaIds);
        } else {
            locationIds = setAllLocationIdNo(createReqVO, task.getId(), laneIds, areaIds, task.getSkuInfo());
        }

        // 插入
        createReqVO.getTaskDetailList()
                .forEach(taskDetail -> {
                    taskDetail.setRobotTaskId(task.getId());
                });
        robotTaskDetailService.insertRobotDetailList(createReqVO.getTaskDetailList());

        List<List<Long>> fromPartition = Lists.partition(locationIds, 100);
        fromPartition.forEach(list -> {
            locationMapper.updateLocationLockList(list, task.getId(), LocationLockEnum.NO.getType());
        });

    }

    @Transactional(rollbackFor = Exception.class)
    public List<Long> setSingleLocationIdNo(List<RobotTaskDetailAddVO> taskDetailList, RobotTaskDO task,
                                            List<Long> laneIds, List<Long> areaIds) {
        //取货中的map_item_id， 此集合不能执行放货的任务
        List<Long> takeMapItemIds = taskDetailMapper.selectTakeMapItemIds();
        if (ObjectUtil.isEmpty(takeMapItemIds)) {
            takeMapItemIds = new ArrayList<>();
        }
        //放货中的map_item_id， 此集合不能执行取货的任务
        List<Long> releaseMapItemIds = taskDetailMapper.selectReleaseMapItemIds();
        if (ObjectUtil.isEmpty(releaseMapItemIds)) {
            releaseMapItemIds = new ArrayList<>();
        }

        List<Long> locationIds = new ArrayList<>();
        Set<Long> chooseLocationIds = new HashSet<>();

        for (RobotTaskDetailAddVO robotTaskVo : taskDetailList) {
            robotTaskVo.setRobotTaskId(task.getId());
            robotTaskVo.setPriority(task.getPriority());
            RobotTaskTypeEnum robotTaskType = RobotTaskTypeEnum.getRobotTaskType(robotTaskVo.getTaskType());
            switch (robotTaskType) {
                case TAKE_RELEASE:
                    doTakeRelease(robotTaskVo, locationIds, takeMapItemIds, releaseMapItemIds, laneIds, areaIds, task.getSkuInfo(), chooseLocationIds);
                    break;
                case PARK:
                    doPark(robotTaskVo, locationIds, takeMapItemIds, releaseMapItemIds);
                    break;
                case CHARGE:
                    doCharge(robotTaskVo, takeMapItemIds, releaseMapItemIds);
                    break;
                case MOVE:
                    doMove(robotTaskVo, locationIds, takeMapItemIds, releaseMapItemIds);
                    break;
                case TAKE:
                    doTake(robotTaskVo, locationIds, takeMapItemIds, releaseMapItemIds, chooseLocationIds);
                    break;
                case RELEASE:
                    doRelease(robotTaskVo, locationIds, takeMapItemIds, releaseMapItemIds, laneIds, areaIds, task.getSkuInfo(), chooseLocationIds);
                    break;
                case SCAN:
                    //暂无此功能
                    doScan(robotTaskVo, locationIds, takeMapItemIds, releaseMapItemIds);
                    break;
                case DETECTING_TRAYS:
                    //暂无此功能
                    doDetectingTrays(robotTaskVo, locationIds, takeMapItemIds, releaseMapItemIds);
                    break;
                case MOVE_TO_POINT:
                    doMoveToPoint(robotTaskVo);
                    break;
                default:
                    log.error("任务类型不存在 ");
                    throw exception0(TASK_CREATE_FAIL.getCode(), "任务类型不存在");
            }
        }
        return locationIds;
    }


    public void doTakeRelease(RobotTaskDetailAddVO robotTaskVo, List<Long> locationIds,
                              List<Long> takeMapItemIds, List<Long> releaseMapItemIds,
                              List<Long> laneIds, List<Long> areaIds, String skuInfo, Set<Long> chooseLocationIds) {


        //校验放货库位
        setToLocation(robotTaskVo, null, locationIds, takeMapItemIds, releaseMapItemIds, laneIds, areaIds, skuInfo, chooseLocationIds);
        locationIds.add(robotTaskVo.getToLocationId());

        //校验取货库位
        setFromLocation(robotTaskVo, null, locationIds, takeMapItemIds, releaseMapItemIds, chooseLocationIds);
        locationIds.add(robotTaskVo.getFromLocationId());

        //验证取货库位是否存在未完成的任务
        Set<Long> set = new HashSet<>();
        set.add(robotTaskVo.getFromLocationId());
    }

    /**
     * 移动到指定点位
     *
     * @param robotTaskVo
     */
    private void doMoveToPoint(RobotTaskDetailAddVO robotTaskVo) {
        PositionMapItemDO positionMapItem = positionMapItemService.getPositionMapItem(robotTaskVo.getReleaseId());
        robotTaskVo.setToLocationNo(String.valueOf(positionMapItem.getSortNum()));
    }

    /**
     * 检测托盘类型
     *
     * @param robotTaskVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void doDetectingTrays(RobotTaskDetailAddVO robotTaskVo, List<Long> locationIds, List<Long> takeMapItemIds,
                                 List<Long> releaseMapItemIds) {
        /*setToLocation(robotTaskVo,getMapIdsByRobotNo(robotTaskVo.getRobotNo()),new ArrayList<>());
        locationIds.add(robotTaskVo.getToLocationId());*/
    }

    /**
     * 扫描码
     *
     * @param robotTaskVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void doScan(RobotTaskDetailAddVO robotTaskVo, List<Long> locationIds, List<Long> takeMapItemIds,
                       List<Long> releaseMapItemIds) {
        /*setToLocation(robotTaskVo,getMapIdsByRobotNo(robotTaskVo.getRobotNo()),new ArrayList<>());
        locationIds.add(robotTaskVo.getToLocationId());*/
    }

    /**
     * 仅放货
     *
     * @param robotTaskVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void doRelease(RobotTaskDetailAddVO robotTaskVo, List<Long> locationIds, List<Long> takeMapItemIds,
                          List<Long> releaseMapItemIds, List<Long> laneIds, List<Long> areaIds, String skuInfo,
                          Set<Long> chooseLocationIds) {
        setToLocation(robotTaskVo, null, locationIds, takeMapItemIds,
                releaseMapItemIds, laneIds, areaIds, skuInfo, chooseLocationIds);
        locationIds.add(robotTaskVo.getToLocationId());
    }

    public void setFromLocation(RobotTaskDetailAddVO robotTaskVo, Set<Long> mapIds, List<Long> locationIds,
                                List<Long> takeMapItemIds, List<Long> releaseMapItemIds, Set<Long> chooseLocationIds) {
        if (ReleaseTakeEnum.TO_LOCATION.getType().equals(robotTaskVo.getTakeType())) {
            WareHouseLocationDO query = WareHouseLocationDO.builder().id(robotTaskVo.getTakeId()).build();
            WareHouseLocationDO locationDO = locationMapper.queryAllByLimit(query);
            robotTaskVo.setFromLocationNo(locationDO.getLocationNo());
            robotTaskVo.setFromLocationId(robotTaskVo.getTakeId());
            robotTaskVo.setFromLocationStorey(locationDO.getLocationStorey());
            robotTaskVo.setFromLaneId(locationDO.getLaneId());
            robotTaskVo.setFromLocationNumber(locationDO.getLocationNumber());
            robotTaskVo.setFromMapItemId(locationDO.getMapItemId());
            takeMapItemIds.add(locationDO.getMapItemId());
        } else if (ReleaseTakeEnum.TO_POINT.getType().equals(robotTaskVo.getTakeType())) {
            WareHouseTakePointDO houseTakePoint = houseTakePointService.getHouseTakePoint(robotTaskVo.getTakeId());
            robotTaskVo.setFromLocationNo(houseTakePoint.getPointName());
            robotTaskVo.setFromLocationId(robotTaskVo.getTakeId());
            robotTaskVo.setFromLocationStorey(houseTakePoint.getLocationStorey());
            robotTaskVo.setFromLaneId(houseTakePoint.getLaneId());
            robotTaskVo.setFromLocationNumber(houseTakePoint.getLocationNumber());
            robotTaskVo.setFromMapItemId(houseTakePoint.getPositionMapItemId());
        } else {
            WareHouseLocationDO wareHouseLocationDO =
                    locationMapper.selectByTypeAndId(LocationUseStatusEnum.YES.getType(), robotTaskVo.getTakeType(),
                            robotTaskVo.getTakeId(), locationIds, null, releaseMapItemIds, new ArrayList<>(), new ArrayList<>());
            robotTaskVo.setFromLocationNo(wareHouseLocationDO.getLocationNo());
            robotTaskVo.setFromLocationId(wareHouseLocationDO.getId());
            robotTaskVo.setFromLocationStorey(wareHouseLocationDO.getLocationStorey());
            robotTaskVo.setFromLaneId(wareHouseLocationDO.getLaneId());
            robotTaskVo.setFromLocationNumber(wareHouseLocationDO.getLocationNumber());
            robotTaskVo.setFromMapItemId(wareHouseLocationDO.getMapItemId());
            takeMapItemIds.add(wareHouseLocationDO.getMapItemId());
        }
    }

    /**
     * 仅取货
     *
     * @param robotTaskVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void doTake(RobotTaskDetailAddVO robotTaskVo, List<Long> locationIds, List<Long> takeMapItemIds,
                       List<Long> releaseMapItemIds, Set<Long> chooseLocationIds) {

        setFromLocation(robotTaskVo, null, locationIds, takeMapItemIds, releaseMapItemIds, chooseLocationIds);
        locationIds.add(robotTaskVo.getFromLocationId());
    }

    public Set<Long> getMapIdsByRobotNo(String robotNo) {
        if (ObjectUtil.isNotEmpty(robotNo)) {
            RobotInformationPageReqVO pageReqVO = new RobotInformationPageReqVO();
            pageReqVO.setRobotNo(robotNo);
            return informationMapper.selectPage(pageReqVO).getList()
                    .stream()
                    .findFirst()
                    .map(RobotInformationDO::getFloorAreaJson)
                    .orElse(new HashSet<>());
        }
        return new HashSet<>();
    }

    /**
     * 移动
     *
     * @param robotTaskVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void doMove(RobotTaskDetailAddVO robotTaskVo, List<Long> locationIds, List<Long> takeMapItemIds,
                       List<Long> releaseMapItemIds) {
        setToLocation(robotTaskVo, null, locationIds, takeMapItemIds, releaseMapItemIds,
                null, null, null, null);
        locationIds.add(robotTaskVo.getToLocationId());
    }

    /**
     * 充电
     *
     * @param robotTaskVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void doCharge(RobotTaskDetailAddVO robotTaskVo, List<Long> takeMapItemIds,
                         List<Long> releaseMapItemIds) {

    }

    /**
     * 停车
     *
     * @param robotTaskVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void doPark(RobotTaskDetailAddVO robotTaskVo, List<Long> locationIds, List<Long> takeMapItemIds,
                       List<Long> releaseMapItemIds) {
        // todo 停车后续停到等待点
        /*setToLocation(robotTaskVo,getMapIdsByRobotNo(robotTaskVo.getRobotNo()),locationIds);
        locationIds.add(robotTaskVo.getToLocationId());*/
    }


    /**
     * 设置放货和取货的id
     *
     * @param createReqVO
     * @param taskId
     */
    @Transactional(rollbackFor = Exception.class)
    public List<Long> setAllLocationIdNo(RobotTaskSaveReqVO createReqVO, Long taskId,
                                         List<Long> laneIds, List<Long> areaIds, String skuInfo) {
        List<RobotTaskDetailAddVO> taskDetailList = createReqVO.getTaskDetailList();

        List<RobotTaskDetailAddVO> newTaskDetailList = new ArrayList<>();
        List<Long> mapItemIds = new ArrayList<>();

        //被此次任务锁定的空库位id
        List<Long> toLocationIds = new ArrayList<>();
        //被此次任务锁定的有货库位id
        List<Long> fromLocationIds = new ArrayList<>();
        for (RobotTaskDetailAddVO robotTaskVo : taskDetailList) {
            Set<Long> mapIds = new HashSet<>();

            //查找有货且非锁定的库位
            WareHouseLocationDO query = null;
            if (ReleaseTakeEnum.TO_LANE.getType().equals(robotTaskVo.getTakeType())) {
                query = WareHouseLocationDO.builder()
                        .laneId(robotTaskVo.getTakeId())
                        .build();
            } else if (ReleaseTakeEnum.TO_AREA.getType().equals(robotTaskVo.getTakeType())) {
                query = WareHouseLocationDO.builder()
                        .areaId(robotTaskVo.getTakeId())
                        .build();
            }

            List<WareHouseLocationDO> stockList = locationMapper.selectLocations(query, toLocationIds, mapIds,
                    new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
            if (ObjectUtil.isEmpty(stockList)) {
                log.error("取货线库/区域为空或者已锁定或者机器人取放货区域受限制 :{}", robotTaskVo.toString());
                throw exception0(TASK_CREATE_FAIL.getCode(), "取货线库/区域为空或者已锁定或者机器人取放货区域受限制");
            }

            //查找空库位
            WareHouseLocationDO releaseQuery = null;
            if (ReleaseTakeEnum.TO_LANE.getType().equals(robotTaskVo.getReleaseType())) {
                releaseQuery = WareHouseLocationDO.builder()
                        .laneId(robotTaskVo.getReleaseId())
                        .build();
            } else if (ReleaseTakeEnum.TO_AREA.getType().equals(robotTaskVo.getReleaseType())) {
                releaseQuery = WareHouseLocationDO.builder()
                        .areaId(robotTaskVo.getReleaseId())
                        .build();
            }

            List<WareHouseLocationDO> releaseStockList = locationMapper.selectLocations(releaseQuery, toLocationIds, mapIds,
                    laneIds, areaIds, mapItemIds);
            if (ObjectUtil.isEmpty(releaseStockList) || releaseStockList.size() < stockList.size()) {
                log.error("放货线库/区域库位数量不足或者机器人取放货区域受限制 :{}", robotTaskVo.toString());
                throw exception0(TASK_CREATE_FAIL.getCode(), "放货线库/区域库位数量不足或者机器人取放货区域受限制");
            }
            Collections.reverse(releaseStockList);
            //赋值数据
            List<RobotTaskDetailAddVO> addTaskDetailList = getTaskDatail(stockList, releaseStockList, toLocationIds,
                    robotTaskVo, taskId, fromLocationIds);
            newTaskDetailList.addAll(addTaskDetailList);

        }
        createReqVO.setTaskDetailList(newTaskDetailList);

        if (ObjectUtil.isNotEmpty(fromLocationIds)) {
            toLocationIds.addAll(fromLocationIds);
        }
        return toLocationIds;
    }

    private List<RobotTaskDetailAddVO> getTaskDatail(List<WareHouseLocationDO> stockList,
                                                     List<WareHouseLocationDO> releaseStockList,
                                                     List<Long> toLocationIds, RobotTaskDetailAddVO robotTaskVo,
                                                     Long taskId, List<Long> fromLocationIds) {
        List<RobotTaskDetailAddVO> addTaskDetailList = new ArrayList<>();
        for (int i = 0; i < stockList.size(); i++) {
            RobotTaskDetailAddVO robotTaskDetailAddVo = new RobotTaskDetailAddVO();
            robotTaskDetailAddVo.setTaskType(robotTaskVo.getTaskType());
            robotTaskDetailAddVo.setReleaseType(robotTaskVo.getReleaseType());
            robotTaskDetailAddVo.setTakeType(robotTaskVo.getTakeType());
            robotTaskDetailAddVo.setTakeId(robotTaskVo.getTakeId());
            robotTaskDetailAddVo.setReleaseId(robotTaskVo.getReleaseId());
            robotTaskDetailAddVo.setRobotNo(robotTaskVo.getRobotNo());
            robotTaskDetailAddVo.setFromLocationNo(stockList.get(i).getLocationNo());
            robotTaskDetailAddVo.setFromLocationId(stockList.get(i).getId());
            robotTaskDetailAddVo.setToLocationNo(releaseStockList.get(i).getLocationNo());
            robotTaskDetailAddVo.setToLocationId(releaseStockList.get(i).getId());
            robotTaskDetailAddVo.setFromLaneId(stockList.get(i).getLaneId());
            robotTaskDetailAddVo.setToLaneId(releaseStockList.get(i).getLaneId());
            robotTaskDetailAddVo.setFromLocationNumber(stockList.get(i).getLocationNumber());
            robotTaskDetailAddVo.setFromMapItemId(stockList.get(i).getMapItemId());
            robotTaskDetailAddVo.setToMapItemId(releaseStockList.get(i).getMapItemId());
            robotTaskDetailAddVo.setRobotTaskId(taskId);
            robotTaskDetailAddVo.setPriority(robotTaskVo.getPriority());
            robotTaskDetailAddVo.setFromLocationStorey(stockList.get(i).getLocationStorey());
            robotTaskDetailAddVo.setToLocationNumber(releaseStockList.get(i).getLocationNumber());
            robotTaskDetailAddVo.setToLocationStorey(releaseStockList.get(i).getLocationStorey());
            addTaskDetailList.add(robotTaskDetailAddVo);

            toLocationIds.add(releaseStockList.get(i).getId());
            fromLocationIds.add(stockList.get(i).getId());
        }
        return addTaskDetailList;
    }

    public void setToLocation(RobotTaskDetailAddVO robotTaskVo, Set<Long> mapIds, List<Long> locationIds,
                              List<Long> takeMapItemIds, List<Long> releaseMapItemIds,
                              List<Long> laneIds, List<Long> areaIds, String skuInfo, Set<Long> chooseLocationIds) {
        if (ReleaseTakeEnum.TO_LOCATION.getType().equals(robotTaskVo.getReleaseType())) {
            WareHouseLocationDO query = WareHouseLocationDO.builder().id(robotTaskVo.getReleaseId()).build();
            WareHouseLocationDO locationDO = locationMapper.queryAllByLimit(query);
            robotTaskVo.setToLocationNo(locationDO.getLocationNo());
            robotTaskVo.setToLocationId(robotTaskVo.getReleaseId());
            robotTaskVo.setToLocationStorey(locationDO.getLocationStorey());
            robotTaskVo.setToLaneId(locationDO.getLaneId());
            robotTaskVo.setToLocationNumber(locationDO.getLocationNumber());
            robotTaskVo.setToMapItemId(locationDO.getMapItemId());
            releaseMapItemIds.add(locationDO.getMapItemId());
        } else if (ReleaseTakeEnum.TO_POINT.getType().equals(robotTaskVo.getReleaseType())) {
            WareHouseTakePointDO houseTakePoint = houseTakePointService.getHouseTakePoint(robotTaskVo.getReleaseId());
            robotTaskVo.setToLocationNo(houseTakePoint.getPointName());
            robotTaskVo.setToLocationId(robotTaskVo.getTakeId());
            robotTaskVo.setToLocationStorey(houseTakePoint.getLocationStorey());
            robotTaskVo.setToLaneId(houseTakePoint.getLaneId());
            robotTaskVo.setToLocationNumber(houseTakePoint.getLocationNumber());
            robotTaskVo.setToMapItemId(houseTakePoint.getPositionMapItemId());
        } else {
            WareHouseLocationDO wareHouseLocationDO =
                    locationMapper.selectByTypeAndId(LocationUseStatusEnum.NO.getType(), robotTaskVo.getReleaseType()
                            , robotTaskVo.getReleaseId(), locationIds, null, takeMapItemIds, laneIds, areaIds);
            robotTaskVo.setToLocationNo(wareHouseLocationDO.getLocationNo());
            robotTaskVo.setToLocationId(wareHouseLocationDO.getId());
            robotTaskVo.setToLocationStorey(wareHouseLocationDO.getLocationStorey());
            robotTaskVo.setToLaneId(wareHouseLocationDO.getLaneId());
            robotTaskVo.setToLocationNumber(wareHouseLocationDO.getLocationNumber());
            robotTaskVo.setToMapItemId(wareHouseLocationDO.getMapItemId());
            releaseMapItemIds.add(wareHouseLocationDO.getMapItemId());
        }
    }


}
