package com.ssj.dao.weixin.user.dao.impl;

import com.ssj.dao.weixin.user.dao.UserQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
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 UserQueryImpl implements  UserQueryDao {
	
	@Autowired
	private PagingHibernateJdbcDao dao;




	@Override
	public Page<Map<String, Object>> getWXUser(
			Map<String, Object> searchParams, Pageable pageable) {
		 StringBuilder selSQL = new StringBuilder();
	        List<Object> queryParams = new ArrayList<Object>();
	        selSQL.append("  select  t.*  ");
	        selSQL.append("  from  tb_user t ");
	        selSQL.append("  WHERE t.wx_account  is not null   ");
	        if (searchParams.get("name") != null && !searchParams.get("name").equals("")) {
	        	selSQL.append(" and  t.wx_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
	        	queryParams.add(searchParams.get("name"));
	        }
	        if (searchParams.get("subscribe") != null && !searchParams.get("subscribe").equals("")) {
	        		selSQL.append(" and t.subscribe = ?  ");
	        	queryParams.add(searchParams.get("subscribe"));
	        }
	        
	        if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(t.create_time) >=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("beginDate"));
	        }
	        if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(t.create_time) <=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("endDate"));
	        }
	        
	        
	        selSQL.append("   order by    ");
	        selSQL.append(" t.create_time  desc ");
	        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
	@Override
	public Page<Map<String, Object>> getTSGUser(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		String type = searchParams.get("type").toString();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append("  SELECT   u.id,u.wx_name as name ,u.photo ,u.subscribe,u.country ,u.mobile  ");
		if("5".equals(type)){
			//管理合伙人
			selSQL.append("  from  tb_user  u  where not exists (select 1 from tb_lib_manager t where t.user_id = u.id and t.state=1 and t.lib_id is not null  and (t.lib_id=? or (t.type BETWEEN 1 and 4 or t.type = 6) ) )   ");
			queryParams.add(searchParams.get("libId"));
		}else if("6".equals(type)){
			//一对一校长
			selSQL.append("  from  tb_user  u  where not exists (select 1 from tb_lib_manager t where t.user_id = u.id and t.state=1  and t.lib_id=? and t.type=6 )   ");
			queryParams.add(searchParams.get("libId"));
		}else if("2".equals(type)){
			//学习导师（在馆老师）
			selSQL.append("  from  tb_user  u  where not exists (select 1 from tb_lib_manager t where t.user_id = u.id and t.state=1 and t.lib_id =?  and t.type =? )   ");
			queryParams.add(searchParams.get("libId"));
			queryParams.add(searchParams.get("type"));
		}else if("0".equals(type)||"1".equals(type)){
			//图书馆馆长以及馆长助理
			selSQL.append("  from  tb_user  u  where not exists (select 1 from tb_lib_manager t where t.user_id = u.id and t.lib_id is not null and t.state=1 and (t.type BETWEEN 0 and 1))    ");
		}else{
			selSQL.append("  from  tb_user  u  where not exists (select 1 from tb_lib_manager t where t.user_id = u.id and t.lib_id is not null and t.state=1 and t.type<>6)   ");
		}

		if (searchParams.get("name") != null && !searchParams.get("name").equals("")) {
			selSQL.append(" and   u.wx_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
			queryParams.add(searchParams.get("name"));
		}
		if (searchParams.get("subscribe") != null && !searchParams.get("subscribe").equals("")) {
				selSQL.append(" and u.subscribe = ?  ");
			queryParams.add(searchParams.get("subscribe"));
		}
		selSQL.append("  and length(u.wx_account)>0   ");
		selSQL.append("   order by   u.create_time desc   ");
		return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> getUserAll(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append("  select  t.id,t.user_id,t.vip_id,t.lib_id,t.type,t.create_time ,  ");
		selSQL.append("  CASE t.type  ");
		selSQL.append("  WHEN 1 THEN '在馆老师提交的时间'  ");
		selSQL.append("  WHEN 2 THEN '老师打回学生模糊更替的时间'  ");
		selSQL.append("  WHEN 3 THEN '老师打回学生不全更替的时间'  ");
		selSQL.append("  WHEN 4 THEN '家长查看作业反馈报告的时间'  ");
		selSQL.append("  WHEN 5 THEN '家长打开错题本的时间'  ");
		selSQL.append("  WHEN 6 THEN '家长打开学习成长报告周报的时间'  ");
		selSQL.append("  WHEN 7 THEN '家长打开学习成长报告月报的时间'  ");
		selSQL.append("  WHEN 8 THEN '家长打开学习成长报告学期的时间'  ");
		selSQL.append("  ELSE '其他' END as type_name ,  ");
		selSQL.append("  t.num ,v.child_name,v.img_url,v.school  ,v.derver ,v.grade ");
		selSQL.append("  from  tb_user_count t left join  tb_lib_vip v on t.vip_id = v.id  ");
		selSQL.append("  WHERE t.lib_id  = ?  ");
		queryParams.add(searchParams.get("libId"));
		if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
			selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
			queryParams.add(searchParams.get("child_name"));
		}
		if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
			selSQL.append(" and TO_DAYS(t.create_time) >=TO_DAYS(?) ");
			queryParams.add(searchParams.get("beginDate"));
		}
		if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
			selSQL.append(" and TO_DAYS(t.create_time) <=TO_DAYS(?) ");
			queryParams.add(searchParams.get("endDate"));
		}
		selSQL.append("   order by    ");
		selSQL.append(" t.create_time  desc ");
		return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}


}
