package com.ssj.dao.kmt.correctsys.impl;

import com.ssj.bean.weixin.libmy.domain.TbLibLoginInfo;
import com.ssj.dao.kmt.correctsys.OneDayAndOneMonthDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.core.util.MapGetterTool;
import com.ssj.framework.core.util.StringUtil;
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.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yanghuan
 * @title: OneDayAndOneMonthDaoImpl
 * @projectName ssj
 * @description: TODO
 * @date 2019/8/1 16:27
 */
@Repository
public class OneDayAndOneMonthDaoImpl implements OneDayAndOneMonthDao {

    @Autowired
    private PagingHibernateJdbcDao dao;

    @Override
    public Page<Map<String, Object>> findAllTeacher(Map searchParams, Pageable pageable) {
        StringBuilder sql = new StringBuilder();
        List<Object> queryParams = new ArrayList<>();
        MapGetterTool map = new MapGetterTool(searchParams);
        String role = map.getString("role");
        String name = map.getString("name");
        String teacherId = map.getString("userId");
        String subject = map.getString("subject");
        String selectDay = map.getString("selectDay");
        int type = map.getInteger("type");
        sql.append("select * from tb_lib_manager where 1=1 and state=1 ");
        if (StringUtil.isNotBlank(name)) {
            sql.append("and name regexp ?");
            queryParams.add(name);
        }
        if (StringUtil.isNotBlank(subject)) {
            sql.append(" and course_name regexp ?");
            queryParams.add(subject);
        }
        if (null != role && "superManager".equals(role)) {
            sql.append("and parent_id=?");
            queryParams.add(teacherId);
        } else {
            sql.append("and (parent_id=? or user_id=?)");
            queryParams.add(teacherId);
            queryParams.add(teacherId);
        }
        if (13 != type) {
            sql.append(" and type in(8,11)");
        } else {
            sql.append(" and type=13");
        }
        if (StringUtil.isNotBlank(selectDay)) {
            //注销的时间需要是在选择时间之前才能看到该老师
            sql.append(" and (cancelled_time is null or cancelled_time >= ?)");
            queryParams.add(selectDay);
        }

        return dao.findPage(sql.toString(), queryParams.toArray(), pageable);
    }

    @Override
    public Map<String, Object> findAllCalculate(Map condition) {
        Map<String, Object> map = new HashMap<>();
        List<Object> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(condition);
        StringBuilder sql = new StringBuilder();
        String userId = mapGetterTool.getString("userId");
        String beginTime = mapGetterTool.getString("beginTime");
        String endTime = mapGetterTool.getString("endTime");
        int type = mapGetterTool.getInteger("type");
        if (type == 8) {
            sql.append("SELECT\n" +
                    "sum( case when s2.is_check_error=1  then 1 else 0 end )  as today_wrong_count,\n" +
                    "sum( ifNull(s2.operate_nums, 0) )  as pic_total_count,\n" +
                    "sum( ifNull(s2.correct_nums, 0)  )  as correct_count,\n" +
                    "sum(case when s2.is_check_error=0  AND s1.is_feedback = 1 AND s1.is_mark IN (0, 2)  then 1 else " +
                    "0 " +
                    "end) homework_pic_count\n" +
                    "FROM  scon_homework_picture s1\n" +
                    "  LEFT JOIN scon_homework_picture_correct s2 ON s1.id = s2.homework_picture_id\n" +
                    "  WHERE s1.teacher_id = ?\n" +
                    "   and s1.service_type=1\n" +
                    "  AND s2.end_time > ?\n" +
                    "  AND s2.end_time <= ? " +
                    " AND  s2.type =? ");
        } else {
            sql.append("SELECT\n" +
                    "sum( case when s2.is_check_error=1  then 1 else 0 end )  as today_wrong_count,\n" +
                    "sum( ifNull(s2.operate_nums, 0))  as pic_total_count,\n" +
                    "sum( ifNull(s2.correct_nums, 0) )  as correct_count,\n" +
                    "sum(case when s2.is_check_error=0  AND s1.is_know_feedback=1  then 1 else 0  " +
                    "end) homework_pic_count\n" +
                    "FROM  scon_homework_picture s1\n" +
                    "  LEFT JOIN scon_homework_picture_correct s2 ON s1.id = s2.homework_picture_id\n" +
                    "  WHERE s1.know_teacher_id = ?\n" +
                    "   and s1.service_type=1\n" +
                    "  AND s2.end_time > ?\n" +
                    "  AND s2.end_time <= ?" +
                    " AND  s2.type =? ");
        }
        queryParams.add(userId);
        queryParams.add(beginTime);
        queryParams.add(endTime);
        queryParams.add(type);
        List<Map<String, Object>> list = dao.findMap(sql.toString(), queryParams.toArray());
        return list.get(0);
    }

