package com.ssj.dao.sys.lesson.dao;

import com.ssj.bean.sys.lesson.domain.LessonGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LessonGroupDao extends JpaRepository<LessonGroup, String> {

    /**
     * 根据小组id和老师id和计划状态查询课程
     */
    @Query(nativeQuery = true, value = "select * from ssj_lesson_group where id = ?1 and teacher_id = ?2 and plan_status = ?3 ")
    LessonGroup findByIdAndTeacherIdAndPlanStatus(String lessonGroupId, String teacherId, Integer planStatus);

    public LessonGroup findByIdAndMasterId(String lessonGroupId, String masterId);

    @Query(nativeQuery = true, value = "select ssj_lesson_group.* " +
            "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.current_count = kmt_course.num " +
            "  and ssj_lesson_group.plan_status < 4 " +
            "  and ssj_lesson_group.teacher_id = ?1 " +
            "  and now() > concat(date(kmt_course.course_time), ' ', kmt_course.end_time, ':00') " +
            "group by ssj_lesson_group.id")
    List<LessonGroup> findLessonGroupTeacherAbsentByTeacherId(String teacherId);

    @Query(nativeQuery = true, value = "select ssj_lesson_group.* " +
            "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.current_count = kmt_course.num " +
            "  and ssj_lesson_group.plan_status < 4 " +
            "  and ssj_lesson_group.master_id = ?1 " +
            "  and now() > concat(date(kmt_course.course_time), ' ', kmt_course.end_time, ':00') " +
            "group by ssj_lesson_group.id")
    List<LessonGroup> findLessonGroupTeacherAbsentByMasterId(String masterId);

    @Query(nativeQuery = true, value = "select * from ssj_lesson_group, tb_lesson_service " +
            "where ssj_lesson_group.id = tb_lesson_service.lesson_group_id and ssj_lesson_group.is_finish = 0 " +
            "   and tb_lesson_service.status < 2 and tb_lesson_service.vip_id = ?1")
    List<LessonGroup> findByVipId(String vipId);
}
