package com.ssj.dao.conch.curator.dao;

import com.ssj.bean.curator.TbVacationLimit;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
public interface IVacationLimitDao extends JpaRepository<TbVacationLimit, String> {

    @Query(nativeQuery = true, value = "select *\n" +
            "from tb_vacation_limit\n" +
            "where set_state = 2\n" +
            "  and (\n" +
            "        (to_days(start_date) >= to_days(?1) AND to_days(start_date) <= to_days(?2))\n" +
            "        OR (to_days(start_date) <= to_days(?1) AND to_days(end_date) >= to_days(?2))\n" +
            "        OR (to_days(end_date) >= to_days(?1) AND to_days(end_date) <= to_days(?2))\n" +
            "    )\n" +
            "  and lib_id = ?3")
    List<TbVacationLimit> findValidVocationLimit(Date beginTime, Date endTime, String libId);

    @Query(nativeQuery = true, value = "select * from tb_vacation_limit where lib_id=?1 and limit_year=?2 order by type asc")
    List<TbVacationLimit> findLibVacationData(String libId,String year);

    @Query(nativeQuery = true, value = "select *\n" +
            "from tb_vacation_limit\n" +
            "where set_state = 2\n" +
            "  and  (to_days(start_date) <= to_days(now()) AND to_days(end_date) >= to_days(now()))\n" +
            "  and lib_id = ?1 limit 1")
    TbVacationLimit findTopValidVocationLimit(String libId);
}
