package com.ssj.dao.weixin.library.dao;

import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
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;

@Repository
public interface IBookManagerDao extends JpaRepository<TbLibManager, String> {

	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id = ?1 and state = 1  limit 1 ")
	TbLibManager findByUserId(String userId);
	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id = ?1 and state = 1  and type = ?2 limit 1 ")
	TbLibManager findByUserId(String userId,Integer type);
	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id = ?1 and state = 1 and type = 7 and master_id is null limit 1 ")
	TbLibManager findByOnoTwoOnoTearcher(String userId);

	List<TbLibManager> findByTypeAndState(Integer type, Integer state);

	List<TbLibManager> findByTypeInAndState(int[] type, Integer state);
	
	@Query(nativeQuery = true, value = "select * from tb_lib_manager  where user_id = ?1 and type = ?2 and state = 1 and is_cancelled=0 limit 1")
	public TbLibManager findByUserIdAndType(String userId, Integer type);

	@Query(nativeQuery = true,value = "select * from tb_lib_manager where user_id=?1 and type=?2 and is_cancelled=0 limit1")
	public TbLibManager findExerciseTeacherByUserIdAndType(String userId,int type);

	@Query(nativeQuery = true, value = "select * from tb_lib_manager  where  lib_id =?1 and user_id = ?2 and type = ?3 and state = 1 and is_cancelled=0 ")
	public TbLibManager findByLibIdAndUserIdAndType(String libId,String userId, Integer type);
	
	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id = ?1 and lib_id = ?2 and state = 1  limit 1")
	TbLibManager findByUserIdAndLibId(String userId, String libId);
	
	@Query(nativeQuery = true, value = "SELECT DISTINCT t12.wx_account as openid from tb_lib_manager t \n"+
			"left join tb_user t12 on t.user_id = t12.id \n"+
			"where t.lib_id = ?1 and t.type = ?2 and state = 1 \n")
	List<String> findManageOpenidList(String libId, int type);
	
	
	@Query(nativeQuery = true, value = "SELECT * from tb_lib_manager t where t.master_id=?1 AND t.user_id=?2 AND t.type=?3 AND t.state=1")
	List<TbLibManager> finBindByMasterAndTeacher(String masterId,String teacherId, int type);

	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id = ?1 and state = 1 and type=?2 and lib_id is not null ")
	List<TbLibManager> findManagerList(String userId, int type);
	
	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id = ?2 and state = 1 and type=?3 and lib_id=?1 ")
	List<TbLibManager> findManagerList(String libId,String userId, int type);

	@Query(nativeQuery = true, value = "select * from tb_lib_manager where type in (?2) and (lib_id in (?1) or lib_id is null) and (master_id=?3 or master_id is null)")
	List<TbLibManager> findManagerList(String[] libIds, int[] types, String masterId);

	@Query(nativeQuery = true, value = "select * from tb_lib_manager where ((type = 2 and lib_id in (?1)) or type = 7) and  name REGEXP ?2 and state=1 ")
	List<TbLibManager> searchTeacherList (String[] libIds, String key);

	@Query(nativeQuery = true, value = "select * from tb_lib_manager where type = 7 and  name REGEXP ?1 and state=1 ")
	List<TbLibManager> search121TeacherList (String key);

	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id = ?1 and state = 1 ")
	public List<TbLibManager> getByUserId(String userId);
	
	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id = ?1 and state = 1 and type BETWEEN 6 and  7 GROUP BY type  ")
	public List<TbLibManager> getByDistinctUserId(String userId);
	
	
	public List<TbLibManager> findByUserIdInAndType(String[] userIds, int type);

	/**
	 * 查询指定角色的用户信息
	 * @param userId
	 * @param type
	 * @param state
	 * @return
	 */
	List<TbLibManager> findByUserIdAndTypeInAndState(String userId, int[] type, int state);

	/**
	 * 查询馆下指定角色的用户信息
	 * @param libId
	 * @param userId
	 * @param type
	 * @param state
	 * @return
	 */
	List<TbLibManager> findByLibIdAndUserIdAndTypeInAndStateOrderByType(String libId, String userId, int[] type, int state);

	List<TbLibManager> findByLibIdAndUserIdAndTypeInOrderByType(String libId, String userId, int[] type);



