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

import com.ssj.bean.sys.homework.domain.HomeworkPicture;
import com.ssj.dao.sys.homework.dao.HomeworkPictureQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.ai.model.AiVipIdsModel;
import com.ssj.service.kmt.correctsys.dto.HomeworkPictureDto;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;



/** 
 * @author  ZhangCaibao 
 * @version 2017年4月24日 上午11:59:29 
 * 类说明 
 */
@Repository
public class HomeworkPictureQueryDaoImpl  implements  HomeworkPictureQueryDao {
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public  List<Map<String, Object>> queryTodayhomeworkRecord(String derver,String today,String tomorrow) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select * from (select `subject`,is_feedback from scon_homework_picture where derver = ? and is_delete = 0 "
				+ " and create_time >= date_format(?,'%Y-%m-%d 15:00:00') and create_time <= date_format(?,'%Y-%m-%d 15:00:00')"
				+ " GROUP BY `subject`,is_feedback ORDER BY is_feedback asc) s GROUP BY s.`subject`" );
		queryParams.add(derver);
		queryParams.add(today);
		queryParams.add(tomorrow);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public Page<Map<String, Object>> findDateByVipId(String vipId, String dateTime, Pageable pageable) {
		StringBuilder sb = new StringBuilder ();
		sb.append("select distinct date(create_time) date from scon_homework where vip_id='")
				.append(vipId).append("' and create_time < '").append(dateTime)
				.append("' order by create_time desc");

		return dao.findPage(sb.toString(), pageable);
	}


	@Override
	public List<AiVipIdsModel> findHomeworkPictureByVipIds(String[] vipIds,String date) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT t.vip_id,t.id as img_id,t.`subject` as course_name,t.file_key as file_url,t.create_time  ");
		selSql.append(" FROM `scon_homework_picture` t ");
		selSql.append(" where t.vip_id IN ( " );