    @Override
    public Map<String, Object> findOneMonthAllCalculate(Map condition) {
        Map<String, Object> map = new HashMap<>();
        List<Object> queryParams = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        MapGetterTool mapGetterTool = new MapGetterTool(condition);
        String userId = mapGetterTool.getString("userId");
        String startTime = mapGetterTool.getString("startTime");
        String endTime = mapGetterTool.getString("endTime");
        int type = mapGetterTool.getInteger("type");
        if (type == 8) {
            sql.append("SELECT\n" +
                    "sum( case when s2.is_check_error=1  then 1 else 0 end )  as today_wrong_count,\n" +
                    "sum( ifNull(s2.operate_nums, 0) )  as pic_total_count,\n" +
                    "sum( ifNull(s2.correct_nums, 0)  )  as correct_count,\n" +
                    "sum(case when s2.is_check_error=0  AND s1.is_feedback = 1 AND s1.is_mark IN (0, 2)  then 1 else " +
                    "0 " +
                    "end) homework_pic_count\n" +
                    "FROM  scon_homework_picture s1\n" +
                    "  LEFT JOIN scon_homework_picture_correct s2 ON s1.id = s2.homework_picture_id\n" +
                    "  WHERE s1.teacher_id = ?\n" +
                    "and s1.service_type=1\n" +
                    "  AND s2.end_time > ?\n" +
                    "  AND s2.end_time <= ? " +
                    " AND  s2.type =? ");
        } else {
            sql.append("SELECT\n" +
                    "sum( case when s2.is_check_error=1  then 1 else 0 end )  as today_wrong_count,\n" +
                    "sum( ifNull(s2.operate_nums, 0))  as pic_total_count,\n" +
                    "sum( ifNull(s2.correct_nums, 0) )  as correct_count,\n" +
                    "sum(case when s2.is_check_error=0  AND s1.is_know_feedback=1  AND s1.is_mark in(0,2) then 1 else" +
                    " 0  " +
                    "end) homework_pic_count\n" +
                    "FROM  scon_homework_picture s1\n" +
                    "  LEFT JOIN scon_homework_picture_correct s2 ON s1.id = s2.homework_picture_id\n" +
                    "  WHERE s1.know_teacher_id = ?\n" +
                    "   and s1.service_type=1\n" +
                    "  AND s2.end_time > ?\n" +
                    "  AND s2.end_time <= ?" +
                    " AND  s2.type =? ");
        }
        queryParams.add(userId);
        queryParams.add(startTime);
        queryParams.add(endTime);
        queryParams.add(type);
        List<Map<String, Object>> list = dao.findMap(sql.toString(), queryParams.toArray());
        return list.get(0);
    }

    @Override
    public Map<String, Object> findWhetherOnline(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("userId");
        String date = mapGetterTool.getString("date");
        sql.append("select count(id) from tb_lib_manager where user_id=? and login_time regexp ?");
        queryParams.add(teacherId);
        queryParams.add(date);
        List<Map<String, Object>> list = dao.findMap(sql.toString(), queryParams.toArray());
        return list.get(0);
    }

    @Override
    public Map<String, Object> findAttendacneRate(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("teacherId");
        String startTime = mapGetterTool.getString("startMonth");
        String endTime = mapGetterTool.getString("endMonth");
        sql.append("SELECT  count(id) as attendance_count ,   sum( case when attend_type = 1 then 1 else 0 end )  as " +
                "actual_attendance_count ");
        sql.append("FROM  kmt_arrange_allot  WHERE  teacher_id=? and arrange_date>=? and arrange_date<? and  status=1" +
                " ");
        queryParams.add(teacherId);
        queryParams.add(startTime);
        queryParams.add(endTime);
        return dao.findMap(sql.toString(), queryParams.toArray()).get(0);

    }

