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

import com.ssj.dao.kmt.kmt.dao.TeacherArrangeQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.core.util.MapGetterTool;
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 TeacherArrangeQueryDaoImpl implements TeacherArrangeQueryDao {
    @Autowired
    private PagingHibernateJdbcDao dao;

    @Override
    public List<Map<String,Object>> findTeacheDutyList(String teacherUserId,String theMonth) {
        StringBuffer sql  = new StringBuffer();
        sql.append("SELECT t.id,t.arrange_template_id,t.start_interval,t.end_interval,t.arrange_date,t.max_num,t.have_num,t.is_open,t12.attend_type from kmt_arrange_detail t \n");
        sql.append("INNER  join tb_lib_manager t11 on t.`subject` = t11.course_name and t.type=t11.type and t11.user_id = ? \n");
        sql.append("left join kmt_arrange_allot t12 on t11.user_id = t12.teacher_id and t.arrange_date= t12.arrange_date and t.id= t12.arrange_detail_id and t12.`status`=1 \n");
        sql.append("where t.`status`= 1 and t.arrange_date like concat(?,'%') and t11.type in (8,13)  and t11.state=1   \n");
        sql.append("order by t.arrange_date,t.start_interval");
        List<Object> queryParams = new ArrayList<Object>();
        queryParams.add(teacherUserId);
        queryParams.add(theMonth);
        return dao.findMap(sql.toString(),queryParams.toArray());
    }

    @Override
    public List<Map<String, Object>> findTemplateList(String preMonth) {
        StringBuffer sql  = new StringBuffer();
        sql.append("SELECT arrange_date,is_open  from kmt_arrange_detail where  SUBJECT='语文' and type=8  and arrange_date like concat(?,'%') \n");
        sql.append("GROUP BY arrange_date \n");
        sql.append("ORDER BY arrange_date");
        List<Object> queryParams = new ArrayList<Object>();
        queryParams.add(preMonth);
        return dao.findMap(sql.toString(),queryParams.toArray());
    }

    @Override

    public  List<Map<String,Object>> findAShouldtendanTime(Map map){
        StringBuilder sql=new StringBuilder();
        MapGetterTool mapGetterTool=new MapGetterTool(map);
        List<Object> queryParams = new ArrayList<Object>();
        String teacherId=mapGetterTool.getString("userId");
        int type=mapGetterTool.getInteger("type");
        String date=mapGetterTool.getString("date");
        sql.append("select t2.start_interval,t2.end_interval,t1.id from kmt_arrange_allot t1 left join kmt_arrange_detail t2");
        sql.append(" on t1.arrange_detail_id=t2.id where t1.teacher_id=? and t2.type=? and t1.arrange_date regexp? and t1.status=1");
        queryParams.add(teacherId);
        queryParams.add(type);
        queryParams.add(date);//kmt_arrange_detail
        return dao.findMap(sql.toString(),queryParams.toArray());


    }

    @Override
    public List<Map<String, Object>> findIsOver(List<String> resultList, String teacherUserId) {
        StringBuffer sql  = new StringBuffer();
        sql.append("SELECT DISTINCT a.time_interval,a.arrange_date from (\n");
        sql.append("SELECT CONCAT(t.start_interval,'~',t.end_interval) as time_interval,(t.max_num-t.have_num+case when t11.id is not null then 1 else 0 end) as need_num,t.arrange_date from  kmt_arrange_detail t\n");
        sql.append("left join  kmt_arrange_allot t11 on t.id = t11.arrange_detail_id  and t11.teacher_id = ? and  t11.`status`=1 \n");
        sql.append("where  t.id in ('").append(String.join("','",resultList)).append("') order by t.arrange_date ");
        sql.append(") a where a.need_num <1");
        List<Object> queryParams = new ArrayList<Object>();
        queryParams.add(teacherUserId);
        return dao.findMap(sql.toString(),queryParams.toArray());
    }
}
