package com.xhpc.evcs.jpa;

import com.xhpc.common.domain.XhpcTerminal;
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.QueryByExampleExecutor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * SpringJPA框架，DAO层
 * 继承三个接口，泛型为将记录封装到哪个实体类对象中。
 */
@Repository
public interface XhpcTerminalRepository extends JpaRepository<XhpcTerminal, Integer>,
        QueryByExampleExecutor<XhpcTerminal>, JpaSpecificationExecutor<XhpcTerminal> {

    /**
     * 通过枪序列号查询到枪所在的桩的桩编码
     *
     * @param serialNumber
     * @return
     */
    Optional<XhpcTerminal> findOneBySerialNumber(String serialNumber);

    @Query("select t.terminalId from XhpcTerminal as t where t.delFlag = 0 and t.serialNumber = ?1")
    Long queryDbId(String serialNumber);

    @Query("select t.pileSerialNumber from XhpcTerminal as t where t.delFlag = 0 and t.serialNumber = ?1")
    String selectBySql(String serialNumber);


    @Query(value = "select new XhpcTerminal(t.serialNumber, t.status) from XhpcTerminal as t where t.delFlag = 0")
    List<XhpcTerminal> selectStatusBySql();
}
