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

import com.ssj.dao.conch.teacher.LibraryTeacherQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.service.conch.teacher.library.dto.AllStudentDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
/**
 * @author Mount Liu
 * @date 2019/7/10
 */
@Repository
public class LibraryTeacherQueryDaoImpl implements LibraryTeacherQueryDao {
	
	
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> findTodayHomeworkByVipId(List<String> vipIds){
		StringBuilder sql = new StringBuilder();
		sql.append("select t1.id vip_id,t1.child_name,t1.img_url,t2.id push_id,t2.type,t3.course_name subject, t2.push_time,t3.type work_type,\n" +
				"       (select count(1) from scon_homework_picture where homework_record_id = t3.id and is_delete = 0) homework_num,\n" +
				"		((select count(1) from scon_homework_picture where homework_record_id = t3.id and is_delete = 0 and is_feedback = 1) + (select count(1) from scon_homework_picture where homework_record_id = t3.id and is_delete = 0 and is_feedback = 0 and is_mark in(1,3))) feedback_num\n" +
				"from tb_lib_vip t1\n" +
				" left join zyb_student_push t2 on t1.id = t2.vip_id and to_days(t2.push_time) = to_days(now())\n" +
				" left join scon_no_homework_record t3 on t1.id = t3.vip_id and to_days(t3.create_time) = to_days(now())\n" +
				"where 1=1\n");
		sql.append("and t1.id in ('").append(String.join("','",vipIds)).append("')\n");
		sql.append("order by t2.push_time,t1.id,t3.course_name desc\n");
		return dao.findMap(sql.toString(), null);
	}

	@Override
	public List<Map<String, Object>> findTodayBadHomeworkByVipId(List<String> vipIds) {
		StringBuilder sql = new StringBuilder();
		sql.append("select *\n" +
				"from (select t1.id vip_id,t1.child_name,t1.img_url,t2.is_mark,t2.file_key,count(t2.id) pic_mun,min(t2.create_time) remind_time,t2.subject\n" +
				"from tb_lib_vip t1\n" +
				" join scon_homework_picture t2 on t1.id=t2.vip_id and t2.is_mark in (1,3) and to_days(t2.create_time) = to_days(now())\n" +
				"where 1=1\n");
		sql.append("and t1.id in ('").append(String.join("','", vipIds)).append("')\n");
		sql.append("group by t1.id,t2.is_mark,t2.subject) t\n" +
				"order by t.remind_time\n");
		return dao.findMap(sql.toString(), null);
	}

	@Override
	public List<AllStudentDto> findAllStudentList(String studentName, String libId, boolean isOpen) {
		if (isOpen) {
			String sql =
					"select tb_lib_vip.id                             vip_id,\n" +
							"       tb_lib_vip.child_name                     student_name,\n" +
							"       tb_lib_vip.school,\n" +
							"       tb_lib_vip.grade,\n" +
							"       tb_lib_vip.img_url,\n" +
							"       if(tb_lib_subscribe.id is not null, 1, 0) checked_in\n" +
							"from tb_lib_vip\n" +
							"         left join tb_lib_subscribe on tb_lib_vip.id = tb_lib_subscribe.vip_id and\n" +
							"                                       to_days(tb_lib_subscribe.ryke_time) = to_days(now()) and\n" +
							"                                       tb_lib_subscribe.flow_stat = '3'\n" +
							"where 1=1\n" +
							"  and tb_lib_vip.lib_id = ?\n";
			if (StringUtils.isNotBlank(studentName)) {
				sql += "  and tb_lib_vip.child_name like '%" + studentName + "%'";
			}
			return dao.findList(sql, new Object[]{libId}, AllStudentDto.class);
		} else {
			String sql =
					" select t.* from " +
							"(select tb_lib_vip.id                             vip_id,\n" +
							"       tb_lib_vip.child_name                     student_name,\n" +
							"       tb_lib_vip.school,\n" +
							"       tb_lib_vip.grade,\n" +
							"       tb_lib_vip.img_url,\n" +
							"       if(tb_lib_subscribe.id is not null, 1, 0) checked_in\n" +
							"from tb_lib_vip_service,\n" +
							"     tb_lib_vip\n" +
							"         left join tb_lib_subscribe on tb_lib_vip.id = tb_lib_subscribe.vip_id and\n" +
							"                                       to_days(tb_lib_subscribe.ryke_time) = to_days(now()) and\n" +
							"                                       tb_lib_subscribe.flow_stat = '3'\n" +
							"where tb_lib_vip_service.vip_id = tb_lib_vip.id\n" +
							"  and tb_lib_vip_service.species in ('S003','S999')\n" +
							"  and tb_lib_vip_service.stat = 1\n" +
			                "  and tb_lib_vip.lib_id=?\n" ;
			if (StringUtils.isNotBlank(studentName)) {
				sql += "  and tb_lib_vip.child_name like '%" + studentName + "%'";
			}
			sql+=	"  and tb_lib_vip_service.end_time > now()\n"+
					"  group  by vip_id) t";
			return dao.findList(sql,new Object[]{libId}, AllStudentDto.class);
		}
	}


}