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

import com.ssj.dao.sys.lesson.dao.LessonServiceQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.weixin.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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

@Repository
public class LessonServiceQueryDaoImpl implements LessonServiceQueryDao{
	
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> findByCoursePlan(String masterId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.vip_id,t.course_count,t.current_count,t.is_listen_test,t.course_name,t.grade,t.start_time,t.end_time,t.`status`,t.teacher_lever,");
		selSql.append(" k.id,k.lesson_service_id,k.teacher_id,k.course_time,k.start_time as course_start,k.end_time as course_end,k.plan_status,k.course_type,k.num,k.is_apply,k.refuse_reason");
		selSql.append(" from tb_lesson_service t LEFT JOIN kmt_course k on t.id = k.lesson_service_id");
		selSql.append(" where t.master_id = ? and (t.`status` = 1 or (t.`status` = 0 and k.course_type = 0))");
		selSql.append(" and t.current_count = k.num and t.is_group = 0 order by t.create_time desc");
		queryParams.add(masterId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	
	public List<Map<String, Object>> findByCourseFeedback(String masterId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.vip_id,t.course_count,t.current_count,t.is_listen_test,t.course_name,t.grade,t.start_time,t.end_time,t.`status`,t.teacher_lever,");
		selSql.append(" k.id,k.teacher_id,k.course_time,k.start_time as course_start,k.end_time as course_end,k.plan_status,k.course_type,k.num,k.is_apply,k.lesson_service_id");
		selSql.append(" from tb_lesson_service t LEFT JOIN kmt_course k on t.id = k.lesson_service_id");
		selSql.append(" where t.master_id = ? and (t.`status` = 1 or (t.`status` = 0 and k.course_type = 0) or t.`status` = 2)");
		selSql.append(" and k.plan_status = 5 and k.course_time = ? and t.is_group = 0 order by t.create_time desc");
		queryParams.add(masterId);
		queryParams.add(DateUtil.format_yyyyMMdd(new Date()));
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	
	public List<Map<String, Object>> findByCourseFeedback(String masterId,String vipId,String courseName) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.vip_id,t.course_count,t.current_count,t.is_listen_test,t.course_name,t.grade,t.start_time,t.end_time,t.`status`,");
		selSql.append(" k.id,k.teacher_id,k.course_time,k.start_time as course_start,k.end_time as course_end,k.plan_status,k.course_type,k.num,k.is_apply,k.lesson_service_id");
		selSql.append(" from tb_lesson_service t LEFT JOIN kmt_course k on t.id = k.lesson_service_id");
		selSql.append(" where t.master_id = ? and t.vip_id = ? and t.course_name = ? and (t.`status` = 1 or (t.`status` = 0 and k.course_type = 0))");
		selSql.append(" and t.current_count-1 = k.num order by k.course_time desc limit 1");
		queryParams.add(masterId);
		queryParams.add(vipId);
		queryParams.add(courseName);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findByCourseRefund(String masterId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.id,t.vip_id,t.is_listen_test,t.order_id,t.order_no,t.grade,t.course_name,t.teacher_lever,");
		selSql.append(" o.initiate_time,o.review_time,o.price,o.refunds_reason,o.refunds_info,o.stat ");
		selSql.append(" from tb_lesson_service t ");
		selSql.append(" LEFT JOIN tb_lib_order_refunds o ON t.id  = o.order_details_id");
		selSql.append(" where t.master_id = ? and t.`status` = 3 and o.stat_void = 1 and o.accept_role='SSJ-TK-XZ' ");
		queryParams.add(masterId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findByNextCoursePlan(String masterId, String vipId,String courseName) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.vip_id,t.course_count,t.current_count,t.is_listen_test,t.course_name,t.grade,t.start_time,t.end_time,t.`status`,");
		selSql.append(" k.id,k.teacher_id,k.course_time,k.start_time as course_start,k.end_time as course_end,k.plan_status,k.course_type,k.num,k.is_apply");
		selSql.append(" from tb_lesson_service t LEFT JOIN kmt_course k on t.id = k.lesson_service_id");
		selSql.append(" where t.master_id = ? and t.vip_id = ? and t.course_name = ? and (t.`status` = 1 or (t.`status` = 0 and k.course_type = 0))");
		selSql.append(" and t.current_count+1 = k.num order by k.course_time asc limit 1");
		queryParams.add(masterId);
		queryParams.add(vipId);
		queryParams.add(courseName);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findParentNoPayLessonService(String vipId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.master_id,t.order_no,t.vip_id,t.course_count,t.current_count,t.is_listen_test,t.course_name,t.grade,t.start_time,t.end_time,t.`status`,t.teacher_lever,");
		selSql.append(" k.id,k.lesson_service_id,k.teacher_id,k.apply_teacher_id,k.course_time,k.start_time as course_start,k.end_time as course_end,k.plan_status,k.course_type,k.num,k.is_apply");
		selSql.append(" from tb_lesson_service t LEFT JOIN kmt_course k on t.id = k.lesson_service_id");
		selSql.append(" where t.vip_id = ? and t.`status` = 0");
		selSql.append(" and t.current_count = k.num and t.is_group = 0 order by t.create_time desc");
		queryParams.add(vipId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findParentCoursePlan(String vipId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.master_id,t.order_no,t.vip_id,t.course_count,t.current_count,t.is_listen_test,t.course_name,t.grade,t.start_time,t.end_time,t.`status`,t.teacher_lever,");
		selSql.append(" k.id,k.lesson_service_id,k.teacher_id,k.apply_teacher_id,k.course_time,k.start_time as course_start,k.end_time as course_end,k.plan_status,k.course_type,k.num,k.is_apply,k.refuse_reason");
		selSql.append(" from tb_lesson_service t LEFT JOIN kmt_course k on t.id = k.lesson_service_id");
		selSql.append(" where t.vip_id = ? and t.`status` = 1");
		selSql.append(" and t.current_count = k.num and t.is_group = 0 order by t.create_time desc");
		queryParams.add(vipId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findParentCourseFeedback(String vipId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.master_id,t.order_no,t.vip_id,t.course_count,t.current_count,t.is_listen_test,t.course_name,t.grade,t.start_time,t.end_time,t.`status`,t.teacher_lever,");
		selSql.append(" k.id,k.lesson_service_id,k.teacher_id,k.apply_teacher_id,k.course_time,k.start_time as course_start,k.end_time as course_end,k.plan_status,k.course_type,k.num,k.is_apply");
		selSql.append(" from tb_lesson_service t LEFT JOIN kmt_course k on t.id = k.lesson_service_id");
		selSql.append(" where t.vip_id = ? and (t.`status` = 1 or t.`status` = 2)");
		selSql.append(" and k.plan_status = 5 and k.course_time = ? and t.is_group = 0 order by t.create_time desc");
		queryParams.add(vipId);
		queryParams.add(DateUtil.format_yyyyMMdd(new Date()));
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findParentGroupNoPayLessonService(String vipId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.id,t.master_id,t.order_no,t.vip_id,t.course_count,t.current_count,t.course_name,t.grade,t.start_time,t.end_time,t.`status`,t.is_change_group,t.lesson_group_id,");
		selSql.append(" s.teacher_id");
		selSql.append(" from tb_lesson_service t LEFT JOIN ssj_lesson_group s on t.lesson_group_id = s.id");
		selSql.append(" where t.vip_id = ? and t.`status` = 0 and t.is_group = 1 order by t.create_time desc");
		queryParams.add(vipId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findParentGroupCoursePlan(String vipId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.master_id,t.vip_id,t.course_count,t.current_count,t.course_name,t.grade,t.`status`,t.lesson_group_id,");
		selSql.append(" k.id,k.lesson_service_id,k.teacher_id,k.course_time,k.start_time as course_start,k.end_time as course_end,k.plan_status,");
		selSql.append(" s.group_name,s.course_count as sum_course");
		selSql.append(" from tb_lesson_service t LEFT JOIN kmt_course k on t.id = k.lesson_service_id LEFT JOIN ssj_lesson_group s ON t.lesson_group_id = s.id");
		selSql.append(" where t.vip_id = ? and t.`status` = 1 and t.current_count = k.num and t.is_group = 1 order by t.create_time desc;");
		queryParams.add(vipId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findParentGroupCourseFeedback(String vipId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.vip_id,t.course_count,t.`status`,t.lesson_group_id,");
		selSql.append(" k.id,k.lesson_service_id,k.plan_status,k.num,");
		selSql.append(" s.course_count as sum_course");
		selSql.append(" from tb_lesson_service t LEFT JOIN kmt_course k on t.id = k.lesson_service_id LEFT JOIN ssj_lesson_group s ON t.lesson_group_id = s.id");
		selSql.append(" where t.vip_id = ? and (t.`status` = 1 or t.`status` = 2)");
		selSql.append(" and k.plan_status = 5 and k.course_time = ? and t.is_group = 1 order by t.create_time desc");
		queryParams.add(vipId);
		queryParams.add(DateUtil.format_yyyyMMdd(new Date()));
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
}