		for (int j = 0; j < vipIds.length; j++) {
			selSql.append(" ? ");
    		if(vipIds.length-1>j){
    			selSql.append(" , ");
    		}
        	queryParams.add(vipIds[j]);
		}
		selSql.append(" ) ");
		selSql.append(" AND TO_DAYS(t.create_time)=TO_DAYS(?)  ");
		selSql.append(" ORDER BY t.vip_id ASC,t.create_time ASC ");
		queryParams.add(date);
		return dao.findList(selSql.toString(), queryParams.toArray(),AiVipIdsModel.class);
	}

	@Override
	public List<Map<String, Object>> findTeacherCorrectList (String teacherId, String date){
		String sql = "select distinct subject, vip_id from scon_homework_picture where date(create_time)=? and teacher_id=?";
		List<Object> params = new ArrayList<>();
		params.add(date);
		params.add(teacherId);
		return dao.findMap(sql, params.toArray());
	}

	@Override
	public List<HomeworkPicture> findTeacherNotFeedbackHomework(String teacherId, String date) {
		String sql = " select distinct homework_id, vip_id, subject from scon_homework_picture where teacher_id=? and date(create_time)=? " +
				"and is_feedback=0 and  is_mark in (0, 2)";
		List<Object> params = new ArrayList<>();
		params.add(date);
		params.add(teacherId);
		return dao.findList(sql, params.toArray(), HomeworkPicture.class);
	}

	@Override
	public Page<Map<String, Object>> findHomeworkByPage(Map<String, Object> searchParam, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		String childName = "";
		if (StringUtil.isNotBlank(searchParam.get("LIKE_childName").toString())) {
			childName = searchParam.get("LIKE_childName").toString();
		}
		sb.append("select t.*,t1.child_name,t2.wx_name parent_name,t3.name teacher_name from scon_homework_picture t ")
				.append(" left join tb_lib_vip t1 on t.vip_id = t1.id ")
				.append(" left join tb_user t2 on t.parent_id = t2.id ")
				.append(" left join (select * from tb_lib_manager t where t.type=8 and t.state=1) t3 on t.teacher_id = t3.user_id ");
		if (StringUtil.isNotBlank (childName )) {
			sb.append(" where t1.child_name like '%").append(childName).append("%' ");
		}
		sb.append(" order by t.create_time desc ");
		return dao.findPage(sb.toString(), pageable);
	}


	@Override
	public List<Map<String, Object>> findByTeacherIdGroupBySubjectAndVipId(String teacherId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select s.vip_id,s.subject from scon_homework_picture s ");
		selSql.append("where s.teacher_id = ? and s.is_feedback = 0 ");
		selSql.append("GROUP BY s.subject,s.vip_id");
		queryParams.add(teacherId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	
	@Override
	public List<Map<String, Object>> findByKnowTeacherIdGroupBySubjectAndVipId(String teacherId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select s.vip_id,s.subject from scon_homework_picture s ");
		selSql.append("where s.know_teacher_id = ? and s.is_feedback = 1  ");
		selSql.append("GROUP BY s.subject,s.vip_id");
		queryParams.add(teacherId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	

	@Override
	public List<Map<String, Object>> schoolHomeworkPictureTotal(String schoolName, String startTime, String endTime) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select s.subject,s.corrected_picture_url,h.grade from scon_homework_picture s  ");
		selSql.append("LEFT JOIN scon_homework h ON s.homework_id = h.id ");
		selSql.append("LEFT JOIN tb_lib_vip t on s.vip_id = t.id ");
		selSql.append("where t.school = ? and s.create_time >= ? and s.create_time <= ? and s.corrected_picture_url is not null and s.corrected_picture_url != ''");
		queryParams.add(schoolName);
		queryParams.add(startTime);
		queryParams.add(endTime);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	
	
	@Override
	public List<Map<String, Object>> findSubjectCharts(String startTime, String endTime) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT t.subject ,DATE_FORMAT(create_time,'%H') hours,count(t.id) count  ");
		selSql.append(" from scon_homework_picture t where t.`subject` in('语文','英语','数学') AND TO_DAYS(t.create_time)>=TO_DAYS(?)  ");
		selSql.append(" AND TO_DAYS(t.create_time)<=TO_DAYS(?)  ");
		selSql.append(" GROUP BY t.subject ,DATE_FORMAT(create_time, '%H')  ORDER BY   hours ASC ");
		queryParams.add(startTime);
		queryParams.add(endTime);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<HomeworkPictureDto> findCheckPictureByHomeworkId (int type, String homeworkId){
		StringBuffer sb = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		sb.append("SELECT distinct hp.id, hp.subject, hp.file_key, hp.is_feedback, hp.is_delete, hp.is_mark, hp.vip_id,")
				.append(" hp.is_composition, hp.composition_comment, ")
				.append(" hp.teacher_id, hp.allot_time,  hp.create_time, hp.corrected_picture_url, hpc.check_date, hpc.checker, hpc.is_check_error," )
				.append(" hpc.error_reason, hp.is_checker_correct is_correct")
				.append(" FROM scon_homework_picture hp left join scon_homework_picture_correct hpc on hp.id=hpc.homework_picture_id ")
				.append(" where hp.homework_id=? and hpc.type = ? and (hp.is_feedback=1 or hp.is_mark in (1,3,4)) order by hp.create_time");
		param.add(homeworkId);
		param.add(type);
		return dao.findList(sb.toString(), param.toArray(), HomeworkPictureDto.class);
	}

	@Override
	public List<HomeworkPictureDto> findKnowCheckPictureByHomeworkId(String homeworkId){
		StringBuffer sb = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		sb.append("SELECT distinct hp.id, hp.subject, hp.file_key, hp.is_know_feedback, hp.is_delete, hp.is_mark, hp.vip_id,")
				.append(" hp.know_teacher_id, hp.create_time, hp.corrected_picture_url, hpc.check_date, hpc.checker, hpc.is_check_error," )
				.append(" hpc.error_reason")
				.append(" FROM scon_homework_picture hp left join scon_homework_picture_correct hpc on hp.id=hpc.homework_picture_id ")
				.append("where hp.homework_id=?  and hpc.type = 13 and is_know_feedback=1 and is_needing_know=1 ");
		param.add(homeworkId);
		return dao.findList(sb.toString(), param.toArray(), HomeworkPictureDto.class);
	}

}
