package cmm.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cmm.service.CmmAmsAssetMediaVO;
import cmm.service.CmmCouncilInfoVO;
import cmm.service.CmmPmsMapContentsService;
import cmm.service.CmmPmsMapContentsVO;
import cmm.util.DataMap;
import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;
import egovframework.rte.fdl.cmmn.exception.EgovBizException;


/**  
 * 맵 콘텐츠에 대한 서비스 클래스를 구현한다
 * @Class Name : CmmPmsMapContentsServiceImpl.java
 * @author ecplaza 
 * @since 2018-06-26
 * @version 1.0
 */
@Service("cmmPmsMapContentsService")
public class CmmPmsMapContentsServiceImpl extends EgovAbstractServiceImpl implements CmmPmsMapContentsService {

	/** LOGGER */
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(CmmPmsMapContentsServiceImpl.class);
	
	/** cmmPmsMapContentsDAO */
    @Resource(name="cmmPmsMapContentsDAO")
    private CmmPmsMapContentsDAO cmmPmsMapContentsDAO;
    
    @Override
	public int selectCmmPmsMapCntnsStoryCnt(CmmPmsMapContentsVO cmmPmsMapContentsVO) throws EgovBizException {
		return cmmPmsMapContentsDAO.selectCmmPmsMapCntnsStoryCnt(cmmPmsMapContentsVO);
	}
    
    @Override
   	public int selectCmmPmsMapLocalCultureCnt(CmmPmsMapContentsVO cmmPmsMapContentsVO) throws EgovBizException {
   		return cmmPmsMapContentsDAO.selectCmmPmsMapLocalCultureCnt(cmmPmsMapContentsVO);
   	}
    
    @Override
  	public List<CmmCouncilInfoVO> selectCmmPmsMapAoiCouncilInfoList(CmmPmsMapContentsVO cmmPmsMapContentsVO) throws EgovBizException{
  		return (List<CmmCouncilInfoVO>)cmmPmsMapContentsDAO.selectCmmPmsMapAoiCouncilInfoList(cmmPmsMapContentsVO);
  	}
    @Override
	public List<CmmPmsMapContentsVO> selectCmmPmsMapContentsList(CmmPmsMapContentsVO cmmPmsMapContentsVO) throws EgovBizException{
		List<CmmPmsMapContentsVO> cmmPmsMapContentsList = (List<CmmPmsMapContentsVO>)cmmPmsMapContentsDAO.selectCmmPmsMapContentsList(cmmPmsMapContentsVO);
		
		/*
		 * 쿼리에서 문화원명 구하도록 수정되어 주석처리 (2020.02.06)
		 * 
		// COUNTIL_CD 값이 '||' 로 연결되어 있는 경우 각각의 문화원명 구하기 (2020.02.05)
		DataMap councilData = new DataMap(); // 문화원코드-문화원명 맵 (불필요한 DB 호출을 방지하기 위하여 사용)
		for (int i=0; cmmPmsMapContentsList != null && i<cmmPmsMapContentsList.size(); i++) {
			CmmPmsMapContentsVO vo = cmmPmsMapContentsList.get(i);
			String councilCd = vo.getCouncilCd();
			String councilNm = vo.getCouncilNm();
			if (StringUtils.isNotEmpty(councilCd) && councilCd.indexOf("||") > 0) {
				boolean isNumberDiff = true;
				String[] cdArr = councilCd.split("\\|\\|"); // 문화원 코드 배열
				if (StringUtils.isNotEmpty(councilNm)) {
					String[] nmArr = councilNm.split(","); // 문화원명 배열
					if (cdArr.length == nmArr.length) { // 문화원 코드 개수와 문화원명 개수가 동일한 경우 (쿼리에서 문화원명 구한 경우)
						isNumberDiff = false;
					}
				}
				if (isNumberDiff) {
					councilNm = "";
					for (int j=0; cdArr!=null && j<cdArr.length; j++) {
						String currCouncilCd = cdArr[j];
						if (StringUtils.isEmpty(councilData.getString(currCouncilCd))) { // councilData 맵에 문화원 정보 없을 경우에만 DB에서 조회
							String currCouncilNm = cmmPmsMapContentsDAO.selectCouncilNm(currCouncilCd);
							LOGGER.info("selectCmmPmsMapContentsList() selectCouncilNm... currCouncilCd={}, currCouncilNm={}", currCouncilCd, currCouncilNm);
							councilData.setString(currCouncilCd, currCouncilNm);
						}
						
						if (j > 0) councilNm += ",";
						councilNm += councilData.getString(currCouncilCd);
					}
				}
				LOGGER.info("selectCmmPmsMapContentsList() i={}, cdArr={}, councilNm={}",i,  cdArr==null ? cdArr : Arrays.toString(cdArr), councilNm);
				vo.setCouncilNm(councilNm);
			}
		}
		 */
		
		return cmmPmsMapContentsList;
	}
    
    @Override
	public List<CmmPmsMapContentsVO> selectCmmPmsMapContentsSearchList(CmmPmsMapContentsVO cmmPmsMapContentsVO) throws EgovBizException{
		List<CmmPmsMapContentsVO> cmmPmsMapContentsList = (List<CmmPmsMapContentsVO>)cmmPmsMapContentsDAO.selectCmmPmsMapContentsSearchList(cmmPmsMapContentsVO);
		return cmmPmsMapContentsList;
	}
}
