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

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("GetCityInfoByProjectId")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class GetCityInfoByProjectId 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) || projectId.length() < 8) {
			throw new Exception(Const.ErrorTag.paramFormat);
		}
		
		String dicCode = projectId.substring(2, 8);
		
		String name = null;
		CityDTO cityDTO = ConfigUtil.dicCityObjMap().get(dicCode);
		if(null != cityDTO) {
			name = cityDTO.getDicName();
		}
		
		if(name == null) {
			throw new Exception(Const.ErrorTag.cityNotFound);
		}
		
		String cityId = ConfigUtil.getDicCode2CityId().get(dicCode);
		
		Map<String,String> resultMap = new HashMap<>();
		if(cityId != null) {
			resultMap.put("name", name);
			resultMap.put("cityId", cityId);
			content.add(resultMap);
		}
	}
}
