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

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 com.ssj.bean.sys.lesson.domain.LessonService;

import java.util.List;

@Repository
public interface LessonServiceDao extends JpaRepository<LessonService, String> {
    public LessonService findFirstByVipIdAndMasterIdAndStatusOrderByCreateTimeDesc(String vipId, String masterId, int status);
    @Query(nativeQuery = true, value = "select * from tb_lesson_service t where t.master_id = ?2 and t.vip_id = ?1 and t.course_name=?3 limit 1")
    public LessonService findFirstByVipIdAndMasterIdAndCourseNameOrderByCreateTimeDesc(String vipId, String masterId, String subject);

    public LessonService findByOrderNo(String orderNo);
    

    
    @Query(nativeQuery = true, value = "update tb_lesson_service set `status` = 2 where id =?1")
	@Modifying
	public int updateServiceStatus(String id);

    @Query(nativeQuery = true, value = "select * from  tb_lesson_service  where order_no =?1 ")
	public LessonService findServiceByOrderNo(String orderNo);
    
    public LessonService findByOrderIdAndStatusIn (String orderId, int[] status);

    public int countDistinctByMasterId(String masterId);
    
    public LessonService findByOrderId(String orderId);
    
    @Query(nativeQuery = true, value = "select * from tb_lesson_service where id = ?1 and master_id = ?2 and vip_id = ?3 and course_name = ?4 and (`status` = 1 or (`status` = 0 and is_listen_test = 1))")
    public LessonService findByIdAndMasterIdAndVipIdAndCourseName(String id,String masterId,String vipId,String courseName);
    
    @Query(nativeQuery = true, value = "delete tb_lesson_service,kmt_course from tb_lesson_service LEFT JOIN kmt_course ON tb_lesson_service.id = kmt_course.lesson_service_id "
    		+ "where tb_lesson_service.`status` = 0 and date_add(tb_lesson_service.create_time, interval 12 hour) < NOW()")
	@Modifying
    public void deleteByInvalidLessonService();
    
    @Query(nativeQuery = true, value = "delete from ssj_lesson_group where not exists (select * from tb_lesson_service where tb_lesson_service.lesson_group_id = ssj_lesson_group.id)")
	@Modifying
    public void deleteByLessonGroup();
    
    @Query(nativeQuery = true, value = "SELECT * from tb_lesson_service t where t.master_id=?2 AND t.vip_id=?1 AND t.`status` !=0")
    public List<LessonService> findLessonServiceByVipId(String vipId,String masterId);

    @Query(nativeQuery = true, value = "select t.id from tb_lesson_service t LEFT JOIN kmt_course k ON t.id = k.lesson_service_id "
    		+ "where t.`status` = 0 and t.is_listen_test = 1 and k.course_type = 0 and k.plan_status <4 and k.course_time < ?1")
    public List<String> findByNoPayIsListen(String courseTime);

    @Query(nativeQuery = true, value = "delete from kmt_course where kmt_course.lesson_service_id = ?1")
    @Modifying
    public void deleteKmtCourse(String lessonServiceId);

    @Query(nativeQuery = true, value = "select * from tb_lesson_service where master_id = ?1 and vip_id = ?2 and `status` > 0 ORDER BY create_time desc")
    public List<LessonService> findAllPayLessonService(String masterId, String vipId);
    
    @Query(nativeQuery = true, value = "select * from tb_lesson_service where master_id = ?1 and vip_id = ?2 and course_name = ?3 and `status` > 0 ORDER BY create_time desc")
    public List<LessonService> findAllPayLessonService(String masterId, String vipId,String courseName);
    
    public int countByVipIdAndMasterId(String vipId,String masterId);

    /**
     * 根据校长的userId查询其名下的一对一订单
     * @param schoolmasterUserId 校长的userId
     * @return
     */
    List<LessonService> findByMasterId(String schoolmasterUserId);
    
    @Query(nativeQuery = true, value = "select count(1) from tb_lesson_service where vip_id = ?1 and is_group = ?2 and `status` != 2 and `status` != 4")
    public int checkIsLessonOrGroupService(String vipId, int isGroup);

    List<LessonService> findAllByLessonGroupId(String lessonGroupId);

    Integer countByLessonGroupIdAndStatusIn(String lessonGroupId, List<Integer> statusList);

    /**
     * 根据校长的userId查询其名下小于status的订单
     * @param masterId 校长的userId
     * @param status 哪个状态
     * @return
     */
    List<LessonService> findByMasterIdAndStatusLessThan(String masterId, int status);
    
    public int countByLessonGroupIdAndVipIdIn(String lessonGroupId, List<String> vipIdList);
    
    @Query(nativeQuery = true, value = "select t.* from tb_lesson_service t LEFT JOIN ssj_lesson_group g ON t.lesson_group_id = g.id "
    		+ "LEFT JOIN kmt_course k ON t.id = k.lesson_service_id "
    		+ "where k.is_group = 1 and k.sign_in_off = 0 and k.num = g.current_count and t.`status` = 1 and g.id = ?1")
    public List<LessonService> findNoSignInOffLessonList(String lessonGroupId);

    @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 kmt_course.num = ssj_lesson_group.current_count " +
            "   and tb_lesson_service.lesson_group_id = ssj_lesson_group.id and tb_lesson_service.lesson_group_id = ?1" +
            "   and tb_lesson_service.status in ?2 ")
    Integer countCurrentCourseByLessonGroupIdAndStatus(String lessonGroupId, List<Integer> lessonServiceStatusList);
}
