/*
*  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.PickDetail;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

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

    @Query(value = "SELECT * FROM `data_pick_detail` a WHERE pick_ticket_id = :id", nativeQuery = true)
    List<PickDetail> queryByPickTicketId(@Param("id") Long id);
    @Query(value = "SELECT * FROM `data_pick_detail` a WHERE pick_ticket_id = :pickTicketId and item_id = :itemId", nativeQuery = true)
    PickDetail queryByPickTicketIdItemId(@Param("pickTicketId")Long pickTicketId,@Param("itemId")Long itemId);

    @Query(value = "SELECT * FROM `data_pick_detail` a WHERE prop_c3 = :propC3 ", nativeQuery = true)
    PickDetail queryByPropC3(@Param("propC3")String propC3);

    @Query(value = "SELECT * FROM `data_pick_detail` d WHERE d.type='ZZJL' and d.picked_qty=0", nativeQuery = true)
    List<PickDetail> notPickList();


    /**
     * 未拣货完成，返回0则全部拣货完成
     * @param id
     * @return
     */
    @Query(value = "SELECT id FROM `data_pick_detail` a WHERE pick_ticket_id = :id and allocated_qty>0 and allocated_qty>picked_qty", nativeQuery = true)
    List queryPickQty(@Param("id") Long id);
    /**
     * 未发运完成，返回0则全部发运完成
     * @param id
     * @return
     */
    @Query(value = "SELECT id FROM `data_pick_detail` a WHERE pick_ticket_id = :id and picked_qty>0 and picked_qty>shipped_qty", nativeQuery = true)
    List queryShipQty(@Param("id") Long id);

    @Query(value = "SELECT id FROM `data_pick_detail` a WHERE pick_ticket_id = :id and picked_qty>0 and picked_qty>shipped_qty", nativeQuery = true)
    List queryBLZsc(@Param("id") Long id);

    @Query(value = "SELECT IFNULL(sum(allocated_qty),0) FROM `data_pick_detail` a WHERE source_id=:id", nativeQuery = true)
    Double getBcQty(@Param("id") Long id);

}