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

import com.ssj.dao.conch.parents.dao.ParentQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.service.conch.parents.request.QuerySchoolList;
import org.apache.commons.lang3.StringUtils;
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 ParentQueryDaoImpl implements ParentQueryDao {
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> getSchoolList(QuerySchoolList request) {
		List<Object> queryParams = new ArrayList<Object>();
		String sql ="SELECT\n" +
				"	t.name as schoolName,t.school_address as schoolAdd , ROUND(\n" +
				"		6378.138 * 2 * ASIN(\n" +
				"			SQRT(\n" +
				"				POW(\n" +
				"					SIN(\n" +
				"						(\n" +
				"							? * PI() / 180 - t.school_lat * PI() / 180\n" +
				"						) / 2\n" +
				"					),\n" +
				"					2\n" +
				"				) + COS(? * PI() / 180) * COS(t.school_lat * PI() / 180) * POW(\n" +
				"					SIN(\n" +
				"						(\n" +
				"							? * PI() / 180 - t.school_lng * PI() / 180\n" +
				"						) / 2\n" +
				"					),\n" +
				"					2\n" +
				"				)\n" +
				"			)\n" +
				"		)\n" +
				"	,2) AS distance \n" +
				"FROM\n" +
				"	sys_cn_area t  \n" ;
		sql += " where  t.type =4 ";
		if( StringUtils.isNoneBlank(request.getSearch())){
			sql += "  and  ( t.school_address LIKE CONCAT(CONCAT('%',?,'%'))   " ;
			sql += "  or    t.`name` LIKE CONCAT(CONCAT('%',?,'%')) ) " ;
		}

		sql	+=	 " ORDER BY\n" +
				"	distance ASC  limit 10 ";
		queryParams.add(request.getLatitude());
		queryParams.add(request.getLatitude());
		queryParams.add(request.getLongitude());
		if(StringUtils.isNoneBlank(request.getSearch())){
			queryParams.add(request.getSearch());
			queryParams.add(request.getSearch());
		}
		return dao.findMap(sql, queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getSayList(String parentId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT count(if(t.is_read=0,true,null)) as unreadnum,v.child_name as childname,v.img_url as imgurl,c.user_id as childuserId ");
		selSql.append(" FROM tb_lib_vip v");
		selSql.append(" LEFT JOIN conch_derver c on v.derver = c.derver ");
		selSql.append(" LEFT JOIN scon_parents_say t on c.derver = t.derver ");
		selSql.append(" where v.user_id = ? and (t.parent_id = ? or t.parent_id is null) and v.derver is not null and c.user_id is not null ");
		selSql.append(" GROUP BY v.derver " );
		queryParams.add(parentId);
		queryParams.add(parentId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> refundOrdersInfo(String userId) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append("SELECT DISTINCT e.id,e.stat,t11.is_group,t11.id as lesson_service_id,f.child_name,DATE_FORMAT(ifnull(e.initiate_time,t.create_time),'%Y.%m.%d') as create_time,t11.course_name,t11.course_count,t11.is_listen_test,t11.teacher_lever,t11.grade,t11.start_time,t11.end_time,t11.vip_id,t11.status,d.name as teacher_name,t.order_no,t.order_money,t11.order_id,c.teacher_id ");
		selSQL.append("FROM tb_lib_order_refunds e ");
		selSQL.append("LEFT JOIN tb_lesson_service t11 ON e.order_details_id = t11.id ");
		selSQL.append("LEFT JOIN tb_orders t ON t11.order_no = t.order_no ");
		selSQL.append("LEFT JOIN tb_lib_vip f ON t11.vip_id = f.id ");
		selSQL.append("LEFT JOIN kmt_course c ON t11.id = c.lesson_service_id ");
		selSQL.append("LEFT JOIN tb_lib_manager d ON c.teacher_id = d.user_id AND d.type = 7 ");
		selSQL.append("WHERE t.user_id = ? ORDER BY e.initiate_time DESC");
		queryParams.add(userId);
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

}
