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

import com.ssj.dao.sys.teacher.dao.TeacherDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
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 org.springframework.transaction.annotation.Transactional;

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


/**
 * 类名称：Comment<br>
 * 类描述：评论数据访问接口<br>
 * 
 * @version v1.0
 */
@Repository
public class TeacherDaoImpl implements TeacherDao {

    @Autowired
    private PagingHibernateJdbcDao dao;

    @Override
    public Page<Map<String, Object>> findForPageList(Map<String, Object> searchParams, Pageable pageable) {
        // TODO Auto-generated method stub

        StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" select su.id as  studentId, su.parent_id as  parentId,su.nickname as studentNickname,su.image_key as studentImageKey, count(aa.bizId) as uploadNum "
                + "from scon_user_info su LEFT JOIN (");
        selSQL.append("select id as bizId , a.parent_id as parentId, a.biz_type as bizType ,  a.file_key as fileKey, a.classify_time as classifyTime , a.is_feedback as isFeedBack from (");
        selSQL.append(" select p.parent_id , p.id  , 'PROBLEM' as biz_type ,  p.file_key , p.is_feedback ,  p.create_time as classify_time from  scon_problem_picture p where p.is_delete=0 and  "
                + "p.is_feedback = 0 ");
        selSQL.append("and p.parent_id in (");
        selSQL.append(searchParams.get("pids"));
        selSQL.append(")");
        selSQL.append("union all");
        selSQL.append(" select h.parent_id , h.id , 'HOMEWORK' as biz_type ,  h.file_key , h.is_feedback , h.create_time  as classify_time from scon_homework_picture h where h.is_delete=0 and  "
                + "h.is_feedback = 0 ");
        selSQL.append("and h.parent_id in (");
        selSQL.append(searchParams.get("pids"));
        selSQL.append(")");
        selSQL.append(") a ) aa");
        selSQL.append(" on  su.parent_id = aa.parentId  where su.parent_id in (");
        selSQL.append(searchParams.get("pids"));
        selSQL.append(")");
        selSQL.append("group by su.parent_id ORDER BY uploadNum desc  ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
    }

