package cmm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.Resource;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.annotation.Transactional;

import cmm.Constant;
import cmm.service.CmmApiService;
import cmm.service.CmmService;
import cmm.service.ExtService;
import cmm.util.CmmCodeUtil;
import cmm.util.CmmCodeVO;
import cmm.util.CmmUtil;
import cmm.util.DataMap;
import cmm.util.DateUtil;
import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;
import egovframework.rte.fdl.cmmn.exception.EgovBizException;
import egovframework.rte.fdl.idgnr.EgovIdGnrService;
import egovframework.rte.fdl.property.EgovPropertyService;


/**  
 * 외부 API를 통한 데이터 관리 서비스 클래스를 구현한다.
 * @Class Name : EtServiceImpl.java
 * @author ecplaza 
 * @since 2018-04-18
 * @version 1.0
 */
@Service("extService")
public class ExtServiceImpl extends EgovAbstractServiceImpl implements ExtService {

	/** LOGGER */
	private static final Logger LOGGER = LoggerFactory.getLogger(ExtServiceImpl.class);
	
	  /** EgovPropertyService */
    @Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
    
	/** cmmApiService */
	@Resource(name = "cmmApiService")
	private CmmApiService cmmApiService;
    
	@Resource(name = "cmmService")
	private CmmService cmmService;
	
    /** extDAO */
    @Resource(name="extDAO")
    private ExtDAO extDAO;
//    
//    /** egovDfInfoIdGnrService */
//    @Resource(name = "egovDfInfoIdGnrService")
//    private EgovIdGnrService dfInfoIdGnrService; 
    
//    /** egovFdInfoIdGnrService */
//    @Resource(name = "egovFdInfoIdGnrService")
//    private EgovIdGnrService fdInfoIdGnrService;
    
//    /** egovGiaInfoIdGnrService */
//    @Resource(name = "egovGiaInfoIdGnrService")
//    private EgovIdGnrService giaInfoIdGnrService;
    
