package com.ssj.dao.conch.schoolmaster.v1.dao.impl;

import com.ssj.dao.conch.schoolmaster.v1.dao.SchoolV1QueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.service.conch.schoolmaster.v1.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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

@Repository
public class SchoolV1QueryDaoImpl implements SchoolV1QueryDao {

    @Autowired
    private PagingHibernateJdbcDao pagingHibernateJdbcDao;

    @Override
    public List<LessonGroupDTO> findLessonGroupList(String masterId) {
        String sql =
                "select ssj_lesson_group.id lesson_group_id, 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.confirm_time course_confirm_time, " +
                "       ssj_lesson_group.call_time course_call_time, " +
                "       ssj_lesson_group.course_time course_date, " +
                "       ssj_lesson_group.feedback_time course_feedback_time, " +
                "       ssj_lesson_group.grade, ssj_lesson_group.start_time, ssj_lesson_group.end_time, tb_lib_join.league_name address " +
                "from ssj_lesson_group, tb_lib_join, tb_lib_manager " +
                "where ssj_lesson_group.lib_id = tb_lib_join.id " +
                "  and ssj_lesson_group.teacher_id = tb_lib_manager.user_id " +
                "  and tb_lib_manager.state = 1 " +
                "  and tb_lib_manager.type = 9 " +
                "  and ssj_lesson_group.is_finish = 0 " +
                "  and ssj_lesson_group.master_id = '" + masterId + "' ";

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

    @Override
    public List<StudentGroupDTO> findStudentGroupList(String lessonGroupId, Integer lessonServiceStatus) {
        String sql =
                "select tb_lesson_service.vip_id, tb_lesson_service.id lesson_service_id, tb_lib_vip.child_name, tb_lib_vip.img_url child_img, tb_lib_vip.grade, " +
                "       tb_lib_vip.school, tb_lib_vip.parent_name, tb_lesson_service.status order_status " +
                "from tb_lesson_service, tb_lib_vip " +
                "where tb_lesson_service.vip_id = tb_lib_vip.id " +
                (lessonServiceStatus == null ? "" : "  and tb_lesson_service.status = " + lessonServiceStatus) +
                "  and tb_lesson_service.is_group = 1 " +
                "  and tb_lesson_service.lesson_group_id = '" + lessonGroupId + "' ";

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

    @Override
    public LessonGroupDetailDTO findLessonGroupDetail(String lessonGroupId) {
        String sql ="SELECT\n" +
        		"	p.course_time  ,\n" +
        		"	p.start_time as start_time ,\n" +
        		"	p.end_time , t.`name` teacher_name,\n" +
        		"	p.course_name,\n" +
        		"	p.grade,\n" +
        		"	p.course_count,\n" +
        		"	p.price course_price\n" +
        		"FROM\n" +
        		"	ssj_lesson_group p,\n" +
        		"	tb_lib_manager t\n" +
        		"WHERE\n" +
        		"	p.teacher_id = t.user_id\n" +
        		"AND t.type = 9\n" +
        		"AND p.id = ?";
        List<Object> queryParams = new ArrayList<Object>();
        queryParams.add(lessonGroupId);
        LessonGroupDetailDTO lessonGroupDetail = null;
        List<LessonGroupDetailDTO> lessonGroupDetailDTOList = pagingHibernateJdbcDao.findList(sql,queryParams.toArray(), LessonGroupDetailDTO.class);
        if (lessonGroupDetailDTOList != null && !lessonGroupDetailDTOList.isEmpty()) {
            lessonGroupDetail = lessonGroupDetailDTOList.get(0);
        }

        return lessonGroupDetail;
    }

    @Override
    public List<StudentGroupDTO> findStudentGroupListByVipIds(List<String> vipIdList) {
        String sql =
                "select tb_lib_vip.id vip_id, tb_lib_vip.child_name, tb_lib_vip.img_url child_img, tb_lib_vip.grade, " +
                        "       tb_lib_vip.school, tb_lib_vip.parent_name " +
                        "from tb_lib_vip " +
                        "where tb_lib_vip.stat = 1 ";

        StringBuilder vipIdSb = new StringBuilder();
        if (vipIdList != null && !vipIdList.isEmpty()) {
            vipIdSb.append("and tb_lib_vip.id in (");
            vipIdList.forEach(vipId -> vipIdSb.append("'").append(vipId).append("',"));
            vipIdSb.deleteCharAt(vipIdSb.length() - 1);
            vipIdSb.append(") ");
        }
        if (vipIdSb.length() > 0) {
            sql += vipIdSb.toString();
        }

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

    @Override
    public String findUUID() {
        String sql = "select uuid() from dual";
        List<String> list = pagingHibernateJdbcDao.findList(sql, String.class);
        if (list != null && list.size() > 0){
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> findTimeAndGroupCourseIds(String lessonGroupId) {
        String sql = "select distinct CONCAT(t1.course_time,' ',t1.start_time, ':00') time, t1.group_course_id " +
                " from kmt_course t1 left join tb_lesson_service t2 on t1.lesson_service_id =t2.id where t2.lesson_group_id = ?";
        List<Object> param = new ArrayList<>();
        param.add(lessonGroupId);
        return pagingHibernateJdbcDao.findMap(sql, param.toArray());
    }

    @Override
    public List<AppStudentChangeBindDTO> findStudentListChangeBind(String masterId, List<String> studentVipIdList) {
        String sql =
                "select tb_lib_vip.id vip_id, tb_lib_vip.child_name, tb_lib_vip.img_url child_img, tb_lib_vip.grade, " +
                "       tb_lib_vip.school, tb_lib_vip.parent_name " +
                "from tb_lib_vip " +
                "where tb_lib_vip.stat = 1 ";

        StringBuilder vipIdSb = new StringBuilder();
        if (studentVipIdList != null && !studentVipIdList.isEmpty()) {
            vipIdSb.append("and tb_lib_vip.id in (");
            studentVipIdList.forEach(vipId -> vipIdSb.append("'").append(vipId).append("',"));
            vipIdSb.deleteCharAt(vipIdSb.length() - 1);
            vipIdSb.append(") ");
        }
        if (vipIdSb.length() > 0) {
            sql += vipIdSb.toString();
        }

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

    @Override
    public List<AppMasterChangeBindDTO> findMasterListChangeBind(String masterId) {
        String sql = "" +
                "select user_id master_id, name master_name, t_photo master_img " +
                "from tb_lib_manager " +
                "where type = 6 " +
                "  and state = 1 " +
                "  and user_id <> '" + masterId + "'";

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