/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.youchain.businessdata.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.youchain.Netty.NettyUtils;
import com.youchain.basicdata.domain.Box;
import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.repository.BoxRepository;
import com.youchain.basicdata.repository.PointRepository;
import com.youchain.basicdata.repository.StockRepository;
import com.youchain.basicdata.service.BoxService;
import com.youchain.basicdata.service.ItemService;
import com.youchain.basicdata.service.PointService;
import com.youchain.basicdata.service.impl.BoxServiceImpl;
import com.youchain.businessdata.domain.*;
import com.youchain.businessdata.repository.AsnDetailRepository;
import com.youchain.businessdata.repository.TaskRepository;
import com.youchain.businessdata.service.*;
import com.youchain.exception.handler.ApiResult;
import com.youchain.modules.quartz.utils.TimeNumberUtils;
import com.youchain.modules.system.service.DictService;
import com.youchain.modules.system.service.dto.DictDto;
import com.youchain.modules.system.service.dto.DictQueryCriteria;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.repository.AgvTaskRepository;
import com.youchain.businessdata.service.dto.AgvTaskDto;
import com.youchain.businessdata.service.dto.AgvTaskQueryCriteria;
import com.youchain.businessdata.service.mapstruct.AgvTaskMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;


/**
 * @author baobinglin
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-08-18
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class AgvTaskServiceImpl implements AgvTaskService {

    private final AgvTaskRepository agvTaskRepository;
    private final AgvTaskMapper agvTaskMapper;
    private final TaskRepository taskRepository;
    private final AsnDetailService asnDetailService;
    private final InventoryService inventoryService;
    private final AsnService asnService;
    private final ItemService itemService;
    private final InventoryLogService inventoryLogService;
    private final TaskService taskService;
    private final PointService pointService;
    private final PointRepository pointRepository;
    private final AsnDetailRepository asnDetailRepository;
    private final StockRepository stockRepository;
    private final DictService dictService;
    private final EntityManager entityMapper;

    @Override
    public Map<String, Object> queryAll(AgvTaskQueryCriteria criteria, Pageable pageable) {
        Page<AgvTask> page = agvTaskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(agvTaskMapper::toDto));
    }

    @Override
    public List<AgvTaskDto> queryAll(AgvTaskQueryCriteria criteria) {
        return agvTaskMapper.toDto(agvTaskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public AgvTaskDto findById(Integer id) {
        AgvTask agvTask = agvTaskRepository.findById(id).orElseGet(AgvTask::new);
        ValidationUtil.isNull(agvTask.getId(), "AgvTask", "id", id);
        return agvTaskMapper.toDto(agvTask);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgvTaskDto create(AgvTask resources) {
        return agvTaskMapper.toDto(agvTaskRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgvTask resources) {
        AgvTask agvTask = agvTaskRepository.findById(resources.getId()).orElseGet(AgvTask::new);
        ValidationUtil.isNull(agvTask.getId(), "AgvTask", "id", resources.getId());
        agvTask.copy(resources);
        agvTaskRepository.save(agvTask);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            agvTaskRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AgvTaskDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AgvTaskDto agvTask : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("容器码", agvTask.getStockCode());
            map.put("容器类型", agvTask.getStockTypeCode());
            map.put("起点位置", agvTask.getStartSlotCode());
            map.put("终点位置", agvTask.getEndSlotCode());
            map.put("容器当前位置", agvTask.getSlotCode());
            map.put("任务状态", agvTask.getStatus());
            map.put("任务类型", agvTask.getType());
            map.put("下游任务号", agvTask.getJobId());
            map.put("工作类型", agvTask.getJobType());
            map.put("优先级", agvTask.getJobPriority());
            map.put("强制优先级", agvTask.getJobPriorityType());
            map.put("任务组", agvTask.getJobForce());
            map.put("任务组执行顺序", agvTask.getJobForceAsc());
            map.put("返回报文", agvTask.getJobMessage());
            map.put("变动时间", agvTask.getJobMessageTime());
            map.put("创建时间", agvTask.getCreateTime());
            map.put("开始时间", agvTask.getStartTime());
            map.put("完成时间", agvTask.getEndTime());
            map.put("创建人", agvTask.getCreateBy());
            map.put("修改人", agvTask.getUpdateBy());
            map.put("修改时间", agvTask.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * @param agvTask
     * @return
     */
    @Override
    public synchronized String sendAgvTaskImpl(AgvTask agvTask) {
        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        String api="";
        if (agvTask.getType().equals(BizStatus.AGV)) {
            //搬运任务
            jsonObject.put("requestId", agvTask.getId());
            jsonObject.put("missionCode", agvTask.getId());
            jsonObject.put("priority", agvTask.getJobPriority());
            jsonObject.put("containerModeCode", agvTask.getStockTypeCode());
            jsonObject.put("startPosition", agvTask.getStartSlotCode());
            jsonObject.put("endPosition", agvTask.getEndSlotCode());
            api=UrlApi.submitMission;
        }
        if (agvTask.getType().equals(BizStatus.AGV_R)){
            jsonObject.put("requestId", agvTask.getId());
            jsonObject.put("containerModeCode", agvTask.getStockTypeCode());
//            jsonObject.put("containerCode", agvTask.getStockTypeCode());
            jsonObject.put("position", agvTask.getEndSlotCode());
            api=UrlApi.containerIn;
        }
        if (agvTask.getType().equals(BizStatus.AGV_C)){
            jsonObject.put("requestId", agvTask.getId());
            jsonObject.put("position", agvTask.getEndSlotCode());
            api=UrlApi.containerOut;
        }
        String resultJson = HttpPostUtil.sendPostReq(api, jsonObject.toString());//返回ResponseJson*/
        JSONObject resulObject = JSON.parseObject(resultJson);
        String code = resulObject.getString("code") == null ? "" : resulObject.getString("code");
        if (code.equals("0")) {
            agvTask.setStatus(BizStatus.ATCALL);
            agvTask.setJobMessage(resultJson);
            agvTask.setReqMessage(jsonObject.toString());
            agvTask.setStartTime(new Timestamp(new Date().getTime()));
            update(agvTask);
        } else {
            agvTask.setJobMessage(resultJson);
            agvTask.setReqMessage(jsonObject.toString());
            update(agvTask);
        }
        return resultJson;
    }

    @Override
    public synchronized String sendAgvTaskLXImpl(List<AgvTask> agvTasks) {
        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        String agvCode = TimeNumberUtils.getXJTaskCode();
        Map<String, Object> objMap = new LinkedHashMap<>();
        objMap.put("orgId", agvCode);//库存组织 ID(或工厂代码，供应商代码)
        objMap.put("requestId", agvCode);//请求 id
        objMap.put("missionCode", agvCode);//任务编号
        objMap.put("missionType", "PICKER_MOVE");//任务类型 ：料箱
        objMap.put("viewBoardType", "");//上视识别类型:
        objMap.put("robotType", "PICKER");//机器人功能类型
        JSONArray robotModels = new JSONArray();
        objMap.put("robotModels", robotModels);//机器人具体型号
        JSONArray robotIds = new JSONArray();
        objMap.put("robotIds", robotIds);//机器人编号
        objMap.put("priority", "1");//数值越小，优先级越高，默认是 1
        objMap.put("templateCode", "");//作业流程模板编号
        objMap.put("lockRobotAfterFinish", false);//是否需要流程结束后机器人保持任务锁定状态
        objMap.put("unlockRobotId", "");//解锁当前小车的在上个任务的锁定状态
        objMap.put("unlockMissionCode", "");//当前小车的上一个任务
        objMap.put("idleNode", "");//作业流程完成后，指定机器人停放区域/点

        JSONArray missionDataArray = new JSONArray();
        for (int i = 0; i < agvTasks.size(); i++) {
            AgvTask agvTask = agvTasks.get(i);
            JSONObject missionDataObj = new JSONObject(new LinkedHashMap<>());
            Map<String, Object> missionDataMap = new LinkedHashMap<>();
            missionDataMap.put("sequence", (i + 1));//序号
            missionDataMap.put("binCode", agvTask.getStockCode());//料箱号
            missionDataMap.put("startPosition", "");//料箱所在的起始点位
            missionDataMap.put("startSlotCode", agvTask.getStartSlotCode());//料箱所在的起始槽位
            missionDataMap.put("takeActionConfirm", false);//取料箱时之前是否需确认
            missionDataMap.put("endPosition", "");//料箱所在的目标点位
            missionDataMap.put("endSlotCode", agvTask.getEndSlotCode());//料箱所在的目标槽位
            missionDataMap.put("putActionConfirm", false);//放料箱时之前是否需要确认
            missionDataObj.putAll(missionDataMap);
            missionDataArray.add(missionDataObj);
        }

        objMap.put("missionData", missionDataArray);
        jsonObject.putAll(objMap);
        System.out.println(jsonObject.toString());
        String resultJson = HttpPostUtil.sendPostReq(UrlApi.submitMission, jsonObject.toString());
        JSONObject resulObject = JSON.parseObject(resultJson);
        String code = resulObject.getString("code") == null ? "" : resulObject.getString("code");
        if (code.equals("0")) {
            for (int i = 0; i < agvTasks.size(); i++) {
                AgvTask agvTask = agvTasks.get(i);
                agvTask.setJobForce(agvCode);//任务组
                agvTask.setJobForceAsc((i + 1));
                agvTask.setStatus(BizStatus.ATCALL);
                agvTask.setJobMessage(resultJson);
                agvTask.setReqMessage(jsonObject.toString());
                agvTask.setStartTime(new Timestamp(new Date().getTime()));
                update(agvTask);
            }
        } else {
            for (int i = 0; i < agvTasks.size(); i++) {
                AgvTask agvTask = agvTasks.get(i);
                agvTask.setJobMessage(resultJson);
                agvTask.setReqMessage(jsonObject.toString());
                update(agvTask);
            }
        }
        return resultJson;
    }

    @Override
    public synchronized String sendAgvTaskGTImpl(List<AgvTask> agvTasks1, List<AgvTask> agvTasks2) {
        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        String agvCode = TimeNumberUtils.getGTTaskCode();
        Map<String, Object> objMap = new LinkedHashMap<>();
        objMap.put("orgId", agvCode);//库存组织 ID(或工厂代码，供应商代码)
        objMap.put("requestId", agvCode);//请求 id
        objMap.put("missionCode", agvCode);//任务编号
        objMap.put("missionType", "ROLLER_MOVE");//任务类型 ：滚筒
        objMap.put("viewBoardType", "");//上视识别类型:
        JSONArray missionDataArray = new JSONArray();
        for (int i = 0; i < agvTasks1.size(); i++) {
            AgvTask agvTask = agvTasks1.get(i);
            JSONObject missionDataObj = new JSONObject(new LinkedHashMap<>());
            Map<String, Object> missionDataMap = new LinkedHashMap<>();
            missionDataMap.put("sequence", (i + 1));//序号
            missionDataMap.put("binCode", agvTask.getStockCode());//料箱号
            missionDataMap.put("startDeviceCode", agvTask.getStartSlotCode());//料箱所在的起始槽位
            missionDataMap.put("endDeviceCode", agvTask.getEndSlotCode());//料箱所在的目标槽位
            missionDataMap.put("rollerLevel", 2);//层
            missionDataMap.put("rollerColumn", (i + 1));//列
            missionDataObj.putAll(missionDataMap);
            missionDataArray.add(missionDataObj);
        }
        int sum = agvTasks1.size();
        if (agvTasks2.size() > 0) {
            for (int i = 0; i < agvTasks2.size(); i++) {
                AgvTask agvTask = agvTasks2.get(i);
                JSONObject missionDataObj = new JSONObject(new LinkedHashMap<>());
                Map<String, Object> missionDataMap = new LinkedHashMap<>();
                missionDataMap.put("sequence", (sum + (i + 1)));//序号
                missionDataMap.put("binCode", agvTask.getStockCode());//料箱号
                missionDataMap.put("startDeviceCode", agvTask.getStartSlotCode());//料箱所在的起始槽位
                missionDataMap.put("endDeviceCode", agvTask.getEndSlotCode());//料箱所在的目标槽位
                missionDataMap.put("rollerLevel", 1);//层
                missionDataMap.put("rollerColumn", (i + 1));//列
                missionDataObj.putAll(missionDataMap);
                missionDataArray.add(missionDataObj);
            }
        }


        objMap.put("missionData", missionDataArray);
        jsonObject.putAll(objMap);
        System.out.println(jsonObject.toString());
        String resultJson = HttpPostUtil.sendPostReq(UrlApi.submitMission, jsonObject.toString());//返回ResponseJson*/
        JSONObject resulObject = JSON.parseObject(resultJson);
        String code = resulObject.getString("code") == null ? "" : resulObject.getString("code");
        if (code.equals("0")) {
            for (int i = 0; i < agvTasks1.size(); i++) {
                AgvTask agvTask = agvTasks1.get(i);
                agvTask.setJobForce(agvCode);//任务组
                agvTask.setJobForceAsc((i + 1));
                agvTask.setStatus(BizStatus.ATCALL);
                agvTask.setJobMessage(resultJson);
                agvTask.setReqMessage(jsonObject.toString());
                agvTask.setStartTime(new Timestamp(new Date().getTime()));
                update(agvTask);
            }
            for (int i = 0; i < agvTasks2.size(); i++) {
                AgvTask agvTask = agvTasks2.get(i);
                agvTask.setJobForce(agvCode);//任务组
                agvTask.setJobForceAsc((i + 1));
                agvTask.setStatus(BizStatus.ATCALL);
                agvTask.setJobMessage(resultJson);
                agvTask.setReqMessage(jsonObject.toString());
                agvTask.setStartTime(new Timestamp(new Date().getTime()));
                update(agvTask);
            }
        } else {
            for (int i = 0; i < agvTasks1.size(); i++) {
                AgvTask agvTask = agvTasks1.get(i);
                agvTask.setJobMessage(resultJson);
                agvTask.setReqMessage(jsonObject.toString());
                update(agvTask);
            }
            for (int i = 0; i < agvTasks2.size(); i++) {
                AgvTask agvTask = agvTasks2.get(i);
                agvTask.setJobMessage(resultJson);
                agvTask.setReqMessage(jsonObject.toString());
                update(agvTask);
            }
        }
        return resultJson;
    }

    @Override
    public synchronized String containerIn(String containerCode, String position, String enterOrientation) {
        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        jsonObject.put("requestId", String.valueOf(new Date().getTime()));//请求 id
        jsonObject.put("containerType", "");//容器类型
        jsonObject.put("containerCode", containerCode);//容器编号
        jsonObject.put("position", position);//容器当前位置
        jsonObject.put("enterOrientation", enterOrientation);//角度
        return jsonObject.toString();
    }

    @Override
    public synchronized String containerOut(String containerCode) {
        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        jsonObject.put("requestId", String.valueOf(new Date().getTime()));//请求 id
        jsonObject.put("containerType", "");//容器类型
        jsonObject.put("containerCode", containerCode);//容器编号
        jsonObject.put("position", "");//容器当前位置
        return jsonObject.toJSONString();
    }

    @Override
    public String containerQuery(String containerCode) {
        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        jsonObject.put("nodeCode", "");//点位编码
        jsonObject.put("containerModelCode", "");//容器模型编码
        jsonObject.put("containerCode", containerCode);//容器编号
        jsonObject.put("areaCode", "");//区域编码
        jsonObject.put("emptyFullStatus", 2);//容器的空满状态 空 0  满 1  全部 2
        return jsonObject.toJSONString();
    }

    @Override
    public int QueryCount(String taskCode) {

        String jsonObject = "{\"taskCode\":\"" + taskCode + "\"}";//查询出库缓存输送线上满料箱总数
        String resultJson = HttpPostUtil.sendPostReq(UrlApi.QueryCount, jsonObject);
        JSONObject json = JSON.parseObject(resultJson);
        String queryCount = json.getString("queryCount");

        return Integer.parseInt(queryCount);
    }

    @Override
    public int querypoint(String pointCode) {
        String jsonObject = "{\"pointCode\":\"" + pointCode + "\"}";//查询出库缓存输送线上满料箱总数
        String resultJson = HttpPostUtil.sendPostReq(UrlApi.querypoint, jsonObject);
        JSONObject resulObject = JSON.parseObject(resultJson);
        String code = resulObject.getString("status") == null ? "" : resulObject.getString("status");
        if (code.equals("200")) {
            String data1 = resulObject.getString("data") == null ? "" : resulObject.getString("data");
            JSONArray jsonArray = JSON.parseArray(data1);
            int count = 0;
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject dataObject = JSON.parseObject(jsonArray.get(i).toString());
                String pointStatus = dataObject.getString("pointStatus") == null ? "" : dataObject.getString("pointStatus");
                count += Integer.parseInt(pointStatus);
            }
            return count;
        }
        return 0;
    }

    @Override
    public void getConveyorState(String conveyorId) {
        /*  String resultJson= HttpPostUtil.sendPostReq(UrlApi.agv_url, jsonObject.toString());//返回ResponseJson*/
        String resultJson = "{\n" +
                "\"success\":true,\n" +
                "\"code\":200,\n" +
                "\"message\":\"\",\n" +
                "\"conveyorId\":\"1\",\n" +
                "\"state\":\"1\"\n" +
                "}";
        JSONObject resulObject = JSON.parseObject(resultJson);
        String code = resulObject.getString("code") == null ? "" : resulObject.getString("code");
        if (code.equals("200")) {
            /**
             * state
             * 1：允许取货
             * 2：允许放货
             * 3、不允许取货
             * 4、不允许放货
             */
            String state = resulObject.getString("state") == null ? "" : resulObject.getString("state");
            if (state.equals("1")) {

            } else if (state.equals("2")) {

            } else if (state.equals("3")) {

            } else if (state.equals("4")) {

            }
        }

    }

    public AgvTask toEntity(AgvTaskDto agvTaskDto) {
        return agvTaskMapper.toEntity(agvTaskDto);
    }

    @Override
    public void agvTaskCallback(String taskCode, String status) {
        // agvTask搬运任务
        AgvTask agvTask=agvTaskRepository.getById(Integer.valueOf(taskCode));
        // task库存任务
//        List<Task> tasks=taskRepository.getAgvTaskList(agvTask.getId(),BizStatus.ASN);
        List<Map<String,Object>> taskMapList=taskRepository.findByAgvTask(agvTask.getId(),"ASN",1l);
        Point startPoint=null;
        Point endPoint=null;
        if (status.equals("1")){
            // 开始执行
            agvTask.setStatus(BizStatus.ATWORK);
        }else if (status.equals("2")){
            // 取料完成
            startPoint=pointRepository.findByCode(agvTask.getStartSlotCode());
            startPoint.setStatus(BaseStatus.FREE);
            startPoint.setStorageType(null);
            pointRepository.save(startPoint);
            agvTask.setStartTime(new Timestamp((new Date()).getTime()));
        } else if (status.equals("3")) {
            // 放料完成
            endPoint=pointRepository.findByCode(agvTask.getEndSlotCode());
            endPoint.setStatus(BaseStatus.FREE);
            pointRepository.save(endPoint);
            agvTask.setStatus(BizStatus.FINISH);
            agvTask.setEndTime(new Timestamp((new Date()).getTime()));
            
            if (taskMapList.size()>0){
                // 生成库存
                Inventory inventory = inventoryService.asnAddInventory(taskMapList);
            }
        } else if (status.equals("4")) {
            // 取消任务
        }
        agvTaskRepository.save(agvTask);
    }

    @Override
    public AgvTask findBycontainerCode(String containerCode) {
        return agvTaskRepository.findBycontainerCode(containerCode);
    }

    @Override
    public List<AgvTask> queryByAgvTask(String stockCode, String status, String type, int be_scan, String jobType) {
        return agvTaskRepository.queryByAgvTask(stockCode, status, type, be_scan, jobType);
    }

    @Override
    public List<AgvTask> queryByAgvTask(String stockCode, String type, String jobType) {
        String hql = " from AgvTask agv" +
                " where  agv.stockCode='" + stockCode + "' " +
                " and agv.type='" + type + "' " +
                " and agv.jobType='" + jobType + "' " +
                " and agv.status in ('OPEN','ATCALL') ";
        Query query = entityMapper.createQuery(hql);
        List<AgvTask> agvTaskList = query.getResultList();
        return agvTaskList;
    }

    @Override
    public boolean findByendSlotCode(String endSlotCode,String type,String jobType) {
        String hql = " from AgvTask agv " +
                " where  agv.endSlotCode='" + endSlotCode + "' " +
                " and agv.type='"+type+"' " +
                " and agv.jobType='"+jobType+"' " +
                " and agv.status in ('ATCALL','UP_CONTAINER') ";

        Query query = entityMapper.createQuery(hql);
        List<AgvTask> agvTaskList = query.getResultList();
        if (agvTaskList.size() > 0) {
            return false;
        }
        return true;
    }

    @Override
    public AgvTask findByjobCode(String jobForce, String containerCode) {
        String hql = " from AgvTask agv where  agv.jobForce='" + jobForce + "' and agv.stockCode='" + containerCode + "' ";
        AgvTask agvTask = null;
        Query query = entityMapper.createQuery(hql);
        List<AgvTask> agvTaskList = query.getResultList();
        if (agvTaskList.size() > 0) {
            agvTask = agvTaskList.get(0);
        }
        return agvTask;
    }

    @Override
    public List<AgvTask> findByjobCode(String jobForce) {
        String hql = " from AgvTask agv where  agv.jobForce='" + jobForce + "' ";
        Query query = entityMapper.createQuery(hql);
        List<AgvTask> agvTaskList = query.getResultList();
        return agvTaskList;
    }

    @Override
    public AgvTask findByStockCode(String containerCode, String type, String jobType) {
        String hql = " from AgvTask agv where  agv.stockCode='" + containerCode + "' and agv.type='" + type + "' and agv.jobType='" + jobType + "'  order by agv.updateTime desc ";
        AgvTask agvTask = null;
        Query query = entityMapper.createQuery(hql);
        List<AgvTask> agvTaskList = query.getResultList();
        if (agvTaskList.size() > 0) {
            agvTask = agvTaskList.get(0);
        }
        return agvTask;
    }

    @Override
    public List<AgvTask> findByAgvList(String type, String jobType) {
        String hql = " from AgvTask agv where  agv.type='" + type + "' "
                + " and agv.jobType='" + jobType + "' "
                + " and agv.status not in ('OPEN','FINISH','CANCEL') ";
        Query query = entityMapper.createQuery(hql);
        List<AgvTask> agvTaskList = query.getResultList();
        return agvTaskList;
    }

    @Override
    public List<AgvTask> findById(List<Integer> ids) {
        String agvTaskIds = StringUtils.strip(ids.toString(), "[]");
        String hql = " from AgvTask agv where  agv.id in (" + agvTaskIds + ") ";
        Query query = entityMapper.createQuery(hql);
        List<AgvTask> agvTaskList = query.getResultList();
        return agvTaskList;
    }

    @Override
    public void pointStockRk(String storageType, Long id) {
        Point point=pointRepository.getById(id);
        System.out.println("调用容器入场接口");
        point.setStorageType(storageType);
        pointRepository.save(point);
    }

    @Override
    public AgvTask addAgvTask(String type, String stockTypeCode, String startSlotCode, String endSlotCode, String status, String jobType) {
        AgvTask agvTask = new AgvTask();
        List<AgvTask> agvTaskList=agvTaskRepository.findByRptAgvTask(startSlotCode,endSlotCode);
        if (agvTaskList.size()>0){
            return agvTaskList.get(0);
        }
        agvTask.setType(type);
        agvTask.setStockTypeCode(stockTypeCode);
        agvTask.setStartSlotCode(startSlotCode);
        agvTask.setEndSlotCode(endSlotCode);
        agvTask.setStatus(status);
        agvTask.setJobType(jobType);
        AgvTask agv=agvTaskRepository.save(agvTask);
        // 生成任务占用库位
        Point startPoint=pointRepository.findByCode(startSlotCode);
        Point endPoint=pointRepository.findByCode(endSlotCode);
        if (startPoint.getType().equals("STORAGE")){
            startPoint.setStatus(BaseStatus.USED);
            pointRepository.save(startPoint);
        }
        if (startPoint.getType().equals("STORAGE")){
            endPoint.setStatus(BaseStatus.USED);
            endPoint.setStorageType(startPoint.getStorageType());
            pointRepository.save(endPoint);
        }

        return agv;
    }

    @Override
    public String addAgvTask(Long id, String pointEndCode) {
        Point startPoint=pointRepository.getById(id);
        Point endPoint=pointRepository.findByCode(pointEndCode);
        if (endPoint!=null){
            if (!startPoint.getStatus().equals(BaseStatus.FREE)){
                return startPoint.getCode()+":被任务占用";
            }
            if (!endPoint.getStatus().equals(BaseStatus.FREE)){
                return endPoint.getCode()+":被任务占用";
            }
            AgvTask agvTask = addAgvTask(BizStatus.AGV, startPoint.getStorageType(), startPoint.getCode(),endPoint.getCode() , BizStatus.OPEN, BizStatus.ASN);
        }else {
           return pointEndCode+":目标点错误";
        }
        return null;
    }

    @Override
    @Transactional
    public ApiResult addAgvAsn() {
        //半成品入库
        List<Map<String,Object>> asnDetailSumQty=asnDetailRepository.findByAsn(BizStatus.RECEIVING,"SM_RK");
        List<Map<String,Object>> taskSumQty=taskRepository.findBySumPlanQty(BizStatus.RECEIVING,1,"SMGS");
        if (taskSumQty.size()>1){
            //异常
            List<Task> taskList=taskRepository.findByItemType(BizStatus.RECEIVING,1,"SMGS");
            for (int i = 0; i < taskList.size(); i++) {
                Task task=taskList.get(i);
                task.setTaskStatus(BizStatus.UNUSUAL);
                taskRepository.save(task);
            }
            return ApiResult.success(500, "任务异常:单次任务中有多个物料", "");
        } else if (taskSumQty.size()==1) {
            //半成品入库
            Map<String,Object> objectMap=taskSumQty.get(0);
            Long itemId=Long.valueOf(objectMap.get("itemId")+"");
            Double taskQty=Double.valueOf(objectMap.get("taskQty")+"");
            for (int i = 0; i < asnDetailSumQty.size(); i++) {
                Map<String,Object> sumQty=asnDetailSumQty.get(i);
                if (Long.valueOf(sumQty.get("itemId")+"")==itemId){
                    if (taskQty>Double.valueOf(sumQty.get("qty")+"")){
                        return ApiResult.success(500, "任务异常:RFID读取到的物料数量大于入库单中的数量", "");
                    }
                }
            }
            Item item=itemService.findByItemId(itemId);
            if (taskQty>=item.getExtendD1()){
                //入库
                //查询入库库位
               List<Point> pointList = pointRepository.findByAreaCode("SMGS");
               if (pointList.size()>0){
                   Point point=pointList.get(0);
                   List<Task> taskList=taskRepository.findByItemType(BizStatus.RECEIVING,1,"SMGS");
                   for (int i = 0; i < taskList.size(); i++) {
                       Task task=taskList.get(i);
                       //跟新Task任务
                       ApiResult success = taskService.getAsnTask(point, task);
                       if (success != null) return success;
                       AgvTask agvTask=addAgvTask("叉车搬运", "半成品托盘", task.getSrcPoint().getCode(), point.getCode(), BizStatus.OPEN, "ASN2");
                       task.setAgvTask(agvTask);
                       taskRepository.save(task);
                   }
               }else {
                   return ApiResult.success(500, "任务异常:存储库存不足", "");
               }
            }else {
                //数量不够
                return ApiResult.success(201, "检测到数量不足一托！！", "");
            }
        }else {
            //未查询半成品入库到数据
            return ApiResult.success(202, "未检测到数据", "");
        }

        return ApiResult.success(200, "操作成功", "");
    }


    public void sendStockRk(String storageType, String pointCode) {
        System.out.println();

        /*AgvTask agvTask = new AgvTask();
        agvTask.setEndSlotCode(pointCode);
        agvTask.setType(BizStatus.);
        this.type=type;
        this.stockCode=stockCode;
        this.startSlotCode=startSlotCode;
        this.endSlotCode=endSlotCode;
        this.status=status;
        this.jobType=jobType;*/
    }

}
