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

import java.util.ArrayList;
import java.util.Collection;
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.CollectionUtils;
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.constant.DisasterConstant;
import com.persagy.functions.weather.service.DisasterWarningService;

@Service("QueryDisasterService")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class QueryDisasterService 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");
	    if ((StringUtils.isEmpty(projectId)) && (StringUtils.isEmpty(cityCode)) && (StringUtils.isEmpty(name))) {
	      throw new Exception(Const.ErrorTag.paramMis);
	    }
	    String cityId = ConfigUtil.getLocationStrict(name, cityCode, projectId);
	    if (StringUtils.isEmpty(name)) {
	      throw new Exception(Const.ErrorTag.cityNotFound);
	    }
	    Date dateEnd = new Date();
	    Date dateStart = DateUtils.getDayOff(dateEnd, -7);
	    
	    List<DisasterWarning> disasterList = this.service.query(cityId, type, level, dateStart, dateEnd);
	    if (!CollectionUtils.isEmpty(disasterList)) {
	      content.addAll(filterDisaster(disasterList));
	    }
	}

	private Collection filterDisaster(List<DisasterWarning> disasterList) {
		List<Map<String, Object>> resultList = new ArrayList();
		if (!CollectionUtils.isEmpty(disasterList)) {
			for (DisasterWarning item : disasterList) {
				String diasterType = item.getType();
				String diasterLevel = item.getLevel();
				Date pubTime = item.getPubTime();
				Date nowTime = new Date();

				Integer continuedTime = DisasterConstant.getType2TimeMap()
						.get(diasterType + "-" + diasterLevel);
				continuedTime = continuedTime == null ? 8 : continuedTime;
				if (nowTime.before(DateUtils.getHourOff(pubTime, continuedTime.intValue()))) {
					resultList.add(getResultItem(item));
				}
			}
		}
		return resultList;
	}
	
	private Map<String, Object> getResultItem(DisasterWarning obj) {
		Map<String, Object> resultItem = new HashMap<>();
		resultItem.put("cityId", obj.getCityId());
		resultItem.put("name", obj.getName());
		resultItem.put("path", obj.getPath());
		resultItem.put("pubTime", obj.getPubTime());
		resultItem.put("insertTime", obj.getInsertTime());
		resultItem.put("title", obj.getTitle());
		resultItem.put("type", obj.getType());
		resultItem.put("level", obj.getLevel());
		resultItem.put("description", obj.getDescription());
		return resultItem;
	}
}
