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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ssj.dao.sys.teacher.dao.TeacherV1QueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.service.conch.teacher.v1.dto.LessonGroupDTO;
import com.ssj.service.conch.teacher.v1.dto.LessonGroupFeedbackStudentDTO;
import com.ssj.service.conch.teacher.v1.dto.LessonGroupSignStudentDTO;

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

@Repository
public class TeacherV1QueryDaoImpl implements TeacherV1QueryDao {

    @Autowired
    private PagingHibernateJdbcDao pagingHibernateJdbcDao;

    @Override
    public List<LessonGroupDTO> findLessonGroupList(String teacherId) {
        String sql =
                "select ssj_lesson_group.id     lesson_group_id, " +
                "       ssj_lesson_group.group_name, " +
                "       tb_lib_manager.name     teacher_name, " +
                "       tb_lib_manager.t_photo  teacher_img, " +
                "       ssj_lesson_group.current_count, " +
                "       ssj_lesson_group.course_count, " +
                "       ssj_lesson_group.plan_status, " +
                "       ssj_lesson_group.course_name, " +
                "       ssj_lesson_group.grade, " +
                "       ssj_lesson_group.handout_time, " +
                "       ssj_lesson_group.start_time, " +
                "       ssj_lesson_group.end_time, " +
                "       ssj_lesson_group.confirm_time course_confirm_time, " +
                "       ssj_lesson_group.call_time course_call_time, " +
                "       ssj_lesson_group.feedback_time course_feedback_time, " +
                "       tb_lib_join.league_name address, " +
                "       kmt_course.course_time course_date " +
                "from ssj_lesson_group, " +
                "     tb_lib_join, " +
                "     tb_lib_manager, " +
                "     tb_lesson_service, " +
                "     kmt_course " +
                "where ssj_lesson_group.lib_id = tb_lib_join.id " +
                "  and ssj_lesson_group.teacher_id = tb_lib_manager.user_id " +
                "  and tb_lesson_service.lesson_group_id = ssj_lesson_group.id " +
                "  and kmt_course.lesson_service_id = tb_lesson_service.id " +
                "  and tb_lesson_service.status < 2 " +
                "  and kmt_course.num = ssj_lesson_group.current_count " +
                "  and tb_lib_manager.state = 1 " +
                "  and tb_lib_manager.type = 9 " +
                "  and ssj_lesson_group.teacher_id = '" + teacherId + "' " +
                "group by lesson_group_id " +
                "order by kmt_course.course_time ";

        return pagingHibernateJdbcDao.findList(sql, LessonGroupDTO.class);
    }

    @Override
    public List<LessonGroupFeedbackStudentDTO> findLessonGroupFeedbackStudentList(String lessonGroupId, String childName) {
        String sql =
                "select tb_lesson_service.id lesson_service_id, tb_lib_vip.child_name, tb_lib_vip.img_url child_img, tb_lesson_service.grade, tb_lib_vip.school " +
                "from tb_lesson_service, tb_lib_vip " +
                "where tb_lesson_service.vip_id = tb_lib_vip.id " +
                "  and tb_lesson_service.is_group = 1 " +
                "  and tb_lesson_service.status in (1, 2) " +
                "  and tb_lesson_service.lesson_group_id = '" + lessonGroupId + "' ";
        if (childName != null) {
            sql += "  and tb_lib_vip.child_name like '%" + childName + "%' ";
        }

        return pagingHibernateJdbcDao.findList(sql, LessonGroupFeedbackStudentDTO.class);
    }

    @Override
    public List<LessonGroupSignStudentDTO> findLessonGroupStudentSignList(String lessonGroupId, Integer signInOff, String childName) {
        String sql =
                "select kmt_course.id      course_id, " +
                "       kmt_course.course_time, " +
                "       kmt_course.start_time, " +
                "       kmt_course.end_time, " +
                "       tb_lib_vip.child_name, " +
                "       tb_lib_vip.img_url child_img, " +
                "       tb_lesson_service.grade, " +
                "       tb_lib_vip.school, " +
                "       tb_lib_vip.flow_id, " +
                "       kmt_course.sign_in_off " +
                "from tb_lesson_service, " +
                "     kmt_course, " +
                "     tb_lib_vip, " +
                "     ssj_lesson_group " +
                "where tb_lesson_service.id = kmt_course.lesson_service_id " +
                "  and tb_lesson_service.lesson_group_id = ssj_lesson_group.id " +
                "  and ssj_lesson_group.current_count = kmt_course.num " +
                "  and tb_lesson_service.current_count = ssj_lesson_group.current_count " +
                "  and tb_lesson_service.vip_id = tb_lib_vip.id " +
                "  and tb_lesson_service.is_group = 1 " +
                "  and kmt_course.is_group = 1 " +
                "  and tb_lesson_service.lesson_group_id = '" + lessonGroupId + "' ";

        if (signInOff == 0) {//查询需要签到的学生列表时，需要订单的状态是1（已支付待服务）
            sql += "  and tb_lesson_service.status = 1 ";
        }

        if (childName != null) {
            sql += "  and tb_lib_vip.child_name like '%" + childName + "%' ";
        }

        return pagingHibernateJdbcDao.findList(sql, LessonGroupSignStudentDTO.class);
    }

    @Override
    public List<Map<String, Object>> findLessonGroupFeedbackStudentsList(String lessonGroupId, int num) {
        StringBuilder sb = new StringBuilder();
        sb.append("select c.id, c.course_know_id, v.child_name, v.img_url, v.grade, v.school, v.tags_character, v.tags_habit from tb_lesson_service s ")
                .append(" left join  tb_lib_vip v on s.vip_id=v.id left join kmt_course c on s.id= c.lesson_service_id ")
                .append("  and c.num = ? where s.lesson_group_id = ? and c.sign_in_off = 1 order by v.id");
        List<Object> param = new ArrayList<>();
        param.add(num);
        param.add(lessonGroupId);
        return pagingHibernateJdbcDao.findMap(sb.toString(), param.toArray());
    }
}
