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

@Repository
public interface PurchaseOrderChangeDao extends JpaSpecificationExecutor<PurchaseOrderChange>, JpaRepository<PurchaseOrderChange, Long> {

	/**
	 * 按所属企业及编号查找变更单
	 * 
	 * @param enUU
	 * @param code
	 * @return
	 */
	@Query("from PurchaseOrderChange r where r.order.enUU = :enUU and r.code = :code")
	public List<PurchaseOrderChange> findByEnUUAndCode(@Param("enUU") long enUU, @Param("code") String code);

	/**
	 * (买家)按回复的状态查找变更单回复信息
	 * 
	 * @param enUU
	 * @param status
	 * @param sendStatus
	 * @return
	 */
	@Query("from PurchaseOrderChange r where r.order.enUU = :enUU and r.status = :status and r.backStatus = :backStatus")
	public List<PurchaseOrderChange> findByEnUUAndStatusAndBackStatus(@Param("enUU") long enUU, @Param("status") short status,
			@Param("backStatus") short backStatus);

	/**
	 * 卖家按上传状态查找变更单
	 * 
	 * @param vendUU
	 *            卖方企业ID
	 * @param sendStatus
	 *            上传到卖家ERP的状态
	 * @return
	 */
	@Query("from PurchaseOrderChange r where r.order.vendUU = :vendUU and r.sendStatus = :sendStatus")
	public List<PurchaseOrderChange> findByVendUUAndSendStatus(@Param("vendUU") long vendUU, @Param("sendStatus") short sendStatus);

	/**
	 * 卖家按回复信息上传状态查找变更单
	 * 
	 * @param vendUU
	 *            卖方企业ID
	 * @param status
	 *            是否已回复的状态
	 * @param replySendStatus
	 *            回复信息上传到卖家ERP的状态
	 * @return
	 */
	@Query("from PurchaseOrderChange r where r.order.vendUU = :vendUU and r.status = :status and r.replySendStatus = :replySendStatus")
	public List<PurchaseOrderChange> findByVendUUAndStatusAndReplySendStatus(@Param("vendUU") long vendUU, @Param("status") short status,
			@Param("replySendStatus") short replySendStatus);
}
