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

import com.ssj.framework.core.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ssj.bean.sys.knowledge.domain.KnowDimension;
import com.ssj.dao.sys.situation.dao.SituationDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;

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

@Repository("situationDao")
public class SituationDaoImpl implements SituationDao {

    @Autowired
    private PagingHibernateJdbcDao dao;

    @Override
    public List<KnowDimension> findKnowDimesion() {
        String sql = "select * from know_dimension";
        return dao.findList(sql, null, KnowDimension.class);
    }

    public List<Map<String, Object>> findKnowledgeDimesion (String subject) {
        String sql = "select distinct t2.course_name, t1.module from zyb_know_how_details t1 left join zyb_know_how_details_sort t2 on t1.id=t2.how_details_id and t2.stat=2 where t1.module is not null  \n" +
                " and t1.module != '' and t2.course_name = ?";
        List<Object> params = new ArrayList<>();
        params.add(subject);
        return dao.findMap(sql, params.toArray());
    }

    public List<Map<String, Object>> findChineseKnowledgeDimension () {
        String sql = "select distinct t2.course_name, t1.two_module module from zyb_know_how_details t1 left join zyb_know_how_details_sort t2 on t1.id=t2.how_details_id and t2.stat=2 where t1.two_module is not null  \n" +
                " and t1.two_module != '' and t2.course_name='语文'";
        return dao.findMap(sql, null);
    }


    @Override
    public List<Map<String, Object>> getHomeworkKnowModuleData(String vipId, String subject, String beginTime, String endTime, int type) {
        StringBuilder sb = new StringBuilder ();
        sb.append("select t3.module, count(1) count from kmt_homework_wrong_question t1 left join scon_homework t4 on t1.homework_id=t4.id ")
                .append(" left join kmt_exercise_question_know t2 on t1.question_id=t2.question_id left join zyb_know_how_details t3 on t2.how_details_id=t3.id")
                .append("  where t4.subject = ? and t1.create_time BETWEEN ? and  ?");
        List<Object> param = new ArrayList<>();
        param.add(subject);
        param.add(beginTime);
        param.add(endTime);
        if (type == 1){
            sb.append(" and t1.type=1");
        }
        if (!StringUtil.isEmpty(vipId)){
            sb.append(" and t1.student_id= ? ");
            param.add(vipId);
        }
        sb.append(" GROUP BY t3.module");
        return  dao.findMap(sb.toString(), param.toArray());
    }

    @Override
    public List<Map<String, Object>> getChineseHomeworkKnowModuleData(String vipId, String subject, String beginTime, String endTime, int type) {
        StringBuilder sb = new StringBuilder ();
        sb.append("select t3.two_module module, count(1) count from kmt_homework_wrong_question t1 left join scon_homework t4 on t1.homework_id=t4.id ")
                .append(" left join kmt_exercise_question_know t2 on t1.question_id=t2.question_id left join zyb_know_how_details t3 on t2.how_details_id=t3.id")
                .append("  where t4.subject = ? and t1.create_time BETWEEN ? and  ?");
        List<Object> param = new ArrayList<>();
        param.add(subject);
        param.add(beginTime);
        param.add(endTime);
        if (type == 1){
            sb.append(" and t1.type=1");
        }
        if (!StringUtil.isEmpty(vipId)){
            sb.append(" and t1.student_id= ? ");
            param.add(vipId);
        }
        sb.append(" GROUP BY t3.two_module");
        return  dao.findMap(sb.toString(), param.toArray());
    }


    @Override
    public List<Map<String, Object>> getHomeworkKnowClassData(String vipId, String subject, String beginTime, String endTime, int type) {
        StringBuilder sb = new StringBuilder();
        sb.append("select sum(t3.memorize) memorize, sum(t3.understanding) understanding,")
                .append(" sum(t3.apply) apply from kmt_homework_wrong_question t4  ")
                .append(" left join kmt_exercise_question_know t1 on t1.question_id=t4.question_id left join scon_homework t2 ")
                .append(" on t4.homework_id = t2.id left join zyb_know_how_details t3 on t1.how_details_id=t3.id where t2.subject=?")
                .append(" and t2.create_time between ? and ? and t2.vip_id=?");
        if (type == 1){
            sb.append(" and t4.type = 1 ");
        }
        List<Object> param = new ArrayList<>();
        param.add(subject);
        param.add(beginTime);
        param.add(endTime);
        param.add(vipId);
        return dao.findMap(sb.toString(), param.toArray());
    }

