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

import com.ssj.dao.sys.teacher.app.dao.TeacherAppQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.conch.teacher.app.dto.ErrorWarnDTO;
import com.ssj.service.conch.teacher.app.dto.HomeworkPictureDTO;
import com.ssj.service.conch.teacher.app.dto.SystemMessageDTO;
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.Collections;
import java.util.Date;
import java.util.List;

@Repository
public class TeacherAppQueryDaoImpl implements TeacherAppQueryDao {

	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<SystemMessageDTO> findSystemMessageList(String teacherId) {
		List<Object> paramList = new ArrayList<>();

		String beginTimeStr = DateUtil.format_yyyyMMdd(DateUtil.AddDay(new Date(), -2)) + " 00:00:00";
		paramList.add(teacherId);
		paramList.add(beginTimeStr);

		String sql =
				"select title, content, create_time\n" +
				"from scon_push_message\n" +
				"where push_user_id = ?\n" +
				"  and create_time > ?\n" +
				"  and platform_type = 4\n" +
				"order by create_time desc";

		return dao.findList(sql, paramList.toArray(), SystemMessageDTO.class);
	}

	@Override
	public Page<ErrorWarnDTO> findErrorWarnNoticeList(String teacherId, Pageable pageable) {
		String sql=" select t1.homework_picture_id,t2.subject,t1.check_picture_url as picture_url,t3.img_url,t3.child_name,t1.create_time check_date,t1.error_reason,t1.checker\n"+
	            "  from scon_homework_picture_errorcheck t1 left join scon_homework_picture t2 on t1.homework_picture_id=t2.id  \n" +
			    " left join tb_lib_vip t3 on t2.vip_id=t3.id where t1.type=8 and t2.teacher_id=? order by t1.create_time desc \n";

		return dao.findPage(sql, new Object[]{teacherId}, pageable, ErrorWarnDTO.class);
	}

    @Override
    public List<HomeworkPictureDTO> findHomeworkPictureByHomeworkId(String homeworkId) {
		String sql =
				"select scon_homework_picture.id homework_picture_id,\n" +
				"       scon_homework_picture.file_key,\n" +
				"       scon_homework_picture.is_feedback,\n" +
				"       scon_homework_picture.corrected_picture_url,\n" +
				"       scon_homework_picture.teacher_id,\n" +
				"       scon_homework_picture.is_mark,\n" +
				"       scon_homework_picture.is_composition composition_corrected,\n" +
				"       scon_homework_picture.is_correct,\n" +
				"       scon_homework_picture.homework_comment,\n"+
				"       scon_homework_picture.is_needing_know second_type,\n" +
				"       scon_picture_ai_predeal.task_status,\n" +
				"       scon_picture_ai_predeal.deal_flag,\n" +
				"       scon_picture_ai_predeal.book_id,\n" +
				"       scon_picture_ai_predeal.unit_id,\n" +
				"       scon_picture_ai_predeal.module_id period_id,\n" +
				"       scon_picture_ai_predeal.book_page_no page_index_str,\n" +
				"       scon_picture_ai_predeal.question_ids question_id_str,\n" +
				"       scon_homework_picture.create_time\n" +
				"from scon_homework_picture\n" +
				"    left join scon_picture_ai_predeal on scon_homework_picture.id = scon_picture_ai_predeal.homework_picture_id\n" +
				"where scon_homework_picture.homework_id = ?\n" +
				"  and scon_homework_picture.is_delete = 0 order by scon_homework_picture.create_time\n";

        return dao.findList(sql, Collections.singletonList(homeworkId).toArray(), HomeworkPictureDTO.class);
    }
}
