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

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

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

@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(a.user_id)) from tb_lib_manager a  where a.state=1  AND a.type in (2,10,15) AND a.lib_id=t.id  ) as teacher_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 t.type=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.type in (2,10,15) AND t.lib_id=?    ");
		queryParams.add(searchParam.get("libId"));
		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>> 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);
	}




}
