/*
*  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.repository;
import com.youchain.businessdata.domain.AgvTask;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* @website https://eladmin.vip
* @author baobinglin
* @date 2023-08-18
**/
public interface AgvTaskRepository extends JpaRepository<AgvTask, Integer>, JpaSpecificationExecutor<AgvTask> {

    /**
     * 查询滚筒出库任务执行中的任务
     * @return
     */
    @Query(value = "SELECT * FROM data_agv_task  where type='PICK' and job_type='ROLLER_MOVE' and  status not in ('FINISH','CANCEL') and stock_code=?1  ", nativeQuery = true)
    AgvTask findBycontainerCode(String containerCode);

    /**
     * 查询前后桶正在执行的任务
     * @return
     */
    @Query(value = "select * FROM data_agv_task a  where a.`status` in ('ATCALL','UP_CONTAINER','OPEN') AND a.end_slot_code like 'MA1151-QH%'" +
            " UNION select * FROM data_agv_task a  where a.`status` in ('ATCALL','UP_CONTAINER','OPEN') AND a.start_slot_code like 'MA1151-QH%' ", nativeQuery = true)
    List<AgvTask> queryAgvTaskPerformedQH();

    /**
     * @param status-状态
     * @param type-类型
     * @param be_scan-是否扫描
     * @return
     */
    @Query(value = "SELECT * FROM data_agv_task  where 1=1 and  if((?1 !='' or ?1 is not null) ,stock_code=?1,1=1)  and status=?2 and type=?3 and be_scan=?4 and job_type=?5 order by update_time ", nativeQuery = true)
    List<AgvTask> queryByAgvTask(String stockCode, String status, String type, int be_scan,String jobType);

    /**
     * 查询正在执行的开始点位有没有这个点位的任务
     * @param startPoint 开始点位
     * @return
     */
    @Query(value = "select * FROM data_agv_task a  where a.`status` in ('ATCALL','UP_CONTAINER') AND a.start_slot_code = ?1", nativeQuery = true)
    List<AgvTask> findStartTask(String startPoint);

    /**
     * 查询正在执行的结束点位有没有这个点位的任务
     * @param endPoint
     * @return
     */
    @Query(value = "select * FROM data_agv_task a  where a.`status` in ('ATCALL','UP_CONTAINER') AND a.end_slot_code = ?1", nativeQuery = true)
    List<AgvTask> findEndTask(String endPoint);
    /** 查询重复任务*/
    @Query(value = "select a FROM AgvTask a  where a.status in ('ATCALL','ATWORK','OPEN') AND a.startSlotCode = ?1 and a.endSlotCode=?2")
    List<AgvTask> findByRptAgvTask(String startPoint,String endPoint);

    /**
     * 根据线边分组
     * @return
     */
    @Query(value = "select line_slot_code from data_agv_task where type='PICK' and `status`='OPEN' and line_slot_code is not null GROUP BY line_slot_code order by line_slot_code ", nativeQuery = true)
    List<Object> queryGroupAgvTask();

    /**
     * 查询线边任务待执行的任务
     * @return
     */
    @Query(value = "select * from data_agv_task where  line_slot_code=?1 and type='PICK' and `status`='OPEN' and line_slot_code is not null  ORDER BY create_time ", nativeQuery = true)
    List<AgvTask> queryLineAgvTask(String line_slot_code);
}
