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

import com.ssj.bean.weixin.zyb.domain.ZuoybKnowHowDetailsSort;
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 2018年3月8日 下午4:34:40 
 * 类说明 
 */
@Repository
public interface ZuoybKnowHowDetailsSortDao extends JpaRepository<ZuoybKnowHowDetailsSort, String>{

	 public Page<ZuoybKnowHowDetailsSort> findAll(Specification<ZuoybKnowHowDetailsSort> specification, Pageable page);
	 
	 @Query(nativeQuery = true, value = "select  *  from  zyb_know_how_details_sort where how_details_id =?1 and type=?2   ")
	 public ZuoybKnowHowDetailsSort findByHowDetailsIdAndType(String howDetailsId,Integer type);
	 @Query(nativeQuery = true, value = "update zyb_know_how_details_sort set num = ?2 where id =?1 ")
	 @Modifying
	 public void  updateKnowHowDetailSortNum(String id,Integer num);
	 @Query(nativeQuery = true, value = "update zyb_know_how_details_sort set before_how_details_kd_id = ?1 where id =?2 ")
	 @Modifying
	 public void  updateBeforeKd(String beforeKdId,String sortId);
	 
	 @Query(nativeQuery = true, value = "	 SELECT   \n" +
			 "GROUP_CONCAT(s.alias)\n" +
			 "		 FROM \n" +
			 "		 zyb_know_how_details_sort  s\n" +
			 "		 where \n" +
			 "s.course_name=?1 and \n" +
			 "s.how_details_id = ?5  AND s.stat = 3 \n" +
			 "and   s.grade =?2 \n" +
			 " and   s.semester =?4 \n" +
			 "and   s.type =?3 \n" +
			 "ORDER BY s.grade_num asc ,s.semester asc,  s.num  asc  , s.create_time asc")
	 public String getZuoybKnowHowDetailsKdName(String courseName,String grade,String banben,String semester,String howDetailsId);

	/**
	 * 根据考点id集合查询对应考点版本记录集合
	 */
   List<ZuoybKnowHowDetailsSort> findByHowDetailsIdInAndStat(List<String> knowHowDetailsKdId2DeleteList, Integer stat);

	/**
	 * 根据知识点id查询对应知识点版本记录
	 */
	List<ZuoybKnowHowDetailsSort> findByHowDetailsIdAndStat(String knowHowDetailId, int stat);

	/**
	 * 根据单元id查询对应知识点版本记录
	 */
	ZuoybKnowHowDetailsSort findByHowIdAndStat(String howId, int stat);

	/**
	 * 根据版本和知识点id集合查询知识点的版本记录
	 */
   	List<ZuoybKnowHowDetailsSort> findByTypeAndStatAndHowDetailsIdIn(Integer type, Integer stat, List<String> knowHowDetailsIdList);

	List<ZuoybKnowHowDetailsSort> findByCourseNameAndGradeAndSemesterAndTypeAndStatAndHowIdOrderByNum(String courseName, String grade, int semester, int type, int stat, String howId);

	/**
	 * 查找单元
	 *
	 * @param type
	 * @param howId
	 * @param stat
	 * @return
	 */
	ZuoybKnowHowDetailsSort findTopByTypeAndHowIdAndStat(int type, String howId, int stat);

	@Query(nativeQuery = true, value = "select * from zyb_know_how_details_sort where stat=1 and grade =?1 and course_name=?2 and type=?3 and semester=?4 and alias=?5 limit 1")
	ZuoybKnowHowDetailsSort findKnowHow(String grade,String subject,int version,int semester,String unit);
}
