package com.uas.platform.b2b.dao;

import java.util.List;

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 org.springframework.stereotype.Repository;

import com.uas.platform.b2b.model.PurchaseInquiryItem;

@Repository
public interface PurchaseInquiryItemDao extends JpaSpecificationExecutor<PurchaseInquiryItem>, JpaRepository<PurchaseInquiryItem, Long> {

	/**
	 * 按卖家企业ID和明细传到卖家ERP状态查找询价明细
	 * 
	 * @param vendUU
	 * @param sendStatus
	 * @return
	 */
	List<PurchaseInquiryItem> findByVendUUAndSendStatus(long vendUU, short sendStatus);

	/**
	 * 按买家企业ID和明细传报价状态查找询价明细
	 * 
	 * @param enUU
	 * @param backStatus
	 * @return
	 */
	@Query("from PurchaseInquiryItem d where d.inquiry.enUU = :enUU and d.backStatus = :backStatus")
	List<PurchaseInquiryItem> findByEnUUAndBackStatus(@Param("enUU") long enUU, @Param("backStatus") short backStatus);

	/**
	 * 按买家企业ID、单号和序号查找询价单明细
	 * 
	 * @param enUU
	 * @param code
	 * @param number
	 * @return
	 */
	@Query("from PurchaseInquiryItem d where d.inquiry.enUU = :enUU and d.inquiry.code = :code and d.number = :number")
	List<PurchaseInquiryItem> findByEnUUAndCodeAndNumber(@Param("enUU") long enUU, @Param("code") String code, @Param("number") short number);

	/**
	 * 按卖家企业ID和询价明细的报价信息传到卖家ERP的状态查找询价明细
	 * 
	 * @param vendUU
	 * @param replySendStatus
	 * @return
	 */
	List<PurchaseInquiryItem> findByVendUUAndReplySendStatus(long vendUU, short replySendStatus);

	/**
	 * 按卖家企业ID和询价明细的报价信息的采纳结果传到卖家ERP的状态查找询价明细
	 * 
	 * @param vendUU
	 * @param decideStatus
	 * @return
	 */
	List<PurchaseInquiryItem> findByVendUUAndDecideStatus(long vendUU, short decideStatus);

	/**
	 * 按询价单的供应商企业ID和询价单状态来统计条数
	 * 
	 * @param vendUU
	 * @param status
	 * @return
	 */
	@Query("select count(i) from PurchaseInquiryItem i where i.vendUU = :vendUU and i.status = :status")
	public int getCountByVendUUAndStatus(@Param("vendUU") long vendUU, @Param("status") short status);

}
