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

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

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("LongHuDataService")
@Scope("prototype")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class LongHuDataService extends BaseBusinessService<Map> implements BusinessService {

	@Override
	public void handle(Map dto, List content) throws Exception {
		String startDate = (String) dto.get("startDate");
		String endDate = (String) dto.get("endDate");
		
		Date dateStart = null, dateEnd = null;
		try {
			dateStart = DateUtils.str2Date(startDate, DateUtils.SDF_SECOND);
			dateEnd = DateUtils.str2Date(endDate, DateUtils.SDF_SECOND);
		}catch (Exception e) {
		}
		
		if(null == dateStart || dateEnd == null) {
			throw new Exception("时间参数有误！");
		}

		String cityId = ConfigUtil.longHu5MinCitiesGet().get("北京朝阳");
		if(null == cityId) {
			return;
		}
		
		if((dateEnd.getTime() - dateStart.getTime()) > (1000 * 60 * 60 * 24 * 366)) {
			throw new Exception("请求参数时间跨度太大！");
		}
		
		MinuteRecordData queryObj = new MinuteRecordData();
		queryObj.setBuildingForContainer(cityId);
		queryObj.setType(1);
		queryObj.setSort("minuteTime", EMSOrder.Asc);
		queryObj.setSpecialOperation("minuteTime", SpecialOperator.$gte, dateStart);
		queryObj.setSpecialOperation("minuteTime", SpecialOperator.$lt, dateEnd);
		List<MinuteRecordData> rList = coreService.query(queryObj);
		if(null != rList) {
			for(MinuteRecordData item : rList) {
				Map<String, Object> itemMap = new HashMap();
				itemMap.put("receivetime", DateUtils.date2Str(item.getMinuteTime(), DateUtils.SDF_SECOND));
				itemMap.put("data", item.getData());
				itemMap.put("type", 1);
				content.add(itemMap);
			}
		}
	}
}