    @Override
    public List<String> queryAllServiceStudent () {
        StringBuilder sb = new StringBuilder();
        sb.append("select DISTINCT vip_id from tb_lib_vip_service");
        return dao.findList(sb.toString(), String.class);
    }

    @Override
    public List<Map<String, Object>> get121KnowModuleData(String vipId, String subject, String beginTime, String endTime) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT d.module ,COUNT(DISTINCT(d.id)) as count FROM `kmt_course_question_detail`  t  ")
                .append(" LEFT JOIN kmt_course c ON t.course_know_id=c.course_know_id   LEFT JOIN zyb_know_how_details d ON t.how_details_id=d.id  ")
                .append(" LEFT JOIN zyb_know_how h ON h.id=d.how_id   where  h.course_name='").append(subject)
                .append("' AND c.create_time between '")
                .append(beginTime).append("' and '").append(endTime).append("'");
        if (!StringUtils.isEmpty(vipId)){
            sb.append(" and c.vip_id='").append(vipId).append("'");
        }
        sb.append(" GROUP BY d.module");

        return dao.findMap(sb.toString(), null);
    }

    @Override
    public List<Map<String, Object>> get121WrongKnowModuleData(String vipId, String subject, String beginTime, String endTime) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT d.module, COUNT(DISTINCT(d.id)) as count from kmt_img_face_detail t")
                .append(" LEFT JOIN kmt_img_face f ON t.face_id=f.id  LEFT JOIN kmt_course c ON f.course_know_id=c.course_know_id ")
                .append(" LEFT JOIN zyb_know_how_details d ON t.how_details_id=d.id  LEFT JOIN zyb_know_how h ON h.id=d.how_id")
                .append(" where h.course_name='").append(subject).append("' and c.create_time between '").append(beginTime).append("' and '").append(endTime).append("'");
        if (!StringUtils.isEmpty(vipId)){
            sb.append(" and c.vip_id='").append(vipId).append("'");
        }
        sb.append(" GROUP BY d.module");

        return dao.findMap(sb.toString(), null);
    }

    @Override
    public List<String> get121SkillData(String vipId, String subject, String beginTime, String endTime) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT q.skill module from kmt_course_question_detail t  LEFT JOIN kmt_question q ON t.question_id=q.id ")
                .append(" LEFT JOIN kmt_course c ON t.course_know_id=c.course_know_id where q.skill is NOT null ")
                .append(" AND q.skill !='' AND q.course_name='").append(subject).append("' AND  c.create_time between '")
                .append(beginTime).append("' and '").append(endTime).append("'");
        if (!StringUtils.isEmpty(vipId)){
            sb.append("  and c.vip_id='").append(vipId).append("'");
        }
        return dao.findList(sb.toString(), String.class);
    }

    @Override
    public List<String> get121SkillWrongData(String vipId, String subject, String beginTime, String endTime) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT q.skill module from kmt_img_face_detail t LEFT JOIN kmt_question q ON t.question_id=q.id")
                .append(" LEFT JOIN kmt_img_face f ON t.face_id=f.id LEFT JOIN kmt_course c ON f.course_know_id=c.course_know_id ")
                .append(" where q.skill is NOT null AND q.skill !='' AND q.course_name='").append(subject).append("' AND  c.create_time between '")
                .append(beginTime).append("' and '").append(endTime).append("'");
        if (!StringUtils.isEmpty(vipId)){
            sb.append(" and c.vip_id='").append(vipId).append("'");
        }
        return dao.findList(sb.toString(), String.class);
    }

    @Override
    public List<Map<String, Object>> getHomeworkWrongDetail(String vipId, String subject, String beginTime, String endTime) {
        StringBuilder sb = new StringBuilder();
        sb.append("select distinct t5.alias name, t2.ydsl, t2.memorize, t2.understanding, t2.apply from scon_homework_how_details t1 left join zyb_know_how_details t2 on t1.how_detail_id=t2.id ")
        	.append(" left join scon_homework t4 on t1.homework_id=t4.id ")
    		.append("	LEFT JOIN zyb_know_how_details_sort t5 on t2.id = t5.how_details_id \n")
    		.append("	and t5.stat = 2 and t5.type = t1.knowledge_version where work_detail_type = 1 and t4.vip_id=?")
    		.append(" and t4.subject=? and t1.create_time between ? and ? AND t5.id is not null");
        List<Object> param = new ArrayList<>();
        param.add(vipId);
        param.add(subject);
        param.add(beginTime);
        param.add(endTime);
        return dao.findMap(sb.toString(), param.toArray());
    }

    @Override
    public List<Map<String, Object>> getOne2OneWrongDetail(String vipId, String subject, String beginTime, String endTime) {
        StringBuilder sb = new StringBuilder();
        sb.append("select DISTINCT t6.alias name, t4.ydsl, t4.memorize, t4.understanding, t4.apply from kmt_img_face_detail t1 LEFT JOIN  kmt_img_face t2 on t1.face_id=t2.id ")
            .append(" left join kmt_course_know t3 on t2.course_know_id=t3.id left join zyb_know_how_details t4 on t1.how_details_id=t4.id \n")
            .append(" 	LEFT JOIN zyb_know_how_details_sort t6 ON t4.id = t6.how_details_id\n")
    		.append(" 	and t6.stat = 2 and t6.type = t3.teaching_type")
            .append(" where t3.course_name=? and t2.vip_id= ? and t2.create_time between ? and ? AND t6.id is not null");
        List<Object> param = new ArrayList<>();
        param.add(subject);
        param.add(vipId);
        param.add(beginTime);
        param.add(endTime);
        return dao.findMap(sb.toString(), param.toArray());
    }

    @Override
    public List<Map<String, Object>> getHomeworkKnowHowModuleData(String vipId, String subject, String beginTime, String endTime, int type) {
        StringBuilder sb = new StringBuilder ();
        sb.append("select t3.module");
        if (type == 1){
            sb.append(", sum(detail_count) count");
        } else {
            sb.append(", sum(total_count) count");
        }
        sb.append(" from scon_homework_how_details t1 left join scon_homework t4 on t1.homework_id=t4.id ")
                .append(" left join zyb_know_how_details t3 on t1.how_detail_id=t3.id")
                .append("  where t4.subject = ? and t1.create_time BETWEEN ? and  ?");
        List<Object> param = new ArrayList<>();
        param.add(subject);
        param.add(beginTime);
        param.add(endTime);
        if (!StringUtil.isEmpty(vipId)){
            sb.append(" and t4.vip_id= ? ");
            param.add(vipId);
        }
        sb.append(" GROUP BY t3.module");
        return  dao.findMap(sb.toString(), param.toArray());
    }

    @Override
    public List<Map<String, Object>> getChineseHomeworkKnowHowModuleData(String vipId, String subject, String beginTime, String endTime, int type) {
        StringBuilder sb = new StringBuilder ();
        sb.append("select t3.two_module module");
        if (type == 1){
            sb.append(", sum(detail_count) count");
        } else {
            sb.append(", sum(total_count) count");
        }
        sb.append(" from scon_homework_how_details t1 left join scon_homework t4 on t1.homework_id=t4.id ")
                .append(" left join zyb_know_how_details t3 on t1.how_detail_id=t3.id")
                .append("  where t4.subject = ? and t1.create_time BETWEEN ? and  ?");
        List<Object> param = new ArrayList<>();
        param.add(subject);
        param.add(beginTime);
        param.add(endTime);
        if (!StringUtil.isEmpty(vipId)){
            sb.append(" and t4.vip_id= ? ");
            param.add(vipId);
        }
        return  dao.findMap(sb.toString(), param.toArray());
    }

    @Override
    public List<Map<String, Object>> getHomeworkWrongKnowHowClassData(String vipId, String subject, String beginTime, String endTime) {
        StringBuilder sb = new StringBuilder();
        sb.append("select sum(t3.memorize * detail_count) memorize, sum(t3.understanding * detail_count) understanding,")
                .append(" sum(t3.apply * detail_count) apply from scon_homework_how_details t4 ")
                .append(" left join scon_homework t2 ")
                .append(" on t4.homework_id = t2.id left join zyb_know_how_details t3 on t4.how_detail_id=t3.id where t2.subject=?")
                .append(" and t2.create_time between ? and ? and t2.vip_id=?");
        List<Object> param = new ArrayList<>();
        param.add(subject);
        param.add(beginTime);
        param.add(endTime);
        param.add(vipId);
        return dao.findMap(sb.toString(), param.toArray());
    }

    @Override
    public List<Map<String, Object>> getHomeworkAllKnowHowClassData(String vipId, String subject, String beginTime, String endTime) {
        StringBuilder sb = new StringBuilder();
        sb.append("select sum(t3.memorize * total_count) memorize, sum(t3.understanding * total_count) understanding,")
                .append(" sum(t3.apply * total_count) apply from scon_homework_how_details t4 ")
                .append(" left join scon_homework t2 ")
                .append(" on t4.homework_id = t2.id left join zyb_know_how_details t3 on t4.how_detail_id=t3.id where t2.subject=?")
                .append(" and t2.create_time between ? and ? and t2.vip_id=?");
        List<Object> param = new ArrayList<>();
        param.add(subject);
        param.add(beginTime);
        param.add(endTime);
        param.add(vipId);
        return dao.findMap(sb.toString(), param.toArray());
    }



    @Override
    public List<Map<String, Object>> getHomeworkWrongDetailDemo(String vipId, String subject, String dimension, String grade) {
        StringBuilder sb = new StringBuilder();
        sb.append("select distinct t5.alias name from scon_homework_how_details t1 left join zyb_know_how_details t2 on t1.how_detail_id=t2.id ")
                .append(" left join scon_homework t4 on t1.homework_id=t4.id ")
                .append("	LEFT JOIN zyb_know_how_details_sort t5 on t2.id = t5.how_details_id \n")
                .append("	and t5.stat = 2 and t5.type = t1.knowledge_version where work_detail_type = 1 and t4.vip_id=?")
                .append(" and t4.subject=?  and t4.grade=? ");

        List<Object> param = new ArrayList<>();
        param.add(vipId);
        param.add(subject);
        param.add(grade);
        if(subject.equals("语文") || subject.equals("数学")) {
            sb.append("AND t2.two_module = ?");
        }else {
            sb.append("AND t2.module = ?");
        }
        sb.append(" AND t5.id is not null");
        param.add(dimension);
        return dao.findMap(sb.toString(), param.toArray());
    }

    @Override
    public List<Map<String, Object>> getOne2OneWrongDetailDemo(String vipId, String subject, String dimension) {
        StringBuilder sb = new StringBuilder();
        sb.append("select DISTINCT t6.alias name, t4.ydsl, t4.memorize, t4.understanding, t4.apply from kmt_img_face_detail t1 LEFT JOIN  kmt_img_face t2 on t1.face_id=t2.id ")
            .append(" left join kmt_course_know t3 on t2.course_know_id=t3.id left join zyb_know_how_details t4 on t1.how_details_id=t4.id \n")
            .append(" 	LEFT JOIN zyb_know_how_details_sort t6 ON t4.id = t6.how_details_id\n")
    		.append(" 	and t6.stat = 2 and t6.type = t3.teaching_type")
            .append(" where t3.course_name=? and t2.vip_id= ?  ");
        List<Object> param = new ArrayList<>();
        param.add(subject);
        param.add(vipId);
        if(subject.equals("语文") || subject.equals("数学")) {
        	sb.append("AND t4.two_module = ?");
        }else {
        	sb.append("AND t4.module = ?");
        }
        sb.append(" AND t6.id is not null");
        param.add(dimension);
        return dao.findMap(sb.toString(), param.toArray());
    }
}
