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

import java.util.List;
import java.util.Map;

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


/** 
 * @author  ZhangCaibao 
 * @version 2017年9月26日 下午1:59:50 
 * 类说明 
 */
@Repository
public interface ZuoybKnowHowDao extends JpaRepository<ZuoybKnowHow, String>{
	
	@Query(nativeQuery = true, value = "     SELECT h.`id`, h.`course_name`, h.`create_time`, h.`lib_id`,t.alias as  `name`, h.`num`, h.`module`, h.`is_zt`   from  zyb_know_how  h  \n" +
			"		 JOIN zyb_know_how_details_sort t on t.how_id = h.id \n" +
			"		where  t.stat =1 and  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  ORDER BY t.num asc , h.create_time  asc  ")
	public  List<ZuoybKnowHow> findByCourseNameByGrade(String libId,String courseName,String grade,String semester ,String type,int weekNum );
	@Query(nativeQuery = true, value = "     SELECT h.`id`, h.`course_name`, h.`create_time`, h.`lib_id`,t.alias as  `name`, h.`num`, h.`module`, h.`is_zt`   from  zyb_know_how  h  \n" +
			"		 JOIN zyb_know_how_details_sort t on t.how_id = h.id \n" +
			"		where  t.stat =1 and t.type = ?4 and t.course_name =?1 and t.grade =?2 and t.semester = ?3\n" +
			"		  ORDER BY t.num asc , h.create_time  asc  " )
	public  List<ZuoybKnowHow> findByCourseNameByGrade(String courseName,String grade,String semester ,String type );
	
	@Query(nativeQuery = true, value = "     SELECT h.`id`, h.`course_name`, h.`create_time`, h.`lib_id`,t.alias as  `name`, h.`num`, h.`module`, h.`is_zt`   from  zyb_know_how  h  \n" +
			"		 JOIN zyb_know_how_details_sort t on t.how_id = h.id \n" +
			"		where  t.stat =2 and t.type = ?4 and t.course_name =?1 and t.grade =?2 and t.semester = ?3 and h.is_zt = ?5 \n" +
			"		  ORDER BY t.num asc , h.create_time  asc  " )
	public  List<ZuoybKnowHow> findKnowByCourseNameByGrade(String courseName,String grade,String semester ,String type ,Integer isZt);
	
	@Query(nativeQuery = true, value = "     SELECT h.`id`, h.`course_name`, h.`create_time`, h.`lib_id`,t.alias as  `name`, h.`num`, h.`module`, h.`is_zt`   from  zyb_know_how  h  \n" +
			"		 JOIN zyb_know_how_details_sort t on t.how_id = h.id \n" +
			"		where  t.stat =2 and t.type = ?4 and t.course_name =?1 and t.grade =?2 and t.semester = ?3\n" +
			"		  ORDER BY t.num asc , h.create_time  asc  " )
	public  List<ZuoybKnowHow> findKnowByCourseNameByGrade(String courseName,String grade,String semester ,String type);
	
	@Query(nativeQuery = true, value = "SELECT DISTINCT h.*  from  zyb_know_how  h  JOIN  zyb_know_how_details s \n" +
			"on h.id =s.how_id\n" +
			" JOIN zyb_know_how_details_sort t on t.how_details_id = s.id  and \n" +
			"t.how_id =h.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  ORDER BY t.num asc ,h.create_time asc  \n")
	public  List<ZuoybKnowHow> getTheMonthKnowHowAndModule(String libId,String courseName,String grade,String semester ,String type,int start ,int end );
	
	@Query(nativeQuery = true, value = "SELECT \n" +
			"s.module \n" +
			"FROM\n" +
			"	 zyb_know_how_details s\n" +
			"LEFT JOIN zyb_know_how_details_sort t ON t.how_details_id = s.id\n" +
			"WHERE\n" +
			"	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 t.num >=?6 AND t.num <=?7\n" +
			" GROUP BY s.module  " +
			"ORDER BY\n" +
			"	t.num ASC\n" +
			"	")
	public  List<String> getTheMonthModule(String libId,String courseName,String grade,String semester ,String type,int start ,int end );
	
	
	public Page<ZuoybKnowHow> findAll(Specification<ZuoybKnowHow> specification, Pageable page);
	
