package com.its.api.its.repository.unit;

import com.its.api.its.model.dto.unit.TbUnitSystSttsHsStatInf;
import com.its.api.its.model.entity.unit.TbUnitSystSttsHs;
import com.its.api.its.model.entity.unit.TbUnitSystSttsHsKey;
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 java.util.List;

@Repository
public interface TbUnitSystSttsHsRepository extends JpaRepository<TbUnitSystSttsHs, TbUnitSystSttsHsKey>, JpaSpecificationExecutor<TbUnitSystSttsHs> {

    @Query("select p from TbUnitSystSttsHs p where p.systId = :systId and p.crtnDt between :fromDt and :toDt")
    List<TbUnitSystSttsHs> findAllHistByDateRange(@Param("systId") String systId, @Param("fromDt") String fromDt, @Param("toDt") String toDt);

    //SELECT SYST_ID, COUNT(SYST_ID) AS CNT FROM TB_UNIT_SYST_STTS_HS WHERE SYST_STTS_CD = 'SPS0' AND CRTN_DT BETWEEN #{FROM_DT} AND #{TO_DT} GROUP BY SYST_ID
    @Query("select p.systId as systId, p.systSttsCd as systSttsCd, count(p.systId) as sttsCnt from TbUnitSystSttsHs p where p.crtnDt between :fromDt and :toDt group by p.systId, p.systSttsCd")
    List<TbUnitSystSttsHsStatInf> findAllStatByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

}