	/**
	 * 查询非注销用户角色
	 * @param userId
	 * @param type
	 * @param state
	 * @return
	 */

	List<TbLibManager> findByUserIdAndTypeInAndStateAndIsCancelled(String userId, int[] type, int state, int isCancelled);
	/**
	 * 查询指定角色的用户信息（所有）
	 * @param userId
	 * @param type
	 * @return
	 */
	List<TbLibManager> findByUserIdAndTypeInOrderByCreateTimeDesc(String userId, List<Integer> type);



	@Modifying
	@Query(nativeQuery = true,value = "update tb_lib_manager set state=2 where user_id=?1 and type=?2 and is_cancelled=?3 and state=1")
	void updateByUserIdAndTypeAndCancelled(String userId,int type,int cancelled);

	/**
	 * 查询指定角色的用户信息（所有）
	 * @param userId
	 * @param type
	 * @return
	 */
	List<TbLibManager> findByUserIdAndTypeInAndIsCancelled(String userId, List<Integer> type, int isCancelled);

	/**
	 * 查询指定角色的所有用户信息
	 *
	 * @param type
	 * @param userId
	 * @return
	 */
	TbLibManager findByTypeAndUserId(int type, String userId);

	/**
	 * 查询指定角色的所有用户信息,区分注销
	 *
	 * @param type
	 * @param userId
	 * @return
	 */
	@Query(nativeQuery = true,value = "select * from tb_lib_manager where user_id=?1 and type=?2 and is_cancelled=0 limit 1")
	TbLibManager findByTypeAndUserIdAndCancelled(String userId, int type);

	/*@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where lib_id=?1 and user_id=?2 and type=?3 and state=?4 limit 1 ")
	TbLibManager findAllByUserIdAndTypeAndState(String libId,String userId,int type,int state);*/
	TbLibManager findTopByLibIdAndUserIdAndTypeAndState(String libId,String userId,int type,int state);

	@Query(nativeQuery = true,value = "select * from tb_lib_manager where user_id=?1 and type=?2 and state=1 and is_cancelled=0")
	List<TbLibManager> findAllByUserIdAndTypeAndStatus(String teacherId,int type);
	
	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id=?1 and type=?2 and state=?3 limit 1 ")
	TbLibManager findAllByUserIdAndTypeAndState(String userId,int type,int state);

	@Query(nativeQuery = true, value = "SELECT * FROM tb_lib_manager t WHERE t.state = 1 and t.user_id = ?1 and t.type = ?2 and t.grade REGEXP ?3 and t.teacher_lever REGEXP ?4 limit 1")
	public TbLibManager findByUserIdAndTypeAndGradeAndTeacherLever(String userId,int type,String grade, String teacherLever);
	
	@Query(nativeQuery = true, value = "select * from tb_lib_manager t1 where t1.user_id in(select master_id from tb_lib_manager t2 where t1.user_id = t2.master_id "
			+ "and t2.user_id = ?1 and t2.type = ?2 and t2.state = 1) and t1.type = 6 and t1.state=1")
	public List<TbLibManager> findMasterListByUserIdAndType(String userId, int type);
	
	@Query(nativeQuery = true, value = "select * from tb_lib_manager t where t.user_id = ?1 and t.state = 1")
	public List<TbLibManager> findTeacherByUserId(String userId);
	
	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where user_id = ?1 and state = 1 and type<>6  and lib_id is not null limit 1 ")
	TbLibManager findByUserIdAndLibIsNotNull(String userId);

	@Query(nativeQuery = true, value = "select  * from tb_lib_manager  where lib_id=?1 and type=?2 and state=1 ")
	List<TbLibManager> findByLibIdAndType(String libId, int type);

	/**
	 * 通过类型查找角色用户列表
	 * @param type
	 * @return
	 */
	List<TbLibManager> findByType(int type);

	/**
	 * 查询下级角色用户
	 * @param parentId
	 * @param state
	 * @return
	 */
	List<TbLibManager> findByParentIdAndState(String parentId, int state);

	List<TbLibManager> findByParentIdAndStateAndType(String parentId,int state,int type);