	 @Query(nativeQuery = true, value = "update zyb_know_how set num = ?2 where id =?1 ")
	 @Modifying
	 public void  updateKnowHowNum(String id,Integer num);
	 
   @Query(nativeQuery = true, value =" select  count(1)  from  zyb_know_how  where lib_id=?1   ")
   public Integer getLibIdCount(String libId);	 
   
   public List<ZuoybKnowHow> findByLibId(String libId);
   
   
    @Query(nativeQuery = true, value ="INSERT  into   zyb_know_how_details ( id,how_id,create_time,lib_id,num,name )\n" +
			 "			 SELECT  UUID(),?3,now(),?1,num,name  \n" +
			 "       from  zyb_know_how_details where lib_id ='SSJ' and  how_id=?2 ")
	 @Modifying
	 public void insertKnowHowDetail(String libId,String oldHowId,String newHowId);
    
    
    @Query(nativeQuery = true, value ="SELECT\n" +
    		" w.*\n" +
    		"FROM\n" +
    		"  zyb_know_how  w \n" +
    		"	 JOIN    zyb_know_how_details s \n" +
    		"  on s.how_id =w.id \n" +
    		"WHERE\n" +
    		"	 FIND_IN_SET( s.id,?1)\n" +
    		"GROUP BY w.id ")
    public  List<ZuoybKnowHow> getChindIdParentName(String howDetialsId);
    
    
    @Query(nativeQuery = true, value = "select *  from zyb_know_how  where lib_id = ?2 and  course_name = ?3  and name=?1 and num =?4   ")
    public ZuoybKnowHow findByNameAndLibIdAndCourseName(String name,String libId,String courseName,Integer num);
    @Query(nativeQuery = true, value = "select *  from zyb_know_how  where lib_id = ?2 and  course_name = ?3  and name=?1   ")
    public ZuoybKnowHow findByNameAndLibIdAndCourseName(String name,String libId,String courseName);

    @Query(nativeQuery = true, value = "SELECT DISTINCT(h.how_details_id)  from  zyb_student_work_how_details h	LEFT JOIN  zyb_student_work t ON t.id=h.student_work_id  where t.vip_id=?1    ")
    public List<Map<String, Object>> findMasterByVip(String vipId);
    
	@Query(nativeQuery = true, value = " SELECT t.* from zyb_know_how t LEFT JOIN zyb_know_how_details d ON t.id=d.how_id WHERE d.id in (?1) GROUP BY t.id ")
	public List<ZuoybKnowHow> getZybKnowHowByHowDetailsId(String howDetailsIds);

	@Query(nativeQuery = true, value = " SELECT t.* from zyb_know_how t LEFT JOIN zyb_know_how_details d ON t.id=d.how_id WHERE d.id in (?1) GROUP BY t.id ")
	public List<ZuoybKnowHow> getZybKnowHowByHowDetailsIdIn(List<String> howDetailsIds);

	public List<ZuoybKnowHow> findByIdIn(List<String> ids);

	/**
	 * 根据年级、科目、版本、单元名查询单元
	 */
	@Query(nativeQuery = true, value = "select zyb_know_how.* " +
			"from zyb_know_how, zyb_know_how_details_sort " +
			"where zyb_know_how.id = zyb_know_how_details_sort.how_id " +
			"  and zyb_know_how_details_sort.stat = 1 " +
			"  and zyb_know_how_details_sort.grade_num = ?1 " +
			"  and zyb_know_how_details_sort.course_name = ?2 " +
			"  and zyb_know_how_details_sort.type = ?3 " +
			"  and zyb_know_how_details_sort.semester = ?4 " +
			"  and zyb_know_how.name = ?5 " +
			"limit 1 ")
    ZuoybKnowHow findOneByCondition(String gradeNum, String subject, String version, String semester, String knowHowName);
	
