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

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.pojo.wz.DisasterWarning;
import com.persagy.framework.util.ConfigUtil;
import com.persagy.framework.util.DateUtils;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.service.DisasterWarningService;

@Service("QueryHistoryDisasterService")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class QueryHistoryDisasterService extends BaseBusinessService<Map> implements BusinessService {
	@Resource
	private DisasterWarningService service;

	@Override
	public void handle(Map dto, List content) throws Exception {
		String name = (String) dto.get("name");
		String projectId = (String) dto.get("projectId");
		String cityCode = (String) dto.get("cityCode");
		String type = (String) dto.get("type");
		String level = (String) dto.get("level");
		String time = (String) dto.get("time");
		
		if ((time == null) || ((StringUtils.isEmpty(projectId)) && (StringUtils.isEmpty(cityCode))
				&& (StringUtils.isEmpty(name)))) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		
		Date dayStart = null;
		try {
			dayStart = DateUtils.str2Date(time, "yyyy-MM-dd");
		} catch (Exception localException) {
		}
		if (dayStart == null) {
			throw new Exception(Const.ErrorTag.paramFormat);
		}
		
		Date dayEnd = DateUtils.getDayOff(dayStart, 1);

		String cityId = ConfigUtil.getLocationStrict(name, cityCode, projectId);
		if (cityId == null) {
			throw new Exception(Const.ErrorTag.cityNotFound);
		}
		List<DisasterWarning> list = this.service.query(cityId, type, level, dayStart, dayEnd);
		if (list != null) {
			for (DisasterWarning obj : list) {
				Map<String, String> item = new HashMap();
				item.put("cityId", obj.getCityId());
				item.put("name", obj.getName());
				item.put("path", obj.getPath());
				item.put("title", obj.getTitle());
				item.put("type", obj.getType());
				item.put("level", obj.getLevel());
				item.put("status", obj.getStatus());
				item.put("description", obj.getDescription());
				item.put("pubTime", DateUtils.date2Str(obj.getPubTime(), "yyyy-MM-dd HH:mm:ss"));
				item.put("insertTime", DateUtils.date2Str(obj.getInsertTime(), "yyyy-MM-dd HH:mm:ss"));
				content.add(item);
			}
		}
	}
}
