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

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

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.framework.util.ConfigUtil;
import com.persagy.functions.weather.constant.Const;
import com.persagy.functions.weather.dto.CityDTO;

@Service("QueryCityInfoByProjectId")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class QueryCityInfoByProjectId extends BaseBusinessService<Map> implements BusinessService {

	@Override
	public void handle(Map dto, List content) throws Exception {
		String projectId = (String) dto.get("projectId");
		if (StringUtils.isEmpty(projectId)) {
			throw new Exception(Const.ErrorTag.paramMis);
		}
		if (projectId.length() < 8) {
			throw new Exception(Const.ErrorTag.paramUnrecognized);
		}
		String dicCode = projectId.substring(2, 8);
		CityDTO cityDTO = (CityDTO) ConfigUtil.dicCityObjMap().get(dicCode);

		String cityName = cityDTO != null ? cityDTO.getDicName() : null;
		String cityId = (String) ConfigUtil.getDicCode2CityId().get(dicCode);
		if ((cityName == null) || (cityId == null)) {
			throw new Exception(Const.ErrorTag.cityNotFound);
		}
		Map<String, String> resultMap = new HashMap();
		resultMap.put("name", cityName);
		resultMap.put("cityId", cityId);
		content.add(resultMap);
	}
}
