package com.ssj.dao.weixin.zuoyb.dao;


import com.ssj.bean.weixin.zyb.domain.ZuoybKnowHowDetails;
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;

/** 
 * @author  ZhangCaibao 
 * @version 2017年9月26日 下午2:02:05 
 * 类说明 
 */
@Repository
public interface ZuoybKnowHowDetailsDao extends JpaRepository<ZuoybKnowHowDetails, String>{

	
	 @Query(nativeQuery = true, value = "delete from  zyb_know_how_details  where how_id = ?1   ")
	 @Modifying
	 public void deleteByHowId(String howId);
	 @Query(nativeQuery = true, value = "delete from  zyb_know_how_details_sort  where how_id = ?1   ")
	 @Modifying
	 public void deleteSortByHowId(String howId);
	 @Query(nativeQuery = true, value = "delete from  zyb_know_how_details_kd  where how_id = ?1   ")
	 @Modifying
	 public void deleteKdByHowId(String howId);
	 @Query(nativeQuery = true, value = "delete from  zyb_know_how_details_sort  where how_details_id = ?1   ")
	 @Modifying
	 public void deleteSortByHowDetailsId(String detailshowId);
	 @Query(nativeQuery = true, value = "delete from  zyb_know_how_details_kd  where how_details_id = ?1   ")
	 @Modifying
	 public void deleteKdByHowDetailsId(String detailshowId);
	 
	 @Query(nativeQuery = true, value = "select  *   from  zyb_know_how_details  where id  in  (?1)   ")
	 public List<ZuoybKnowHowDetails> findByIds(List<String> ids);
	 @Query(nativeQuery = true, value = "select  name   from  zyb_know_how_details  where id  in  (?1)   ")
	 public List<String> findKnowHowDetailsByIds(List<String> ids);
	  
	 public Page<ZuoybKnowHowDetails> findAll(Specification<ZuoybKnowHowDetails> specification, Pageable page);
	 
	 public List<ZuoybKnowHowDetails> findByHowId(String howId);
	 
	 @Query(nativeQuery = true, value = " SELECT \n" +
			 "t.`id`, t.`how_id`, t.`create_time`, t.`lib_id`, s.alias as  `name`, t.`num`, t.`before_name`,\n" +
			 "t.`after_name`, t.`type`, t.`is_hot`, s.`memorize`, s.`understanding`,\n" +
			 "s.`apply`, t.`duration`, s.`two_module`, s.`module`, s.`jxmb`, s.`zdbx`, s.`ydsl`, t.`img`, t.`is_zt`\n" +
			 "			   from    zyb_know_how_details t \n" +
			 "			  JOIN zyb_know_how_details_sort s on s.how_details_id = t.id  \n" +
			 "			 where  s.stat =2 and  s.lib_id =?1 and s.type = ?5 and s.course_name =?2 and s.grade =?3 and s.semester = ?4\n" +
			 "			 and t.how_id =?6 \n" +
			 "	GROUP BY t.id		 ORDER BY s.num asc ")
	 public List<ZuoybKnowHowDetails> findByHowIdNotNull(String libId,String courseName,String grade,String semester ,String type,String howId);
	 @Query(nativeQuery = true, value = " SELECT \n" +
			 "t.`id`, t.`how_id`, t.`create_time`, t.`lib_id`, s.alias as  `name`, t.`num`, t.`before_name`,\n" +
			 "t.`after_name`, t.`type`, t.`is_hot`, t.`memorize`, t.`understanding`,\n" +
			 "t.`apply`, t.`duration`, t.`two_module`, t.`module`, t.`jxmb`, t.`zdbx`, t.`ydsl`, t.`img`, t.`is_zt`\n" +
			 "			   from    zyb_know_how_details t \n" +
			 "			  JOIN zyb_know_how_details_sort s on s.how_details_id = t.id  \n" +
			 "			 where s.stat =2 and s.lib_id =?1 and s.type = ?5 and s.course_name =?2 and s.grade =?3 and s.semester = ?4\n" +
			 "			 and t.how_id =?6 and s.num = ?7 \n" +
			 "		GROUP BY t.id	 ORDER BY s.num asc ")
	 public List<ZuoybKnowHowDetails> findByHowIdNotNull(String libId,String courseName,String grade,String semester ,String type,String howId
			 ,int weekNum);
	 

	 @Query(nativeQuery = true, value = "update zyb_know_how_details set num = ?2 where id =?1 ")
	 @Modifying
	 public void  updateKnowHowDetailNum(String id,Integer num);
	 
	 @Query(nativeQuery = true, value = "SELECT\n" +
			 " s.*\n" +
			 "FROM\n" +
			 "      zyb_know_how_details s \n" +
			 "WHERE\n" +
			 "	 FIND_IN_SET( s.id,?1)")
	 public  List<ZuoybKnowHowDetails> getNameByHowId(String howDetialsId);
	 
