package com.uas.platform.b2b.dao;

import com.uas.platform.b2b.model.PurchaseApBill;
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.jpa.repository.query.Procedure;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface PurchaseApBillDao extends JpaSpecificationExecutor<PurchaseApBill>, JpaRepository<PurchaseApBill, Long> {

	/**
	 * 按所属企业及编号查找客户应付发票
	 * 
	 * @param enUU
	 * @param code
	 * @return
	 */
	public List<PurchaseApBill> findByEnUUAndCode(Long enUU, String code);
	
	/**
	 * 卖家根据卖家UU号和下载状态获取需要下载的客户应付发票
	 * @return
	 */
	public List<PurchaseApBill> findByVendUUAndSendStatus(Long vendUU, Short sendStatus);
	
	/**
	 * 卖家根据卖家UU号和下载状态和反过账状态获取客户反过账的客户应付发票
	 * @param vendUU
	 * @param nonPosting
	 * @return
	 */
	public List<PurchaseApBill> findByVendUUAndNonPosting(Long vendUU, Short nonPosting);
	
	/**
	 * 根据截止日期获取（发票日期）
	 * @param vendUU
	 * @param endDate
	 * @return
	 */
	@Query("select p.id from PurchaseApBill p where p.vendUU=:vendUU and p.date <= :endDate")
	public List<Long> findByEndDate(@Param("vendUU") long vendUU, @Param("endDate")Date endDate);
	
	/**
	 * 根据起始日期获取（发票日期）
	 * @param formDate
	 * @param vendUU
	 * @return
	 */
	@Query("select p.id from PurchaseApBill p where p.vendUU=:vendUU and p.date >= :fromDate")
	public List<Long> findByFromDate(@Param("vendUU") long vendUU, @Param("fromDate")Date formDate);
	
	@Query("select count(p) from PurchaseApBill p where p.vendUU=:vendUU")
	public Long countByVendUU(@Param("vendUU") Long vendUU);

	/**
	 * 根据UU号和出入库单号更新发票主表的相关数据
	 *
	 * @param uu
	 * @param inoutno
	 */
	@Procedure(procedureName = "refreshamountandtaxsum")
    void refreshAmountAndTaxSum(Long uu, String inoutno);

	/**
	 * 通过企业UU和erpId查询单据是否存在
	 *
	 * @param enUU 企业UU
	 * @param erpId erpId
	 * @return
	 */
    List<PurchaseApBill> findByEnUUAndErpId(Long enUU, Long erpId);
}
