package com.ssj.dao.weixin.library.dao;
import com.ssj.bean.weixin.libmy.domain.TbLibLoginInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author yanghuan
 * @title: TbLibLoginInfoDao
 * @projectName ssj
 * @description: TODO
 * @date 2019/8/15 16:24
 */
@Repository
public interface TbLibLoginInfoDao extends JpaRepository<TbLibLoginInfo,String> {

    @Modifying
    @Query(nativeQuery = true, value = " update tb_lib_login_info set logout_time=?1 where logout_time is null and arrange_date regexp ?2 and teacher_id=?3  ")
    void updateTbLibLoginInfoByLogoutTime(String logoutTime,String arrangeDate,String teacherId);

    @Query(nativeQuery = true,value = "select count(id) from tb_lib_login_info where teacher_id=?1 and type=?2 and login_time<=?3 and (logout_time is null or logout_time>=?4) and arrange_date regexp ?5")
    int findCountByTeacherIdAndTime(String teacherId,int type,String logintime,String logoutTime,String arrangeTime);

    @Query(nativeQuery = true,value = "select * from tb_lib_login_info where teacher_id=?1 and type=?2 and login_time<=?3 and logout_time>=?4 and logout_time<=?5 and logout_time is not null and arrange_date regexp ?6 limit 1")
    TbLibLoginInfo findTbLibLoginInfoByLoginTimeAndLogoutTime(String teacherId,int type,String loginTime,String time,String logoutTime,String arrangeDate);

    @Query(nativeQuery = true,value = "select * from tb_lib_login_info where teacher_id=?1 and type=?2 and login_time>=?3 and login_time<=?4 and arrange_date regexp ?5 order by login_time asc")
    List<TbLibLoginInfo> findAllList(String teahcerId,int type,String date,String endTime,String arrangeDate);

    @Query(nativeQuery = true,value = "select id,teacher_id,arrange_date,min(login_time) as login_time,logout_time,type from tb_lib_login_info where teacher_id=?1 and type=?2  and logout_time>=?3 and logout_time<=?4 and arrange_date regexp ?5 group by logout_time order by login_time asc")
    List<TbLibLoginInfo> findCount(String teahcerId,int type,String beforeTime,String logoutTime,String arrangeDate);

    @Query(nativeQuery = true,value = "select * from tb_lib_login_info where teacher_id=?1 and type=?2 and login_time<=?3 and (logout_time>?4 or logout_time is null ) and arrange_date regexp ?5 ")
    List<TbLibLoginInfo> findListByTime(String teacherId,int type,String specialTime,String endTime,String date);

    @Query(nativeQuery = true,value = "select * from tb_lib_login_info where teacher_id=?1 and type=?2 and login_time>?3  and arrange_date regexp ?4 order by login_time limit 1")
    TbLibLoginInfo findByTheFirstTime(String teacherId,int type,String beginTime,String date);

    @Query(nativeQuery = true,value = "select * from tb_lib_login_info where teacher_id=?1 and type=?2 and arrange_date regexp ?3 order by login_time asc")
    List<TbLibLoginInfo> findByArrangeDate(String teacherId,int type,String date);

    @Query(nativeQuery = true, value = "select * from tb_lib_login_info where teacher_id=?1 and arrange_date regexp ?2 order by login_time asc")
    List<TbLibLoginInfo> findByTeacherIdAndArrangeDate(String teacherId, String date);


}