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

import com.ssj.dao.sys.teacher.dao.CorrectTeacherQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.service.sys.teacher.dto.CorrectionCountDTO;
import com.ssj.service.sys.teacher.dto.OnlineCorrectTeacherDTO;
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.Date;
import java.util.List;

/**
 * @author yrf
 * @date 2019/4/9 13:59
 */
@Repository
public class CorrectTeacherQueryDaoImpl implements CorrectTeacherQueryDao {

    @Autowired
    private PagingHibernateJdbcDao pagingHibernateJdbcDao;

    @Override
    public Page<OnlineCorrectTeacherDTO> findCorrectTeacherPage(String teacherName, Pageable pageable) {
        String sql =
                "select tb_lib_manager.user_id teacher_id,\n" +
                "       tb_lib_manager.t_photo                                                         teacher_img,\n" +
                "       tb_lib_manager.name                                                            teacher_name,\n" +
                "       tb_lib_manager.course_name,\n" +
                "       tb_lib_manager.grade,\n" +
                "       tb_lib_manager.correct_homework_num                                  correct_homework_picture_count,\n" +
                "       (select count(*)\n" +
                "        from scon_homework_picture\n" +
                "        where scon_homework_picture.is_delete = 0\n" +
                "          and is_feedback = 0\n" +
                "          and is_mark in (0, 2)\n" +
                "          and to_days(scon_homework_picture.create_time) = to_days(now())\n" +
                "          and scon_homework_picture.teacher_id = tb_lib_manager.user_id) correct_homework_num,\n" +
                "       (select count(*)\n" +
                "        from scon_homework_picture\n" +
                "        where scon_homework_picture.is_delete = 0\n" +
                "          and (is_feedback = 1 or is_mark = 1 or is_mark = 3)\n" +
                "          and to_days(scon_homework_picture.create_time) = to_days(now())\n" +
                "          and scon_homework_picture.teacher_id = tb_lib_manager.user_id) corrected_homework_num,\n" +
                "       (select count(distinct vip_id)\n" +
                "        from scon_homework_picture\n" +
                "        where scon_homework_picture.is_delete = 0\n" +
                "          and is_feedback = 0\n" +
                "          and is_mark in (0, 2)\n" +
                "          and to_days(scon_homework_picture.create_time) = to_days(now())\n" +
                "          and scon_homework_picture.teacher_id = tb_lib_manager.user_id) correct_student_num,\n" +
                "       tb_lib_manager.is_online,\n" +
                "       tb_lib_manager.login_time,\n" +
                "       tb_lib_manager.logout_time,\n" +
                "       tb_lib_manager.create_time\n" +
                "from tb_lib_manager\n" +
                "where tb_lib_manager.type = 8\n" +
                "  and tb_lib_manager.state = 1\n";

        if (StringUtils.isNotBlank(teacherName)) {
            sql += "and tb_lib_manager.name like '" + teacherName + "%'";
        }

        sql += "order by is_online desc, correct_homework_num desc, id \n";

        return pagingHibernateJdbcDao.findPage(sql, pageable, OnlineCorrectTeacherDTO.class);
    }

    @Override
    public Page<CorrectionCountDTO> correctionCount(String teacherName, Date beginTime, Date endTime, Pageable pageable) {
        List<Object> paramList = new ArrayList<>();

        StringBuilder sql = new StringBuilder(
                "select temp.teacher_name,\n" +
                "       temp.teacher_img,\n" +
                "       temp.subject,\n" +
                "       temp.user_id,\n" +
                "       sum(temp.picture_num)     homework_picture_count,\n" +
                "       sum(temp.correction_time) correction_time_sum\n" +
                "from (select tb_lib_manager.user_id,\n" +
                "             tb_lib_manager.name             teacher_name,\n" +
                "             tb_lib_manager.t_photo          teacher_img,\n" +
                "             scon_homework_picture.subject,\n" +
                "             scon_homework.correction_time,\n" +
                "             scon_homework.id ,\n" +
                "             count(scon_homework_picture.id) picture_num\n" +
                "      from scon_homework,\n" +
                "           scon_homework_picture,\n" +
                "           tb_lib_manager\n" +
                "      where ? < scon_homework.create_time\n" +
                "        and scon_homework.create_time < ?\n" +
                "        and scon_homework.id = scon_homework_picture.homework_id\n" +
                "        and scon_homework_picture.is_feedback = 1\n" +
                "        and scon_homework_picture.is_delete = 0\n" +
                "        and scon_homework_picture.teacher_id = tb_lib_manager.user_id\n" +
                "        and tb_lib_manager.type = 8\n" +
                "        and tb_lib_manager.state = 1\n");

        paramList.add(beginTime);
        paramList.add(endTime);

        if (StringUtils.isNotBlank(teacherName)) {
            sql.append("  and tb_lib_manager.name like '").append(teacherName).append("%'\n");
        }

        sql.append("      group by tb_lib_manager.user_id, scon_homework.id) temp\n");
        sql.append("group by temp.user_id, temp.subject\n");
     //   sql.append("order by sum(temp.correction_time) / sum(temp.picture_num), user_id\n");
        sql.append("order by user_id ");
        return pagingHibernateJdbcDao.findPage(sql.toString(), paramList.toArray(), pageable, CorrectionCountDTO.class);
    }

}
