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

import com.its.op.entity.its.incident.TbIncdOcrr;
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 TbIncdOcrrRepository extends JpaRepository<TbIncdOcrr, String>, JpaSpecificationExecutor<TbIncdOcrr> {

    @Query("select p from TbIncdOcrr p where p.incdEndPrarDt >= to_char(sysdate, 'YYYYMMDDHH24MISS')")
    List<TbIncdOcrr> findAll();

    @Query("select p from TbIncdOcrr p where p.incdEndPrarDt between :fromDt and :toDt")
    List<TbIncdOcrr> findAllProgressHistory(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query("select p from TbIncdOcrr p where p.incdPrgrStepCd = 'ISS2' and p.incdStrtDt between :fromDt and :toDt")
    List<TbIncdOcrr> findAllProgressIncidentByDateRange(@Param("fromDt") String fromDt, @Param("toDt") String toDt);

    @Query(value = "SELECT LPAD(NVL(MAX(SUBSTR(INCD_OCRR_ID, -9)), '000000000') + 1, 9, '0') AS NEWID " +
                   "  FROM TB_INCD_OCRR " +
                   " WHERE SUBSTR(INCD_OCRR_ID, 1, 3) = :centerId ", nativeQuery = true)
    String getNewIncdId(@Param("centerId") String centerId);
}
