package com.uas.platform.b2b.dao;

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

import javax.transaction.Transactional;
import java.util.List;

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

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

	/**
	 * 按卖家企业ID和询价主记录ID查找
	 * 
	 * @param vendUU
	 * @param inquiryId
	 * @return
	 */
	@Query("from PurchaseInquiryItem d where d.vendUU = :vendUU and d.inquiry.id = :inquiryId")
	List<PurchaseInquiryItem> findByVendUUAndInquiry(@Param("vendUU") long vendUU, @Param("inquiryId") long inquiryId);

	/**
	 * 按买家企业ID和明细传报价状态查找询价明细
	 * 
	 * @param enUU
	 * @param backStatus
	 * @return
	 */
	@Query("from PurchaseInquiryItem d where d.inquiry.enUU = :enUU and d.backStatus = :backStatus and IFNULL(d.sourceApp, ' ') <> 'B2B'")
	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更新采纳结果
	 * 
	 */
	@Modifying(clearAutomatically = true)
	@Query("update PurchaseInquiryItem p set p.agreed = :agreed,p.decideStatus = :decideStatus where p.id = :id")
	public void updateDecideStatusByID(@Param("agreed") short agreed, @Param("decideStatus") short decideStatus, @Param("id") Long id);

	/**
	 * 按卖家企业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);

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

	/**
	 * 查询单据是否存在
	 * 
	 * @param enuu
	 * @param sourceId
	 * @return
	 */
	public PurchaseInquiryItem findByVendUUAndSourceId(Long enuu, Long sourceId);

	/**
	 * 按卖家企业ID和询价明细的报价信息的作废处理传到卖家ERP的状态查找询价明细
	 *
	 * @param uu
	 * @param value
	 * @return
	 */
    List<PurchaseInquiryItem> findByVendUUAndInvalidStatus(Long uu, short value);

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

    /**
     * 更新询价报价传输到erp的状态
     *
     * @param enuu
     * @param status
     */
	@Procedure(procedureName = "inquiryitem_erpstatus_update")
    void updateStatus(Long enuu, Short status);

	/**
	 * 更新首页公共询价单据的阅读状态
	 *
	 * @param id
	 * @param userUU
	 */
	@Procedure(procedureName = "inquiryitems_readstatus")
	void setReadStatus(Long id, Long userUU);
}
