/*
*  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.alibaba.fastjson.JSONArray;
import com.youchain.businessdata.domain.AgvTask;
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);
    /**
     * 查询出入库相关的AGV任务
     * @param agvTaskId
     * @param type ASN
     * @param agvFlag 是否AGV任务
     * @return
     */
    @Query(value = " select sum(plan_qty)-sum(move_qty) qty,GROUP_CONCAT(id) taskIds,item_key_id ikID,dst_point_id pointId from data_task where if((?1 !='' or ?1 is not null),agv_task_id=?1,1=1) " +
            "and if((?4 !='' or ?4 is not null),item_key_id=?4,1=1)" +
            "and task_type=?2 and be_skip=?3 and task_status in ('RECEIVING','PUTAWAY')  group by item_key_id ",nativeQuery = true)
    List<Map<String,Object>> findByAgvTask(Integer agvTaskId,String type,Long agvFlag,Long itemKeyId);
    /**taskStatus(RECEIVING-收货中)*/
    @Query(value = "SELECT t from Task t where t.agvTask.id=?1 and t.taskStatus=?2")
    List<Task> findByAgvTask(Integer agvTaskId,String taskStatus);
    @Query(value = "SELECT t from Task t where t.agvTask.id=?1 and t.taskStatus=?2")
    List<Task> findByAgvTaskSj(Integer agvTaskId,String taskStatus);
    /**
     * 根据出库单明细查询未完成的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集合
     * @return
     */
    @Query(value = "SELECT t FROM Task t WHERE t.agvTask.id=?1 and t.taskStatus=?2")
    List<Task> getAgvTaskList(Integer agvTaskId,String taskStatus);
    @Query(value = "SELECT t FROM Task t WHERE t.agvTask.id=?1")
    List<Task> findByAgvTask(Integer agvTaskId);
    /**
     * 根据 收货明细序号 进行删除数据
     */
    @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);
    @Query(value = "select t from Task t where t.itemKey.id =?1 and t.taskStatus=?2")
    List<Task> findByItemKey(Long id,String taskStatus);
    @Query(value = "select t from Task t where t.inventory.id =?1 and t.taskStatus in ('ALLOCATE') and t.pickDetail is not null")
    List<Task> findByInv(Long invId);
    @Query(value = " select i.id 'itemId',SUM(t.plan_qty) 'taskQty' from data_task t  "
            +"  left join data_item_key ik on t.item_key_id=ik.id "
            +"  left join base_item i on i.id=ik.item_id "
            +"  where t.task_status=?1 and t.be_skip=?2 "
            +"  and t.task_type=?3 "
            +"  group by i.id ",nativeQuery = true)
    List<Map<String,Object>> findBySumPlanQty(String taskStatus,int beSkip,String taskType);
    @Query(value = "select t from Task t where t.taskStatus =?1 and t.beSkip=?2 and t.taskType=?3")
    List<Task> findByItemType(String taskStatus, int beSkip, String taskType);

    Task findByPickDetailAndItem(Long id, String sku);

    @Query(value = "select t from Task t where t.taskStatus='RECEIVING'",nativeQuery = false)
    List<Task> findByStartPoint();
    @Query(value = "select t from Task t where t.taskStatus =?1 and t.beSkip=?2 and t.taskType=?3 group by t.itemKey.id")
    List<Task> findByGroupItemKey(String taskStatus, int beSkip, String taskType);
    @Query(value = " select i.code 'sku',t.id,p.related_bill1 'relatedBill1',sp.`code` 'startPointCode',ep.`code` 'endPointCode',t.plan_qty 'quantity',ik.prop_c1 'pch',ik.prop_c6 'lsh' from data_task t  "
            +" left join data_item_key ik on ik.id=t.item_key_id "
            +" left join base_item i on i.id=ik.item_id "
            +" left join data_pick_detail pd on pd.id=t.pick_detail_id "
            +" left join data_pick p on p.id=pd.pick_id "
            +" left join base_point ep on t.dst_point_id=ep.id "
            +" left join base_point sp on t.src_point_id=sp.id "
            +" left join data_agv_task agv on t.agv_task_id=agv.id "
            +" where t.task_status='ALLOCATE' and t.be_skip=0 " +
            " and (?1 is null or ?1=ik.prop_c7)"
            +" group by t.item_key_id ",nativeQuery = true)
    List<Map<String,Object>>  findByRgJhTask(String rfid);
    @Query(value = " select agv.`status` 'agvStatus',i.code 'sku',t.agv_task_id 'agvId',t.id,p.`code` 'pointCode',sum(t.plan_qty) 'quantity',ik.prop_c1 'pch',ik.prop_c6 'lsh' from data_task t  "
            +" left join data_item_key ik on ik.id=t.item_key_id "
            +" left join base_item i on i.id=ik.item_id "
            +" left join base_point p on t.dst_point_id=p.id "
            +" left join data_agv_task agv on t.agv_task_id=agv.id "
            +" where t.task_status='PUTAWAY' "
            +" group by t.item_key_id ",nativeQuery = true)
    List<Map<String,Object>>  findBySjTask();
    @Query(value = " select i.`code` 'sku',agv.`status` 'agvStatus',t.agv_task_id 'agvId',t.id,p.`code` 'pointCode',sum(t.plan_qty) 'quantity',ik.prop_c1 'pch',ik.prop_c6 'lsh' from data_task t  "
            +" left join data_item_key ik on ik.id=t.item_key_id "
            +" left join base_item i on i.id=ik.item_id "
            +" left join base_point p on t.dst_point_id=p.id "
            +" left join data_agv_task agv on t.agv_task_id=agv.id "
            +" where t.task_status='PUTAWAY' and ik.prop_c7=?1"
            +" group by t.item_key_id ",nativeQuery = true)
    List<Map<String,Object>>  findBySjTask(String propC7);
    @Query(value = " select ik.`prop_c7` 'rfid',i.`code` 'sku',t.id,p.related_bill1 'relatedBill1',sp.`code` 'startPointCode',ep.`code` 'endPointCode',t.plan_qty 'quantity',ik.prop_c1 'pch',ik.prop_c6 'lsh' from data_task t  "
            +" left join data_item_key ik on ik.id=t.item_key_id "
            +" left join base_item i on i.id=ik.item_id "
            +" left join data_pick_detail pd on pd.id=t.pick_detail_id "
            +" left join data_pick p on p.id=pd.pick_id "
            +" left join base_point ep on t.dst_point_id=ep.id "
            +" left join base_point sp on t.src_point_id=sp.id "
            +" left join data_agv_task agv on t.agv_task_id=agv.id "
            +" where t.task_status='ALLOCATE' and t.be_skip=1 and t.agv_task_id is null " +
            " and (?1 is null or ?1=ik.prop_c7)"
            +" group by t.item_key_id ",nativeQuery = true)
    List<Map<String, Object>> findByLxJhTask(Object o);
}