package com.ssj.dao.sys.imlibuser.dao.impl;

import com.ssj.bean.weixin.libmy.domain.TbLibJoin;
import com.ssj.dao.sys.imlibuser.dao.TbClassQueryDao;
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;

@Repository
public class TbClassQueryDaoImpl implements TbClassQueryDao {
	
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public Page<Map<String, Object>> findLibImByPage(Map<String, Object> searchParam, Pageable pageable) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT t.*    ");
		selSql.append(" ,( SELECT COUNT(DISTINCT(v.id)) from tb_lib_vip v  where v.stat=1 AND v.lib_id=t.id  ) as student_num     ");
		selSql.append(" ,( select COUNT(DISTINCT(t12.vip_id)) from tb_lib_vip_service t12  where t12.lib_id=t.id and t12.type in('C024','C025') and t12.species='S003' and t12.stat=2 ) as invalid_student_num     ");
		selSql.append(" ,( SELECT COUNT(a.teacher_id) from tb_class_teacher a  where a.status=1 and a.type=1 AND a.lib_id=t.id ) as teacher_num    ");
		selSql.append(" ,( SELECT COUNT(DISTINCT(a.user_id)) from tb_lib_manager a  where a.state=1  AND a.type in (4) AND a.lib_id=t.id  ) as principal_num    ");
		selSql.append(" ,( SELECT COUNT(DISTINCT(a.user_id)) from tb_lib_manager a  where a.state=1  AND a.type in (19) AND a.lib_id=t.id  ) as leader_num    ");
		selSql.append(" ,( SELECT COUNT(a.teacher_id) from tb_class_teacher a  where a.status=1 and a.type=2 AND a.lib_id=t.id  ) as headmaster_num    ");
		selSql.append(" ,IFNULL(( SELECT SUM(m.port_num) from fx_merchant m WHERE m.lib_id=t.id GROUP BY m.lib_id),0) as port_num ");
		selSql.append(" from tb_lib_join t    ");
		selSql.append(" where 1=1    ");
		if(!"".equals(searchParam.get("leagueNname"))){
	          selSql.append(" AND t.league_name like CONCAT('%',?,'%') ");
	          queryParams.add(searchParam.get("leagueNname"));
	    }
		selSql.append(" ORDER BY teacher_num DESC,student_num DESC,t.create_time DESC");
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findStudentByPage(Map<String, Object> searchParam, Pageable pageable) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("  SELECT v.* from  tb_lib_vip v     ");
		selSql.append("   where v.stat=1  AND v.lib_id=?   ");   
		queryParams.add(searchParam.get("libId"));
		if(!"".equals(searchParam.get("name"))){
			  selSql.append(" AND v.child_name LIKE  CONCAT('%',?,'%')   ");
	          queryParams.add(searchParam.get("name"));
	    }
		selSql.append(" ORDER BY v.create_time DESC  ");
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findStudentErrorByPage(Map<String, Object> searchParam, Pageable pageable) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		
		selSql.append(" SELECT * from tb_class_student_temp t where  t.`status`=0   ");
		if(searchParam.get("startTime")!=null && StringUtils.isNotEmpty(searchParam.get("startTime").toString())){
			 selSql.append("  AND TO_DAYS(t.create_time)>=TO_DAYS(?)   ");
	         queryParams.add(searchParam.get("startTime"));
	    }
		if(searchParam.get("endTime")!=null && StringUtils.isNotEmpty(searchParam.get("endTime").toString())){
			 selSql.append("  AND TO_DAYS(t.create_time)<=TO_DAYS(?)   ");
	         queryParams.add(searchParam.get("endTime"));
	    }
		if(searchParam.get("remarks")!=null && StringUtils.isNotEmpty(searchParam.get("remarks").toString())){
			 selSql.append(" AND t.error_reason LIKE  CONCAT('%',?,'%')   ");
	         queryParams.add(searchParam.get("remarks"));
	    }
		