	 @Query(nativeQuery = true, value = "select  *  from  zyb_know_how_details where how_id =?1 and name=?2   ")
	 public ZuoybKnowHowDetails findByHowIdAndName(String howId,String name);
	 
	 @Query(nativeQuery = true, value = "SELECT DISTINCT s.id,s.how_id,s.create_time,s.lib_id,\n" +
			 "			 s.`name`,s.before_name,s.after_name,s.type,s.is_hot,s.memorize\n" +
			 "			 ,s.understanding,s.apply,s.duration,s.two_module ,t.num,s.module \n" +
			 "			   from    zyb_know_how_details s \n" +
			 "			  JOIN zyb_know_how_details_sort t on t.how_details_id = s.id  \n" +
			 "			 where t.lib_id =?1 and t.type = ?5 and t.course_name =?2 and t.grade =?3 and t.semester = ?4\n" +
			 "			 and  t.num >=?6 and  t.num <= ?7 \n" +
			 "			 ORDER BY t.num asc ")
	 public List<Object[]> nextMonthHowDtails(String libId,
				String courseName, String grade, String semester, String type,
				int start ,int  end);
	 
	 @Query(nativeQuery = true, value = "SELECT DISTINCT s.`name` \n" +
			 "			   from    zyb_know_how_details s \n" +
			 "			  JOIN zyb_know_how_details_sort t on t.how_details_id = s.id  \n" +
			 "			 where t.lib_id =?1 and t.type = ?5 and t.course_name =?2 and t.grade =?3 and t.semester = ?4\n" +
			 "			 and  t.num >?6 and  t.num <= ?7 \n" +
			 "			 ORDER BY t.num asc ")
	 public List<String> nextMonthZuoybHowDtails(String libId,
			 String courseName, String grade, String semester, String type,
			 int start ,int  end);
	 
	 @Query(nativeQuery = true, value = " SELECT t.* from zyb_know_how_details t  WHERE t.id in (?1) GROUP BY t.id ")
	  public List<ZuoybKnowHowDetails> getZybKnowHowById(List<String> ids);
	 
	 
	 @Query(nativeQuery = true, value = " SELECT DISTINCT d.* FROM `kmt_course_know_detail` t LEFT JOIN zyb_know_how_details d ON t.know_id=d.id  where t.course_know_id=? AND d.id is NOT null ORDER BY t.num ASC ")
	  public List<ZuoybKnowHowDetails> findCourseKnowDetailList(String courseKnowId);
	
	 @Query(nativeQuery = true, value = "select  name,ydsl   from  zyb_know_how_details  where id  in  (?1)   ")
	  public List<Object[]> findKnowAndYdslByIds(List<String> ids);

	/**
	 * 查找错题知识点
	 * @param courseKnowId
	 * @return
	 */
	@Query(nativeQuery = true, value = " SELECT distinct t2.two_module FROM kmt_img_face_detail t1 " +
			" JOIN  zyb_know_how_details t2 ON t1.how_details_id = t2.id  " +
			" join kmt_img_face t3 on t1.face_id = t3.id " +
			"where t3.course_know_id=?1 ")
	List<ZuoybKnowHowDetails> findWrongCourseKnowDetailList(String courseKnowId);

	List<ZuoybKnowHowDetails> findByNum(int num);

	@Query(nativeQuery = true, value = "select * from zyb_know_how_details, scon_homework_wrong_question, scon_homework_how_details " +
			"where zyb_know_how_details.id = how_detail_id and " +
			"wrong_question_id = scon_homework_wrong_question.id and " +
			"scon_homework_wrong_question.homework_id = ?1")
   List<ZuoybKnowHowDetails> findForWrongQuestion(String workId);




	@Query(nativeQuery = true, value = "SELECT\n" +
			"	d.id  \n" +
			"FROM\n" +
			"	zyb_know_how_details d\n" +
			" JOIN zyb_know_how_details_sort t ON d.id = t.how_details_id\n" +
			"AND t.stat = 2\n" +
			"WHERE  \n" +
			"EXISTS (\n" +
			"select h.id from  zyb_know_how h LEFT JOIN   zyb_know_how_details_sort rt ON h.id = rt.how_id and rt.stat=1\n" +
			"where \n" +
			"	rt.lib_id =?1\n" +
			"AND rt.type = ?5\n" +
			"AND rt.course_name =?2\n" +
			"AND rt.grade =?3\n" +
			"AND rt.semester = ?4\n" +
			"and rt.alias LIKE '%unit%'\n" +
			"and t.how_id = h.id\n" +
			"and h.num >=?6 and h.num <=?7\n" +
			")\n" +
			"and t.lib_id =?1\n" +
			"AND t.type = ?5\n" +
			"AND t.course_name =?2\n" +
			"AND t.grade =?3\n" +
			"AND t.semester = ?4 order by t.num")
	public List<String> getHowDetailsIds(String libId, String courseName,String grade, String semester, String type, int start, int end);


