package com.ssj.dao.kmt.kmt.dao;

import com.ssj.bean.kmt.kmt.domain.KmtCourse;
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.Date;
import java.util.List;
import java.util.Map;

@Repository
public interface KmtCourseDao   extends JpaRepository<KmtCourse, String>{

	@Query(nativeQuery = true, value = " SELECT * FROM `kmt_course` t where t.course_know_id=?1 LIMIT 1  ")
	KmtCourse findCourseByKnowId(String courseKnowId);
	
	
	@Query(nativeQuery = true, value = "  SELECT * FROM `kmt_course` t where t.teacher_id=?1 AND t.course_time=?2 AND t.start_time=?3 AND t.course_know_id is NOT NULL ORDER BY t.create_time DESC LIMIT 1   ")
	KmtCourse findFaceCourseByTime(String teacherId, String courseTime, String startTime);
	
	@Query(nativeQuery = true, value = " SELECT * FROM `kmt_course` t where t.teacher_id =?1 AND t.course_know_id is NOT NULL AND ((t.course_time=CURDATE() AND t.end_time < DATE_FORMAT(NOW(),'%H:%i')) or t.course_time<CURDATE()) ORDER BY t.course_time DESC,t.end_time desc LIMIT 1 ")
	KmtCourse findFaceCourseByTime(String teacherId);

	@Query(nativeQuery = true, value = " select teacher_id, count(1) count from kmt_course where teacher_id in (?1) and plan_status = ?2 group by teacher_id")
	List<Map<String, Object>> getTeachCount(String[] teacherIds, int planStatus);

	@Query(nativeQuery = true, value = " select teacher_id, count(distinct vip_id) count from kmt_course where teacher_id in (?1) group by teacher_id")
	List<Map<String, Object>> getTeachStudentCount(String[] teacherIds);

	@Query(nativeQuery = true, value = " select count(distinct vip_id) count from kmt_course where teacher_id = ?1 group by teacher_id")
	int countDistinctByTeacherId(String teacherId);

	@Query(nativeQuery = true, value = "select teacher_id from kmt_course where vip_id=?1 and TO_DAYS(course_time) > TO_DAYS(now()) order by course_time limit 1")
	String getMyTeacherId(String vipId);

	@Query(nativeQuery = true, value = "select teacher_id from kmt_course where vip_id=?1 and course_time = ?2 order by course_time limit 1")
	String getMyTeacherId(String vipId, String date);

	@Query(nativeQuery = true, value = "select distinct teacher_id from kmt_course where (start_time> ?2 and start_time <?3) or (end_time > ?2 and end_time < ?3) and course_time=?1")
	List<String> getNoTimeTeacherList(String date, String beginTime, String endTime);

	@Query(nativeQuery = true, value = "select distinct teacher_id from kmt_course where (start_time> ?2 and start_time <?3) or (end_time > ?2 and end_time < ?3) and week in (?1)")
	List<String> getNoTimeTeacherListByWeek(int[] weeks, String beginTime, String endTime);

	@Query(nativeQuery = true, value = "select distinct teacher_id from kmt_course where (start_time> ?2 and start_time <?3) or (end_time > ?2 and end_time < ?3) and week in (?1) and teacher_id in (?4)")
	List<String> getNoTimeTeacherListByWeek(int[] weeks, String beginTime, String endTime, List<String> tids);

	@Query(nativeQuery = true, value = "select distinct teacher_id from kmt_course where (start_time> ?2 and start_time <?3) or (end_time > ?2 and end_time < ?3) and course_time=?1 and teacher_id=?4")
	List<String> getNoTimeTeacherList(String date, String beginTime, String endTime, String teacherId);

	List<KmtCourse> findByTeacherId(String teacherId);

	List<KmtCourse> findByLessonServiceIdOrderByCourseTimeAscStartTimeAscIsFinishDesc(String sid);

	List<KmtCourse> findByLessonServiceIdInAndNum(String[] ids, int num);

	List<KmtCourse> findByTeacherIdAndAndCourseTimeAndState(String teacherId, String courseDate, int state);

	@Query(nativeQuery = true, value = "select * from kmt_course where is_finish = 0 and state =1 and vip_id = ?1")
	List<KmtCourse> findCourse4ChangeTeacher(String vipId);

	@Modifying
	@Query(nativeQuery = true, value = " update `kmt_course` set plan_status=3,confirm_time=now() where plan_status < 5 and lesson_service_id=?1  ")
	void confirmLesson(String lessonServiceId);
	
