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

import com.ssj.dao.sys.problem.dao.ProblemPictureQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.conch.parents.request.version4.ProblemKnowHowReq;
import org.apache.commons.lang3.StringUtils;
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 ProblemPictureQueryDaoImpl implements ProblemPictureQueryDao {
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public Page<Map<String, Object>> findByProblemListByParent(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSql.append(" SELECT s.id,s.parent_id,s.file_key,s.create_time,s.is_feedback from scon_problem_picture s ");
		selSql.append(" where s.parent_id = ? and s.is_delete = ?  and s.vip_id = ? ");
		queryParams.add(searchParams.get("parentId"));
		queryParams.add(searchParams.get("isDelete"));
		queryParams.add(searchParams.get("vipId"));
		String createTime = (String)searchParams.get("createTime");
		if (StringUtils.isNoneBlank(createTime)) {
			selSql.append(" and s.create_time <= ?");
			queryParams.add(createTime);
		}
		String time = (String)searchParams.get("time");
		if (StringUtils.isNoneBlank(time)) {
			selSql.append(" and s.is_feedback = 1 ");
			selSql.append(" and s.create_time >= ? and s.create_time <= date_format(?,'%Y-%m-%d 23:59:59') ");
			queryParams.add(time);
			queryParams.add(time);
		}
		selSql.append(" ORDER BY s.is_feedback asc,s.create_time desc ");
		return dao.findPage(selSql.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public List<Map<String, Object>> findProblemByTeacher (String teacherId, String beginTime){
		StringBuilder sb = new StringBuilder ();
		sb.append("select p.id pid, p.file_key,p.is_feedback, p.subject,p.create_time, t.id vip_id, t.child_name, t.grade  from ")
				.append(" scon_problem_picture p left join tb_lib_vip t on p.vip_id=t.id where p.create_time > '")
				.append(beginTime).append("' and p.teacher_id = '").append(teacherId).append("' order by is_feedback");
		return dao.findMap(sb.toString(), null);
	}

	@Override
	public List<Map<String, Object>> findHistoryNotFeedbackProblem (String teacherId, String endTime){
		StringBuilder sb = new StringBuilder ();
		sb.append("select p.id pid, p.file_key,p.is_feedback, p.subject, p.create_time, t.id vip_id, t.child_name, t.grade  from ")
				.append(" scon_problem_picture p left join tb_lib_vip t on p.vip_id=t.id where p.is_feedback = 0 and p.create_time <='")
				.append(endTime).append("' and p.teacher_id = '").append(teacherId).append("'");
		return dao.findMap(sb.toString(), null);
	}


	@Override
	public Page<Map<String, Object>> findProblemListOfParent(Map<String, Object> searchParams, Pageable pageable){
		//0-未解决难题，1-已解决难题
		int type = (int) searchParams.get("type");
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSql.append(" SELECT t1.id,t1.file_key,t1.subject,DATE_FORMAT(t1.create_time,'%Y-%m-%d %H:%i:%s') create_time,t3.id vip_id, t3.img_url,t3.child_name ");
		if(type == 1){
			//难题老师建议DATE_FORMAT
			selSql.append(" ,t3.content ");
			selSql.append(" ,DATE_FORMAT(t2.create_time, '%Y-%m-%d %H:%i:%s') feedback_time ");
		}
		selSql.append(" from scon_problem_picture t1 " +
				" left join tb_lib_vip t3 on t1.vip_id = t3.id ");
		if(type == 1){
			selSql.append(" left join scon_feedback t2 on t1.id = t2.biz_id ")
					.append(" left join (select distinct feedback_id,content from scon_text_feedback_detail where type = '4') t3 on t2.id = t3.feedback_id ");
		}
		//是否解决
		selSql.append(" where t1.is_feedback = ? ");
		queryParams.add(type);
		//会员id
		if (!"no_need".equals(searchParams.get("vipId"))) {
			String vipId = (String) searchParams.get("vipId");
			selSql.append(" and t1.vip_id = ? ");
			queryParams.add(vipId);
		}
		//最大时间,格式(yyyy-MM-dd HH:mm:ss)
		String maxCreateTime = (String) searchParams.get("maxCreateTime");
		if(StringUtil.isNotBlank(maxCreateTime)){
			selSql.append(" and t1.create_time <= ? ");
			queryParams.add(maxCreateTime);
		}
		//老师id
		String teacherId = (String) searchParams.get("teacherId");
		if(StringUtil.isNotBlank(teacherId)){
			selSql.append(" and t1.teacher_id = ? ");
			queryParams.add(teacherId);
		}
		if(type == 1){
			//起始时间格式(yyyy-MM)
			String startTime = (String) searchParams.get("startTime");
			if (StringUtil.isNotBlank(startTime)) {
				selSql.append(" and t1.create_time > ? ");
				queryParams.add(startTime);
			}
			//结束时间格式(yyyy-MM)
			String endTime = (String) searchParams.get("endTime");
			if (StringUtil.isNotBlank(endTime)) {
				selSql.append(" and t1.create_time < ? ");
				queryParams.add(endTime);
			}
			//科目类型:全部(传空),语文，数学，英语
			String courseName = (String)searchParams.get("courseName");
			if (StringUtils.isNoneBlank(courseName)) {
				selSql.append(" and t1.subject = ? ");
				queryParams.add(courseName);
			}
			//知识模块名称
			String module = (String) searchParams.get("module");
			if (StringUtils.isNoneBlank(module)) {
				selSql.append(" and exists( select 1 from scon_know_feedback_detail t11 join zyb_know_how_details t22 on t22.how_id = t11.know_how_id" +
						" where t11.problem_id = t1.id and t22.module = ? ) ");
				queryParams.add(module);
			}
			//反馈起始时间格式(yyyy-MM-dd)
			String feedbackStartTime = (String) searchParams.get("feedbackStartTime");
			if (StringUtil.isNotBlank(feedbackStartTime)) {
				selSql.append(" and t2.create_time > ? ");
				queryParams.add(feedbackStartTime);
			}
			//反馈结束时间格式(yyyy-MM-dd)
			String feedbackEndTime = (String) searchParams.get("feedbackEndTime");
			if (StringUtil.isNotBlank(feedbackEndTime)) {
				selSql.append(" and t2.create_time < ? ");
				queryParams.add(feedbackEndTime);
			}
			selSql.append(" ORDER BY t2.create_time desc ");
		}else{
			selSql.append(" ORDER BY t1.create_time desc ");
		}
		return dao.findPage(selSql.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public List<Map<String, Object>> findProblemListOfParent(Map<String, Object> searchParams){
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<>();
		selSql.append(" SELECT t1.id,t1.file_key,t1.subject,DATE_FORMAT(t1.create_time,'%Y-%m-%d %H:%i:%s') create_time,t3.content ");
		selSql.append(" from scon_problem_picture t1 ");
		selSql.append(" left join scon_feedback t2 on t1.id = t2.biz_id ")
				.append(" left join (select * from scon_text_feedback_detail where type = '4') t3 on t2.id = t3.feedback_id ");
		selSql.append(" where t1.is_feedback = 1 ");
		//会员id
		String vipId = (String) searchParams.get("vipId");
		selSql.append(" and t1.vip_id = ? ");
		queryParams.add(vipId);
		//时间格式(yyyy-MM-dd)
		String date = (String) searchParams.get("date");
		if(StringUtil.isNotBlank(date)){
			selSql.append(" and to_days(t1.create_time) = to_days(?) ");
			queryParams.add(date);
		}
		selSql.append(" ORDER BY t1.create_time desc ");
		return dao.findMap(selSql.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findKownHowOfParent (ProblemKnowHowReq request){
		StringBuilder sb = new StringBuilder ();
		sb.append("select distinct t1.module from zyb_know_how_details t1 " +
				" join scon_know_feedback_detail t2 on t1.how_id = t2.know_how_id " +
				" join scon_problem_picture t3 on t2.problem_id = t3.id " +
				" where 1=1 ");
		sb.append("and t3.vip_id = '").append(request.getVipId()).append("'");
		if(StringUtil.isNotBlank(request.getCourseName())){
			sb.append(" and t3.subject = '").append(request.getCourseName()).append("'");
		}
		if(StringUtil.isNotBlank(request.getStartTime())){
			sb.append(" and t3.create_time >  '").append(request.getStartTime()).append("'");
		}
		if(StringUtil.isNotBlank(request.getEndTime())){
			sb.append(" and t3.create_time <  '").append(request.getEndTime()).append("'");
		}
		return dao.findMap(sb.toString(), null);
	}
}