    @Override
    public Page<Map<String, Object>> findForPageStudentList(Map<String, Object> searchParams, Pageable pageable) {
        StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  select DISTINCT a.*,'2' as source from (  ");
        selSQL.append(" SELECT v.* from (select DISTINCT lib_id from tb_lib_manager  where type=2 and state=1 and user_id =?)l ");
        selSQL.append(" INNER  JOIN tb_lib_vip_service m ON l.lib_id = m.lib_id and m.species='S003' and m.stat =1   ");
        selSQL.append(" LEFT JOIN tb_lib_vip v ON m.vip_id = v.id ");
        selSQL.append(" where  v.user_id is not NULL and v.stat=1 ");

        selSQL.append(" union ALL ");
        selSQL.append(" SELECT v.* from  (select DISTINCT vip_id from kmt_course where teacher_id = ? and plan_status < 5) t  ");
        selSQL.append(" left join tb_lib_vip v on v.id = t.vip_id    ");
        selSQL.append(" where  v.user_id is not NULL and v.stat=1 ");
        selSQL.append(" ) a");
        queryParams.add(searchParams.get("teacherId"));
        queryParams.add(searchParams.get("teacherId"));
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
    }


    @Override
    public List<Map<String, Object>> findForPageSignList(String teacherId, int type, String vipId) {
        StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  SELECT t.child_name,t.id,t12.vip_id,t.img_url,p.id as push_id,s.ryke_time,");
        if (type == 0) {
            selSQL.append("  s.ryke_time as show_time, ");
        } else {
            selSQL.append("   ifnull(p.update_time,p.submit_time) as show_time, ");
        }

        selSQL.append(" p.suggest,p.img as djimg,p.type from   \n");
        selSQL.append("  (SELECT lib_id,user_id from tb_lib_manager where type=2 and state=1 and user_id=?) t11  \n");
        selSQL.append("  inner join (SELECT t.lib_id,t.ryke_time,t.vip_id from tb_lib_subscribe t where t.lib_id not in ('844abb1c-0599-435d-b125-3a6e65411bf7','cdf451d8-4764-4522-a591-64531cef0394') \n");
        selSQL.append("  union ALL \n");
        selSQL.append("  select '844abb1c-0599-435d-b125-3a6e65411bf7',NOW(),'' from DUAL  ");
        selSQL.append("  union ALL \n");
        selSQL.append("  select 'cdf451d8-4764-4522-a591-64531cef0394',NOW(),'' from DUAL ) ");
        selSQL.append("  s on t11.lib_id = s.lib_id and TO_DAYS(s.ryke_time)=TO_DAYS(NOW())\n");
        selSQL.append("  INNER JOIN tb_lib_vip_service t12 on  t11.lib_id = t12.lib_id and t12.species='S003' \n");
        selSQL.append("  INNER JOIN tb_lib_vip t on t12.vip_id = t.id \n");
        selSQL.append("  left join zyb_student_push p on t.id = p.vip_id and s.lib_id = p.lib_id and TO_DAYS(p.push_time)=TO_DAYS(NOW()) \n");
        selSQL.append("  where  1=1 \n");

        queryParams.add(teacherId);
        if (type == 0) {
            //未完成, 包含初始状态0， 编辑状态1, 线上已批改完成等待在馆老师提交4
            selSQL.append("  AND (( p.id IS NULL) or ((p.teacher_id = ? or p.teacher_id is null) and (p.type = 0 or p.type = 1 or p.type=4))) \n");
            queryParams.add(teacherId);
        } else {
            //已完成, 包含已发送2以及提交等待在线老师批改3
            selSQL.append("  AND p.id IS NOT NULL AND (p.type =2 or p.type = 3)  and p.teacher_id = ?  \n");
            queryParams.add(teacherId);
        }
        if (vipId != null && !"".equals(vipId)) {
            selSQL.append("  AND t12.vip_id = ?  ");
            queryParams.add(vipId);
        }
        selSQL.append("  and (t12.lib_id in ('844abb1c-0599-435d-b125-3a6e65411bf7','cdf451d8-4764-4522-a591-64531cef0394') or s.vip_id=t12.vip_id)  \n");
        selSQL.append("   GROUP BY t.id  ");
        return dao.findMap(selSQL.toString(), queryParams.toArray());
    }

    @Override
    public List<Map<String, Object>> findForKmtEditList(String teacherUserId, String vipId) {
        StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("SELECT t.vip_id,t12.child_name,t11.`subject`,t11.create_time,GROUP_CONCAT(t11.file_key) as file_key,20-TIMESTAMPDIFF(MINUTE,t11.create_time,NOW()) as overtime_info FROM scon_homework t    ");
        selSQL.append("INNER  join `scon_homework_picture` t11 on t.id = t11.homework_id  ");
        selSQL.append("left join tb_lib_vip t12 on t.vip_id = t12.id   ");
        selSQL.append("where t11.teacher_id = ? and TO_DAYS(t11.create_time)=TO_DAYS(NOW()) and t11.is_feedback =0  ");
        queryParams.add(teacherUserId);
        if (vipId != null && !"".equals(vipId)) {
            selSQL.append(" and t.vip_id = ? ");
            queryParams.add(vipId);
        }
        selSQL.append("GROUP BY t.id  ");

        return dao.findMap(selSQL.toString(), queryParams.toArray());
    }


    @Override
    public List<Map<String, Object>> findNoConfirmStudentList(String userId) {
        StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("select a.* from tb_lib_vip a left join  zyb_student_push b on a.id=b.vip_id ");
        selSQL.append("left join tb_lib_subscribe c on b.vip_id=c.vip_id and b.lib_id=c.lib_id ");
        selSQL.append("left join tb_lib_manager d on c.lib_id=d.lib_id ");
        selSQL.append("where b.teacher_id is null and  TO_DAYS(b.push_time) = TO_DAYS(NOW()) ");
        selSQL.append("and TO_DAYS(c.create_time) = TO_DAYS(NOW()) ");
        selSQL.append("and c.flow_stat=3 ");
        selSQL.append("and d.user_id=? and d.type=2 and d.state=1 group by a.id");
        queryParams.add(userId);
        return dao.findMap(selSQL.toString(), queryParams.toArray());

    }
}


