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

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.constant.ConstStatisticType;
import com.persagy.functions.weather.service.DayHisRecordService;
import com.persagy.functions.weather.service.DayStaticService;
import com.persagy.functions.weather.service.HourRecordService;
import com.persagy.functions.weather.thread.HourToDayDataComputeThread;

/**
 * 通过日小时数据来计算得出日数据和日统计数据 
 */
@Service("HourToDayData")
@SuppressWarnings({"unchecked","rawtypes" })
public class HourToDayData extends BaseBusinessService<Map> implements BusinessService {
	@Resource
	private HourRecordService hourDataService;
	
	@Resource
	private DayHisRecordService dayHisRecordService;
	
	@Resource
	private DayStaticService dayStaticService;
	
	@Override
	public void handle(Map dto, List content) throws Exception {
		// ALL，所有城市
		String cityId = (String) dto.get("cityId");
		List<String> cityIdList = (List<String>) dto.get("cityIds");
		// yyyy-MM-dd
		String startDay = (String) dto.get("startDay");
		String endDay = (String) dto.get("endDay");
		String type = (String) dto.get("type");
		ConstStatisticType stype = ConstStatisticType.getItem(type);
		
		if((cityId == null && cityIdList == null) || startDay == null || endDay == null || stype == null) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		
		Date fromDay = DateUtils.str2Date(startDay, DateUtils.SDF_DAY);
		Date toDay = DateUtils.str2Date(endDay, DateUtils.SDF_DAY);
		
		if(cityId != null) {
			new Thread(new HourToDayDataComputeThread(hourDataService, dayHisRecordService, dayStaticService, 
					cityId, fromDay, toDay, stype)).start();
		}else {
			new Thread(new HourToDayDataComputeThread(hourDataService, dayHisRecordService, dayStaticService, 
					new HashSet<>(cityIdList), fromDay, toDay, stype)).start();
		}
		
		content.add("Task start running...statisticType="+type);
	}
	
}
