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

import java.util.List;

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.ZuoybCourse;


/** 
 * @author  ZhangCaibao 
 * @version 2017年7月4日 上午11:29:05 
 * 类说明 
 */
@Repository
public interface ZuoybCourseDao extends JpaRepository<ZuoybCourse, String>{
	
	 @Query(nativeQuery = true, value = "update zyb_week set teacher_id = ?3  , teacher_name =?4 , update_time =now() where zyb_course_id = ?1 and days =?2 ")
	 @Modifying
	 public void updateCourseWeek(String zybCourseId,Integer days,String teacherId,String teacherName);
	 
	 @Query(nativeQuery = true, value = "update zyb_course set num = ?2 where id =?1 ")
	 @Modifying
	 public void  updateCourseNum(String id,Integer num);

	 @Query(nativeQuery = true, value = "select count(1) from zyb_course  where course_name = ?1 and course_hour =?2 and lib_id =?3 ")
	 public Integer getCourseNameAndHourAndLibId(String courseName ,String courseHour,String libId);

	 @Query(nativeQuery = true, value = "delete from  zyb_week  where zyb_course_id = ?1 ")
	 @Modifying
	 public void deleteZybWorkByZybCourseId(String zybCourseId );
	 
	 @Query(nativeQuery = true, value = "delete from  zyb_scoring_content  where zyb_course_id = ?1")
	 @Modifying
	 public void deleteZybScoringContentV1(String zybCourseId );
	@Query(nativeQuery = true, value = "DELETE from  zyb_scoring_content_details where  scoring_content_id in (\n" +
			"SELECT id  from   zyb_scoring_content where  zyb_course_id=?1 \n" +
			")")
	@Modifying
	public void deleteZybScoringContentV2(String zybCourseId );
	@Query(nativeQuery = true, value = "DELETE s,t FROM zyb_scoring_content_details s\n" +
			"INNER JOIN  zyb_scoring_content t\n" +
			"ON s.scoring_content_id = t.id \n" +
			"WHERE t.zyb_course_id =?1 ")
	@Modifying
	public void deleteZybScoringContent(String zybCourseId );
	 
	 @Query(nativeQuery = true, value = "select * from zyb_course  where  lib_id =?1  GROUP BY  course_hour  order by  course_hour asc ,create_time asc ")
	 public List<ZuoybCourse> findByLibId(String libId);
	 
	 @Query(nativeQuery = true, value = "SELECT  c.* from    zyb_course  c  LEFT JOIN   tb_lib_vip_service  s \n" +
			 " on c.id = s.zyb_course_id\n" +
			 "where c.lib_id  =?1 and s.vip_id =?2  order by  c.course_hour asc ,c.create_time  asc  ")
	 public List<ZuoybCourse> findByLibIdAndVipId(String libId ,String vipId);
	 
	 @Query(nativeQuery = true, value = "SELECT  c.* from    zyb_course  c  LEFT JOIN   tb_lib_vip_service  s \n" +
			 "  on c.course_hour = s.course_hour and c.lib_id = s.lib_id  \n" +
			 "where c.lib_id  =?1 and s.vip_id =?2 and c.course_hour =?3  order by  c.course_hour asc ,c.create_time  asc  ")
	 public List<ZuoybCourse> findByLibIdAndVipIdAndCourseHour(String libId ,String vipId,String courseHour);
	 
	 @Query(nativeQuery = true, value = "SELECT * from  zyb_course where lib_id = ?1 and course_hour =?2  \n" +
			 " ORDER BY  num ASC,create_time ASC LIMIT  1  ")
	 public ZuoybCourse findByLibIdAndCourseHour(String libId,String courseHour);
	 
	
	 @Query(nativeQuery = true, value = "SELECT * from  zyb_course where lib_id = ?1 and course_hour =?2  and icon !='iconzt'  \n" +
			 " ORDER BY  num ASC,create_time ASC   ")
	 public List<ZuoybCourse>  getLibIdAndCourseHour(String libId,String courseHour);
	 
	 @Query(nativeQuery = true, value = "SELECT  c.* from    zyb_course  c  LEFT JOIN   zyb_course  s \n" +
			 "on  c.course_hour = s.course_hour and  c.lib_id = s.lib_id\n" +
			 " where s.id =?1 \n" +
			 " order by c.num asc , c.course_hour asc ,c.create_time  asc")
	 public List<ZuoybCourse> findBySiblings(String courseId);
	 
	 @Query(nativeQuery = true, value = "update zyb_student_work_img set object_id = ?1   where object_id = ?2 ")
	 @Modifying
	 public void updateWorkImgByObjectId(String oldObjectId,String newObjectId);
	 
	 @Query(nativeQuery = true, value = "select count(1) from zyb_scoring_content  where num = ?1 and zyb_course_id =?2 and lib_id =?3 ")
	 public Integer getZybCourseIdAndLibIdAndNum(Integer num ,String zybCourseId,String libId);
	 
	 @Query(nativeQuery = true, value = "SELECT count(1) from  zyb_course where  course_hour =?1 and  lib_id = ?2 ")
	 public Integer fingByCourseHourAndLibIdCount(String courseHour,String libId);
	 @Query(nativeQuery = true, value = "SELECT count(1) from  zyb_course where  lib_id = ?1 ")
	 public Integer fingByLibIdCount(String libId);
	 
	 @Query(nativeQuery = true, value = "select * from zyb_course  where course_name = ?1 and course_hour =?2 and lib_id =?3 ")
	 public ZuoybCourse getCourseNameAndHour(String courseName,String courseHour, String libId);
	 
	 @Query(nativeQuery = true, value = "select course_hour from zyb_course  where  lib_id =?1  GROUP BY  course_hour  order by  course_hour asc ,create_time asc ")
	 public List<String> getCourseAndLibId(String libId);
}