		if(searchParam.get("lookup")!=null && StringUtils.isNotEmpty(searchParam.get("lookup").toString())){
			 selSql.append(" AND ( ");
			 selSql.append(" t.student_name LIKE  CONCAT('%',?,'%') or  ");
			 selSql.append(" t.parent_name LIKE  CONCAT('%',?,'%')  or  ");
			 selSql.append(" t.parent_phone LIKE  CONCAT('%',?,'%') or  ");
			 selSql.append(" t.parent_role LIKE  CONCAT('%',?,'%') ) ");
	         queryParams.add(searchParam.get("lookup"));
	         queryParams.add(searchParam.get("lookup"));
	         queryParams.add(searchParam.get("lookup"));
	         queryParams.add(searchParam.get("lookup"));
	    }
		
		
		selSql.append(" ORDER BY t.create_time DESC    ");;
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}
	
	@Override
	public TbLibJoin getTbLibJoinByLeagueName(String leagueName) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT * from tb_lib_join t where t.league_name = ?  LIMIT 1;    ");
		queryParams.add(leagueName);
	    List<TbLibJoin> list=dao.findList(selSql.toString(), queryParams.toArray(),TbLibJoin.class);
	    if(list!=null && list.size()>0){
	    	return list.get(0);
	    }else{
	    	return null;
	    }
	}

	
	@Override
	public Page<Map<String, Object>> findTeacherByPage(Map<String, Object> searchParam, Pageable pageable) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("  SELECT * from tb_lib_manager t  ");
		selSql.append("  where t.state=1 AND t.lib_id=?    ");
		queryParams.add(searchParam.get("libId"));
		
		if("1".equals(searchParam.get("type"))){
			  selSql.append(" AND t.type in (4)   ");
	    }else 
		if("2".equals(searchParam.get("type"))){
			  selSql.append(" AND t.type in (19)   ");
	    }else
		if("3".equals(searchParam.get("type"))){
			  selSql.append(" AND t.type in (4,19,2,10,15)   ");
	    }else
		if("4".equals(searchParam.get("type"))){
			  selSql.append(" AND t.type in (2,10,15) ");
	    }
		if(!"".equals(searchParam.get("name"))){
			  selSql.append(" AND t.name LIKE  CONCAT('%',?,'%')   ");
	          queryParams.add(searchParam.get("name"));
	    }
		selSql.append(" GROUP BY t.user_id ORDER BY t.create_time DESC  ");
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}
	
	
	
	@Override
	public Page<Map<String, Object>> findHeadmasterByPage(Map<String, Object> searchParam, Pageable pageable) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("  SELECT u.*,c.grade,c.class_name,c.id as cid from tb_class_teacher t LEFT JOIN tb_user u ON t.teacher_id=u.id LEFT JOIN tb_class c ON t.class_id=c.id   ");
		selSql.append("  where t.status=1 AND t.lib_id=?  ");
		queryParams.add(searchParam.get("libId"));
		if(!"".equals(searchParam.get("type"))){
			selSql.append(" AND t.type=? ");
	        queryParams.add(searchParam.get("type"));
		}
		if(!"".equals(searchParam.get("name"))){
			 selSql.append(" AND u.user_name LIKE  CONCAT('%',?,'%')   ");
	         queryParams.add(searchParam.get("name"));
	    }
		selSql.append(" ORDER BY t.create_time DESC  ");
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}
	
	
	@Override
	public Page<Map<String, Object>> findTeacherErrorByPage(Map<String, Object> searchParam, Pageable pageable) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		
		selSql.append(" SELECT * from tb_class_teacher_temp t where t.`status`=0  ");
		if(searchParam.get("startTime")!=null && StringUtils.isNotEmpty(searchParam.get("startTime").toString())){
			 selSql.append("  AND TO_DAYS(t.create_time)>=TO_DAYS(?)   ");
	         queryParams.add(searchParam.get("startTime"));
	    }
		if(searchParam.get("endTime")!=null && StringUtils.isNotEmpty(searchParam.get("endTime").toString())){
			 selSql.append("  AND TO_DAYS(t.create_time)<=TO_DAYS(?)   ");
	         queryParams.add(searchParam.get("endTime"));
	    }
		if(searchParam.get("remarks")!=null && StringUtils.isNotEmpty(searchParam.get("remarks").toString())){
			 selSql.append(" AND t.error_reason LIKE  CONCAT('%',?,'%')   ");
	         queryParams.add(searchParam.get("remarks"));
	    }
		
		if(searchParam.get("lookup")!=null && StringUtils.isNotEmpty(searchParam.get("lookup").toString())){
			 selSql.append(" AND ( ");
			 selSql.append(" t.teacher_name LIKE  CONCAT('%',?,'%') or  ");
			 selSql.append(" t.subject LIKE  CONCAT('%',?,'%')  or  ");
			 selSql.append(" t.teacher_phone LIKE  CONCAT('%',?,'%') ) ");
	         queryParams.add(searchParam.get("lookup"));
	         queryParams.add(searchParam.get("lookup"));
	         queryParams.add(searchParam.get("lookup"));
	    }
		
		selSql.append(" ORDER BY t.create_time DESC    ");;
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}
	
	
	
	@Override
	public Page<Map<String, Object>> findClassByPage(Map<String, Object> searchParam, Pageable pageable) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("  SELECT * from tb_class t  ");
		selSql.append("  where t.`status`=1 AND t.lib_id=?    ");
		queryParams.add(searchParam.get("libId"));
		if(!"".equals(searchParam.get("classNname"))){
			  selSql.append(" AND t.class_name LIKE  CONCAT('%',?,'%')   ");
	          queryParams.add(searchParam.get("classNname"));
	    }
		selSql.append(" ORDER BY t.create_time DESC  ");
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findInvalidStudentByPage(Map<String, Object> params, Pageable initPage) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t2.parent_role,t2.parent_name,t.class_name ,t.id class_id, t2.id,t2.child_name ,t2.img_url ,t2.grade,t2.sex,t2.school,IF(t3.mobile is null,t2.phone,t3.mobile) phone\n");
		selSql.append(" from tb_lib_vip t2 \n");
		selSql.append(" left join (select tc.class_name,tc.id,tcs.vip_id\n");
		selSql.append(" from tb_class_student tcs,tb_class tc\n");
		selSql.append(" where tcs.status in (1,2)\n");
		selSql.append(" and tcs.lib_id=?\n");
		selSql.append(" and tc.status=1\n");
		selSql.append(" and tcs.class_id=tc.id\n");
		selSql.append(" ) t on t2.id=t.vip_id\n");
		selSql.append(" left join tb_user t3 on t2.user_id=t3.id\n");
		selSql.append(" left join (select lib_id, vip_id, min(stat) stat from tb_lib_vip_service where lib_id=? and type in('C024','C025') and stat=2 and species='S003' group by lib_id,vip_id) t4\n");
		selSql.append(" on t2.id = t4.vip_id\n");
		selSql.append(" where 1=1\n");
		selSql.append(" and t4.lib_id=?\n");
		queryParams.add(params.get("libId"));
		queryParams.add(params.get("libId"));
		queryParams.add(params.get("libId"));
		if(!"".equals(params.get("name"))){
			selSql.append(" AND t2.child_name LIKE  CONCAT('%',?,'%')   ");
			queryParams.add(params.get("name"));
		}
		return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}


}
