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.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

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

@Repository
@Transactional
public interface PurchaseNoticeDao extends JpaSpecificationExecutor<PurchaseNotice>, JpaRepository<PurchaseNotice, Long> {

	/**
	 * 按买家企业ID和送货提醒来源ID查找送货提醒单
	 * 
	 * @return
	 */
	List<PurchaseNotice> findByEnUUAndSourceId(long enUU, long sourceId);

	/**
	 * 按供应商企业ID和传输状态查找送货提醒单
	 * 
	 * @return
	 */
	List<PurchaseNotice> findByVendUUAndSendStatus(long vendUU, Short sendStatus);

	/**
	 * 按供应商企业ID、是否结案和结案状态查找送货提醒
	 * 
	 * @return
	 */
	List<PurchaseNotice> findByVendUUAndEndStatus(long vendUU, Short endStatus);

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

	/**
	 * 按发货单更新通知单的发货数
	 * 
	 */
	@Modifying(clearAutomatically = true)
	@Query("update PurchaseNotice n set n.endQty=(select sum(s.qty) from SaleSendItem s where s.notice=n) where n.id= :id")
	public void updateBySend(@Param("id") long id);

}