	@Query(nativeQuery = true, value = "update tb_lib_manager set tb_lib_manager.correct_homework_num = 0 where type in (8,11,13) and state = 1 ")
	@Modifying
    void resetCorrectHomeworkNum();

	@Query(nativeQuery = true, value = "select * from tb_lib_manager where type in (8,11) and is_online = 1 and state = 1 and course_name REGEXP ?1 and grade REGEXP ?2   "
			+ "ORDER BY correct_homework_num, allot_time limit 1")
    TbLibManager findByCourseNameAndGrade(String courseName, String grade);
	@Query(nativeQuery = true, value = "select * from tb_lib_manager where type =8 and is_online = 1 and state = 1 and course_name REGEXP ?1 and grade REGEXP ?2 and   user_id <> ?3  "
			+ "ORDER BY correct_homework_num, allot_time limit 1")
	TbLibManager findByCourseNameAndGrade(String courseName, String grade,String teacherId);

	@Query(nativeQuery = true, value = "select * from tb_lib_manager where type =?3 and is_online = 1 and state = 1 and is_banned=0 and course_name REGEXP ?1 and grade REGEXP ?2   "
			+ "ORDER BY correct_homework_num, allot_time limit 1")
	TbLibManager findByCourseNameAndGrade(String courseName, String grade,int teacherType);
	
	@Query(nativeQuery = true, value = "select * from tb_lib_manager where type =13 and is_online = 1 and state = 1 and course_name REGEXP ?1 "
			+ "ORDER BY correct_homework_num limit 1")
	TbLibManager findByCourseName(String courseName);
	
	@Query(nativeQuery = true, value = "select * from tb_lib_manager where type =13 and is_online = 1 and state = 1 and course_name REGEXP ?1 and  user_id <> ?2 "
			+ "ORDER BY correct_homework_num limit 1")
	TbLibManager findByCourseName(String courseName,String teacherId);

	/**
	 * 查看指定批改时间的老师
	 *
	 * @param period
	 * @param type
	 * @param state
	 * @param isBanned
	 * @return
	 */
	List<TbLibManager> findByPeriodLikeAndTypeInAndStateAndIsBanned(String period, int[] type, int state,int isBanned);

    Page<TbLibManager> findAll(Specification<TbLibManager> specification, Pageable pageable);

	@Query(nativeQuery = true, value = "update tb_lib_manager set is_online=0,logout_time = now() where is_online=1 and type in(8,11,13) and state=1 ")
	@Modifying
	void updateTeacherAllotStatus();

	@Modifying
	@Query(nativeQuery = true,value = "update tb_lib_manager set is_online=0,logout_time = now() where user_id=?1 and state=1")
	void updateTeacherOnlineStatusByTeacherid(String teacherId);

	@Query(nativeQuery = true, value = "select user_id from tb_lib_manager where type in(8,11) and is_online=1 and state=1")
	public List<String> queryAllOnlineTeacherId();

	@Query(nativeQuery = true, value = "select * from tb_lib_manager where type =?3 and is_online = 1 and state = 1 and correct_type=?4 and course_name REGEXP ?1 and grade REGEXP ?2   "
			+ "ORDER BY correct_homework_num, allot_time limit 1")
    TbLibManager findByCourseNameAndGrade(String courseName, String grade, int teacherType, int correctType);

	@Query(nativeQuery = true, value = "select DISTINCT t.type from tb_lib_manager t where  t.user_id = ?1 and t.state = 1")
	List<Integer> findTeacherTypeByUserId(String userId);

	TbLibManager findTopByUserIdAndTypeInAndState(String userId, int[] type, int state);

	@Query(nativeQuery = true, value = "SELECT user_id FROM tb_lib_manager t WHERE t.state=1 AND t.is_cancelled!=1 AND t.parent_id=?1 AND t.type=?2  AND t.course_name REGEXP ?3 AND t.name LIKE CONCAT('%',?4,'%') ")
	List<String> findByParentIdAndTypeAndCourseName(String parentId,Integer type,String courseName,String teacherName);

