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

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

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

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.old.WeatherHourData;
import com.persagy.ems.pojo.wz.HourRecordData;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.service.HourRecordService;

/**
 * 将老系统中的小时数据传到新系统
 */
@Service("HourData2VersionTrans")
@SuppressWarnings({"unchecked","rawtypes" })
public class HourData2VersionTrans extends BaseBusinessService<Map> implements BusinessService {
	private static final Logger LOGGER = Logger.getLogger(HourData2VersionTrans.class);
	
	@Resource(name="coreService")
	private CoreService coreService;
	
	@Resource
	private HourRecordService hourDataService;
	
	@Override
	public void handle(Map dto, List content) throws Exception {
		String hourStart = (String) dto.get("hourStart");
		String hourEnd = (String) dto.get("hourEnd");
		final Date start = DateUtils.str2Date(hourStart, "yyyyMMddHH");
		final Date end = DateUtils.str2Date(hourEnd, "yyyyMMddHH");
		final Set<String> cities = ConfigUtil.getHourCitySet();
		final int ctall = cities.size();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				int cindex = 1;
				for(String cityId : cities) {
					List<WeatherHourData> oldList = queryOld(cityId, start, end);
					if(null != oldList && !oldList.isEmpty()) {
						List<HourRecordData> listSave = new ArrayList<>();
						for(WeatherHourData obj : oldList) {
							HourRecordData sobj = new HourRecordData();
							sobj.setCityId(cityId);
							sobj.setClouds(obj.getClouds());
							sobj.setCo(obj.getCo());
							sobj.setCode(obj.getCode());
							sobj.setDew_point(obj.getDewPoint());
							sobj.setHourTime(obj.getReceivetime());
							sobj.setFeels_like(obj.getFeelsLike());
							sobj.setHumidity(obj.getHumidity());
							sobj.setLastUpdateAir(obj.getReceivetime());
							sobj.setLastUpdateNow(obj.getReceivetime());
							sobj.setNo2(obj.getNo2());
							sobj.setO3(obj.getO3());
							sobj.setPm10(obj.getPm10());
							sobj.setPm25(obj.getPm25());
							sobj.setPressure(obj.getPressure());
							sobj.setQuality(obj.getQuality());
							sobj.setSo2(obj.getSo2());
							sobj.setText(obj.getText());
							sobj.setTemperature(obj.getTemperature());
							sobj.setVisibility(obj.getVisibility());
							sobj.setWind_direction(obj.getWindDirection());
							sobj.setWind_direction_degree(obj.getWindDirectionDegree());
							sobj.setWind_scale(obj.getWindScale());
							sobj.setWind_speed(obj.getWindSpeed());
							listSave.add(sobj);
						}
						try {
							hourDataService.saveBatch(listSave);
							LOGGER.error("Success " +cindex + "/" + ctall);
						}catch (Exception e) {
							LOGGER.error("Error city=" + cityId + " --- " +cindex + "/" + ctall);
						}
					}
				}
			}
		}).start();
		
		content.add("Task start running...");
	}
	
	private List<WeatherHourData> queryOld(String cityId, Date start, Date end) {
		List<WeatherHourData> list = new ArrayList<>();
		try {
			WeatherHourData obj = new WeatherHourData();
			obj.setBuildingForContainer(cityId);
			obj.setSpecialOperation("receivetime", SpecialOperator.$gte, start);
			obj.setSpecialOperation("receivetime", SpecialOperator.$lt, end);
			list = coreService.query(obj);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}
}
