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

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


@Repository
public interface KmtArrangeAllotDao extends JpaRepository<KmtArrangeAllot, String> {

    @Query(nativeQuery = true,value = "select count(1) from kmt_arrange_allot where teacher_id=?1 and arrange_date>?2 and status= 1 ")
    Integer findMonthDutyCount(String teacherUserId, String preMonth);



    @Query(nativeQuery = true,value = "update kmt_arrange_detail  \n" +
            "set have_num = have_num + ?1\n" +
            "where  id  in (\n" +
            "SELECT t.arrange_detail_id from kmt_arrange_allot t        \n" +
            "           	where DATE_FORMAT(t.arrange_date,'%Y-%m') =?2   and  t.teacher_id =?3 and t.status= 1\n" +
            ") and DATE_FORMAT(arrange_date,'%Y-%m') =?2 ")
    @Modifying
    void updateArrangeDetailHaveNum(int  num ,String preMonth, String teacherUserId);


    @Query(nativeQuery = true,value = "update   kmt_arrange_allot set status=0 where teacher_id=?2 and DATE_FORMAT(arrange_date,'%Y-%m')=?1")
    @Modifying
    void delOldAllotData(String preMonth, String teacherUserId);
    @Query(nativeQuery = true,value = "insert into kmt_arrange_allot(id,arrange_detail_id,teacher_id,arrange_date,attend_type,status,create_time)\n" +
            "SELECT UUID(),a.id,?2,a.arrange_date,0,1,NOW()\n" +
            "from kmt_arrange_detail a\n" +
            "INNER JOIN tb_lib_manager b on a.`subject` = b.course_name and a.type=b.type and b.user_id=?2\n" +
            "where a.id in (?1) and DATE_FORMAT(a.arrange_date,'%Y-%m') = ?3")
    @Modifying
    void insertNewAllotData(List<String> ids, String teacherUserId, String preMonth);

    @Query(nativeQuery = true,value = "update   kmt_arrange_detail set is_open=0 where arrange_date =?1")
    @Modifying
    void updateTemplateisOpen(String str);

	@Query(nativeQuery = true, value = " SELECT * FROM `kmt_arrange_allot` t where t.arrange_detail_id=?1 AND t.teacher_id=?2 LIMIT 1 ")
	KmtArrangeAllot getKmtArrangeAllotByTeacher(String arrangeDetailId,String teacherId);

	@Modifying
	@Query(nativeQuery = true, value = " update `kmt_arrange_allot` set `status`=?3 where arrange_detail_id=?1 AND teacher_id=?2  ")
	void updateKmtArrangeAllotToStatus(String arrangeDetailId,String teacherId,Integer status);

	@Query(nativeQuery = true, value = " SELECT COUNT(id) from kmt_arrange_allot t where t.arrange_detail_id=?1 AND t.`status`=1 ")
	Integer countKmtArrangeAllotByArrangeDetailId(String arrangeDetailId);

	@Query(nativeQuery = true, value = " SELECT teacher_id from kmt_arrange_allot t where t.arrange_detail_id=?1 AND t.`status`=1 ")
	List<String> findKmtArrangeAllotByArrangeDetailId(String arrangeDetailId);

	@Query(nativeQuery = true,value = "select t2.* from kmt_arrange_allot t1 left join kmt_arrange_detail t2 on t1.arrange_detail_id=t2.id and t1.teacher_id=?1 and t1.arrange_date=?2")
	List<Map<String,Object>> findAttendanceType(String teacherId,String date);

	@Modifying
    @Query(nativeQuery = true,value = "update kmt_arrange_allot set attend_type=1 where id=?1 and arrange_date regexp?2 and status=1")
	void updateKmtArrangeAllotByID(String id,String date);

    @Modifying
    @Query(nativeQuery = true,value = "update kmt_arrange_detail  INNER JOIN (\n" +
            "		SELECT t11.id,count(if(t.id is not null and t.status=1 ,true,null)) as has_num from kmt_arrange_allot t\n" +
            "	RIGHT JOIN kmt_arrange_detail t11 on t.arrange_detail_id=t11.id\n" +
            "	where DATE_FORMAT(t11.arrange_date,'%Y-%m') =?1  \n" +
            "	GROUP BY t11.id\n" +
            ")b on kmt_arrange_detail.id = b.id set kmt_arrange_detail.have_num=b.has_num ")
    void updateDetailData(String preMonth);

    @Modifying
    @Query(nativeQuery = true,value = "update   kmt_arrange_detail set is_open=1 where DATE_FORMAT(arrange_date,'%Y-%m') =?1")
    void updateTemplateisClose(String preMonth);

	@Query(nativeQuery = true,value = "select count(id) from kmt_arrange_allot where teacher_id=?1 and arrange_date regexp ?2  and status=1")
	Integer countByTeacherIdAndAttendType(String teacherId,String date);

    @Query(nativeQuery = true,value = "select count(id) from kmt_arrange_allot where teacher_id=?2 and status=1 and  DATE_FORMAT(arrange_date,'%Y-%m') =?1")
    Integer findCountByTeacherId(String thisMonth, String teacherUserId);

    @Query(nativeQuery = true,value = "select count(id) from kmt_arrange_allot where teacher_id=?1 and arrange_date regexp ?2 and attend_type=1 and status=1")
    Integer countValidByTypeAndState(String teacherId,String date);

    @Query(nativeQuery = true,value = "SELECT t1.teacher_id from  kmt_arrange_allot t1 LEFT JOIN  kmt_arrange_detail  t2 on t1.arrange_detail_id = t2.id " +
            "where  t2.start_interval = ?1 and t2.end_interval = ?2 and TO_DAYS(t2.arrange_date)=TO_DAYS(NOW()) and t1.`status`=1 and t1.teacher_id = ?3 GROUP BY t1.teacher_id ")
    public String findTeacherArrange(String startInterval, String endInterval,String teacherId);
    
    
    @Query(nativeQuery = true, value = " SELECT t.* FROM `kmt_arrange_allot` t LEFT JOIN kmt_arrange_detail d ON t.arrange_detail_id=d.id " +
            "where t.teacher_id=?1 AND NOW()<CONCAT(d.arrange_date,' ',d.start_interval,':00') " +
            "and t.status =1 " +
            "ORDER BY d.arrange_date ASC,d.start_interval ASC ")
	List<KmtArrangeAllot> findKmtArrangeAllotByNextCurTime(String teacherId);


    @Modifying
    @Query(nativeQuery = true,value = "select arrange_detail_id from  kmt_arrange_allot where DATE_FORMAT(arrange_date,'%Y-%m') =?1 and teacher_id=?2 and status=1 ")
    List<String> findTeacherDetailList(String preMonth, String teacherUserId);
}