	@Query(nativeQuery = true, value = "select count(*) from tb_lesson_service t LEFT JOIN kmt_course k ON t.id = k.lesson_service_id "
			+ "where (t.`status` = 0 or t.`status` = 1 or t.`status` = 3) and k.lesson_service_id != '' and (k.teacher_id = ?1 or k.apply_teacher_id = ?1) and ((k.start_time >= ?2 and k.start_time < ?3) or (?2 < k.end_time and k.end_time <= ?3)) and k.course_time = ?4 and k.is_group = 0")
	int validateTeacherCourse(String teacherId, String startTime, String endTime, String courseTime);
	
	List<KmtCourse> findByVipIdAndLessonServiceIdAndPlanStatusLessThanOrderByCourseTime(String vipId, String lessonServiceId, int planStatus);
	
	@Query(nativeQuery = true, value = "select teacher_id from kmt_course k where k.lesson_service_id = ?1 and k.teacher_id != '' GROUP BY k.teacher_id")
	List<String> findTeachersByLessonServiceId(String lessonServiceId);
	
	@Query(nativeQuery = true, value = "select count(*) from kmt_course k where k.lesson_service_id = ?1 and k.course_time >=?2 and k.course_time <=?3 and k.is_finish = 1")
	int countByCurrentMonthFinishLesson(String lessonServiceId, String monthBeginTime, String monthEndTime);

	int countByTeacherIdAndIsFinish(String teacherId, int isFinish);

	@Query(nativeQuery = true, value = "select distinct lesson_service_id from kmt_course where vip_id = ?1 and course_time= ?2 and is_group = ?3")
	List<String> findByVipIdAndDate(String vipId, String date, Integer isGroup);
	
	List<KmtCourse> findByLessonServiceId(String lessonServiceId);
	
	@Query(nativeQuery = true, value = "select * from kmt_course k where k.vip_id = ?1 and k.plan_status = 5 and "
			+ "TO_DAYS(k.course_time) >=  TO_DAYS(?2) and  TO_DAYS(k.course_time) <=  TO_DAYS(?3) GROUP BY k.course_time")
	List<KmtCourse> findFinishCourseByVipIdAndTimes(String vipId, String startTime, String endTime);

	/**
	 * 查找上一节课
	 * @param courseId
	 * @return
	 */
	@Query(nativeQuery = true, value = "select t1.* " +
			"from kmt_course t1, " +
			"  (select " +
			"     t.num, " +
			"     t.lesson_service_id " +
			"   from kmt_course t " +
			"   where t.id = ?1) t2 " +
			"where t1.lesson_service_id = t2.lesson_service_id " +
			"      and t1.num = (t2.num - 1) limit 1")
	KmtCourse findLastCourse(String courseId);

	@Query(nativeQuery = true, value = "select distinct kmt_course.course_time " +
			"from kmt_course, tb_lesson_service, ssj_lesson_group " +
			"where kmt_course.lesson_service_id = tb_lesson_service.id " +
			"  and tb_lesson_service.status = 1 " +
			"  and tb_lesson_service.lesson_group_id = ssj_lesson_group.id " +
			"  and ssj_lesson_group.teacher_id = ?1 " +
			"  and date(kmt_course.course_time) >= date(now()) " +
			"  and tb_lesson_service.create_time <= ?2 " +
			"group by ssj_lesson_group.id, kmt_course.group_course_id " +
			"order by course_time")
	List<Date> findLessonGroupDateList(String teacherId, String timeStr);

	@Query(nativeQuery = true, value = "select count(distinct kmt_course.vip_id) studentSum, tb_lib_join.id libId from kmt_course, tb_lesson_service, tb_lib_join " +
			"where kmt_course.lesson_service_id = tb_lesson_service.id " +
			"and tb_lesson_service.lib_id = tb_lib_join.id " +
			"and date(kmt_course.course_time) between ?1 and ?2 " +
			"and kmt_course.plan_status = 5 " +
			"and tb_lib_join.libtype in (2, 3) " +
			"group by tb_lib_join.id")
    List<Map<String, Object>> countStudentSumStudyingInSharedLib(String startTime, String endTime);

	/**
	 * 根据小组id查询小组内所有学生的课程（用于确认上课意向）
	 */
	@Query(nativeQuery = true, value = "select kmt_course.* " +
			"from kmt_course, tb_lesson_service, ssj_lesson_group " +
			"where kmt_course.lesson_service_id = tb_lesson_service.id " +
			"and tb_lesson_service.lesson_group_id = ssj_lesson_group.id " +
			"and kmt_course.num >= ssj_lesson_group.current_count " +
			"and tb_lesson_service.status in (0, 1) " +
			"and ssj_lesson_group.id = ?1 " +
			"and kmt_course.plan_status in ?2 ")
	List<KmtCourse> findCourseListByLessonGroupId(String lessonGroupId, List<Integer> planStatus);

