package com.its.op.dao.repository.its.weather;

import com.its.op.entity.its.weather.TbVilgFrcsPnst;
import com.its.op.entity.its.weather.TbVilgFrcsPnstKey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TbVilgFrcsPnstRepository extends JpaRepository<TbVilgFrcsPnst, TbVilgFrcsPnstKey>, JpaSpecificationExecutor<TbVilgFrcsPnst> {

    @Query("SELECT p FROM TbVilgFrcsPnst p inner join fetch p.frcs m")
    List<TbVilgFrcsPnst> findAll();

    @Query("SELECT p FROM TbVilgFrcsPnst p inner join fetch p.frcs m                                                          " +
            " WHERE (p.vilgFrcsZoneCd, p.anncDt) IN (SELECT h.vilgFrcsZoneCd, MAX(h.anncDt)                               " +
            "                                              FROM TbVilgFrcsPnst h                                              " +
            "                                             WHERE h.anncDt >= TO_CHAR(SYSDATE-240/1440,'YYYYMMDDHH24MISS')  " +
            "                                               AND h.ord = 0                                                 " +
            "                                             GROUP BY h.vilgFrcsZoneCd)                                      " +
            "   AND p.ord = 0")
    List<TbVilgFrcsPnst> findAllList();
}
