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

import com.ssj.dao.sys.usercenter.dao.UserCenterQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.core.util.MapGetterTool;
import com.ssj.framework.core.util.StringUtil;
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 Mount Liu
 * @date 2018/9/12
 */
@Repository
public class UserCenterQueryDaoImpl implements UserCenterQueryDao {
	
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> findStudentServiceList(String vipId, String species){
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSQL.append(" SELECT t1.*,t2.league_name,t2.leadue_manager from tb_lib_vip_service t1  ");
		selSQL.append(" LEFT JOIN tb_lib_join t2 on t1.lib_id = t2.id ");
		selSQL.append(" where t1.vip_id = ?  AND t1.species=? ");
		queryParams.add(vipId);
		queryParams.add(species);
		return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

    @Override
    public List<Map<String, Object>> findStudentOneToOneServiceList(String vipId) {
        StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<>();
        selSQL.append(" SELECT t1.*,t2.league_name,t2.leadue_manager from tb_lesson_service t1  ");
        selSQL.append(" LEFT JOIN tb_lib_join t2 on t1.lib_id = t2.id ");
        selSQL.append(" where t1.vip_id = ? ");
        queryParams.add(vipId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
    }

	@Override
	public List<Map<String, Object>> findStudentOneToOneServiceList(List<String> vipIdList) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSQL.append(" SELECT t1.*,t2.league_name,t2.leadue_manager from tb_lesson_service t1  ");
		selSQL.append(" LEFT JOIN tb_lib_join t2 on t1.lib_id = t2.id ");
		if (vipIdList != null && vipIdList.size() > 0) {
			selSQL.append(" where t1.vip_id in (");
			for (int i = 0; i < vipIdList.size(); i++) {
				String vipId = vipIdList.get(i);
				if (i == vipIdList.size() - 1) {
					selSQL.append("'" + vipId + "'");
				} else {
					selSQL.append("'" + vipId + "',");
				}
			}
			selSQL.append(" )");
		}
		return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findStudentWorkList(String vipId, String[] parentNames){
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSQL.append(" select t1.parent_name,t1.child_stat,count(t1.child_stat) counts from zyb_student_work_details t1  ");
		selSQL.append(" left join zyb_student_work t2 on t1.student_work_id = t2.id ");
		selSQL.append(" where t2.vip_id = ? ");
		queryParams.add(vipId);
		selSQL.append(" and t1.parent_name in (");
		selSQL.append("'"+StringUtils.join(parentNames, "','")+"'");
		selSQL.append(")");
		selSQL.append(" group by t1.parent_name,t1.child_stat ");
		return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findRoleList(String userId) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSQL.append("select id,'学生' as userRole from ssj_student_v where user_id = ? \n" +
				"union all\n" +
				"select id,'家长' as userRole from ssj_parent_v where id = ? \n" +
				"union all\n" +
				"select id,\n" +
				"  case when type = 2 then '在馆老师' \n" +
				"    when type = 7 then '一对一老师'\n" +
				"      when type = 8 then '在线老师' end as userRole from ssj_teacher_v where user_id = ? \n" +
				"union all\n" +
				"select user_id as id,'校长' as userRole from ssj_schoolmaster_v where user_id = ? \n" +
				"union all\n" +
				"select id,'馆长' as userRole from ssj_curator_v where user_id = ?\n" +
				"union all\n" +
				"select user_id id," +
				"	case when type = 8 then '批改老师'\n" +
						"when type = 11 then '学习管理员'\n" +
						"when type = 12 then '超级管理员'\n" +
						"when type = 13 then '匹配老师'\n" +
						"when type = 14 then '质检员'\n " +
				" end as userRole from tb_lib_manager where user_id = ? and type in (8,11,12,13,14) and state=1\n"
				);
		for (int i = 1; i <= 6; i++) {
			queryParams.add(userId);
		}
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findStudentSubscribeCountList(String id){
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSQL.append("select t.vip_id,t3.child_name,count(1) num from tb_lib_subscribe t\n" +
				"join tb_lib_join t1 on t.lib_id = t1.id\n" +
				"join ssj_teacher_v t2 on t1.id = t2.lib_id\n" +
				"  left join tb_lib_vip t3 on t.vip_id = t3.id\n" +
				"where t.ryke_time is not null\n" +
				"  and t2.id = ? \n" +
				"group by t.vip_id,t3.child_name " +
				"order by num desc ");
		queryParams.add(id);
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findOneByOneStudentList(String userId) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSQL.append("select distinct t.* from tb_lib_vip t\n" +
				"join kmt_course t1 on t.id = t1.vip_id\n" +
				"where t1.teacher_id = ? ");
		queryParams.add(userId);
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findWorkCountOfTeacherList(String userId) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSQL.append("select COUNT(1) as num,'finished_week' as type\n" +
				"from scon_homework_picture t\n" +
				"  left join scon_homework t1 on t.homework_id = t1.id\n" +
				"where t.is_feedback = 1\n" +
				"      and DATE_SUB(CURDATE(), INTERVAL 1 week) <= date(t1.correct_time)\n" +
				"and t.teacher_id = ?\n" +
				"union all\n" +
				"select COUNT(1) as num,'all_week' as type\n" +
				"from scon_homework_picture t\n" +
				"  left join scon_homework t1 on t.homework_id = t1.id\n" +
				"where DATE_SUB(CURDATE(), INTERVAL 1 week) <= date(t1.correct_time)\n" +
				"and t.teacher_id = ?\n" +
				"union all\n" +
				"select COUNT(1) as num,'finished_month' as type\n" +
				"from scon_homework_picture t\n" +
				"  left join scon_homework t1 on t.homework_id = t1.id\n" +
				"where t.is_feedback = 1\n" +
				"      and DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(t1.correct_time)\n" +
				"and t.teacher_id = ?\n" +
				"union all\n" +
				"select COUNT(1) as num,'all_month' as type\n" +
				"from scon_homework_picture t\n" +
				"  left join scon_homework t1 on t.homework_id = t1.id\n" +
				"where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(t1.correct_time)\n" +
				"and t.teacher_id = ?\n" +
				"union all\n" +
				"select COUNT(1) as num,'finished_year' as type\n" +
				"from scon_homework_picture t\n" +
				"  left join scon_homework t1 on t.homework_id = t1.id\n" +
				"where t.is_feedback = 1\n" +
				"      and DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= date(t1.correct_time)\n" +
				"and t.teacher_id = ?\n" +
				"union all\n" +
				"select COUNT(1) as num,'all_year' as type\n" +
				"from scon_homework_picture t\n" +
				"  left join scon_homework t1 on t.homework_id = t1.id\n" +
				"where DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= date(t1.correct_time)\n" +
				"and t.teacher_id = ?\n" +
				"union all\n" +
				"select COUNT(1) as num,'finished_all' as type\n" +
				"from scon_homework_picture t\n" +
				"  left join scon_homework t1 on t.homework_id = t1.id\n" +
				"where t.is_feedback = 1\n" +
				"and t.teacher_id = ?\n" +
				"union all\n" +
				"select COUNT(1) as num,'all' as type\n" +
				"from scon_homework_picture t\n" +
				"  left join scon_homework t1 on t.homework_id = t1.id\n" +
				"where t.teacher_id = ?");
		for (int i = 1; i <= 8; i++) {
			queryParams.add(userId);
		}
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public Page<Map<String, Object>> findConchListByPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder sql = new StringBuilder();
		MapGetterTool map = new MapGetterTool(params);
		sql.append("select t1.*\n" +
				/*"           (select v.child_name from tb_lib_vip v where t1.vip_id=v.id) child_name,\n" +
				"           (select v.parent_name from tb_lib_vip v where t1.vip_id=v.id) parent_name\n" +*/
				"    from conch_derver t1\n" +
				"	where 1=1\n");
		List<Object> queryParams = new ArrayList<>();
		String likeValue = map.getString("LIKE_value");
		if (StringUtil.isNotBlank(likeValue)) {
			sql.append("and (t1.derver regexp ? or t1.imei regexp ?)\n");
			queryParams.add(likeValue);
			queryParams.add(likeValue);
		}
		String userType = map.getString("use_type");
		if (StringUtil.isNotBlank(userType)) {
			sql.append("and t1.use_type = ").append(userType).append("\n");
		}
		sql.append("order by t1.use_type desc,t1.is_give_invalid");
		return dao.findPage(sql.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findStudentListByPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder sql = new StringBuilder();
		MapGetterTool map = new MapGetterTool(params);
		sql.append("select t1.*\n" +
				"    from tb_lib_vip t1\n" +
				"	where 1=1 and t1.user_id is not null \n");
		List<Object> queryParams = new ArrayList<>();
		String likeValue = map.getString("LIKE_value");
		if (StringUtil.isNotBlank(likeValue)) {
			sql.append("and (t1.child_name regexp ? or t1.parent_name regexp ? or t1.school regexp ? or t1.grade regexp ? or t1.derver regexp ? )\n");
			queryParams.add(likeValue);
			queryParams.add(likeValue);
			queryParams.add(likeValue);
			queryParams.add(likeValue);
			queryParams.add(likeValue);
		}
		String childType = map.getString("child_type");
		if (StringUtil.isNotBlank(childType)) {
			sql.append("and t1.child_type = ").append(childType).append("\n");
		}

		String startTime = map.getString("beginDate");
		if (StringUtil.isNotBlank(startTime)) {
			sql.append("and to_days(t1.activate_time) >= to_days(?)\n");
			queryParams.add(startTime);
		}

		String endTime = map.getString("endDate");
		if (StringUtil.isNotBlank(endTime)) {
			sql.append("and to_days(t1.activate_time) <= to_days(?)\n");
			queryParams.add(endTime);
		}
		sql.append("order by t1.activate_time desc");
		return dao.findPage(sql.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findVipStudentList(Map<String, Object> params, Pageable initPage) {
		StringBuilder sql = new StringBuilder();
		MapGetterTool map = new MapGetterTool(params);
		sql.append("select t.id,ifnull(t.wx_name,t.user_name) as user_name,t.mobile,DATE_FORMAT(t.create_time,'%Y-%m-%d %H:%i:%s') as create_time from tb_user t \n");

		sql.append(" left join tb_lib_vip t1 on t.id = t1.user_id ");
		sql.append("	where  mobile is not null \n");
		List<Object> queryParams = new ArrayList<>();
		String likeValue = map.getString("LIKE_value");
		String likeValue2 = map.getString("LIKE_value2");

		if (StringUtil.isNotBlank(likeValue2)) {
			sql.append("and (t.wx_name regexp ? or t.user_name regexp ? or t.mobile regexp ?  )\n");
			queryParams.add(likeValue2);
			queryParams.add(likeValue2);
			queryParams.add(likeValue2);
		}
		if (StringUtil.isNotBlank(likeValue)) {
			sql.append("and (t1.child_name regexp ? or t1.parent_name regexp ? or t1.school regexp ? or t1.grade regexp ?  )\n");
			queryParams.add(likeValue);
			queryParams.add(likeValue);
			queryParams.add(likeValue);
			queryParams.add(likeValue);
		}
		String startTime = map.getString("beginDate");
		if (StringUtil.isNotBlank(startTime)) {
			sql.append("and to_days(t.create_time) >= to_days(?)\n");
			queryParams.add(startTime);
		}

		String endTime = map.getString("endDate");
		if (StringUtil.isNotBlank(endTime)) {
			sql.append("and to_days(t.create_time) <= to_days(?)\n");
			queryParams.add(endTime);
		}
		sql.append("group by t.id ");
		sql.append("order by t.create_time desc,t.id");
		return dao.findPage(sql.toString(), queryParams.toArray(), initPage);
	}

	@Override
	public List<Map<String, Object>> findVipListByUserId(Map<String, Object> params) {
		StringBuilder sql = new StringBuilder();
		MapGetterTool map = new MapGetterTool(params);
		sql.append("select t.league_name,t1.child_name,t1.img_url,case when t1.sex=1 then '男' when t1.sex=2 then '女' else '未知' end sex,t1.phone,t1.parent_name,t1.school,t1.grade,DATE_FORMAT(t1.activate_time,'%Y-%m-%d %H:%i:%s') as activate_time from  tb_lib_vip t1 ");
        sql.append("	left join tb_lib_join t on t1.lib_id = t.id  \n");
		sql.append("	where  t1.user_id=? \n");
		List<Object> queryParams = new ArrayList<>();
		queryParams.add(params.get("userId"));
		String likeValue = map.getString("LIKE_value");
		if (StringUtil.isNotBlank(likeValue)) {
			sql.append("and (t1.child_name regexp ? or t1.parent_name regexp ? or t1.school regexp ? or t1.grade regexp ?  )\n");
			queryParams.add(likeValue);
			queryParams.add(likeValue);
			queryParams.add(likeValue);
			queryParams.add(likeValue);
		}
		sql.append("order by t1.activate_time desc");
		return dao.findMap(sql.toString(), queryParams.toArray());
	}

}
