package com.persagy.functions.weather.business.v2020.customapi;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.persagy.core.enumeration.EMSOrder;
import com.persagy.core.enumeration.SpecialOperator;
import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.pojo.wz.MinuteRecordData;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;

/**
 * 根据城市名称及开始时间，结束时间获取城市某时间段某种时间粒度下的温度信息
 */
@Service("CommonTempratureDataService")
@Scope("prototype")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class CommonTempratureDataService extends BaseBusinessService<Map> implements BusinessService {
	public static final String sdf_sec = "yyyy-MM-dd HH:mm:ss";

	@Override
	public void handle(Map dto, List content) throws Exception {
		Integer type = (Integer) dto.get("type");
		String startDate = (String) dto.get("startDate");
		String endDate = (String) dto.get("endDate");
		String cityName = (String) dto.get("cityName");
		// 0-分钟；1-小时；2-天
		Integer timeType = (Integer) dto.get("timeType");
		if (type == null) {
			type = 1;
		}
		if(timeType == null) {
			timeType = 0;
		}else if(timeType > 2) {
			throw new Exception("无法识别timeType:"+timeType);
		}
		
		String cityId = ConfigUtil.longHu5MinCitiesGet().get(cityName);
		if(cityId == null) {
			throw new Exception("无法识别cityName:"+cityName);
		}
		
		Date dateStart = null, dateEnd = null;
		try {
			dateStart = DateUtils.str2Date(startDate, sdf_sec);
			dateEnd = DateUtils.str2Date(endDate, sdf_sec);
		}catch (Exception e) {
		}
		
		if(null == dateStart || dateEnd == null) {
			throw new Exception("时间参数有误！");
		}
		
		MinuteRecordData queryObj = new MinuteRecordData();
		queryObj.setBuildingForContainer(cityId);
		queryObj.setType(type);
		queryObj.setSort("minuteTime", EMSOrder.Asc);
		queryObj.setSpecialOperation("minuteTime", SpecialOperator.$gte, dateStart);
		queryObj.setSpecialOperation("minuteTime", SpecialOperator.$lt, dateEnd);
		
		int totalNum = coreService.count(queryObj);
		if(totalNum > 12 * 24 * 365) {
			throw new Exception("请求参数时间跨度太大！");
		}
		
		if(totalNum == 0) {
			return;
		}
		
		List<MinuteRecordData> rList = coreService.query(queryObj);
		TreeMap<String, Double> time_value = new TreeMap<String, Double>();
		String timeStr = null;
		if(timeType == 1) {
			for(MinuteRecordData item : rList) {
				timeStr = new SimpleDateFormat(sdf_sec).format(item.getMinuteTime());
				if(timeStr.endsWith("00:00")) {
					time_value.put(timeStr, item.getData());
				}
			}
		}else if(timeType == 2) {
			for(MinuteRecordData item : rList) {
				timeStr = new SimpleDateFormat(sdf_sec).format(item.getMinuteTime());
				if(timeStr.endsWith("00:00:00")) {
					time_value.put(timeStr, item.getData());
				}
			}
		} else {
			for(MinuteRecordData item : rList) {
				timeStr = new SimpleDateFormat(sdf_sec).format(item.getMinuteTime());
				time_value.put(timeStr, item.getData());
			}
		}
		//{"时间点":温度值}
		content.add(time_value);
	}
}
