package com.xhpc.evcs.jpa;

import com.xhpc.order.domain.XhpcHistoryOrder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
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.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.stereotype.Repository;

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

@Repository
public interface XhpcHistoryOrderRepository extends JpaRepository<XhpcHistoryOrder, Long>,
        QueryByExampleExecutor<XhpcHistoryOrder>, JpaSpecificationExecutor<XhpcHistoryOrder>, PagingAndSortingRepository<XhpcHistoryOrder, Long> {

    XhpcHistoryOrder findBySerialNumber(String serialNumber);

    List<XhpcHistoryOrder> findTop100ByHistoryOrderIdGreaterThanAndStateGreaterThanOrderByHistoryOrderIdAsc(Long hisOrderId, int i);

    List<XhpcHistoryOrder> findTop100ByHistoryOrderIdGreaterThanEqualAndConfirmResultAndUserIdOrderByHistoryOrderIdAsc(Long hisOrderId,int confirmResult, Long userId);

    List<XhpcHistoryOrder> findByConfirmResultNotAndOperatorId3rdptyEvcsIsNotNull(int succCode);

    List<XhpcHistoryOrder> findByConfirmResultNotAndSource(int confirmResult, int source);

    List<XhpcHistoryOrder> findByConfirmResultAndSource(int confirmResult, int source);

    List<XhpcHistoryOrder> findByCreateTimeGreaterThanAndConfirmResultAndSource(Date createTime, int confirmResult, int source);

    List<XhpcHistoryOrder> findTop1000ByHistoryOrderIdGreaterThanEqualOrderByHistoryOrderIdAsc(Long hisOrderId);

    List<XhpcHistoryOrder> findTop100ByHistoryOrderIdGreaterThanEqualOrderByHistoryOrderIdAsc(Long hisOrderId);

    @Query("select ho from XhpcHistoryOrder ho join EtPushFailedOrder fo on ho.historyOrderId = fo.id where fo.operatorId3rdpty = ?1")
    List<XhpcHistoryOrder> findJoinPushFailedOrders(@Param("operatorId3irdpty")String operatorId3irdpty, Pageable pageable);

    default List<XhpcHistoryOrder> findTop10JoinPushFailedOrders(String operatorId3irdpty) {

        return findJoinPushFailedOrders(operatorId3irdpty, PageRequest.of(0, 500));
    }
}
