/*
*  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 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 hjl
* @date 2023-08-14
**/
public interface AsnDetailRepository extends JpaRepository<AsnDetail, Long>, JpaSpecificationExecutor<AsnDetail> {
    @Query("select d from AsnDetail d where d.asn.id=?1 and d.lineNo=?2")
    List<AsnDetail> findByLineNo(Long id, Long lineNo);
    @Query(value = " select i.id itemId,sum(ad.order_qty) qty from data_asn_detail ad  "
            +" left join data_asn a on ad.asn_id=a.id "
            +" left join base_bill_type bt on bt.id=a.bill_type_id "
            +" left join base_item i on ad.item_id=i.id "
            +" where a.`status`=?1 and bt.`code`=?2 "
            +" group by i.id ",nativeQuery = true)
    List<Map<String,Object>> findByAsn(String asnStatus, String billTypeCode);
    @Query("select d from AsnDetail d where d.asn.relatedBill1=?1 and d.item.code=?2 and d.orderQty>d.receivedQty")
    List<AsnDetail> findByAsnAndItem(String taskBillCode, String itemCode);
    @Query("select d from AsnDetail d where d.asn.id=?1 and d.asn.status=?2")
    List<AsnDetail> findByAsnId(Long asnId,String asnStatus);
}