package kcg.faics.tg.service.impl;

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

import javax.annotation.Resource;

import kcg.faics.cmmn.egov.file.EgovFileMngUtil;
import kcg.faics.cmmn.service.CodeService;
import kcg.faics.tg.service.CorpService;
import kcg.faics.tg.vo.CorpSearchVO;
import kcg.faics.tg.vo.CorpVO;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;

/**
 * 치안대상 관련 비지니스로직.
 * 
 * @author kimnomin
 *
 */
@Service("corpService")
public class CorpServiceImpl extends EgovAbstractServiceImpl implements CorpService {
    /**
	 * 전자정부프레임워크에서 제공되는 파일 업로드 처리 관련 Utility.
	 **/
	@Resource(name = "EgovFileMngUtil")
	private EgovFileMngUtil fileUtil;	
	
	/**
	 * 치안대상 Mapper 클래스.
	 */
	@Resource(name = "corpMapper")
	private CorpMapper corpMapper;
	
	
	/**
	 * orgNo의 최대값을 반환한다.
	 * 
	 * @param corpVO
	 *            치안대상 VO
	 * @return orgNo 최대값
	 * @throws Exception
	 *             기본 예외 처리
	 */
	private int selectNextOrgNo(final CorpVO corpVO) throws Exception {
		int result = 1;
		try {
			result = corpMapper.selectMaxOrgNo(corpVO) + 1; 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result; 
	}
	
	@Override
	public CorpVO select(final CorpVO dataVO) throws Exception {
		return corpMapper.select(dataVO);
	}

	@Override
	public List<CorpVO> selectListAll(final CorpSearchVO searchVO) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CorpVO> selectList(final CorpSearchVO searchVO) throws Exception {
		if (CodeService.LIST_ALL_VAL.equalsIgnoreCase(searchVO.getPlace1())) {
			searchVO.setPlace1("");
		}
		return corpMapper.selectList(searchVO);
	}

	@Override
	public int selectListCnt(final CorpSearchVO searchVO) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int selectTotalCnt(final CorpSearchVO searchVO) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public HashMap<String, Object> insert(final CorpVO dataVO,
			final Map<String, MultipartFile> fileMap) throws Exception {
		int nextOrgNo = selectNextOrgNo(dataVO);
		dataVO.setOrgNo(nextOrgNo);
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("result", corpMapper.insert(dataVO));
		
		return map;
	}

	@Override
	public HashMap<String, Object> update(final CorpVO dataVO,
			final Map<String, MultipartFile> fileMap, final String[] deleteFiles)
			throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("result", corpMapper.update(dataVO));
		
		return map;
	}

	@Override
	public HashMap<String, Object> delete(final CorpVO dataVO) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("result", corpMapper.delete(dataVO));
		
		return map;
	}

	@Override
	public List<HashMap<String, Object>> selectStats() throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("placeList", CodeService.POLICE_STATION_LIST);
		
		return corpMapper.selectStats(map);
	}
}