	@Query(nativeQuery = true, value = "SELECT user_id FROM tb_lib_manager t WHERE t.state=1 AND t.is_cancelled!=1 AND t.type=?1  AND t.course_name REGEXP ?2 AND t.name LIKE CONCAT('%',?3,'%') ")
	List<String> findByParentIdAndTypeAndCourseName(Integer type,String courseName,String teacherName);
	
	
	@Query(nativeQuery = true,value = "select * from tb_lib_manager where type in(8,11,13) and user_id is not null limit 1000")
	List<TbLibManager> findAllTeacher();

	@Query(nativeQuery = true,value = "select * from tb_lib_manager where type in(8,11) and state=1 and user_id is not null limit 1000")
	List<TbLibManager> findAllCorrectTeacher();

	@Query(nativeQuery = true,value = "select * from tb_lib_manager where type =13 and state=1 and user_id is not null")
	List<TbLibManager> findKonwTeacher();

	@Query(nativeQuery = true,value = "select * from tb_lib_manager where telephone=?1 and state=1 limit 1")
    TbLibManager findUserInfoByPhone(String phone);

	@Query(nativeQuery = true, value = "update tb_lib_manager set telephone=?2 where user_id=?1 ")
	@Modifying
    void updateManagerPhone(String userId, String phone);
	
	@Query(nativeQuery = true, value = "update tb_lib_manager set t_photo=?2 where t_photo is null and user_id=?1 ")
	@Modifying
    void updateManagerPhoto(String userId, String photo);
	

	@Query(nativeQuery = true, value = "select * from tb_lib_manager where type =?3 and is_online = 1 and state = 1 and course_name REGEXP ?1 and grade REGEXP ?2  and user_id<>?4  "
			+ "ORDER BY correct_homework_num, allot_time limit 1")
	TbLibManager findByCourseNameAndGradeNotThis(String subject, String grade, int type, String teacherId);

	List<TbLibManager> findByNameAndTelephoneAndStateAndType(String name, String telephone, int state, int type);
	@Query(nativeQuery = true,value = "select user_id from tb_lib_manager where is_cancelled=?1 and type in ?2")
	List<String> findByIsCancelledAndTypeIn(int isCancelled, List<Integer> type);

	@Query(nativeQuery = true,value = "select count(distinct user_id) from tb_lib_manager where login_time>?1 and type in(8,11,13) and state=1 ")
    int findActiveTeacherByLoginTime(String loginTime);

	@Query(nativeQuery = true,value = "select count(distinct vip_id) from tb_lib_vip_service where species='S003'and end_time>now()")
	int findRegisterUser();

	@Query(nativeQuery = true,value = "select count(distinct vip_id) from scon_homework_picture where create_time>?1 ")
	int findActiveUserByCreateTime(String createTime);

	List<TbLibManager> findByLibIdAndTypeAndStateAndIsCorrectOrderByIsOnlineDescAllotTimeAsc(String libId, int type, int state, int isCorrect);

	List<TbLibManager> findByLibIdAndTypeAndStateAndCourseNameLikeOrderByIsOnlineDescAllotTimeAsc(String libId, int type, int state, String courseName);
	
	@Query(nativeQuery = true,value = "  SELECT * from tb_lib_manager t  where t.state=?3  AND t.type =?2 AND t.lib_id=?1  ORDER BY t.create_time DESC LIMIT 10000 ")
	List<TbLibManager> findTbLibManagerByLibId(String libId,int type, int status);

	@Query(nativeQuery = true, value = "SELECT t2.*\n" +
			"from tb_class_teacher t\n" +
			"         join tb_class_student t1 on t.class_id = t1.class_id\n" +
			"         join tb_lib_manager t2 on t.teacher_id = t2.user_id and t1.lib_id = t2.lib_id and t2.state = 1\n" +
			"where t.status = 1\n" +
			"  and t1.status = 1\n" +
			"  and t1.vip_id = ?1\n" +
			"  and t2.course_name regexp ?2\n" +
			"  and t2.type = ?3\n" +
			"  order by t2.is_online desc,t2.allot_time")
	List<TbLibManager> findClassTeacherList(String vipId, String subject, int type);

