package com.ssj.dao.course.dao.impl;

import com.ssj.bean.course.domain.CourseSeries;
import com.ssj.dao.course.dao.CourseQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

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


/** 
 * @author  ZhangCaibao 
 * @version 2018年10月16日 下午5:42:59 
 * 类说明 
 */
@Repository
public class CourseQueryDaoImpl  implements CourseQueryDao{

	@Autowired
	private PagingHibernateJdbcDao dao;


	@Override
	public List<Map<String, Object>> getCourseAll(int courseTypeLabel ,int offset) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT  t.id,t.course_name,t.cover_url, t.score,t.course_hot_lable,t.official_price, course_summary, ");
        selSQL.append(" ( SELECT COUNT(1) from  tb_course_series_details  s where s.series_id  = t.id and s.state =1  ) as courses_num ,  ");
        selSQL.append(" CASE WHEN INSTR(t.course_hot_lable,2)>0 THEN 1 else 0 END AS is_free   ");
        selSQL.append("   from  tb_course_series t where INSTR (t.course_hot_lable,?) and t.state =1   ");
        selSQL.append("   ORDER BY t.last_update_time desc   LIMIT ?   ");
        queryParams.add(courseTypeLabel);
        queryParams.add(offset);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getManageJoin(String userId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" select j.id ,j.league_name  from tb_lib_join  j join  tb_lib_manager  e ");
        selSQL.append(" on j.id  =e.lib_id  ");
        selSQL.append(" where e.state =1 and e.user_id =?  and  e.type  in (1,4) GROUP BY  j.id  ");
        queryParams.add(userId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findCourseSeriesJoinByUserId(String userId,String vipId,String id,String libId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT GROUP_CONCAT(DISTINCT(t.open_time)  ORDER BY t.open_time ASC) as open_time, ");
	        selSQL.append(" t.course_series_id,t.lib_id,s.color from tb_course_series_join t  ");
	        selSQL.append("  LEFT JOIN tb_course_series s ON t.course_series_id=s.id    ");
	        selSQL.append(" where  t.user_id=?    ");
	        queryParams.add(userId);
	        if(StringUtils.isNotEmpty(vipId)){
	        	 selSQL.append(" AND t.vip_id=?  ");
	        	 queryParams.add(vipId);
	        }
	        if(StringUtils.isNotEmpty(id)){
	        	 selSQL.append(" AND t.course_series_id=?  ");
	        	 queryParams.add(id);
	        }
	        if(StringUtils.isNotEmpty(libId)){
	        	 selSQL.append(" AND t.lib_id=?  ");
	        	 queryParams.add(libId);
	        }
	        selSQL.append(" GROUP BY t.course_series_id,t.lib_id ");
       
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findCourseSeriesByUserId(String userId,String vipId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
    	selSQL.append(" SELECT tmp.lib_id,c.*,   ");
        selSQL.append(" (   ");
        selSQL.append(" 	SELECT m.`name` from tb_course_series_setting s   ");
        selSQL.append(" 	LEFT JOIN tb_lib_manager m ON s.teacher_id=m.user_id AND m.type=2   ");
        selSQL.append(" 	where s.course_series_id=tmp.course_series_id AND s.lib_id=tmp.lib_id LIMIT 1   ");
        selSQL.append(" ) as `name` from (  ");
        selSQL.append(" SELECT t.course_series_id,t.lib_id from tb_course_series_join t    ");
        selSQL.append(" where  t.user_id=?   ");
        if(StringUtils.isNotEmpty(vipId)){
        	selSQL.append(" and  t.vip_id=?      ");
        }
        selSQL.append(" GROUP BY t.course_series_id,t.lib_id   ");
        selSQL.append(" ) as tmp    ");
        selSQL.append(" LEFT JOIN tb_course_series c ON tmp.course_series_id=c.id   ");
        queryParams.add(userId);
        if(StringUtils.isNotEmpty(vipId)){
        	queryParams.add(vipId);
        }
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getCourseFeedback(String courseSeriesId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT  k.pleased_score as score ,k.evaluate ,v.child_name ,v.img_url , DATE_FORMAT(k.create_time, '%Y-%m-%d') as  create_time ");
        selSQL.append(" from  tb_course_series_details_feedback  k LEFT JOIN  tb_lib_vip v  on v.id  =k.vip_id  ");
        selSQL.append(" where k.course_series_id =? and k.pleased_score is not null  LIMIT 3 ");
        queryParams.add(courseSeriesId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}
	@Override
	public List<Map<String, Object>> getCourseFeedback(String courseSeriesId,String libId) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT  k.pleased_score as score ,k.evaluate ,v.child_name ,v.img_url , DATE_FORMAT(k.create_time, '%Y-%m-%d') as  create_time ");
		selSQL.append(" from  tb_course_series_details_feedback  k LEFT JOIN  tb_lib_vip v  on v.id  =k.vip_id  ");
		selSQL.append(" where k.course_series_id =? and k.lib_id=?  and k.pleased_score is not null  LIMIT 3 ");
		queryParams.add(courseSeriesId);
		queryParams.add(libId);
		return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findCourseSeriesFeedbackByUserId(String userId,String vipId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t.open_time as  openTime,GROUP_CONCAT(DISTINCT(t.state)) as state from tb_course_series_join t where  ");
        selSQL.append(" TO_DAYS( NOW( ) ) - TO_DAYS(t.open_time) <= 7 AND TO_DAYS( NOW( ) ) - TO_DAYS(t.open_time) >= 0   ");
        selSQL.append(" AND t.user_id =? AND t.state!=0 AND t.state is NOT NULL   ");
        queryParams.add(userId);
        if(StringUtils.isNotEmpty(vipId)){
        	selSQL.append(" AND t.vip_id =? ");
        	queryParams.add(vipId);
        }
        selSQL.append(" GROUP BY t.open_time  ORDER BY t.open_time ASC ,t.state ASC");
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getTeacherCourseInfo(String libId, String userId,int searchType) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" select t.course_series_id  ");
        selSQL.append(" from tb_course_series_setting t   ");
        selSQL.append(" inner join tb_course_series t11 on t.course_series_id = t11.id   ");
        selSQL.append(" inner join tb_lib_manager t12 on t.lib_id = t12.lib_id and t.teacher_id = t12.user_id  ");
        selSQL.append(" where t.teacher_id=? and t.lib_id=?  and  t11.state=1  and t12.state=1 AND  TO_DAYS(NOW())<=TO_DAYS(t.end_time)    ");
        if(searchType==2){
        	selSQL.append(" AND t.course_date LIKE concat('%', STR_TO_DATE(NOW(),'%Y-%m-%d'), '%')    ");
        }
        selSQL.append(" GROUP BY t.course_series_id ");
        queryParams.add(userId);
        queryParams.add(libId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}
	@Override
	public Page<Map<String, Object>> findFeedbackPageByUserId(Map<String, Object> searchParams, Pageable pageable) {
		 StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT * FROM `tb_course_series_join` t ");
        selSQL.append(" where t.open_time <= NOW() AND t.user_id=? ");
        queryParams.add(searchParams.get("userId"));
        if(searchParams.get("vipId")!=null && StringUtils.isNotEmpty(searchParams.get("vipId").toString())){
        	selSQL.append("  AND t.vip_id=? ");
        	queryParams.add(searchParams.get("vipId"));
        }
        selSQL.append("  AND t.state in (1,2)  ORDER BY t.begin_time desc");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Map<String, Object> getThisCourseInfo(Map<String, Object> search) {
		
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
//        selSQL.append(" select a.id as course_series_id,a.cover_url,a.course_name,a.total_course,IFNULL(b.is_send,0) as is_send,b.id as feedback_id from (  ");
//        selSQL.append(" SELECT t.cover_url,t.course_name,t11.lib_id,t11.teacher_id,t.id,   ");
//        selSQL.append(" ifnull((select count(1) from tb_course_series_details t12 where t.id=t12.series_id and t12.state =1),0) as total_course,    ");
//        selSQL.append("  CEILING((TO_DAYS(NOW())-TO_DAYS(open_time))/(days+1)) as studyed_course,");
//        selSQL.append(" ifnull((select count(DISTINCT t15.vip_id) from tb_course_series_join t15 where t.id=t15.course_series_id and t15.lib_id=? ),0) as total_student,");
//        selSQL.append(" from_days(TO_DAYS(t11.open_time)+(t11.days+1)*(CEILING((TO_DAYS(NOW())-TO_DAYS(open_time))/(days+1))))  as next_course,  ");
//        selSQL.append(" (select b.id from tb_course_series_details b where b.series_id = t.id and b.course_index=(CEILING((TO_DAYS(NOW())-TO_DAYS(open_time))/(days+1))+1) limit 1) as course_details_id, ");
//        selSQL.append(" (select b.course_unit from tb_course_series_details b where b.series_id = t.id and b.course_index=(CEILING((TO_DAYS(NOW())-TO_DAYS(open_time))/(days+1))+1) limit 1) as course_unit    ");
//        selSQL.append(" from tb_course_series t   ");
//        selSQL.append(" left join tb_course_series_setting t11 on t.id = t11.course_series_id ");
//        selSQL.append(" where t11.lib_id = ? and t11.teacher_id=? and t.id=? ");
//        selSQL.append(") a left join tb_course_lib_feedback b on a.lib_id = b.lib_id and a.teacher_id = b.user_id and TO_DAYS(b.open_time)=TO_DAYS(NOW()) and a.course_details_id= b.course_details_id ");
       
        selSQL.append(" SELECT t.cover_url,t.course_name,t11.lib_id,t11.teacher_id,t11.course_date,t.id AS course_series_id, ");
        selSQL.append(" ifnull((SELECT count(1) FROM tb_course_series_details t12 WHERE t.id = t12.series_id AND t12.state = 1 ),0 ) AS total_course, ");
        selSQL.append(" ifnull((SELECT count(DISTINCT t15.vip_id) FROM tb_course_series_join t15	WHERE t.id = t15.course_series_id AND t15.lib_id =? ),0 ) AS total_student ");
        selSQL.append(" FROM tb_course_series t ");
        selSQL.append(" LEFT JOIN tb_course_series_setting t11 ON t.id = t11.course_series_id ");
        selSQL.append(" WHERE t.state=1  ");
        selSQL.append(" AND t11.lib_id =? ");
        selSQL.append(" AND t11.teacher_id =? ");
        selSQL.append(" AND t.id =? ");
        queryParams.add(search.get("libId"));
        queryParams.add(search.get("libId"));
        queryParams.add(search.get("userId"));
        queryParams.add(search.get("courseSeriesId"));
        List<Map<String, Object>> list = dao.findMap(selSQL.toString(),queryParams.toArray());
        if(list!=null && list.size()>0){
        	return list.get(0);
        }else{
        	return null;
        }
        
	}

	@Override
	public List<Map<String, Object>> getCourseStudentList(Map<String, Object> search) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t.vip_id ,t.course_details_id,t11.child_name,t11.img_url,IFNULL(t12.is_send,0) as is_send,t12.id as feedback_id  from tb_course_series_join  t  ");
        selSQL.append(" left join tb_lib_vip t11 on t.vip_id = t11.id    ");
        selSQL.append(" left join tb_course_series_details_feedback t12 on t.course_details_id = t12.course_details_id and t.vip_id = t12.vip_id ");
        selSQL.append(" where t.course_details_id =? and t.lib_id=? ");
        queryParams.add(search.get("courseDetailId"));
        queryParams.add(search.get("libId"));

        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public Map<String, Object> getFeedBackInfo(String courseDetailId, String vipId,String libId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t12.open_time as course_time,t12.vip_id,t11.child_name,t11.img_url,t.saying,ifnull(t.active_score,0) as active_score,ifnull(t.focus_score,0) as focus_score,a.id as course_details_id,a.series_id,a.course_unit,a.course_content,ifnull(t.is_send,0) as is_send,ifnull(t.id,'0') as is_save,t.id as feedback_id   ");
        selSQL.append(" from tb_course_series_details a    ");
        selSQL.append(" left join tb_course_series_details_feedback t on a.id = t.course_details_id   and t.lib_id= ? and t.vip_id= ? ");
        selSQL.append(" left join tb_course_series_join t12 on a.id = t12.course_details_id     ");
        selSQL.append(" left join tb_lib_vip t11 on t12.vip_id=t11.id     ");
        selSQL.append(" where a.id=? and t12.vip_id=? and a.state =1 ");
        queryParams.add(libId);
        queryParams.add(vipId);
        queryParams.add(courseDetailId);
        queryParams.add(vipId);
        
        List<Map<String, Object>> list = dao.findMap(selSQL.toString(),queryParams.toArray());
        if(list!=null && list.size()>0){
        	return list.get(0);
        }else{
        	return null;
        }
	}

	@Override
	public List<CourseSeries> findByBuy(String libId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT	 	s.`id`, 	s.`course_name`,	s.`course_target`,	s.`class_pnum`,	s.`course_plan`,	s.`recommend_reason`,  ");
        selSQL.append(" s.`teacher_require`, 	s.`official_price`,	s.`course_type_label`,	s.`course_hot_lable`,		s.`cover_url`,  ");
        selSQL.append("	s.`create_time`,		s.`last_update_time`,		s.`state`,		s.`course_summary`,		s.`score`, s.color, ");
        selSQL.append(" (SELECT count(f.id) from tb_course_series_details f where f.series_id = s.id and f.state =1) as course_series_detail_size,    ");
        selSQL.append(" (SELECT GROUP_CONCAT(u.name) from  tb_course_series_setting g join  tb_lib_manager  u on u.user_id = g.teacher_id where g.course_series_id = s.id and u.type =2 and t.lib_id= u.lib_id and u.state = 1 ) as teacher_name ");
        selSQL.append(" FROM  tb_course_series s join   tb_course_series_order t  on  s.id = t.course_series_id  	WHERE  	t.stat = 1  ");
        selSQL.append(" AND TO_DAYS(t.end_time) >= TO_DAYS(now()) and t.lib_id=?  	ORDER BY  t.create_time DESC  ");
        queryParams.add(libId);
        return dao.findList(selSQL.toString(), queryParams.toArray(),  CourseSeries.class);
	}

	@Override
	public List<Map<String, Object>> findVipIsBuy(String userId,
			String courseDetailsId, String libId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT  ");
        selSQL.append(" v.id as vip_id,	v.child_name,v.img_url,count(t.id) as type   ");
        selSQL.append("   FROM   tb_lib_vip  v LEFT JOIN  	tb_course_series_join t  on v.id  =t.vip_id  AND   ");
        selSQL.append("   t.course_series_id =? AND t.lib_id =?  ");
        selSQL.append("   WHERE  v.user_id =?  GROUP BY v.id  ");
        queryParams.add(courseDetailsId);
        queryParams.add(libId);
        queryParams.add(userId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public Map<String, Object> getFeedBackInfoById(String id) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t12.open_time as course_time,t.lib_id,t12.vip_id,t11.child_name,t11.img_url,t.saying,ifnull(t.active_score,0) as active_score,ifnull(t.focus_score,0) as focus_score,a.id as course_details_id,a.series_id,a.course_unit,a.course_content,ifnull(t.is_send,0) as is_send,ifnull(t.id,'0') as is_save,t.id as feedback_id,ifnull(t.evaluate,'0') as is_evaluate   ");
        selSQL.append(" from tb_course_series_details a    ");
        selSQL.append(" left join tb_course_series_details_feedback t on a.id = t.course_details_id    ");
        selSQL.append(" left join tb_course_series_join t12 on a.id = t12.course_details_id     ");
        selSQL.append(" left join tb_lib_vip t11 on t12.vip_id=t11.id     ");
        selSQL.append(" where t.id = ? and a.state =1");
        queryParams.add(id);
        List<Map<String, Object>> list = dao.findMap(selSQL.toString(),queryParams.toArray());
        if(list!=null && list.size()>0){
        	return list.get(0);
        }else{
        	return null;
        }
	}
	
	@Override
	public List<String> getCourseSeriesByColor() {
		StringBuilder selSQL = new StringBuilder();
        selSQL.append(" SELECT t.color from tb_course_series t GROUP BY t.color  ");
        return dao.findList(selSQL.toString(),String.class);
       
	}

	@Override
	public List<Map<String, Object>> findSeriesDetailsByUserId(String seriesId,String userId,String vipId, String libId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t.*,j.open_time from tb_course_series_details t   ");
        selSQL.append(" LEFT JOIN tb_course_series_join j ON t.id=j.course_details_id   ");
        selSQL.append(" AND j.user_id=?  ");
        if(StringUtils.isNotEmpty(vipId)){
        	selSQL.append(" AND j.vip_id=?  ");
        }
        selSQL.append(" AND j.lib_id=?  ");
        selSQL.append(" where t.series_id=? GROUP BY t.id  ORDER BY t.course_index ASC  ");
        queryParams.add(userId);
        if(StringUtils.isNotEmpty(vipId)){
        	queryParams.add(vipId);
        }
        queryParams.add(libId);
        queryParams.add(seriesId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}
	

	@Override
	public List<Map<String, Object>> findCourseRecordList(String userId,String vipId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  SELECT t.id,t.course_name as courseName,t.course_target as courseTarget , ");
        selSQL.append("  t.class_pnum as classPnum,t.course_plan  as coursePlan,t.recommend_reason  as recommendReason, ");
        selSQL.append("  t.teacher_require as teacherRequire,t.official_price as officialPrice, ");
        selSQL.append("  t.course_type_label as  courseTypeLabel,t.course_hot_lable as courseHotLable,  ");
        selSQL.append("  t.cover_url as coverUrl,t.create_time as createTime,  ");
        selSQL.append("  t.last_update_time as lastUpdateTime ,t.state,t.course_summary as courseSummary,  ");
        selSQL.append("  t.score as score,t.course_content as courseContent,t.color ,COUNT(DISTINCT(j.id)) as count, ");
        selSQL.append("  ( ");
	    selSQL.append("  	SELECT COUNT(DISTINCT(vj.id)) from tb_course_series_join vj   where vj.course_series_id=t.id  ");
	    selSQL.append("  	AND vj.vip_id=? ");
	    queryParams.add(vipId);
	    selSQL.append("  	AND vj.open_time<=NOW()  ");
        selSQL.append("  ) as passCount, ");
        selSQL.append("  ( ");
        selSQL.append(" 	SELECT vj.open_time from tb_course_series_join vj   ");
        selSQL.append(" 	where vj.course_series_id=t.id   ");
        selSQL.append("   	AND vj.vip_id=?   ");
        queryParams.add(vipId);
        selSQL.append("  	AND vj.open_time>=NOW() AND vj.begin_time IS  NULL ORDER BY vj.open_time ASC LIMIT 1 ");
        selSQL.append("  ) as nextOpenTime, ");
        selSQL.append("  (  ");
        selSQL.append(" 	SELECT GROUP_CONCAT(DISTINCT(m.`name`)) from tb_course_series_setting s  ");
        selSQL.append("  	LEFT JOIN tb_lib_manager m ON s.teacher_id=m.user_id AND s.lib_id=m.lib_id ");
        selSQL.append("  	where s.course_series_id=t.id AND m.state=1 ");
        selSQL.append("  ) as teacherName ");
        selSQL.append("  from tb_course_series t  ");
        selSQL.append("  LEFT JOIN tb_course_series_join j ON t.id=j.course_series_id  ");
        selSQL.append("  where j.vip_id=? AND t.state=1 ");
        queryParams.add(vipId);
        selSQL.append("  GROUP BY t.id   ");
        selSQL.append("  ORDER BY j.create_time asc;  ");
        
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getCourseStudentListV2(Map<String, Object> search) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t.vip_id ,t11.user_id,t11.child_name,t11.img_url,(select count(1) from tb_course_feedback_file t12 where t12.vip_id = t11.id and t.lib_id=t12.lib_id and t.open_time = t12.open_time and t.course_details_id=t12.course_details_id)  as flower_count  from tb_course_series_join  t  ");
        selSQL.append(" left join tb_lib_vip t11 on t.vip_id = t11.id    ");
        selSQL.append(" where t.course_details_id =? and t.lib_id=? ");
        queryParams.add(search.get("courseDetailId"));
        queryParams.add(search.get("libId"));

        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getNextFile(String courseSeriesId,
			int start, int end, String fileType) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT  f.file_url,s.course_unit,f.course_details_id  ");
        selSQL.append(" FROM tb_course_series_details  s  LEFT JOIN   tb_course_series_details_file  f    ");
        selSQL.append(" on s.id = f.course_details_id  WHERE");
        selSQL.append(" s.series_id =? ");
     //   selSQL.append(" and s.course_index >=? and   s.course_index <=?  ");
        selSQL.append(" and f.file_type =? ");
        selSQL.append(" ORDER BY  s.course_index asc  ");
        queryParams.add(courseSeriesId);
     //   queryParams.add(start);
     //   queryParams.add(end);
        queryParams.add(fileType);

        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findOrderCourseList(String libId,String userId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t11.id,t11.course_name,t11.cover_url,t12.teacher_id,  ");
        selSQL.append(" (select count(DISTINCT a.vip_id) from tb_course_series_join a where a.course_series_id=t11.id and a.lib_id=t.lib_id GROUP BY a.course_series_id limit 1) as student_count,    ");
        selSQL.append(" (select count(1) from tb_course_series_details a where a.series_id = t11.id limit 1) as lesson_count, ");
        selSQL.append(" (SELECT a.open_time from tb_course_series_join a where a.course_series_id=t11.id and a.lib_id=t.lib_id and  TO_DAYS(a.open_time)>TO_DAYS(NOW()) and a.state=0  order by a.open_time asc limit 1) as next_course_time ");
        selSQL.append(" from tb_course_series_order t  ");
        selSQL.append(" LEFT  JOIN tb_course_series t11 on t.course_series_id = t11.id  ");
        selSQL.append(" left join tb_course_series_setting t12 on t11.id = t12.course_series_id and t.lib_id = t12.lib_id  ");
        selSQL.append(" where t11.state=1 and TO_DAYS(t12.end_time)>TO_DAYS(NOW())  ");
        selSQL.append(" and t.lib_id=?  and t12.teacher_id = ?  ");
        queryParams.add(libId);
        queryParams.add(userId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> studentManagerList(String libId, String courseSeriesId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT DISTINCT t11.id,t11.child_name,t11.img_url from tb_course_series_join t   ");
        selSQL.append(" left join tb_lib_vip t11 on t.vip_id = t11.id    ");
        selSQL.append(" where t.lib_id=? and t.course_series_id=? ");
        queryParams.add(libId);
        queryParams.add(courseSeriesId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findDetailBySeriesId(String libId, String courseSeriesId,String vipId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t.id,t.course_unit,t.course_duration,t.course_content,t11.begin_time from tb_course_series_details t    ");
        selSQL.append(" left join tb_course_series_join t11 on t.id = t11.course_details_id and t11.lib_id=? and t11.vip_id=?    ");
        selSQL.append(" where t.series_id=?   order by t.course_index ASC ");
        queryParams.add(libId);
        queryParams.add(vipId);
        queryParams.add(courseSeriesId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getDateDetailList(String libId, String courseSeriesId, String vipId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t12.id,t12.course_unit,ifnull(DATE_FORMAT(t.begin_time,   '%Y-%m-%d %H:%i:%S'),'') as begin_time,t11.id as feedback_id,t.open_time from tb_course_series_details t12    ");
        selSQL.append(" left join tb_course_series_join t  on t.course_details_id = t12.id and t.vip_id=? and t.lib_id=?     ");
        selSQL.append(" left join tb_course_lib_feedback t11 on t12.id = t11.course_details_id and t11.lib_id=? and t.OPEN_time=t11.open_time \n");
        selSQL.append(" where  t12.series_id=?  order by t.open_time ASC ");
        queryParams.add(vipId);
        queryParams.add(libId);
        queryParams.add(libId);
        queryParams.add(courseSeriesId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

}