package kcg.faics.tg.service.impl;

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

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import kcg.faics.cmmn.egov.file.EgovFileMngUtil;
import kcg.faics.cmmn.egov.vo.FileVO;
import kcg.faics.cmmn.service.CodeService;
import kcg.faics.tg.service.WeakPlaceService;
import kcg.faics.tg.vo.WeakPlaceSearchVO;
import kcg.faics.tg.vo.WeakPlaceVO;
import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;

/**
 * 외사취약지 관련 비지니스로직.
 * 
 * @author kimnomin
 *
 */
@Service("weakPlaceService")
public class WeakPlaceServiceImpl extends EgovAbstractServiceImpl implements WeakPlaceService {
	/**
	 * property 파일에서 읽은 저장 경로 키워드. 
	 */
	private String filePathKeyword = "Target.wp.fileStorePath";	
	
    /**
	 * 전자정부프레임워크에서 제공되는 파일 업로드 처리 관련 Utility.
	 **/
	@Resource(name = "EgovFileMngUtil")
	private EgovFileMngUtil fileUtil;	
	
	/**
	 * 외사취약지 Mapper 클래스.
	 */
	@Resource(name = "weakPlaceMapper")
	private WeakPlaceMapper weakPlaceMapper;
	
	@Override
	public WeakPlaceVO select(final WeakPlaceVO dataVO) throws Exception {
		return weakPlaceMapper.select(dataVO);
	}

	@Override
	public List<WeakPlaceVO> selectListAll(final WeakPlaceSearchVO searchVO)
			throws Exception {
		// 사용하지 않음
		return null;
	}

	@Override
	public List<WeakPlaceVO> selectList(final WeakPlaceSearchVO searchVO)
			throws Exception {
		return weakPlaceMapper.selectList(searchVO);
	}
	
	@Override
	public List<HashMap<String, Object>> selectStatsPerPlace() throws Exception {
		List<HashMap<String, Object>> statsList = new ArrayList<HashMap<String, Object>>();
		try {
			statsList = weakPlaceMapper.selectStatsPerPlace(CodeService.POLICE_STATION_LIST);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return statsList;
	}	

	@Override
	public int selectListCnt(final WeakPlaceSearchVO searchVO) throws Exception {
		// 사용하지 않음
		return 0;
	}

	@Override
	public int selectTotalCnt(final WeakPlaceSearchVO searchVO) throws Exception {
		// 사용하지 않음
		return 0;
	}

	@Override
	public int selectMaxWpNo(final String place1) throws Exception {
		return weakPlaceMapper.selectMaxWpNo(place1);
	}

	@Override
	public HashMap<String, Object> insert(final WeakPlaceVO dataVO,
			final Map<String, MultipartFile> fileMap) throws Exception {
		int result = 0;
		
		try {
			// 파일저장
			if (fileMap != null) {
				List<FileVO> uploadedFile = fileUtil.parseFileInf(fileMap, null, 0, "", filePathKeyword);
				for (FileVO file : uploadedFile) {
					if (file != null) {
						String fileSn = file.getFileSn();
						if (!StringUtils.isBlank(fileSn)) {
							fileSn = fileSn.trim();
						}
						if ("file1".equalsIgnoreCase(fileSn)) {
							dataVO.setFilename1(file.getStreFileNm());
						} else if ("file2".equalsIgnoreCase(fileSn)) {
							dataVO.setFilename2(file.getStreFileNm());		
						}
					}
				}
			}
			
			// DB저장
			result = weakPlaceMapper.insert(dataVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		if (result == 1) {
			map.put("place1", dataVO.getPlace1());
			map.put("wpNo", dataVO.getWpNo());
		}
		
		return map;
	}

	@Override
	public HashMap<String, Object> update(final WeakPlaceVO dataVO,
			final Map<String, MultipartFile> fileMap, final String[] deleteFiles)
			throws Exception {
		int result = 0;
		
		try {
			// 파일삭제 : 기존 PHP 소스에서 실제 물리적인 파일은 삭제하지 않는다. 이를 준용한다.
			if (deleteFiles != null) {
				for (String deleteTarget : deleteFiles) {
					if ("1".equalsIgnoreCase(deleteTarget)) {
						dataVO.setFilename1("");
					} else if ("2".equalsIgnoreCase(deleteTarget)) {
						dataVO.setFilename2("");
					}
				}
			}
			
			// 파일저장
			if (fileMap != null) {
				List<FileVO> uploadedFile = fileUtil.parseFileInf(fileMap, null, 0, "", filePathKeyword);
				for (FileVO file : uploadedFile) {
					if (file != null) {
						String fileSn = file.getFileSn();
						if (!StringUtils.isBlank(fileSn)) {
							fileSn = fileSn.trim();
						}
						if ("file1".equalsIgnoreCase(fileSn)) {
							dataVO.setFilename1(file.getStreFileNm());
						} else if ("file2".equalsIgnoreCase(fileSn)) {
							dataVO.setFilename2(file.getStreFileNm());		
						}
					}
				}
			}
			
			// DB저장
			result = weakPlaceMapper.update(dataVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);

		return map;
	}

	@Override
	public HashMap<String, Object> delete(final WeakPlaceVO dataVO) throws Exception {
		// 기존 PHP 소스에서 실제 물리적인 파일은 삭제하지 않는다. 이를 준용한다.
		int result = weakPlaceMapper.delete(dataVO);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		
		return map;
	}


}
