/** 
 * CommonService.java
 * @author 임새미 
 * @since 2016. 10. 13.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 13. 	    임새미				       최초생성
 * 
 */
package kcg.faics.cmmn.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import kcg.faics.cmmn.service.CodeService;
import kcg.faics.cmmn.vo.CodeVO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 코드 관련 비즈니스 로직.
 * 
 * @author kimnomin
 *
 */
@Service("codeService")
public class CodeServiceImpl implements CodeService {

	/**
	 * CodeMapper.
	 */
	@Resource(name = "codeMapper")
	private CodeMapper codeMapper;

	/**
	 * List 객체 첫번째 인덱스에 "전체" 항목을 추가하여 반환한다.
	 * 
	 * @param list List 객체
	 * @return "전체" 항목이 추가된 List 객체
	 */
	private List<CodeVO> addAll(final List<CodeVO> list) {
		if (list != null) {
			CodeVO allVO = new CodeVO();
			allVO.setCode1(LIST_ALL_VAL);
			allVO.setCode2(LIST_ALL_VAL);
			allVO.setCodenm(LIST_ALL_STR);
			allVO.setCodenmYak(LIST_ALL_STR);
			list.add(0, allVO);
		}
		return list; 
	}
	
	@Override
	public CodeVO getCodeValue(final String code1, final String code2) throws Exception {
		CodeVO codeVO;
		List<CodeVO> codes = getCodeValues(code1, code2);
		if (codes.size() <= 0) {
			codeVO = null;
		} else {
			codeVO = codes.get(0);
		}
		return codeVO;
	}

	@Override
	public List<CodeVO> getCodeValues(final String code1, final String code2) throws Exception {

		List<CodeVO> codeVOList = new ArrayList<CodeVO>();

		try {
			codeVOList = codeMapper.getCodeValues(code1, code2);
		} catch (Exception e) {
			e.printStackTrace();
			codeVOList = null;
		}

		return codeVOList;
	}

	@Override
	public List<CodeVO> getPlace1List(final boolean onlyCentral) throws Exception {
		List<CodeVO> codeVOList = new ArrayList<CodeVO>();

		try {
			codeVOList = codeMapper.getPlace1List(onlyCentral);
		} catch (Exception e) {
			e.printStackTrace();
			codeVOList = null;
		}

		return codeVOList;
	}
	
	@Override
	public List<CodeVO> getPlace1List(final boolean onlyCentral, final boolean includeAll)
			throws Exception {
		List<CodeVO> codeVOList = getPlace1List(onlyCentral);
		if (includeAll) {
			codeVOList = addAll(codeVOList);
		}
		return codeVOList;
	}