    @Override
    public Map<String, Object> findTeacherSatis(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("userId");
        String startTime = mapGetterTool.getString("startTime");
        String endTime = mapGetterTool.getString("endTime");
        String role = mapGetterTool.getString("role");
        sql.append("select avg(IFNULL(judge_level,5)) judge from tb_teacher_assessment t1 left join tb_lib_manager t2");
        sql.append(" on t1.teacher_id=t2.user_id");
        sql.append(" where t1.type=2");
        if (null != role && "superManager".equals(role)) {
            sql.append(" and (t2.parent_id=? or user_id=?)");
            queryParams.add(teacherId);
            queryParams.add(teacherId);
        } else {
            sql.append(" and t2.parent_id=?");
            queryParams.add(teacherId);
        }
        sql.append(" and t2.type in(8,11)");
        sql.append(" and t1.create_time >=? and t1.create_time<?");
        queryParams.add(startTime);
        queryParams.add(endTime);
        return dao.findMap(sql.toString(), queryParams.toArray()).get(0);
    }

    @Override
    public Map<String, Object> findTotalAttenRate(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("userId");
        String startTime = mapGetterTool.getString("startMonth");
        String endTime = mapGetterTool.getString("endMonth");
        int type = mapGetterTool.getInteger("type");
        sql.append(" SELECT count(t1.id) as should_count ,   sum( case when t1.attend_type=1 then 1 else 0 end )  as " +
                "actual_count FROM  kmt_arrange_allot t1");
        sql.append(" LEFT JOIN tb_lib_manager t2 ON t1.teacher_id = t2.user_id WHERE ");
        sql.append(" t1.arrange_date>=? and t1.arrange_date<? and t1.status=1  and (t2.parent_id=? or t2.user_id=?) ");
        queryParams.add(startTime);
        queryParams.add(endTime);
        queryParams.add(teacherId);
        queryParams.add(teacherId);
        if (13 != type) {
            sql.append(" and t2.type in(8,11)");
        } else {
            sql.append(" and t2.type=13");
        }
        return dao.findMap(sql.toString(), queryParams.toArray()).get(0);
    }

    @Override
    public Map<String, Object> findRespTime(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryparams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        int type = mapGetterTool.getInteger("type");
        String teacherId = mapGetterTool.getString("parentId");
        String role = mapGetterTool.getString("role");
        String date = mapGetterTool.getString("date");
        int timeType = mapGetterTool.getInteger("timeType");
        sql.append("select avg(UNIX_TIMESTAMP(IFNULL(t1.start_time,now()))-UNIX_TIMESTAMP(t1.allot_time)) respTime");
        sql.append(" from scon_homework_picture_correct t1 left join  scon_homework_picture t2 ");
        sql.append(" on t1.homework_picture_id=t2.id and t2.service_type=1 ");
        sql.append(" left join tb_lib_manager t3");
        if (13 != type) {
            sql.append(" on t2.teacher_id=t3.user_id");
            sql.append(" where ");
            sql.append("  t1.type=8");
            if (null != role && "superManager".equals(role)) {
                sql.append(" and (t3.parent_id=?)  ");
                sql.append(" and t3.type=8");
                queryparams.add(teacherId);
            } else {
                sql.append(" and (t3.user_id=? or t3.parent_id=?)");
                sql.append(" and t3.type in(8,11)");
                queryparams.add(teacherId);
                queryparams.add(teacherId);
            }
        } else {
            sql.append(" on t2.know_teacher_id=t3.user_id");
            sql.append(" where ");
            sql.append("  t3.type=13");
            sql.append(" and t3.parent_id=?");
            sql.append(" and t1.type=8");
            queryparams.add(teacherId);
        }
        if (0 == timeType) {
            sql.append(" and t1.allot_time>?");
        } else {
            sql.append(" and t1.allot_time regexp ?");
        }
        queryparams.add(date);
        return dao.findMap(sql.toString(), queryparams.toArray()).get(0);
    }

    @Override
    public Map<String, Object> findAvgTime(Map params) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(params);
        int type = mapGetterTool.getInteger("type");
        String teacherId = mapGetterTool.getString("teacherId");
        String date = mapGetterTool.getString("date");
        int timeType = mapGetterTool.getInteger("timeType");
        sql.append("select avg(UNIX_TIMESTAMP(IFNULL(t1.start_time,now()))-UNIX_TIMESTAMP(t1.allot_time)) avgTime");
        sql.append(" from scon_homework_picture_correct t1 left join  scon_homework_picture t2 ");
        sql.append(" on t1.homework_picture_id=t2.id and t2.service_type=1 ");
        if (13 != type) {
            sql.append(" where t2.teacher_id=?");
            sql.append(" and t1.type=8");
            queryParams.add(teacherId);
        } else {
            sql.append(" where t2.know_teacher_id=?");
            sql.append(" and t1.type=13");
            queryParams.add(teacherId);
        }
        if (0 != timeType) {
            sql.append(" and t1.allot_time regexp ?");
        } else {
            sql.append(" and t1.allot_time >=?");
        }