	@Query(nativeQuery = true, value = "SELECT\n" +
			"	d.id,d.name,d.ydsl ,d.how_id \n" +
			"FROM\n" +
			"	zyb_know_how_details d\n" +
			" JOIN zyb_know_how_details_sort t ON d.id = t.how_details_id\n" +
			"AND t.stat = 2\n" +
			"WHERE  \n" +
			"EXISTS (\n" +
			"select h.id from  zyb_know_how h LEFT JOIN   zyb_know_how_details_sort rt ON h.id = rt.how_id and rt.stat=1\n" +
			"where \n" +
			"	rt.lib_id =?1\n" +
			"AND rt.type = ?5\n" +
			"AND rt.course_name =?2\n" +
			"AND rt.grade =?3\n" +
			"AND rt.semester = ?4\n" +
			"and rt.alias LIKE '%unit%'\n" +
			"and t.how_id = h.id\n" +
			"and h.num >=?6 and h.num <=?7\n" +
			")\n" +
			"and t.lib_id =?1\n" +
			"AND t.type = ?5\n" +
			"AND t.course_name =?2\n" +
			"AND t.grade =?3\n" +
			"AND t.semester = ?4\n" +
			"and d.module =?8 order by t.num ")
	public List<Object[]> getHowDetailsIds(String libId, String courseName,String grade, String semester, String type, int start, int end,String module);
	@Query(nativeQuery = true, value = "SELECT\n" +
			"				d.id,d.name,d.ydsl ,d.how_id,d.img \n" +
			"			FROM\n" +
			"				zyb_know_how_details d\n" +
			"			 JOIN zyb_know_how_details_sort t ON d.id = t.how_details_id\n" +
			"			AND t.stat = 2\n" +
			"			WHERE  \n" +
			"			EXISTS (\n" +
			"				select h.id from  zyb_know_how h LEFT JOIN   zyb_know_how_details_sort rt ON h.id = rt.how_id and rt.stat=1\n" +
			"				where \n" +
			"					rt.lib_id =?1\n" +
			"				AND rt.type = ?5\n" +
			"				AND rt.course_name =?2\n" +
			"				AND rt.grade =?3\n" +
			"				AND rt.semester = ?4\n" +
			"				and rt.alias  =?6\n" +
			"				and t.how_id = h.id\n" +
			"				\n" +
			"			)\n" +
			"			and t.lib_id =?1\n" +
			"			AND t.type = ?5\n" +
			"			AND t.course_name =?2\n" +
			"			AND t.grade =?3\n" +
			"			AND t.semester = ?4\n" +
			"			 order by t.num asc ,  d.num asc ")
	public List<Object[]> getMathHowDetailsIds(String libId, String courseName,String grade, String semester, String type, String howName);


   @Query(nativeQuery = true, value = " select t1.* from zyb_know_how_details t1 where t1.id in (?1)" +
           "and exists (select 1 from zyb_know_how t2 where t1.how_id = t2.id and t2.name in (?2))")
   List<ZuoybKnowHowDetails> findApplyKnowHowDetails (List<String> ids, List<String> names);

	/**
	 * 查询在线批改作业问题知识点
	 * @param questionId
	 * @return
	 */
	@Query(nativeQuery = true, value = "select t1.* from zyb_know_how_details t1 " +
			" join kmt_exercise_question_know t2 on t1.id = t2.how_details_id" +
			" where t2.question_id = ?1")
	List<ZuoybKnowHowDetails> findByQuestionId(String questionId);

	@Query(nativeQuery = true, value = "SELECT t1.*\n" +
			"FROM zyb_know_how_details t1\n" +
			"where exists(\n" +
			"              select 1\n" +
			"              from scon_homework_how_details t2\n" +
			"              where homework_id = '1'\n" +
			"                and t1.id = t2.how_detail_id\n" +
			"              group by how_detail_id\n" +
			"          )")
	List<ZuoybKnowHowDetails> findByHomeworkId(String homeworkId);


	@Query(nativeQuery = true, value = "SELECT t1.*\n" +
			"FROM zyb_know_how_details t1\n" +
			"where exists(\n" +
			"              select 1\n" +
			"              from scon_homework_how_details t2\n" +
			"              where homework_id = '1'\n" +
			"                and t1.id = t2.how_detail_id\n" +
			"              group by how_detail_id\n" +
			"          )")
	List<ZuoybKnowHowDetails> findModuleBySubject(String homeworkId);
}