	/**
	 * 根据组课程id和小组id查询小组该课次下的一堂课程
	 * @param groupCourseId
	 * @param lessonGroupId
	 * @param timeStr
	 */
	@Query(nativeQuery = true, value = "select kmt_course.* " +
			"from kmt_course, tb_lesson_service, ssj_lesson_group " +
			"where kmt_course.lesson_service_id = tb_lesson_service.id " +
			"  and tb_lesson_service.status = 1 " +
			"  and tb_lesson_service.lesson_group_id = ssj_lesson_group.id " +
			"  and kmt_course.group_course_id = ?1 " +
			"  and ssj_lesson_group.id = ?2 " +
			"  and tb_lesson_service.create_time <= ?3 " +
			"limit 1")
	KmtCourse findOneByGroupCourseIdAndLessonGroupId(String groupCourseId, String lessonGroupId, String timeStr);

    /**
     * 查找小组备课信息
     * @param groupCourseId
     * @return
     */
    List<KmtCourse> findByGroupCourseId(String groupCourseId);
    
    @Query(nativeQuery = true, value = "select k.*,g.id from kmt_course k LEFT JOIN tb_lesson_service t ON k.lesson_service_id = t.id " + 
    		"LEFT JOIN ssj_lesson_group g ON t.lesson_group_id = g.id LEFT JOIN tb_lib_vip v ON t.vip_id = v.id " +
    		"where t.status = 1 and k.is_group = 1 and k.num = g.current_count and g.id = ?1 and v.child_name = ?2 ")
    public List<KmtCourse> findFaceCourseByGroupIdAndChildName(String lessonGroupId, String childName);
    
    @Query(nativeQuery = true, value = "select k.*,g.id from kmt_course k LEFT JOIN tb_lesson_service t ON k.lesson_service_id = t.id " + 
    		"LEFT JOIN ssj_lesson_group g ON t.lesson_group_id = g.id LEFT JOIN tb_lib_vip v ON t.vip_id = v.id " +
    		"where t.status = 1 and k.is_group = 1 and k.num = g.current_count and g.id = ?1 and v.flow_id = ?2")
    public List<KmtCourse> findFaceCourseByGroupIdAndChildNameAndFlowId(String lessonGroupId, String flowId);

	/**
	 * 查询已完课，家长评星为空
	 * @param isFinish
	 * @return
	 */
	List<KmtCourse>  findByIsFinishAndParentJudgeLeverIsNull(Integer isFinish);
	
	@Query(nativeQuery = true, value = "select k.*,g.id from kmt_course k LEFT JOIN tb_lesson_service t ON k.lesson_service_id = t.id " + 
    		"LEFT JOIN ssj_lesson_group g ON t.lesson_group_id = g.id LEFT JOIN tb_lib_vip v ON t.vip_id = v.id " +
    		"where k.is_group = 1 and k.num = g.current_count and g.plan_status = 4 and g.id = ?1 and v.id = ?2 ")
	public KmtCourse findByLessonGroupIdAndVipId(String lessonGroupId, String vipId);

	@Query(nativeQuery = true, value = "select count(*) from kmt_course k LEFT JOIN tb_lesson_service t ON k.lesson_service_id = t.id " + 
    		"where t.lesson_group_id = ?1 and k.num = ?2 and k.course_know_id is not null")
	public int validateGroupCourseKnow(String lessonGroupId, int currentCount);

	@Query(nativeQuery = true, value = "select count(1) " +
			"from kmt_course, tb_lesson_service, ssj_lesson_group " +
			"where kmt_course.lesson_service_id = tb_lesson_service.id " +
			"  and tb_lesson_service.lesson_group_id = ssj_lesson_group.id " +
			"  and tb_lesson_service.status = 1 " +
			"  and ssj_lesson_group.id = ?1 ")
    Integer countCourseSumByLessonGroupId(String lessonGroupId);
	
	@Query(nativeQuery = true, value = "select k.*,g.id from kmt_course k LEFT JOIN tb_lesson_service t ON k.lesson_service_id = t.id " + 
    		"LEFT JOIN ssj_lesson_group g ON t.lesson_group_id = g.id " +
    		"where t.status = 1 and k.is_group = 1 and k.num = g.current_count and k.plan_status != 5 and k.sign_in_off = 1 and g.id = ?1 limit 1")
	public KmtCourse findCourseRandomStudent(String lessonGroupId);
}