	@Query(nativeQuery = true, value = "SELECT name FROM zyb_know_how WHERE id = ?1")
	public String findNameById(String id);
	
	@Query(nativeQuery = true, value = "SELECT" + 
			"	kd.id detailsId," + 
			"	kh.id howId," + 
			"	kds.alias alias," + 
			" IF(kds.course_name = '英语', kh.name, kd.two_module) twoModule," + 
			"	kd.module module " + 
			"FROM" + 
			"	zyb_know_how_details_sort kds" + 
			"	LEFT JOIN zyb_know_how_details kd ON kds.how_details_id = kd.id" + 
			"	LEFT JOIN zyb_know_how kh ON kd.how_id = kh.id " + 
			"WHERE" + 
			"	kds.course_name = ?1 " + 
			"	AND kds.stat = 2 " + 
			"	AND kds.semester = ?3 " + 
			"	AND kds.grade = ?2 " + 
			"	AND kds.type = ?4 " + 
			"	AND kds.lib_id = ?5 " + 
			"	AND kd.is_zt = ?6 " + 
			"GROUP BY" + 
			"	kd.id " + 
			"ORDER BY" + 
			"	kh.create_time")
	public List<Object> findKnowByCourseNameByGradeNew(String courseName, String grade, String semester,
			Integer banben, String libId, Integer isZt);
	
	@Query(nativeQuery = true, value = "SELECT  " + 
			"	kd.id detailsId," + 
			"	kh.id howId," + 
			"	CONCAT(GROUP_CONCAT( IF(kds.stat = 2, kds.alias, NULL) ORDER BY kds.num ),'(',GROUP_CONCAT( IF(kds.stat = 3, kds.alias, NULL) ORDER BY kds.num ), ')') alias,  " + 
			"	kd.two_module twoModule,  " + 
			"	kd.module  module " + 
			"FROM  " + 
			"	zyb_know_how_details_sort kds  " + 
			"	LEFT JOIN zyb_know_how_details kd ON kds.how_details_id = kd.id  " + 
			"	LEFT JOIN zyb_know_how kh ON kd.how_id = kh.id   " + 
			"WHERE  " + 
			"	kds.course_name = ?1   " + 
			"  AND kds.stat IN (2, 3)   " + 
			"	AND kds.semester = ?3   " + 
			"	AND kds.grade = ?2   " + 
			"	AND kds.type = ?4   " + 
			"	AND kds.lib_id = ?5   " + 
			"GROUP BY  " + 
			"	kd.id   " + 
			"ORDER BY  " + 
			"	kh.create_time")
	public List<Object> findMathModule(String courseName, String grade, String semester, Integer banben, String libId);
	
	@Query(nativeQuery = true, value = "SELECT" + 
			"	kd.id detailsId," + 
			"	kh.id howId," + 
			"	kds.alias alias," + 
			"	kd.two_module twoModule,  " + 
			"	kd.module  module " + 
			"FROM" + 
			"	zyb_know_how_details_sort kds" + 
			"	LEFT JOIN zyb_know_how_details kd ON kds.how_details_id = kd.id" + 
			"	LEFT JOIN zyb_know_how kh ON kd.how_id = kh.id " + 
			"WHERE" + 
			"	kds.course_name = ?1 " + 
			"	AND kds.stat = 2 " + 
			"	AND kds.semester = ?3 " + 
			"	AND kds.grade = ?2 " + 
			"	AND kds.type = ?4 " + 
			"	AND kds.lib_id = ?5 " + 
			"GROUP BY" + 
			"	kd.id " + 
			"ORDER BY" + 
			"	kh.create_time")
	public List<Object> findKnowByCourseNameByGradeAndBanben(String courseName, String grade, String semester,
			Integer banben, String libId);
}
