/*
*  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 io.lettuce.core.dynamic.annotation.Param;
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);

    /***
     * 未拣货完成数据
     * @param id
     * @return
     */
    @Query(value = "select t.* from data_task t \n" +
            "left join data_pick_detail pd on pd.id=t.pick_detail_id \n" +
            "where pd.pick_ticket_id=?1 and t.plan_qty>t.move_qty ", nativeQuery = true)
    List<Task> getNotPickTask(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(Long id);

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

    /**
     * 查询分配记录
     */
    @Query(value = "SELECT * FROM `data_task` dt where dt.bill_code = :billCode", nativeQuery = true)
    List<Task> queryByBillCode(@Param("billCode")String billCode);
}