    /** 관광정보 api 카테고리  **/
    private List<CmmCodeVO> giaCat1List = null;
    private List<CmmCodeVO> giaCat2List = null;
    private List<CmmCodeVO> giaCat3List = null;
    
    
    @Override
    public List<DataMap> selectExtSigunguList(DataMap paramMap) throws EgovBizException{
    	return extDAO.selectExtSigunguList(paramMap);
    }
    @Override
    public List<DataMap> selectExtSigunguList2(DataMap paramMap) throws EgovBizException{
    	return extDAO.selectExtSigunguList2(paramMap);
    }
    @Override
    public List<DataMap> selectExtGisSigunguList(DataMap paramMap) throws EgovBizException{
    	return extDAO.selectExtGisSigunguList(paramMap);
    }
    @Override
    public List<DataMap> selectExtGisDongList(DataMap paramMap) throws EgovBizException{
    	return extDAO.selectExtGisDongList(paramMap);
    }
    @Override
    public List<DataMap> selectExtSidoMapMappingList(DataMap paramMap) throws EgovBizException{
    	if(!"".equals(paramMap.getString("sido")) && "".equals(paramMap.getString("sido2"))){
    		paramMap.setString("sido2", paramMap.getString("sido"));
    	}
    	if( paramMap.getString("sido").indexOf("세종") != -1 
    			|| paramMap.getString("sido1").indexOf("세종") != -1 
    			|| paramMap.getString("sido2").indexOf("세종") != -1 ){
    		paramMap.setString("sigungu", "세종특별자치시"); 
    	}
    	return extDAO.selectExtSidoMapMappingList(paramMap);
    }
    @Override
    public List<DataMap> selectExtDfInfoList(DataMap paramMap) throws EgovBizException{
    	paramMap.setString("dfDt", DateUtil.getNowString(DateUtil.DATE_DB_FORMAT)); 
    	if(paramMap.getString("sido").indexOf("세종") != -1){
    		paramMap.setString("sigungu", "세종특별자치시");
    	}
    	return extDAO.selectExtDfInfoList(paramMap);
    }
    @Override
    @SuppressWarnings("unchecked")
	public List<DataMap> selectExtAisInfoList(String sido, String sigungu) throws EgovBizException{
    	List<DataMap> rtnList = null;
    	if(sido.indexOf("세종") != -1){
    		sigungu = "세종시";
    	}
    	String dataTime = DateUtil.getNowString(DateUtil.DATEHOUR_FORMAT2)+ ":00"; 
    	DataMap rsMap = cmmApiService.getCtprvnMesureSidoList(sido,dataTime);
		if("00".equals(rsMap.getString("resultCode"))){
			List<DataMap> resultList = (List<DataMap>) rsMap.get("resultList");
			if(resultList != null && resultList.size()>0){
				rtnList = new ArrayList<DataMap>();
				for(DataMap map : resultList){
					if(sigungu.equals(map.getString("sigungu"))){
						rtnList.add(map);
						break;
					}
				}
				if(rtnList == null || resultList.size() == 0){
					rtnList.add(resultList.get(0));
				}
			}
		}
    	return rtnList;
    }
    @Override
    @SuppressWarnings("unchecked")
    public List<DataMap> selectExtFtdInfoList(DataMap paramMap) throws EgovBizException{
    	String nx="",ny="";
    	boolean isNext = false;
    	if(paramMap.getString("sido").indexOf("세종") != -1){
    		paramMap.setString("sigungu", "세종특별자치시");
    	}
    	List<DataMap> xyList = this.selectExtSidoMapMappingList(paramMap);
    	if(xyList !=null && xyList.size()>0){
    		DataMap sidoMap = xyList.get(0);
    		nx = sidoMap.getString("mapNx");
    		ny = sidoMap.getString("mapNy");
    		
    		isNext = true;
    	}else{
    		paramMap.remove("dong");
    		xyList = this.selectExtSidoMapMappingList(paramMap);
    		if(xyList !=null && xyList.size()>0){
    			DataMap sidoMap = xyList.get(0);
        		nx = sidoMap.getString("mapNx");
        		ny = sidoMap.getString("mapNy");
        		
        		isNext = true;
    		}
    	}
    	List<DataMap> resultList = null; 
    	if(isNext){
    		int h = 0;
    		if(NumberUtils.toInt(DateUtil.currentDateHh()) < 45){
    			h = -1;
    		}
    		String baseDate = DateUtil.getAddHourFormat(h,DateUtil.DATE_DB_FORMAT);
    		String baseTime = DateUtil.getAddHourFormat(h,DateUtil.TIMEHOUR_FORMAT3) +"00";
        	DataMap resultMap =  cmmApiService.getForecastTimeDataList(baseDate,baseTime,nx,ny);
        	if("0000".equals(resultMap.getString("resultCode"))){
        		resultList = (List<DataMap>) resultMap.get("resultList");
        	}
    	}
    	return resultList;
    }
    @Override
	public List<DataMap> selectCulRelatedNews(DataMap paramMap) throws EgovBizException{
		
		paramMap.put("provider", new String [] {"경향신문","국민일보","내일신문","문화일보","서울신문","세계일보","한겨레","한국일보","경기일보","경인일보","강원도민일보","대전일보","중도일보","중부매일","충북일보","충청일보","충청투데이","경남신문","경남도민일보","경상일보","국제신문","대구일보","매일신문","부산일보","영남일보","울산매일","광주일보","무등일보","전남일보","전북도민일보","전북일보","제민일보","한라일보","매일경제","서울경제","파이낸셜뉴스","한국경제","헤럴드경제","MBC","SBS","YTN","OBS","전자신문","디지털타임스"});
    	paramMap.put("category", new String[] {"문화>영화","문화>요리_여행","문화>학술_문화재","문화>전시_공연","문화>출판","문화>미술_건축","문화>음악","문화>종교"});
    	paramMap.put("fields", new String [] {"hilight","byline","category","category_incident","images"});
    	/*paramMap.setString("hilight", "2000");
    	paramMap.setString("return_from", "0");
    	paramMap.setString("return_size", "15");*/
		paramMap.put("sort", new String [] {"date", "desc"});
		
		List<DataMap> newsList = cmmApiService.getToolsApiNews(paramMap);
		if(newsList != null){
			StringTokenizer stTokenizer = null;
			for(int i=0; i<newsList.size(); i++){
				if(!newsList.get(i).getString("images").isEmpty()){
					stTokenizer = new StringTokenizer(newsList.get(i).getString("images"), "\n");
					String image = propertiesService.getString("openapi.tools.kinds.imageUrl") + stTokenizer.nextToken();
					newsList.get(i).setString("images", image);
				}
				if(!newsList.get(i).getString("dateline").isEmpty()){
					String date = newsList.get(i).getString("dateline").substring(0,4) + "." +
							newsList.get(i).getString("dateline").substring(5,7) + "." +
							newsList.get(i).getString("dateline").substring(8,10);
					newsList.get(i).setString("dateline", date);
				}
			}
		}
		return newsList;
	}
    @SuppressWarnings("unchecked")
	@Override
    @Cacheable(value="giaDetailCache", key="{#contentId}")
    public DataMap selectExtGiaDetail(String contentId, String contentTypeId) throws EgovBizException{
    	
    	DataMap paramMap = new DataMap();
    	paramMap.setString("contentId", contentId);
    	paramMap.setString("contentTypeId", contentTypeId);
    	DataMap rtnMap = new DataMap();
    	
    	// DB 상세정보
    	/*DataMap rsMap = extDAO.selectExtGiaDetail(paramMap);
    	rtnMap.put("giaDetail", rsMap);*/
    	
    	// 상세공통
    	DataMap rsMap  = cmmApiService.getGiaDetailCommon(paramMap);
    	if(rsMap.getInt("totalCount") > 0){
    		List<DataMap> resultList = (List<DataMap>) rsMap.get("resultList");
    		if(resultList != null && resultList.size()>0){
    			rtnMap.put("giaCommon",resultList.get(0));
    		}
    	}
    	// 상세 소개정보
    	rsMap = cmmApiService.getGiaDetailIntro(paramMap);
    	if(rsMap.getInt("totalCount") > 0){
    		List<DataMap> resultList = (List<DataMap>) rsMap.get("resultList");
    		if(resultList != null && resultList.size()>0){
    			rtnMap.put("giaIntro",resultList.get(0));
    		}
    	}
    	// 상세 반복 및 코스 정보 or 룸 이미지
    	rsMap = cmmApiService.getGiaDetailInfo(paramMap);
    	rtnMap.put("giaInfoList",(List<DataMap>) rsMap.get("resultList"));
    	
    	// 상세 추가이미지 
    	rsMap = cmmApiService.getGiaDetailImage(paramMap);
    	rtnMap.put("giaImageList",(List<DataMap>) rsMap.get("resultList"));
    	
    	// 상세 메뉴이미지 
    	paramMap.setString("imageYN", "N");
    	rsMap = cmmApiService.getGiaDetailImage(paramMap);
    	rtnMap.put("giaMenuImageList",(List<DataMap>) rsMap.get("resultList"));
    	
    	return rtnMap;
    }
    @Override
    @Transactional
    public DataMap insertExtDfInfo() {
    	
    	String toDay =  DateUtil.getNowString(DateUtil.DATE_DB_FORMAT);
		String tomorrow =  DateUtil.getAddDayFormat(1,DateUtil.DATE_DB_FORMAT);
		String startDt = DateUtil.getNowString(DateUtil.DATETIME_FORMAT);
		DataMap schedulerHstMap = new DataMap();
		boolean isSuccess = false;
		String msg = "";
		int dataCnt = 0;
		
		try {
			DataMap paramMap = new DataMap();
			List<DataMap> list = new ArrayList<DataMap>();
			boolean isParseErr = false;
			for(int idx=0; idx<Constant.DF_DISS_CODE_ARRAY.length;idx++){
				if(isParseErr){
					 break;
				}
				paramMap.setString("dissCd",Constant.DF_DISS_CODE_ARRAY[idx]);
				for(CmmCodeVO vo : CmmCodeUtil.getSidoCodeList()){
					paramMap.setString("znCd",vo.getCode()); 
					JSONObject jsonObj = cmmApiService.getDissForecastInfoList(paramMap);
					JSONObject jsonResponse = null;
					JSONObject jsonBody = null;
					JSONObject jsonItems = null;
					JSONArray resultJSONArray = null;
					if(jsonObj.has("response")){
						jsonResponse = jsonObj.getJSONObject("response");
						if(jsonResponse.has("body")){
							jsonBody = jsonResponse.getJSONObject("body");
							jsonItems = jsonBody.getJSONObject("items");
							if(jsonItems.get("item") instanceof JSONObject){
								resultJSONArray = new JSONArray();
								resultJSONArray.put(jsonItems.getJSONObject("item"));
							}else{
								resultJSONArray = jsonItems.getJSONArray("item");
							}
							JSONObject obj = null;
							for(int i=0;i<resultJSONArray.length();i++){
								obj = resultJSONArray.getJSONObject(i);
								if(toDay.equals(obj.get("dt").toString().trim()) || tomorrow.equals(obj.get("dt").toString().trim())){
									obj.put("dissNm",Constant.DF_DISS_NAME_ARRAY[idx]);
									list.add(CmmUtil.getMapFromJsonObject(obj));
								}
							}
						}else{
							isParseErr = true;
							msg = "[가공오류]body 미존재";
							break;
						}
					}else{
						isParseErr = true;
						msg = "[통신오류]"+jsonObj.getJSONObject("response").getJSONObject("cmmMsgHeader").get("returnAuthMsg").toString();
						break;
					}
					paramMap.remove("znCd"); 
				} // end of for
				paramMap.remove("dissCd"); 
			} // end of for
			if(!isParseErr){
				if(list.size()> 0){
					extDAO.deleteExtDfInfo();
					for(DataMap map : list){
						//map.setString("dfInfoId", dfInfoIdGnrService.getNextStringId());
						extDAO.insertExtDfInfo(map); 
						map.remove("dfInfoId");
						dataCnt++;
					}
					isSuccess = true;
				}else{
					msg = "[데이터 가공오류]가공된 데이터 size:0";
				}
			}
		} catch (TransactionException ex1) {
			LOGGER.error("fail in insertExtDfInfo()", ex1);
			msg = dataLimit(ex1.toString());
		} catch (Exception ex2) {
			LOGGER.error("fail in insertExtDfInfo()", ex2);
			msg = dataLimit(ex2.toString());
		} finally{
			schedulerHstMap.setString("schedulerMethodNm", "setExtDfInfo");
			schedulerHstMap.setString("schedulerNm", Constant.SCHEDULER_EXT_DF_INFO);
			schedulerHstMap.setString("strTm", startDt);
			schedulerHstMap.setString("endTm", DateUtil.getNowString(DateUtil.DATETIME_FORMAT));
			if(isSuccess){
				schedulerHstMap.setString("rstMsg", "등록성공");
			}else{
				schedulerHstMap.setString("rstMsg", "등록실패");
				schedulerHstMap.setString("etcCn", msg);
			}
			schedulerHstMap.setInt("dataCnt", dataCnt);
			
			try {
   				cmmService.insertCmmSchedulerHst(schedulerHstMap);
   			} catch (EgovBizException ex1) {
   				LOGGER.error("fail in insertCmmSchedulerHst()", ex1);
   			} catch (Exception ex2) {
   				LOGGER.error("fail in insertCmmSchedulerHst()", ex2);
   			}
			
		}
		
		return schedulerHstMap;
	}
    @Override
    @Transactional
	public DataMap insertExtFdInfo() {

    	String fromDate =  DateUtil.getNowString(DateUtil.DATE_DB_FORMAT);
		String toDate =  DateUtil.getAddDayFormat(30,DateUtil.DATE_DB_FORMAT);
    	String startDt = DateUtil.getNowString(DateUtil.DATETIME_FORMAT);
    	DataMap schedulerHstMap = new DataMap();
		boolean isSuccess = true;
		String msg = "";
		int dataCnt = 0;
		
		try {
			DataMap paramMap = new DataMap();
	   		paramMap.setString("viewType", Constant.VIEW_TYPE_L); // 화면 타입(포탈에서만 사용하는 변수)
	   		paramMap.setString("reqUrl", propertiesService.getString("cultureinfoApi.fd.period.url"));
	   		paramMap.setString("serviceKey", propertiesService.getString("cultureinfoApi.serviceKey"));
	   		paramMap.setString("cPage", "1");
	   		paramMap.setString("rows", "2000");
	   		paramMap.setString("from", fromDate);
	   		paramMap.setString("to", toDate);
	   		
	   		JSONObject jsonObj = cmmApiService.getCultureinfoApiList(paramMap);
	   		JSONObject jsonResponse = jsonObj.getJSONObject("response");
	   		JSONObject jsonComMsgHeader = jsonResponse.getJSONObject("comMsgHeader");
	   		
	   		JSONObject jsonObjMsgBody = null;
	   		List<DataMap> resultList = null;
	   		if("Y".equals(jsonComMsgHeader.get("SuccessYN").toString())){
	   			if(jsonResponse.has("msgBody")){
	   				jsonObjMsgBody = jsonResponse.getJSONObject("msgBody");
	   	   			if(jsonObjMsgBody.has("perforList")){
	   	   				JSONArray jsonArray = null;
	   	   				if(jsonObjMsgBody.get("perforList") instanceof JSONObject){
	   	   					jsonArray = new JSONArray();
	   	   					jsonArray.put(jsonObjMsgBody.getJSONObject("perforList"));
	   	   				}else{
	   	   					jsonArray = jsonObjMsgBody.getJSONArray("perforList");
	   	   				}
	   	   				resultList = CmmUtil.getListMapFromJsonArray(jsonArray);
	   	   			}else{
	   	   				msg = "[데이터 가공오류][목록정보]perforList 미존재";
	   	   				isSuccess = false;
	   	   			}
	   			}else{
	   				msg = "[데이터 가공오류][목록정보]msgBody 미존재";
	   				isSuccess = false;
	   			}
	   		}else{
	   			msg = jsonComMsgHeader.get("ErrMsg").toString();
	   			isSuccess = false;
	   		}
	   		if(isSuccess && resultList != null && resultList.size()>0){
	   			DataMap pMap = null; 
	   			DataMap nMap = null;
	   			DataMap jusoMap = null; 
	   			DataMap dMap = null; 
	   			
	   			paramMap.setString("toDay", fromDate);
	   			extDAO.deleteExtFdInfo(paramMap);
	   			for(DataMap map :resultList){
	   				pMap = new DataMap();
	   				pMap.setString("seq",map.getString("seq"));
	   				pMap.setString("viewType", Constant.VIEW_TYPE_D); // 화면 타입(포탈에서만 사용하는 변수)
	   				pMap.setString("reqUrl", propertiesService.getString("cultureinfoApi.fd.period.url"));
	   				pMap.setString("serviceKey", propertiesService.getString("cultureinfoApi.serviceKey"));
	   		   		
	   				jsonObj =cmmApiService.getCultureinfoApiList(pMap);
	   				jsonResponse = jsonObj.getJSONObject("response");
	   		   		jsonComMsgHeader = jsonResponse.getJSONObject("comMsgHeader");
		   		   	if("Y".equals(jsonComMsgHeader.get("SuccessYN").toString())){
			   			if(jsonResponse.has("msgBody")){
			   				jsonObjMsgBody = jsonResponse.getJSONObject("msgBody");
			   	   			if(jsonObjMsgBody.has("perforInfo")){
			   	   				dMap = CmmUtil.getMapFromJsonObject(jsonObjMsgBody.getJSONObject("perforInfo"));
			   	   			}else{
			   	   				msg = "[데이터 가공오류][상세정보]perforInfo 미존재";
			   	   				isSuccess = false;
			   	   				break;
			   	   			}
			   			}else{
			   				msg = "[데이터 가공오류][상세정보]msgBody 미존재";
			   				isSuccess = false;
			   				break;
			   			}
			   		}else{
			   			msg = jsonComMsgHeader.get("ErrMsg").toString();
			   			isSuccess = false;
			   			break;
			   		}
	   		   		nMap = new DataMap();
	   		   		nMap.setString("xy", map.getString("gpsX") +","+ map.getString("gpsY"));
	   		   		jusoMap = cmmApiService.getNaverApiReversegeocode(nMap);
	   		   		
	   		   		dMap.setString("seq", map.getString("seq")); 
	   		   		dMap.setString("gpsX", map.getString("gpsX")); 
	   		   		dMap.setString("gpsY", map.getString("gpsY")); 
	   		   		
		   		   	dMap.setString("sido", jusoMap.getString("sido")); 
		   		   	dMap.setString("sigugun", jusoMap.getString("sigugun")); 
		   		   	dMap.setString("dong", jusoMap.getString("dongmyun")); 
		   		   	dMap.setString("rest", jusoMap.getString("rest")); 
		   		   	
		   		   	//이미지 경로 및 파일에 공백이 들어있을경우 json파싱 후 문제생겨서 치환
		   		   	dMap.setString("imgUrl" , dMap.getString("imgUrl").replaceAll(" ","%20"));
		   		 
		   		   	//dMap.setString("fdInfoId", fdInfoIdGnrService.getNextStringId()); 
		   		   	extDAO.insertExtFdInfo(dMap);
		   		   	dataCnt++;
	   			}
	   		}else{
	   			msg = "[데이터 가공오류]가공된 데이터 size:0";
	   			isSuccess = false;
	   		}
		} catch (TransactionException ex1) {
			LOGGER.error("fail in insertExtFdInfo()", ex1);
			msg = dataLimit(ex1.toString());
			isSuccess = false;
		} catch (Exception ex2) {
			LOGGER.error("fail in insertExtFdInfo()", ex2);
			msg = dataLimit(ex2.toString());
			isSuccess = false;	
		}finally{
			schedulerHstMap.setString("schedulerMethodNm", "setExtFdInfo");
			schedulerHstMap.setString("schedulerNm", Constant.SCHEDULER_EXT_FD_INFO);
			schedulerHstMap.setString("strTm", startDt);
			schedulerHstMap.setString("endTm", DateUtil.getNowString(DateUtil.DATETIME_FORMAT));
			if(isSuccess){
				schedulerHstMap.setString("rstMsg", "등록성공");
			}else{
				schedulerHstMap.setString("rstMsg", "등록실패");
				schedulerHstMap.setString("etcCn", msg);
			}
			schedulerHstMap.setInt("dataCnt", dataCnt);
		}
		return schedulerHstMap;
    }
    @Override
    @SuppressWarnings("unchecked")
   	public DataMap insertExtGiaInfo(String contentTypeId) {

       	String startDt = DateUtil.getNowString(DateUtil.DATETIME_FORMAT);
       	DataMap schedulerHstMap = new DataMap();
   		boolean isSuccess = true, isUse = false, isRun = false;
   		String msg = "";
   		int dataCnt = 0;
   		
		DataMap paramMap = new DataMap();
		int len = Constant.GIA_COTENT_TYPE_ID_ARRAY.length;
		for(int i=0;i<len;i++){
			isRun = false;
			//LOGGER.debug("GIA_COTENT_TYPE_ID_ARRAY [" +i+ "]  : " +  Constant.GIA_COTENT_TYPE_ID_ARRAY[i]);
			if(!StringUtils.isBlank(contentTypeId)){
				if(Constant.GIA_COTENT_TYPE_ID_ARRAY[i].equals(contentTypeId)){
					paramMap.setString("contentTypeId", Constant.GIA_COTENT_TYPE_ID_ARRAY[i]); // api 호출용으로 사용(필수) 및 삭제시에 사용
					isUse = true;
					isRun = true;
				}
			}else{
				paramMap.setString("contentTypeId", Constant.GIA_COTENT_TYPE_ID_ARRAY[i]); //  api 호출용으로 사용(필수) 및 삭제시에 사용
				isRun = true;
			}
			if(isRun){
				try {
					DataMap rtnMap = cmmApiService.getGiaList(paramMap);
					if("0000".equals(rtnMap.getString("resultCode"))){
						List<DataMap> resultList = (List<DataMap>) rtnMap.get("resultList");
						if(resultList != null && resultList.size()>0){
							dataCnt = resultList.size();
							extDAO.deleteExtGiaInfo(paramMap); // 해당하는 것만 삭제
							// 삭제 후 저장
							for(DataMap map :resultList){
								map.setString("contenttypename", Constant.GIA_COTENT_TYPE_NAME_ARRAY[i]);
								if(!StringUtils.isBlank(map.getString("mapx")) && !StringUtils.isBlank(map.getString("mapy"))){
									paramMap.setString("xy", map.getString("mapx").trim()+ "," +map.getString("mapy").trim());
									DataMap nMap = cmmApiService.getNaverApiReversegeocode(paramMap);
									if(nMap.getInt("total") > 0){
										// 좌표로 주소변환
										map.setString("sidoname", nMap.getString("sido"));
										map.setString("sigunguname", nMap.getString("sigugun"));
										if(!StringUtils.isBlank(nMap.getString("sido"))){
											if(nMap.getString("sido").indexOf("세종") > -1){
												map.setString("sigunguname", "세종특별자치시");
											}
										}
										paramMap.remove("xy");
										// 좌표 및 카운트는 정상으로 가져오지만 주소를 못가져오는 경우
										// 주소로 재확인 후 시도, 시군구만 맵핑
										if(StringUtils.isBlank(nMap.getString("sido"))){
											if(!StringUtils.isBlank(map.getString("addr1"))){
												nMap = null;
												paramMap.setString("juso", map.getString("addr1").trim());
												nMap = cmmApiService.getNaverApiJuso(paramMap);
												if(nMap.getInt("total") > 0){
													map.setString("sidoname", nMap.getString("sido"));
													map.setString("sigunguname", nMap.getString("sigugun"));
													if(!StringUtils.isBlank(nMap.getString("sido"))){
														if(nMap.getString("sido").indexOf("세종") > -1){
															map.setString("sigunguname", "세종특별자치시");
														}
													}
												}
												paramMap.remove("juso");
											}
										}
									}else{
										// 주소 변환이 안됐을 경우 : 좌표가 네이버 맵에 찍지 못하는 좌표일 가능성이 크다.
										// 시도, 시군구 변환 등록 및 좌표를 찍기 위해 재정의
										if(!StringUtils.isBlank(map.getString("addr1"))){
											nMap = null;
											paramMap.setString("juso", map.getString("addr1").trim());
											nMap = cmmApiService.getNaverApiJuso(paramMap);
											if(nMap.getInt("total") > 0){
												map.setString("mapx", nMap.getString("point_x"));
												map.setString("mapy", nMap.getString("point_y"));
												map.setString("sidoname", nMap.getString("sido"));
												map.setString("sigunguname", nMap.getString("sigugun"));
												if(!StringUtils.isBlank(nMap.getString("sido"))){
													if(nMap.getString("sido").indexOf("세종") > -1){
														map.setString("sigunguname", "세종특별자치시");
													}
												}
											}
											paramMap.remove("juso");
										}
									}
								}else{
									// api에 좌표가 없는경우 : 주소로 시도, 시군구 변환 및 좌표 획득
									if(!StringUtils.isBlank(map.getString("addr1"))){
										paramMap.setString("juso", map.getString("addr1").trim());
										DataMap nMap = cmmApiService.getNaverApiJuso(paramMap);
										if(nMap.getInt("total") > 0){
											map.setString("mapx", nMap.getString("point_x"));
											map.setString("mapy", nMap.getString("point_y"));
											map.setString("sidoname", nMap.getString("sido"));
											map.setString("sigunguname", nMap.getString("sigugun"));
											if(!StringUtils.isBlank(nMap.getString("sido"))){
												if(nMap.getString("sido").indexOf("세종") > -1){
													map.setString("sigunguname", "세종특별자치시");
												}
											}
										}
										paramMap.remove("juso");
									}
								}
								
								map.setString("cat1nm", this.getGiaCat1Name(map.getString("cat1"))); // 카테고리1 명
								map.setString("cat2nm", this.getGiaCat2Name(map.getString("cat2"))); // 카테고리2 명
								map.setString("cat3nm", this.getGiaCat3Name(map.getString("cat3"))); // 카테고리3 명
								// 지역명(축약)
								map.setString("area", CmmUtil.getAisSidoName(map.getString("sidoname")));
								
								// 이미지 URL https로 저장
								map.setString("firstimage", CmmUtil.changeSchemeToHttps(map.getString("firstimage")));
								map.setString("firstimage2", CmmUtil.changeSchemeToHttps(map.getString("firstimage2")));
								
								
								//map.setString("giaId", giaInfoIdGnrService.getNextStringId());  // key
								
								LOGGER.debug("insertExtGiaInfo() before insert... map={}", map);
								extDAO.insertExtGiaInfo(map);
							}
						}else{
							msg = "[데이터 가공오류]가공된 데이터 size:0";
							isSuccess = false;
						}
					}else{
						msg = rtnMap.getString("resultMsg");
						isSuccess = false;
					}
				} catch (TransactionException ex1) {
		   			LOGGER.error("fail in insertExtFdInfo() [" +Constant.GIA_COTENT_TYPE_NAME_ARRAY[i]+ "]", ex1);
		   			msg = dataLimit(ex1.toString());
		   			isSuccess = false;
		   		} catch (Exception ex2) {
		   			LOGGER.error("fail in insertExtFdInfo() [" +Constant.GIA_COTENT_TYPE_NAME_ARRAY[i]+ "]", ex2);
		   			msg = dataLimit(ex2.toString());
		   			isSuccess = false;
		   		}finally{
		   			schedulerHstMap.setString("schedulerMethodNm", "setExtGiaInfo");
		   			schedulerHstMap.setString("schedulerNm", Constant.SCHEDULER_EXT_GIA_INFO + "["+ Constant.GIA_COTENT_TYPE_NAME_ARRAY[i] +"]");
		   			schedulerHstMap.setString("strTm", startDt);
		   			schedulerHstMap.setString("endTm", DateUtil.getNowString(DateUtil.DATETIME_FORMAT));
		   			if(isSuccess){
		   				schedulerHstMap.setString("rstMsg", "등록성공");
		   			}else{
		   				schedulerHstMap.setString("rstMsg", "등록실패");
		   				schedulerHstMap.setString("etcCn", msg);
		   			}
		   			schedulerHstMap.setInt("dataCnt", dataCnt);
		   			
		   			try {
		   				cmmService.insertCmmSchedulerHst(schedulerHstMap);
		   			} catch (EgovBizException ex1) {
		   				LOGGER.error("fail in insertCmmSchedulerHst()", ex1);
		   				break;
		   			} catch (Exception ex2) {
		   				LOGGER.error("fail in insertCmmSchedulerHst()", ex2);
		   				break;
		   			}
		   			dataCnt = 0;
		   		}
				paramMap.clear(); // 파람 초기화
			}
			if(isUse) break;
		}
		if(isSuccess){
			if(!isUse){
				schedulerHstMap.setString("schedulerNm", Constant.SCHEDULER_EXT_GIA_INFO);
			}
		}
		
		return schedulerHstMap;
    }
    private String getGiaCat1Name(String cat1){
    	if(StringUtils.isBlank(cat1)) return "";
    	String nm  = "";
    	if(giaCat1List == null){
    		CmmCodeVO vo = null;
    		giaCat1List = new ArrayList<CmmCodeVO>();
			vo = new CmmCodeVO();
			vo.setCode("A01");vo.setCodeNm("자연");giaCat1List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02");vo.setCodeNm("인문(문화/예술/역사)");giaCat1List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C01");vo.setCodeNm("추천코스");giaCat1List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03");vo.setCodeNm("레포츠");giaCat1List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02");vo.setCodeNm("숙박");giaCat1List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04");vo.setCodeNm("쇼핑");giaCat1List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05");vo.setCodeNm("음식");giaCat1List.add(vo);
    	}
    	for(CmmCodeVO vo :giaCat1List){
    		if(vo.getCode().equals(cat1.trim())){
    			nm = vo.getCodeNm();
    			break;
    		}
    	}
    	return nm;
    }
    private String getGiaCat2Name(String cat2){
    	if(StringUtils.isBlank(cat2)) return "";
    	String nm  = "";
    	if(giaCat2List == null){
    		CmmCodeVO vo = null;
    		giaCat2List = new ArrayList<CmmCodeVO>();
			vo = new CmmCodeVO();
			vo.setCode("A0101");vo.setCodeNm("자연관광지");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0102");vo.setCodeNm("관광자원");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0201");vo.setCodeNm("역사관광지");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0202");vo.setCodeNm("휴양관광지");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0203");vo.setCodeNm("체험관광지");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0204");vo.setCodeNm("산업관광지");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0205");vo.setCodeNm("건축/조형물");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0206");vo.setCodeNm("문화시설");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0207");vo.setCodeNm("축제");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0208");vo.setCodeNm("공연/행사");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C0112");vo.setCodeNm("가족코스");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C0113");vo.setCodeNm("나홀로코스");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C0114");vo.setCodeNm("힐링코스");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C0115");vo.setCodeNm("도보코스");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C0116");vo.setCodeNm("캠핑코스");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C0117");vo.setCodeNm("맛코스");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0301");vo.setCodeNm("레포츠소개");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0302");vo.setCodeNm("육상 레포츠");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0303");vo.setCodeNm("수상 레포츠");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0304");vo.setCodeNm("항공 레포츠");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0305");vo.setCodeNm("복합 레포츠");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B0201");vo.setCodeNm("숙박시설");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0401");vo.setCodeNm("쇼핑");giaCat2List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0502");vo.setCodeNm("음식점");giaCat2List.add(vo);
    	}
    	for(CmmCodeVO vo :giaCat2List){
    		if(vo.getCode().equals(cat2.trim())){
    			nm = vo.getCodeNm();
    			break;
    		}
    	}
    	return nm;
    }
    private String getGiaCat3Name(String cat3){
    	if(StringUtils.isBlank(cat3)) return "";
    	String nm  = "";
    	if(giaCat3List == null){
    		CmmCodeVO vo = null;
    		giaCat3List = new ArrayList<CmmCodeVO>();
			vo = new CmmCodeVO();
			vo.setCode("A01010100");vo.setCodeNm("국립공원");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01010200");vo.setCodeNm("도립공원");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01010300");vo.setCodeNm("군립공원");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01010400");vo.setCodeNm("산");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01010500");vo.setCodeNm("자연생태관광지");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01010600");vo.setCodeNm("자연휴양림");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01010700");vo.setCodeNm("수목원");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01010800");vo.setCodeNm("폭포");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01010900");vo.setCodeNm("계곡");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011000");vo.setCodeNm("약수터");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011100");vo.setCodeNm("해안절경");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011200");vo.setCodeNm("해수욕장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011300");vo.setCodeNm("섬");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011400");vo.setCodeNm("항구/포구");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011500");vo.setCodeNm("어촌");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011600");vo.setCodeNm("등대");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011700");vo.setCodeNm("호수");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011800");vo.setCodeNm("강");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01011900");vo.setCodeNm("동굴");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01020100");vo.setCodeNm("희귀동.식물");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A01020200");vo.setCodeNm("기암괴석");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02010100");vo.setCodeNm("고궁");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02010200");vo.setCodeNm("성");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02010300");vo.setCodeNm("문");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02010400");vo.setCodeNm("고택");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02010500");vo.setCodeNm("생가");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02010600");vo.setCodeNm("민속마을");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02010700");vo.setCodeNm("유적지/사적지");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02010800");vo.setCodeNm("사찰");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02010900");vo.setCodeNm("종교성지");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02011000");vo.setCodeNm("안보관광");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02020100");vo.setCodeNm("유원지");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02020200");vo.setCodeNm("관광단지");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02020300");vo.setCodeNm("온천/욕장/스파");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02020400");vo.setCodeNm("이색찜질방");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02020500");vo.setCodeNm("헬스투어");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02020600");vo.setCodeNm("테마공원");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02020700");vo.setCodeNm("공원");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02020800");vo.setCodeNm("유람선/잠수함관광");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02030100");vo.setCodeNm("농.산.어촌 체험");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02030200");vo.setCodeNm("전통체험");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02030300");vo.setCodeNm("산사체험");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02030400");vo.setCodeNm("이색체험");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02030500");vo.setCodeNm("관광농원");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02030600");vo.setCodeNm("이색거리");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A0204010");vo.setCodeNm("제철소");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02040200");vo.setCodeNm("조선소");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02040300");vo.setCodeNm("공단");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02040400");vo.setCodeNm("발전소");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02040500");vo.setCodeNm("광산");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02040600");vo.setCodeNm("식음료");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02040700");vo.setCodeNm("화학/금속");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02040800");vo.setCodeNm("기타");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02040900");vo.setCodeNm("전자/반도체");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02041000");vo.setCodeNm("자동차");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02050100");vo.setCodeNm("다리/대교");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02050200");vo.setCodeNm("기념탑/기념비/전망대");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02050300");vo.setCodeNm("분수");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02050400");vo.setCodeNm("동상");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02050500");vo.setCodeNm("터널");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02050600");vo.setCodeNm("유명건물");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02060100");vo.setCodeNm("박물관");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02060200");vo.setCodeNm("기념관");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02060300");vo.setCodeNm("전시관");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02060400");vo.setCodeNm("컨벤션센터");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02060500");vo.setCodeNm("미술관/화랑");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02060600");vo.setCodeNm("공연장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02060700");vo.setCodeNm("문화원");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02060800");vo.setCodeNm("외국문화원");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02060900");vo.setCodeNm("도서관");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02061000");vo.setCodeNm("대형서점");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02061100");vo.setCodeNm("문화전수시설");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02061200");vo.setCodeNm("영화관");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02061300");vo.setCodeNm("어학당");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02061400");vo.setCodeNm("학교");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02070100");vo.setCodeNm("문화관광축제");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02070200");vo.setCodeNm("일반축제");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02080100");vo.setCodeNm("전통공연");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02080200");vo.setCodeNm("연극");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02080300");vo.setCodeNm("뮤지컬");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02080400");vo.setCodeNm("오페라");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02080500");vo.setCodeNm("전시회");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02080600");vo.setCodeNm("박람회");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02080700");vo.setCodeNm("컨벤션");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02080800");vo.setCodeNm("무용");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02080900");vo.setCodeNm("클래식음악회");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02081000");vo.setCodeNm("대중콘서트");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02081100");vo.setCodeNm("영화");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02081200");vo.setCodeNm("스포츠경기");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A02081300");vo.setCodeNm("기타행사");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C01120001");vo.setCodeNm("가족코스");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C01130001");vo.setCodeNm("나홀로코스");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C01140001");vo.setCodeNm("힐링코스");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C01150001");vo.setCodeNm("도보코스");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C01160001");vo.setCodeNm("캠핑코스");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("C01170001");vo.setCodeNm("맛코스");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03010100");vo.setCodeNm("육상레포츠");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03010200");vo.setCodeNm("수상레포츠");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03010300");vo.setCodeNm("항공레포츠");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03020100");vo.setCodeNm("스포츠센터");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03020200");vo.setCodeNm("수련시설");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03020300");vo.setCodeNm("경기장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03020400");vo.setCodeNm("인라인(실내 인라인 포함)");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03020500");vo.setCodeNm("자전거하이킹");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03020600");vo.setCodeNm("카트");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03020700");vo.setCodeNm("골프");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03020800");vo.setCodeNm("경마");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03020900");vo.setCodeNm("경륜");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021000");vo.setCodeNm("카지노");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021100");vo.setCodeNm("승마");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021200");vo.setCodeNm("스키/스노보드");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021300");vo.setCodeNm("스케이트");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021400");vo.setCodeNm("썰매장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021500");vo.setCodeNm("수렵장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021600");vo.setCodeNm("사격장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021700");vo.setCodeNm("야영장,오토캠핑장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021800");vo.setCodeNm("암벽등반");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03021900");vo.setCodeNm("빙벽등반");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03022000");vo.setCodeNm("서바이벌게임");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03022100");vo.setCodeNm("ATV");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03022200");vo.setCodeNm("MTB");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03022300");vo.setCodeNm("오프로드");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03022400");vo.setCodeNm("번지점프");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03022500");vo.setCodeNm("자동차경주");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03022600");vo.setCodeNm("스키(보드) 렌탈샵");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03022700");vo.setCodeNm("트래킹");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03030100");vo.setCodeNm("윈드서핑/제트스키");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03030200");vo.setCodeNm("카약/카누");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03030300");vo.setCodeNm("요트");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03030400");vo.setCodeNm("스노쿨링/스킨스쿠버다이빙");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03030500");vo.setCodeNm("민물낚시");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03030600");vo.setCodeNm("바다낚시");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03030700");vo.setCodeNm("수영");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03030800");vo.setCodeNm("래프팅");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03040100");vo.setCodeNm("스카이다이빙");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03040200");vo.setCodeNm("초경량비행");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03040300");vo.setCodeNm("헹글라이딩/패러글라이딩");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03040400");vo.setCodeNm("열기구");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A03050100");vo.setCodeNm("복합 레포츠");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02010100");vo.setCodeNm("관광호텔");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02010200");vo.setCodeNm("수상관광호텔");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02010300");vo.setCodeNm("전통호텔");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02010400");vo.setCodeNm("가족호텔");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02010500");vo.setCodeNm("콘도미니엄");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02010600");vo.setCodeNm("유스호스텔");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02010700");vo.setCodeNm("펜션");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02010800");vo.setCodeNm("여관");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02010900");vo.setCodeNm("모텔");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02011000");vo.setCodeNm("민박");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02011100");vo.setCodeNm("게스트하우스");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02011200");vo.setCodeNm("홈스테이");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02011300");vo.setCodeNm("서비스드레지던스");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02011400");vo.setCodeNm("의료관광호텔");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02011500");vo.setCodeNm("소형호텔");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("B02011600");vo.setCodeNm("한옥스테이");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04010100");vo.setCodeNm("5일장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04010200");vo.setCodeNm("상설시장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04010300");vo.setCodeNm("백화점");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04010400");vo.setCodeNm("면세점");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04010500");vo.setCodeNm("할인매장");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04010600");vo.setCodeNm("전문상가");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04010700");vo.setCodeNm("공예,공방");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04010800");vo.setCodeNm("관광기념품점");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A04010900");vo.setCodeNm("특산물판매점");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05020100");vo.setCodeNm("한식");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05020200");vo.setCodeNm("서양식");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05020300");vo.setCodeNm("일식");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05020400");vo.setCodeNm("중식");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05020500");vo.setCodeNm("아시아식");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05020600");vo.setCodeNm("패밀리레스토랑");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05020700");vo.setCodeNm("이색음식점");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05020800");vo.setCodeNm("채식전문점");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05020900");vo.setCodeNm("바/까페");giaCat3List.add(vo);
			vo = new CmmCodeVO();
			vo.setCode("A05021000");vo.setCodeNm("클럽");giaCat3List.add(vo);
    	}
    	for(CmmCodeVO vo :giaCat3List){
    		if(vo.getCode().equals(cat3.trim())){
    			nm = vo.getCodeNm();
    			break;
    		}
    	}
    	return nm;
    }
    
    @Override
    @Transactional
    public DataMap insertMediaHstInfo() {
    	
    	String yyyy =  DateUtil.getNowString(DateUtil.DATEYEAR_FORMAT);
		String startDt = DateUtil.getNowString(DateUtil.DATETIME_FORMAT);
		
		String month = DateUtil.getNowString(DateUtil.DATEMONTH_ONLY_FORMAT);
		
		if(month.equals("01")){
			yyyy =  DateUtil.getAddYearFormat(-1,DateUtil.DATEYEAR_FORMAT);
		}
		
		DataMap schedulerHstMap = new DataMap();
		boolean isSuccess = false;
		String msg = "";
		int dataCnt = 0;
		
		try {
			DataMap paramMap = new DataMap();
			paramMap.put("yyyy", yyyy);
			
			List<DataMap> resultList =  extDAO.selectActivityMediaList(paramMap);
			
			if(resultList.size()> 0){
				extDAO.deleteMediaHstInfo(paramMap); 
				for(DataMap map :  resultList){
					extDAO.insertMediaHstInfo(map); 
					dataCnt++;
				}
				isSuccess = true;
			}else{
				msg = "[데이터 가공오류]가공된 데이터 size:0";
			}
		} catch (TransactionException ex1) {
			LOGGER.error("fail in insertMediaHstInfo()", ex1);
			msg = dataLimit(ex1.toString());
		} catch (Exception ex2) {
			LOGGER.error("fail in insertMediaHstInfo()", ex2);
			msg = dataLimit(ex2.toString());
		} finally{
			schedulerHstMap.setString("schedulerMethodNm", "setMediaHstInfo");
			schedulerHstMap.setString("schedulerNm", Constant.SCHEDULER_MEDIA_HST_INFO);
			schedulerHstMap.setString("strTm", startDt);
			schedulerHstMap.setString("endTm", DateUtil.getNowString(DateUtil.DATETIME_FORMAT));
			if(isSuccess){
				schedulerHstMap.setString("rstMsg", "등록성공");
			}else{
				schedulerHstMap.setString("rstMsg", "등록실패");
				schedulerHstMap.setString("etcCn", msg);
			}
			schedulerHstMap.setInt("dataCnt", dataCnt);
			
			try {
				cmmService.insertCmmSchedulerHst(schedulerHstMap);
			} catch (EgovBizException ex1) {
				LOGGER.error("fail in insertCmmSchedulerHst()", ex1);
			} catch (Exception ex2) {
				LOGGER.error("fail in insertCmmSchedulerHst()", ex2);
			}
			
		}
		
		return schedulerHstMap;
	}
    private String dataLimit(String data){
    	String rtnStr = "";
    	if(CmmUtil.checkMaxbyte(data , 1000)){
    		rtnStr = data.substring(0 , 1000);
    	}else{
    		rtnStr = data;
    	}
    	return rtnStr;
    }
	
}
