/*
 *  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.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;

/**
 * @author houjianlan
 * @website https://eladmin.vip
 * @date 2023-08-16
 **/
public interface TaskRepository extends JpaRepository<Task, Long>, JpaSpecificationExecutor<Task> {

    /**
     * 根据agv任务ID查询Task任务列表
     *
     * @param agvTaskId agvId
     */
    @Query(" from Task t where t.agvTask.id =:agvTaskId ")
    List<Task> findTaskByAgvTask(Long agvTaskId);

    /**
     * 根据叫料agv任务ID查询Task任务列表
     *
     * @param callAgvTaskId agvId
     */
    @Query(" from Task t where t.callAgvTaskId =:callAgvTaskId ")
    List<Task> findTaskByCallAgvTask(Long callAgvTaskId);


    /**
     * 根据出库单查询未完成的Task集合
     *
     * @param pickId 出库单ID
     */
    @Query("from Task t where t.planQty > t.moveQty and t.agvTask.id is null")
    List<Task> findByPickNotAllTask();

    /**
     * 根据出库单查询已完成的Task集合
     *
     * @param pickId 出库单ID
     */
    @Query("from Task t where t.pickDetail.pick.id =:pickId and t.planQty - t.moveQty=0 and  t.callAgvTaskId is null ")
    List<Task> findByPickAllTask(Long pickId);

    /**
     * 根据出库单明细查询未完成的Task集合
     *
     * @param pickDetailId 出库明细ID
     */
    @Query("from Task t where t.pickDetail.id =:pickDetailId and t.planQty > t.moveQty")
    List<Task> findByPickDetailNotAllTask(Long pickDetailId);

    /**
     * 根据出库单明细查询已完成的Task集合
     *
     * @param pickDetailId 出库明细ID
     */
    @Query("from Task t where t.pickDetail.id =:pickDetailId and t.planQty = t.moveQty")
    List<Task> findByPickDetailAllTask(Long pickDetailId);

    /**
     * 查询任务列表
     *
     * @param taskType     任务类型
     * @param taskStatus   任务状态
     * @param srcStockCode 原容器号
     */
    @Query(value = " (SELECT * FROM data_task t WHERE t.src_stock_code=:srcStockCode   AND t.task_type=:taskType  AND t.task_status =:taskStatus) UNION ALL (SELECT * FROM data_task t WHERE t.dst_point_code=:srcStockCode   AND t.task_type=:taskType  AND t.task_status =:taskStatus) ", nativeQuery = true)
    List<Task> findTaskByContains(String taskType, String taskStatus, String srcStockCode);

    /**
     * 查询容器是否有出库任务
     *
     * @param stockCode 容器
     */
    @Query(value = " select count(t.id) FROM Task t WHERE t.pickDetail.pick.code!=:pickCode and t.srcStock.code=:stockCode and t.taskType='PICK' and t.taskStatus not in ('FINISH','CANCEL')   ")
    int existsByStock(String pickCode, String stockCode);


}