	@Query(nativeQuery = true, value = "SELECT t2.*\n" +
			"from tb_class_teacher t\n" +
			"         join tb_class_student t1 on t.class_id = t1.class_id\n" +
			"         join tb_lib_manager t2 on t.teacher_id = t2.user_id and t1.lib_id = t2.lib_id and t2.state = 1\n" +
			"where t.status = 1\n" +
			"  and t1.status = 1\n" +
			"  and t1.vip_id = ?1\n" +
			"  and t2.course_name regexp ?2\n" +
			"  and t2.type = ?3\n" +
			"  order by t2.is_online desc,t2.allot_time limit 1")
	TbLibManager findTopClassTeacherList(String vipId, String subject, int type);

	@Query(nativeQuery = true, value = "SELECT t2.*\n" +
			"from tb_class_teacher t\n" +
			"         join tb_class_student t1 on t.class_id = t1.class_id\n" +
			"         join tb_lib_manager t2 on t.teacher_id = t2.user_id and t2.state = 1\n" +
			"where t.status = 1\n" +
			"  and t1.status = 1\n" +
			"  and t1.vip_id = ?1\n" +
			"  and t2.type = ?2\n" +
			"  order by t2.allot_time")
	List<TbLibManager> findClassTeacherList(String vipId, int type);

	@Query(nativeQuery = true, value = "SELECT t2.*\n" +
			"from tb_class_teacher t\n" +
			"         join tb_class_student t1 on t.class_id = t1.class_id\n" +
			"         join tb_lib_manager t2 on t.teacher_id = t2.user_id and t2.state = 1\n" +
			"where t.status = 1\n" +
			"  and t1.status = 1\n" +
			"  and t1.vip_id = ?1\n" +
			"  and t2.type = ?2\n" +
			"  order by t2.allot_time limit 1")
	TbLibManager findTopClassTeacherList(String vipId, int type);

	@Query(nativeQuery = true, value = "SELECT count(1) from tb_lib_manager where user_id=?1 and type in ?2 and state=1 and is_cancelled=0 ")
	int findCountByUserIdAndTypesAndStatus1 (String userId, int[] type);

	@Query(nativeQuery = true, value = "SELECT * from tb_lib_manager where telephone=?1 and type in ?2 and state=1 and is_cancelled=0  order by create_time limit 1 ")
	TbLibManager findByTelephoneAndTypesIn (String telephone, int[] type);
	
	@Query(nativeQuery = true, value = "SELECT count(1) from tb_lib_manager where lib_id=?1 and user_id=?2 and type in ?3 and state=1 and is_cancelled=0 ")
	int findCountByUserIdAndTypesAndStatus1 (String libId,String userId, int[] type);

	@Query(nativeQuery = true, value = "SELECT count(1) from tb_lib_manager where lib_id!=?1 and user_id=?2 and type in ?3 and state=1 and is_cancelled=0 ")
	int findCountIsNotLibByUserIdAndTypesAndStatus (String libId,String userId, int[] type);
	
	@Query(nativeQuery = true,value = "select * from tb_lib_manager where user_id=?1 and state=1")
	List<TbLibManager> findAllManagerByTeacherId(String teacherId);

	@Query(nativeQuery = true,value = "select * from tb_lib_manager where lib_id=?1 and state=1 and type=?2 and user_id is not null")
	List<TbLibManager> findLibTeacherByLibIdAndType(String libId,int type);

	@Modifying
	@Query(nativeQuery = true,value ="update tb_lib_manager set is_cancelled=1 where user_id=?1 and type in(8,13)" )
	void updateCancelStatusByUserId(String teacherId);

	@Modifying
	@Query(nativeQuery = true,value ="update tb_lib_manager set state=2 where user_id=?1 and type in(2,10,15)" )
	void updateStatusByUserId(String teacherId);


	@Query(nativeQuery = true,value ="select * from  tb_lib_manager where user_id=?1 and type in ?2 and state=1 and is_cancelled=0" )
	List<TbLibManager> findAllValidTeacherList(String teacherId,int[]types);

	@Query(nativeQuery = true,value = "select * from tb_lib_manager where type=?1 and state=?2  and create_time  is not null order by create_time desc limit 1")
	TbLibManager findExerciseByTypeAndState(int type,int state);

	@Query(nativeQuery = true,value = "select * from tb_lib_manager where telephone=?1 and type=?2 and  state=1 order by create_time desc  limit 1")
	TbLibManager findManagerByPhoneAndType(String mobile, int type);
}
