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

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

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.persagy.core.enumeration.SpecialOperator;
import com.persagy.core.mvc.service.CoreService;
import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.pojo.TypicalWeather;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.constant.Const;

@Service("SaveTypicalWeather")
@Scope("prototype")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class SaveTypicalWeather extends BaseBusinessService<Map> implements BusinessService {
	@Resource(name = "coreService")
	private CoreService coreService;

	@Transactional
	@Override
	public void handle(Map dto, List content) throws Exception {

		List<Map<String,Object>> dataList = (List<Map<String,Object>>) dto.get("dataList");
	
		String cityCode = (String) dto.get("cityCode");

		
		Map<String,String> fillterDate = (Map<String,String>)dto.get("fillterDate");
		Date fillterStart = null;
		Date fillterEnd = null;
		if(fillterDate != null && !fillterDate.isEmpty()) {
			fillterStart = DateUtils.str2Date((String)fillterDate.get("fillterStart") , DateUtils.SDF_SECOND);
			fillterEnd = DateUtils.str2Date((String)fillterDate.get("fillterEnd") , DateUtils.SDF_SECOND);
		}
		
		if(CollectionUtils.isEmpty(dataList)) {
			throw new Exception("dataList不能为空！");
		}
		
		List<Date> timeList = new ArrayList<>();
		for(Map<String,Object> item : dataList) {
			String time = (String) item.get("date");
			Date date = DateUtils.str2Date(time, DateUtils.SDF_DAY);
			timeList.add(date);
		}
		TypicalWeather deleteObj = new TypicalWeather();
		deleteObj.setCityCode(cityCode);
		deleteObj.setSpecialOperation("date", SpecialOperator.$in, timeList);
		coreService.remove(deleteObj);
		List<TypicalWeather> saveList = new ArrayList<>();
		Date updateDate = new Date();
		for(Map<String,Object> item : dataList) {
			TypicalWeather saveObj = new TypicalWeather();
			String time = (String) item.get("date");
			saveObj.setFillterStart(fillterStart);
			saveObj.setFillterEnd(fillterEnd);
			Date date = DateUtils.str2Date(time, DateUtils.SDF_DAY);
			Double hdd = getValue("hdd", item);
			Double cdd = getValue("cdd", item);
			Double maxTemp = getValue("maxTemp", item);
			Double minTemp = getValue("minTemp", item);
			Double avgTemp = getValue("avgTemp", item);
			Double maxWindSpeed = getValue("maxWindSpeed", item);
			Double minWindSpeed = getValue("minWindSpeed", item);
			Double avgWindSpeed = getValue("avgWindSpeed", item);
			Double maxHumidity = getValue("maxHumidity", item);
			Double minHumidity = getValue("minHumidity", item);
			Double avgHumidity = getValue("avgHumidity", item);
			Double maxPressure = getValue("maxPressure", item);
			Double minPressure = getValue("minPressure", item);
			Double avgPressure = getValue("avgPressure", item);
			Double maxVidibility = getValue("maxVisibility", item);
			Double minVidibility = getValue("minVisibility", item);
			Double avgVidibility = getValue("avgVisibility", item);
			saveObj.setCityCode(cityCode);
			saveObj.setDate(date);
			saveObj.setHdd(hdd);
			saveObj.setCdd(cdd);
			saveObj.setMaxTemp(maxTemp);
			saveObj.setMinTemp(minTemp);
			saveObj.setAvgTemp(avgTemp);
			saveObj.setMaxWindSpeed(maxWindSpeed);
			saveObj.setMinWindSpeed(minWindSpeed);
			saveObj.setAvgWindSpeed(avgWindSpeed);
			saveObj.setMaxHumidity(maxHumidity);
			saveObj.setMinHumidity(minHumidity);
			saveObj.setAvgHumidity(avgHumidity);
			saveObj.setMaxPressure(maxPressure);
			saveObj.setMinPressure(minPressure);
			saveObj.setAvgPressure(avgPressure);
			saveObj.setMaxVidibility(maxVidibility);
			saveObj.setMinVidibility(minVidibility);
			saveObj.setAvgVidibility(avgVidibility);
			saveObj.setUpdateDate(updateDate);
			saveList.add(saveObj);
		}
		coreService.save(saveList);
	}
	
	private Double getValue(String key, Map<String,Object> item) throws Exception{
		Object value = item.get(key);
		if(null == value) {
			return null;
		}
		
		String strValue = value.toString();
		try {
			return Double.valueOf(strValue);
		}catch (Exception e) {
			throw new Exception(Const.ErrorTag.paramUnrecognized);
		}
	}
}
