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

import com.ssj.dao.kmt.kmt.dao.KmtConchQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
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 KmtConchQueryDaoImpl  implements KmtConchQueryDao{
//	private   Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> findHomeworkPictureByList(String teacherId, String beginTime) {
		StringBuilder selSQL = new StringBuilder();
        selSQL.append("select t.id as pid, t.homework_id, t.file_key, t.is_delete, t.subject,t.is_feedback, t.is_mark, t.create_time, ")
				.append("t.vip_id, t.corrected_picture_url editpath, v.child_name, v.grade, v.banben, v.school, predeal.path,")
				.append(" predeal.book_id, predeal.unit_id,predeal.module_id,predeal.book_page_no, hw.correct_time, t.zyb_work_stat,")
                .append(" if(t.teacher_id=?, 1, 0) my_work_flag, predeal.task_status, predeal.deal_flag, predeal.question_ids, t.is_correct, t.is_needing_know	 from ")
                .append(" (select distinct homework_id from scon_homework_picture where teacher_id=? and create_time > ? and is_delete=0) homework")
                .append(" left join scon_homework_picture t on homework.homework_id=t.homework_id ")
				.append(" LEFT JOIN tb_lib_vip v on t.vip_id=v.id LEFT JOIN scon_picture_ai_predeal predeal on t.id=predeal.homework_picture_id ")
                .append(" left join scon_homework hw on t.homework_id=hw.id where t.is_delete=0")
                .append(" order by t.create_time");
        List<Object> param = new ArrayList<>();
        param.add(teacherId);
        param.add(teacherId);
        param.add(beginTime);
        return dao.findMap(selSQL.toString(), param.toArray());
	}

	@Override
	public List<Map<String, Object>> findKnowHomeworkPictureByList(String teacherId, String beginTime) {
		StringBuilder selSQL = new StringBuilder();
		selSQL.append("select t.id as pid, t.homework_id, t.subject, t.is_know_feedback, t.is_mark, t.create_time," )
				.append(" t.vip_id, t.corrected_picture_url editpath, v.child_name, v.grade, v.school, hw.correct_time," )
				.append(" if(t.know_teacher_id=?, 1, 0) my_work_flag, t.is_composition, hpc.second_correct_type from" )
				.append(" (select distinct homework_id from scon_homework_picture where know_teacher_id=? and create_time > ? and is_delete=0 " )
				.append(" and is_mark in (0, 2)) homework left join scon_homework_picture t on homework.homework_id=t.homework_id" )
				.append(" LEFT JOIN tb_lib_vip v on t.vip_id=v.id left join scon_homework hw on t.homework_id=hw.id " )
				.append(" left join scon_homework_picture_correct hpc on t.id=hpc.homework_picture_id and hpc.type=8")
				.append(" where t.is_delete=0 and t.is_feedback = 1 and t.is_needing_know=1 order by t.create_time");
		List<Object> param = new ArrayList<>();
		param.add(teacherId);
		param.add(teacherId);
		param.add(beginTime);
		return dao.findMap(selSQL.toString(), param.toArray());
	}

	@Override
	public List<Map<String, Object>> findStudentWorkImgByList(String studentWorkId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT d.* FROM zyb_student_work_img t LEFT JOIN zyb_know_how_details d ON t.how_details_id=d.id where t.object_id=?  ORDER BY t.num ASC ");
        queryParams.add(studentWorkId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}


	@Override
	public List<Map<String, Object>> findCheckHomeworkPictureList(String homeworkPictureId) {
		StringBuilder selSQL = new StringBuilder();
		selSQL.append("select t.id as pid, t.homework_id, t.file_key, t.is_delete, t.subject,t.is_feedback, t.is_mark, t.create_time, ")
				.append("t.vip_id, t.corrected_picture_url editpath, v.child_name, v.grade, v.banben, v.school, predeal.path,")
				.append(" predeal.book_id, predeal.unit_id,predeal.module_id,predeal.book_page_no, hw.correct_time, t.zyb_work_stat,")
				.append(" 1 my_work_flag, predeal.task_status, predeal.deal_flag, predeal.question_ids, 0 is_correct, is_needing_know from ")
				.append(" scon_homework_picture t")
				.append(" LEFT JOIN tb_lib_vip v on t.vip_id=v.id LEFT JOIN scon_picture_ai_predeal predeal on t.id=predeal.homework_picture_id ")
				.append(" left join scon_homework hw on t.homework_id=hw.id where t.id=? ");
		List<Object> param = new ArrayList<>();
		param.add(homeworkPictureId);
		return dao.findMap(selSQL.toString(), param.toArray());
	}

}