	@Override
	public List<CodeVO> getPlace1List(final String[] codes) {
		List<CodeVO> codeVOList = new ArrayList<CodeVO>();
		List<String> policeStationList = Arrays.asList(CodeService.POLICE_STATION_LIST);
		
		boolean isContains = false;
		try {
			codeVOList = codeMapper.getPlace1List(false);
			for (Iterator<CodeVO> it = codeVOList.iterator(); it.hasNext(); ) {
				isContains = false;
				CodeVO vo = it.next();

				for (String item : policeStationList) {
					if (item.equalsIgnoreCase(vo.getCode2())) {
						isContains = true;
						break;
					}
				}
				
				if (!isContains) {
					it.remove();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			codeVOList = null;
		}


		
		return codeVOList;
	}

	@Override
	public List<CodeVO> getPlace1List(final String[] codes, final boolean includeAll)
			throws Exception {
		List<CodeVO> codeVOList = getPlace1List(codes);
		
		if (includeAll) {
			codeVOList = addAll(codeVOList);
		}
		
		return codeVOList;
	}

	@Override
	public List<CodeVO> getPlace1SubList(final String place1Code) throws Exception {
		CodeVO code = new CodeVO();
		List<CodeVO> codes = new ArrayList<CodeVO>();
		try {
			String[] subCodes = PLACE_CATEGORY.get(place1Code);
 			if (subCodes != null) {
				for (int i = 0; i < subCodes.length; i++) {
					code = getCodeValue(CODE1_PLACE1, subCodes[i]);
					codes.add(code);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			codes = null;
		}
		
		return codes;
	}
	
	@Override
	public List<CodeVO> getPlace1SubListWithMain(final String place1Code) throws Exception {
		CodeVO code = new CodeVO();
		List<CodeVO> codes = new ArrayList<CodeVO>();
		try {
			String[] subCodes = PLACE_CATEGORY.get(place1Code);
 			if (subCodes != null) {
 				code = getCodeValue(CODE1_PLACE1, place1Code);
 				codes.add(code);
 				
 				// 하위 기관
				for (int i = 0; i < subCodes.length; i++) {
					code = getCodeValue(CODE1_PLACE1, subCodes[i]);
					codes.add(code);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			codes = null;
		}
		
		return codes;
	}
	
	@Override
	public String getPlace1HighCode(final String place1Code) throws Exception {
		for (Map.Entry<String, String[]> entry : PLACE_CATEGORY.entrySet()) {
		    String key = entry.getKey();
		    String[] values = entry.getValue();

		    for (String item : values) {
				if (item.equalsIgnoreCase(place1Code)) {
					return key;
				}
			}
		}
		return place1Code;
	}

	@Override
	public String getPlace1Str(final String placeCode) throws Exception {
		String retVal = "";
		if (StringUtils.isBlank(placeCode)) {
			retVal = "";
		} else {
			CodeVO codeVO = getCodeValue(CODE1_PLACE1, placeCode);
			retVal = codeVO.getCodenm();
		}
		return retVal;
	}

	@Override
	public String getPlace2Str(final String place1Code, final String place2Code)
			throws Exception {
		String retVal = "";
		if (StringUtils.isBlank(place1Code) || StringUtils.isBlank(place2Code)) {
			retVal = "";
		} else {
			CodeVO codeVO = getCodeValue(place1Code, place2Code);
			retVal = codeVO.getCodenm();
		}
		return retVal;
	}

	@Override
	public String getPositionStr(final String positionCode) throws Exception {
		String retVal = "";
		if (StringUtils.isBlank(positionCode)) {
			retVal = "";
		} else {
			CodeVO codeVO = getCodeValue(CODE1_POSITION, positionCode);
			retVal = codeVO.getCodenm();
		}
		return retVal;
	}

	/**
	 * 계급 코드 리스트를 반환한다.
	 * @return 계급 코드 리스트
	 * @throws Exception
	 */
	@Override
	public List<CodeVO> getPositionList() throws Exception {
		return codeMapper.getPositionList();
	};
	
	/**
	 * 하위소속 코드 리스트를 반환한다.
	 * @param codeVO 상위소속 코드
	 * @return 소속 코드 리스트
	 * @throws Exception
	 */
	@Override
	public List<CodeVO> getPlace2List(final CodeVO codeVO) throws Exception {
		return codeMapper.getPlace2List(codeVO);
	};	

	/**
	 * 세부소속 코드 리스트를 반환한다.
	 * @param codeVO 상위소속 코드
	 * @return 소속 코드 리스트
	 * @throws Exception
	 */
	@Override
	public List<CodeVO> getPlace3List(final CodeVO codeVO) throws Exception {
		return codeMapper.getPlace3List(codeVO);
	};	
	
	/**
	 * 직위 코드 리스트를 반환한다.
	 * @return 직위 코드 리스트
	 * @throws Exception
	 */
	@Override
	public List<CodeVO> getDutiesList() throws Exception {
		return codeMapper.getDutiesList();
	};	
	
	/**
	 * 직별 코드 리스트를 반환한다.
	 * @return 직별 코드 리스트
	 * @throws Exception
	 */
	@Override
	public List<CodeVO> getJobTypeList() throws Exception {
		return codeMapper.getJobTypeList();
	};	
	
	/**
	 * 학력 코드 리스트를 반환한다.
	 * @return 학력 코드 리스트
	 * @throws Exception
	 */
	@Override
	public List<CodeVO> getScholarshipList() throws Exception {
		return codeMapper.getScholarshipList();
	};	
	
	@Override
	public List<CodeVO> getCorpOrgList() throws Exception {
		return getCodeValues(CODE1_COPR, null);  
	}
	
	@Override
	public List<CodeVO> getEduCourse(final boolean includeAll) throws Exception {
		List<CodeVO> list = getCodeValues(CODE1_EDU_COURSE, null);
		if (includeAll) {
			list = addAll(list);
		}
		return list;
	}

	@Override
	public List<CodeVO> getEduPlace(final boolean includeAll) throws Exception {
		List<CodeVO> list = getCodeValues(CODE1_EDU_PLACE, null);
		if (includeAll) {
			list = addAll(list);
		}
		return list;
	}

	@Override
	public List<CodeVO> getAuthList() throws Exception {
		return getCodeValues(CODE1_AUTH, null);
	}

	/**
	 * code를 수정한다.
	 * 
	 * @param codeVO CodeVO 객체
	 * @return 성공 - 1, 실패 - 0
	 * @throws Exception 기본 예외 처리
	 */
	@Override
	public int updateCode(final CodeVO codeVO) throws Exception {
		return codeMapper.updateCode(codeVO);
	}

	/**
	 * code를 추가한다.
	 * 
	 * @param codeVO CodeVO 객체
	 * @return 성공 - 1, 실패 - 0
	 * @throws Exception 기본 예외 처리
	 */
	@Override
	public int insertCode(final CodeVO codeVO) throws Exception {
		return codeMapper.insertCode(codeVO);
	}
	
	/**
	 * code를 삭제한다.
	 * 
	 * @param codeVO CodeVO 객체
	 * @return 성공 - 1, 실패 - 0
	 * @throws Exception 기본 예외 처리
	 */
	@Transactional
	@Override
	public int deleteCode(final CodeVO codeVO) throws Exception {
		return codeMapper.deleteCode(codeVO);
	}
}
