/*
*  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.AsnDetail;
import com.youchain.businessdata.domain.Task;
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;
import java.util.Map;

/**
* @website https://eladmin.vip
* @author houjianlan
* @date 2023-08-16
**/
public interface TaskRepository extends JpaRepository<Task, Long>, JpaSpecificationExecutor<Task> {
    @Query(value = "SELECT u.id FROM data_task u WHERE u.asn_detail_id=?1", nativeQuery = true)
    public List<Map<String,String>> getAsnTask(Long id);

    /**
     * 根据出库单明细查询未完成的Task集合
     * @param id
     * @return
     */
    @Query(value = "SELECT * FROM data_task t WHERE t.pick_detail_id=?1 and t.plan_qty>t.move_qty ", nativeQuery = true)
    List<Task> getPickNotAllTask(Long id);

    /**
     * 根据出库单明细查询已完成的Task集合
     * @param id
     * @return
     */
    @Query(value = "SELECT * FROM data_task t WHERE t.pick_detail_id=?1 and t.plan_qty=t.move_qty ", nativeQuery = true)
    List<Task> getPickAllTask(Long id);


    /**
     * 根据AGV ID查询对应Task集合
     * @param id
     * @return
     */
    @Query(value = "SELECT * FROM data_task t WHERE t.agv_task_id=?1 ", nativeQuery = true)
    List<Task> getAgvTaskList(Integer id);

    /**
     * 根据 收货明细序号 进行删除数据
     */
    @Query(value = "SELECT Id FROM data_task t WHERE t.asn_detail_id =?1", nativeQuery = true)
    Long getAsnDetailIdToId(Long id);

    /**
     * 查询前后同入库的数据
     * @param time 2023-12-09 00:00:00
     * @param status FINISH 完成  CANCEL 已取消  待执行
     * @return
     */
    @Query(value = "select * from data_task a where a.dst_point_code like 'MA1151-QHL%' and  a.create_time > :time and a.task_status =:status", nativeQuery = true)
    List<Task> queryInQHData(String time, String status);

    /**
     * 查询前后桶入库全部数据
     * @param time 2023-12-09 00:00:00
     * @return
     */
    @Query(value = "select * from data_task a where a.dst_point_code like 'MA1151-QHL%' and  a.create_time > :time", nativeQuery = true)
    List<Task> queryInQHALLData(String time);

    List<Task> findByTaskStatus(String inspect);
}