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.PurchaseInquiryMould;

@Repository
public interface PurchaseInquiryMouldDao
		extends JpaSpecificationExecutor<PurchaseInquiryMould>, JpaRepository<PurchaseInquiryMould, Long> {

	/**
	 * @param enUU
	 *            询价方企业ID
	 * @param code
	 *            流水号
	 * @return
	 */
	public List<PurchaseInquiryMould> findByEnuuAndCode(long enuu, String code);

	/**
	 * @param enUU
	 *            询价方企业ID
	 * @param sourceId
	 *            来源系统的单据ID
	 * @return
	 */
	public List<PurchaseInquiryMould> findByEnuuAndSourceId(long enuu, long sourceId);

	/**
	 * 根据供应商UU号获取客户模具询价单数量
	 * 
	 * @param venduu
	 * @return
	 */
	@Query("select count(p) from PurchaseInquiryMould p where p.venduu = :venduu")
	public long countByVendUU(@Param("venduu") Long venduu);

	List<PurchaseInquiryMould> findByEnuuAndBackStatus(Long uu, short value);

	/**
	 * 根据enuu和erpSourceId获取单据
	 * 
	 * @param venduu
	 * @param erpSourceId
	 * @return
	 */
	public PurchaseInquiryMould findByEnuuAndErpSourceId(Long enuu, Long erpSourceId);

	/**
	 * 获得每种单据状态的未读数量
	 *
	 * @Param ids 未读单据id
	 * @return
	 */
	@Query("select count(p), p.status from PurchaseInquiryMould p where p.id in :ids and (p.overdue is null or p.overdue = 0) and p.valid = 1 group by p.status")
	List<Object[]> getUnreadCountEveryStatus(@Param("ids") List<Long> ids);

}