package kcg.faics.susa.service.impl;

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

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import kcg.faics.cmmn.bbs.BaseBbsService;
import kcg.faics.cmmn.bbs.BaseFileService;
import kcg.faics.cmmn.service.CodeService;
import kcg.faics.susa.vo.SearchVO;
import kcg.faics.susa.vo.SusaFileVO;
import kcg.faics.susa.vo.SusaVO;
import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;

/**
 * 외사수사 비지니스 로직.
 * 
 * @author kimnomin
 *
 */
@Service("susaService")
public class SusaServiceImpl extends EgovAbstractServiceImpl implements
		BaseBbsService<SearchVO, SusaVO> {

	/**
	 * 외사수사 Mapper 클래스.
	 */
	@Resource(name = "susaMapper")
	private SusaMapper susaMapper;

	/**
	 * 첨부파일 관련 Service 클래스.
	 */
	@Autowired
	private BaseFileService<SusaFileVO> susaFileService;
	
	/**
	 * 내사사건 첨부파일 관련 Service 클래스. 
	 */
	@Autowired
	private BaseFileService<SusaFileVO> incidentFileService;
	
	/**
	 * 공작추진현황 관련 Service 클래스.
	 */
	@Autowired
	private BaseFileService<SusaFileVO> promotFileService;

	/**
	 * 코드 관련 Service 클래스.
	 */
	@Autowired
	private CodeService codeService;

	@Override
	public SusaVO select(final SusaVO dataVO) throws Exception {
		return susaMapper.select(dataVO);
	}

	@Override
	public List<SusaVO> selectListAll(final SearchVO searchVO) throws Exception {
		return susaMapper.selectListAll(searchVO);
	}
	
	@Override
	public List<SusaVO> selectList(final SearchVO searchVO) throws Exception {
		return susaMapper.selectList(searchVO);
	}

	@Override
	public int selectListCnt(final SearchVO searchVO) throws Exception {
		return susaMapper.selectListCnt(searchVO);
	}

	@Override
	public int selectTotalCnt(final SearchVO searchVO) throws Exception {
		return susaMapper.selectTotalCnt(searchVO);
	}

	@Override
	public HashMap<String, Object> insert(final SusaVO dataVO,
			final Map<String, MultipartFile> fileMap) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("result", 0);
		map.put("idx", -1);

		try {
			dataVO.setAgency(codeService.getPlace1HighCode(dataVO.getPlace1()));
			HashMap<String, Object> insertResult = susaMapper.insert(dataVO);

			int idx = (Integer) insertResult.get("idx");
			int result = (Integer) insertResult.get("result");

			if (result == 1) {
				SusaFileVO fileVO = new SusaFileVO();
				fileVO.setParentSeq(idx);
				int fileProcResult = 0;
				if ("in".equalsIgnoreCase(dataVO.getType())) {
					fileProcResult = incidentFileService.insertAndSaveFiles(fileVO, fileMap);
				} else {
					fileProcResult = promotFileService.insertAndSaveFiles(fileVO, fileMap);
				}
				if (fileProcResult == 1) {
					map.put("result", 1);
					map.put("idx", idx);
				} else {
					throw new Exception();
				}
			} else {
				throw new Exception();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	@Override
	public HashMap<String, Object> update(final SusaVO dataVO,
			final Map<String, MultipartFile> fileMap, final String[] deleteFiles) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		int result = 0;

		try {
			// 체크된 파일 삭제
			SusaFileVO susaFileVO = new SusaFileVO();	
			if (deleteFiles != null) {
				for (int i = 0; i < deleteFiles.length; i++) {
					susaFileVO.setSeq(Integer.parseInt(deleteFiles[i]));
					if ("in".equalsIgnoreCase(dataVO.getType())) {
						incidentFileService.deleteAndRemoveFile(susaFileVO);
					} else {
						promotFileService.deleteAndRemoveFile(susaFileVO);
					}
				}
			}
			
			// 입력된 파일 등록
			susaFileVO = new SusaFileVO();
			susaFileVO.setParentSeq(dataVO.getIdx());
			if ("in".equalsIgnoreCase(dataVO.getType())) {
				incidentFileService.insertAndSaveFiles(susaFileVO, fileMap);
			} else {
				promotFileService.insertAndSaveFiles(susaFileVO, fileMap);
			}

			result = susaMapper.update(dataVO);

			map.put("result", result);
			map.put("idx", dataVO.getIdx());

		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", 0);
			map.put("idx", -1);			
		}

		return map;
	}

	@Override
	public HashMap<String, Object> delete(final SusaVO dataVO) {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		try {
			SusaFileVO fileVO = new SusaFileVO();
			fileVO.setParentSeq(dataVO.getIdx());
			if ("in".equalsIgnoreCase(dataVO.getType())) {
				incidentFileService.deleteAndRemoveFiles(fileVO);
			} else {
				promotFileService.deleteAndRemoveFiles(fileVO);
			}
			resultMap.put("result", susaMapper.delete(dataVO));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", 0);
		}

		return resultMap;
	}
}