        queryParams.add(date);
        return dao.findMap(sql.toString(), queryParams.toArray()).get(0);
    }

    @Override
    public List<Map<String, Object>> findAllUnfinshHomework(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String date = mapGetterTool.getString("date");
        sql.append("select homework_picture_id from scon_homework_picture_correct where type=13 and not(start_time is" +
                " not null and end_time is not null ) and allot_time regexp ?");
        queryParams.add(date);
        return dao.findMap(sql.toString(), queryParams.toArray());
    }

    @Override
    public Page<Map<String, Object>> findAllExreciseTeacherInfo(Pageable pageable, Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String month = mapGetterTool.getString("month");
        String name = mapGetterTool.getString("name");
        sql.append("select t1.*,\n" +
                " t2.card_bank,t2.subbranch,t2.card_name,t2.card_num,t2.bankcard_face,t2.bankcard_con,t2.bank_icon \n" +
                " from tb_lib_manager t1 left join");
        sql.append(" tb_card_info t2 on t1.user_id=t2.user_id where t2.state=1 and t1.type =17 and t1.state=1");
        if (StringUtil.isNotBlank(month)) {
            sql.append(" and t1.create_time regexp ?");
            queryParams.add(month);
        }
        if(StringUtil.isNotBlank(name)){
            sql.append(" and t1.name regexp ?");
            queryParams.add(name);
        }
        sql.append(" order by t1.create_time,t1.is_online desc");
        return dao.findPage(sql.toString(), queryParams.toArray(), pageable);


    }

    @Override
    public Page<Map<String, Object>> findAllTeacherInfo(Pageable pageable, Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String name = mapGetterTool.getString("name");
        sql.append("select t1.user_id,t1.name,t1.t_photo,t1.type,t1.is_banned,t1.is_online,t1.create_time,t1" +
                ".telephone,t1.id_num,t1.course_name,t1.id_face,t2.card_bank,t2.subbranch,t2.card_name,t2.card_num " +
                "from tb_lib_manager t1 left join");
        sql.append(" tb_card_info t2 on t1.user_id=t2.user_id where t2.state=1 and t1.state=1 and t1.type in(8,13) ");
        if (StringUtil.isNotBlank(name)) {
            sql.append(" and t1.name regexp ?");
            queryParams.add(name);
        }
        sql.append(" order by t1.is_online desc");
        return dao.findPage(sql.toString(), queryParams.toArray(), pageable);


    }

    @Override
    public Map<String, Object> findTeacherCount(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("teacherId");
        String date = mapGetterTool.getString("date");
        int type = mapGetterTool.getInteger("type");
        sql.append("select count(t2.id) count from scon_homework_picture t1 left join scon_homework_picture_correct " +
                "t2 on t1.id=t2.homework_picture_id ");
        sql.append("where t1.teacher_id=? and t2.is_check_error=0 and replace and t2.end_time regexp ? and t1.service_type=1");
        String selsql = sql.toString();
        if (8 == type) {
            selsql = selsql.replace("replace", "t1.is_mark in(0,2) and t1.is_feedback=1");
        } else {
            selsql = selsql.replace("replace", "t1.is_know_feedback=1");
        }

        queryParams.add(teacherId);
        queryParams.add(date);
        return dao.findMap(selsql, queryParams.toArray()).get(0);


    }

    @Override
    public Map<String, Object> findTheCount(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("teacherId");
        int integraType = mapGetterTool.getInteger("integraType");
        String date = mapGetterTool.getString("date");
      /* sql.append("select t1.end_time as time from scon_homework_picture_correct t1 left join scon_homework_picture
       t2 on t1.homework_picture_id=t2.id ");
       sql.append("where t2.teacher_id=? and t1.is_check_error=0 and t2.is_feedback=1 and t2.is_mark in(0,2) and t1
       .end_time regexp? order by t1.end_time asc limit 199,1");*/
        sql.append("select  t3.end_time as time from  scon_homework t4 \n" +
                "               JOIN scon_homework_picture t2 ON t4.id = t2.homework_id  \n" +
                "                JOIN scon_homework_picture_correct t3 ON t2.id = t3.homework_picture_id \n" +
                "               WHERE t2.is_feedback = 1 \n" +
                "               and t2.service_type=1\n" +
                "                AND t2.is_mark IN ( 0, 2 ) \n" +
                "                AND t2.teacher_id =? \n" +
                "                AND t3.is_check_error = 0 \n" +
                "               AND t3.end_time REGEXP ? \n" +
                "                AND t3.type IN ( 8, 11 ) order by t3.end_time asc ");
        if (1 == integraType) {
            sql.append("limit 99,1");
        } else {
            sql.append("limit 199,1");
        }
        queryParams.add(teacherId);
        queryParams.add(date);
        List<Map<String, Object>> list = dao.findMap(sql.toString(), queryParams.toArray());
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    @Override
    public List<Map<String, Object>> findAllCorrectTeacher(Map map) {
        StringBuilder selsql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selsql.append("select t_photo,name,user_id from tb_lib_manager where state=1 and type in(8,11) and " +
                "is_banned=0 ");
        return dao.findMap(selsql.toString(), queryParams.toArray());

    }

    @Override
    public Map<String, Object> findKnowCount(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("teacherId");
        String date = mapGetterTool.getString("date");
        sql.append("select t1.end_time as time from scon_homework_picture_correct t1 left join scon_homework_picture " +
                "t2 on t1.homework_picture_id=t2.id");
        sql.append("where  t2.service_type=1 and t2.teacher_id=? and t1.is_check_error=0 and t1.type=13 and t1.end_time regexp? order by t1.end_time asc limit 299,1");
        queryParams.add(date);
        queryParams.add(teacherId);
        return dao.findMap(sql.toString(), queryParams.toArray()).get(0);
    }

    @Override
    public Map<String, Object> findMonthCount(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("teacherId");
        String date = mapGetterTool.getString("date");
        sql.append("select * from kmt_arrange_allot where arrange_date regexp ? and teacher_id=?");
        queryParams.add(date);
        queryParams.add(teacherId);
        return dao.findMap(sql.toString(), queryParams.toArray()).get(0);
    }

    @Override
    public Map<String, Object> findWrongCount(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("teacherId");
        String date = mapGetterTool.getString("date");
        sql.append("select count(1) wrongCount from scon_homework_picture s1 left join scon_homework_picture_correct " +
                "s2 on s1.id=s2.homework_picture_id where s1.service_type=1 and s1.teacher_id =? and s2.is_check_error=1 and s2.end_time " +
                "regexp ? ");
        queryParams.add(teacherId);
        queryParams.add(date);
        return dao.findMap(sql.toString(), queryParams.toArray()).get(0);
    }

    @Override
    public Page<Map<String, Object>> findByStartTimeAndEndTime(Map map, Pageable pageable) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String teacherId = mapGetterTool.getString("teacherId");
        String startTime = mapGetterTool.getString("startTime");
        String endTime = mapGetterTool.getString("endTime");
        sql.append("select distinct arrange_date from tb_lib_login_info where teacher_id=? and login_time>=? and " +
                "login_time<? order by arrange_date desc");
        queryParams.add(teacherId);
        queryParams.add(startTime);
        queryParams.add(endTime);
        return dao.findPage(sql.toString(), queryParams.toArray(), pageable);
    }

    @Override
    public Map<String, Object> querySimpleTeacherCalculate(Map map) {
        StringBuilder sql = new StringBuilder();
        List<String> queryParams = new ArrayList<>();
        MapGetterTool mapTool = new MapGetterTool(map);
        String teacherId = mapTool.getString("teacherId");
        String date = mapTool.getString("date");
        int type = mapTool.getInteger("type");
        int timeType = mapTool.getInteger("timeType");
        sql.append("select sum(case when t2.is_feedback=1 then t1.correction_time else 0 end ) as totalCorrectTime,\n" +
                "       max(UNIX_TIMESTAMP( IFNULL( t1.start_time, now( ) ) ) - UNIX_TIMESTAMP( t1.allot_time ))\t as" +
                " maxWaitTime ,\n" +
                "       avg( UNIX_TIMESTAMP( IFNULL( t1.start_time, now( ) ) ) - UNIX_TIMESTAMP( t1.allot_time ) ) " +
                " avgTime\n" +
                " FROM  scon_homework_picture_correct t1\n" +
                "          left join scon_homework_picture t2\n" +
                "                    on t1.homework_picture_id=t2.id and t2.service_type=1");
        if (13 != type) {
            sql.append(" where t2.teacher_id=?");
            sql.append(" and t1.type=8");
            queryParams.add(teacherId);
        } else {
            sql.append(" where t2.know_teacher_id=?");
            sql.append(" and t1.type=13");
            queryParams.add(teacherId);
        }
        if (0 != timeType) {
            sql.append(" and t1.allot_time regexp ?");
            queryParams.add(date);
        } else {
            sql.append(" and t1.allot_time >=?");
            queryParams.add(date);
        }

        return dao.findMap(sql.toString(), queryParams.toArray()).get(0);
    }

    @Override
    public Page<Map<String, Object>> sortTeacherInfoByHomePicCount(Map map, Pageable pageable) {
        List<Object> queryParam = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        MapGetterTool mapGetterTool = new MapGetterTool(map);
        String role = mapGetterTool.getString("role");
        String name = mapGetterTool.getString("name");
        String selectDay = mapGetterTool.getString("selectDay");
        String userId = mapGetterTool.getString("userId");
        String startTime = mapGetterTool.getString("startTime");
        String endTime = mapGetterTool.getString("endTime");
        int type = mapGetterTool.getInteger("type");
        sql.append("SELECT\n" +
                "    sum( CASE WHEN s2.is_check_error = 1 THEN 1 ELSE 0 END ) AS today_wrong_count,\n" +
                "    sum( ifNull( s2.operate_nums, 0 ) ) AS pic_total_count,\n" +
                "    sum( ifNull( s2.correct_nums, 0 ) ) AS correct_count,\n" +
                "    sum( CASE WHEN s2.is_check_error = 0 AND replace1 AND s1.is_mark IN ( 0, 2 ) THEN 1 ELSE 0 END )" +
                " " +
                " homework_pic_count,\n" +
                "    avg( IFNULL( s2.correction_time, 0 ) ) avg_time,\n" +
                "    s1.teacher_id,\n" +
                "    e.*\n" +
                " FROM\n" +
                "    scon_homework_picture s1\n" +
                "        LEFT JOIN scon_homework_picture_correct s2 ON s1.id = s2.homework_picture_id\n" +
                "        JOIN tb_lib_manager e ON e.user_id = s1.replace7\n" +
                "         replace4  AND e.state = 1\n" +
                "      replace2  AND e.type replace6\n" +
                "        replace5\n" +
                " WHERE\n" +
                "    s1.replace3 IS NOT NULL\n" +
                "    and s1.service_type=1\n" +
                "    AND s2.end_time > ?\n" +
                "    AND s2.end_time <=?\n" +
                "    AND s2.type = ?\n" +
                " GROUP BY\n" +
                "    s1.teacher_id\n" +
                " ORDER BY\n" +
                "   e.is_online desc, homework_pic_count DESC");
        String sqlString = sql.toString();
        if (8 == type) {
            sqlString = sqlString.replace("replace1", "s1.is_feedback=1");
            sqlString = sqlString.replace("replace3", "teacher_id");
            sqlString = sqlString.replace("replace6", "in(8,11)");
            sqlString = sqlString.replace("replace7", "teacher_id");
        } else {
            sqlString = sqlString.replace("replace1", "s1.is_know_feedback=1");
            sqlString = sqlString.replace("replace3", "know_teacher_id");
            sqlString = sqlString.replace("replace6", "=13");
            sqlString = sqlString.replace("replace7", "know_teacher_id");
        }
        if (null != role && role.equals("superManager")) {
            sqlString = sqlString.replace("replace2", " and e.parent_id=? ");
            queryParam.add(userId);
        } else {
            sqlString = sqlString.replace("replace2", " and (e.parent_id=? or e.user_id=?)");
            queryParam.add(userId);
            queryParam.add(userId);
        }
        if (StringUtil.isNotBlank(name)) {
            sqlString = sqlString.replace("replace4", " and e.name regexp ? ");
            queryParam.add(name);
        } else {
            sqlString = sqlString.replace("replace4", " and 1=1");
        }
        if (StringUtil.isNotBlank(selectDay)) {
            sqlString = sqlString.replace("replace5", " and (cancelled_time is null or cancelled_time >= ?)");
            queryParam.add(selectDay);
        } else {
            sqlString = sqlString.replace("replace5", " and 2=2");
        }
        queryParam.add(startTime);
        queryParam.add(endTime);
        queryParam.add(type);
        return dao.findPage(sqlString, queryParam.toArray(), pageable);
    }

}



