package kcg.faics.report.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.report.service.ReportFileService;
import kcg.faics.report.service.ReportService;
import kcg.faics.report.vo.ReportGoalVO;
import kcg.faics.report.vo.ReportSearchVO;
import kcg.faics.report.vo.ReportVO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;

/**
 * 견문 비즈니스 로직
 * 
 * @author 하영지
 */
@Service("reportService")
public class ReportServiceImpl extends EgovAbstractServiceImpl implements ReportService { 
	
	/**
	 * 견문 Mapper 클래스.
	 */
	@Resource(name = "reportMapper")
	private ReportMapper reportMapper;

	/**
	 * 첨부파일 관련 Service 클래스.
	 */
	@Resource(name = "reportFileService")
	private ReportFileService reportFileService;
	
	/**
	 * 코드 관련 Service 클래스.
	 */
	@Resource(name = "codeService")
	private CodeService codeService;

    /**
	 * 전자정부프레임워크에서 제공되는 파일 업로드 처리 관련 Utility.
	 **/
	@Resource(name = "EgovFileMngUtil")
	private EgovFileMngUtil fileUtil;	
	
	@Override
	public ReportVO select(final ReportVO dataVO) throws Exception {
		return reportMapper.select(dataVO);
	}

	@Override
	public List<ReportVO> selectListAll(final ReportSearchVO searchVO) throws Exception {
		return reportMapper.selectListAll(searchVO);
	}

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

	@Override
	public List<ReportVO> selectList(final ReportSearchVO searchVO) throws Exception {
		return reportMapper.selectList(searchVO);
	}

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

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

		/* seq 및 insert 결과 */
		map = reportMapper.insert(dataVO);
		
		int seq = (Integer) map.get("seq");
		int result = (Integer) map.get("result");

		if (result == 1) {
			ReportVO fileVO = new ReportVO();
			fileVO.setInSeq(dataVO.getInSeq());
			int fileProcResult = 0;
			fileProcResult = reportFileService.insertAndSaveFiles(fileVO, fileMap);
			
			if (fileProcResult == 1) {
				map.put("result", 1);
				map.put("seq", seq);
			} else {
				throw new Exception();
			}
		} else {
			throw new Exception();
		}
		
		return map;
	}

	@Override
	public HashMap<String, Object> update(final ReportVO dataVO, 
			final Map<String, MultipartFile> fileMap, final String[] deleteFiles) throws Exception {
		ReportVO vo = select(dataVO);
		dataVO.setInFile1(vo.getInFile1());
		dataVO.setInFile2(vo.getInFile2());
		dataVO.setInFile3(vo.getInFile3());
		dataVO.setInFile4(vo.getInFile4());
		dataVO.setInFile5(vo.getInFile5());
		
		reportFileService.insertAndSaveFiles(dataVO, fileMap);
		if (deleteFiles != null) {
			reportFileService.deleteAndRemoveFiles(vo, deleteFiles);
		}
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("result", reportMapper.update(dataVO));
		map.put("seq", dataVO.getInSeq());
		
		return map;
	}

	@Override
	public HashMap<String, Object> delete(final ReportVO dataVO) throws Exception {
		ReportVO vo = select(dataVO);
		HashMap<String, Object> map = new HashMap<String, Object>();
		

		String[] delFiles = {"1", "2", "3", "4", "5"};
		int result = reportFileService.deleteAndRemoveFiles(vo, delFiles);
		result = reportMapper.delete(dataVO);
		
		map.put("result", result);

		return map;
	}

	@Override
	public int selectPlaceNum(final ReportVO reportVO) {
		return reportMapper.selectPlaceNum(reportVO);
	}

	@Override
	public List<ReportVO> selectSaveList(final ReportSearchVO reportSearchVO) {
		return reportMapper.selectSaveList(reportSearchVO);
	}
	
	@Override
	public int selectSaveListCnt(final ReportSearchVO reportSearchVO) {
		return reportMapper.selectSaveListCnt(reportSearchVO);
	}
	
	@Override
	@Transactional
	public int deleteSelected(final String[] selectedSeq) throws Exception {
		int result = 1;
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		ReportVO reportVO = new ReportVO();
		for (int i = 0; i < selectedSeq.length; i++) {
			reportVO.setInSeq(Integer.parseInt(selectedSeq[i]));
			map = delete(reportVO);
			if ("0".equals(String.valueOf(map.get("result")))) {
				throw new Exception();
			}
		} 
		
		return result;
	}

	@Override
	public String selectAutoRemoveList() {
		return reportMapper.selectAutoRemoveList();
	}

	@Override
	public List<ReportVO> selectPresentList(final ReportSearchVO reportSearchVO) throws Exception {
		return reportMapper.selectPresentList(reportSearchVO);
	}

	@Override
	public int selectPresentListCnt(final ReportSearchVO reportSearchVO) throws Exception {
		return reportMapper.selectPresentListCnt(reportSearchVO);
	}

	@Override
	public List<ReportVO> selectPresentListAll(final ReportSearchVO reportSearchVO) throws Exception {
		return reportMapper.selectPresentListAll(reportSearchVO);
	}

	@Override
	public List<ReportVO> selectRecordList(final ReportSearchVO reportSearchVO) throws Exception {
		return reportMapper.selectRecordList(reportSearchVO);
	}

	@Override
	public int selectRecordListCnt(final ReportSearchVO reportSearchVO) throws Exception {
		return reportMapper.selectRecordListCnt(reportSearchVO);
	}

	@Override
	public List<ReportVO> selectRecordListAll(final ReportSearchVO reportSearchVO) throws Exception {
		return reportMapper.selectRecordListAll(reportSearchVO);
	}

	@Override
	public List<ReportGoalVO> selectGoalList(final ReportSearchVO reportSearchVO) throws Exception {
		return reportMapper.selectGoalList(reportSearchVO);
	}

	@Override
	public int updateGoal(final ReportGoalVO reportGoalVO) throws Exception {
		int result = 0;
		
		for (ReportGoalVO vo : reportGoalVO.getReportGoalList()) {
			vo.setGoPlace1(reportGoalVO.getGoPlace1());
			vo.setGoYear(reportGoalVO.getGoYear());
			vo.setGoMonth(reportGoalVO.getGoMonth());
			
			try {
				ReportGoalVO originalVO = reportMapper.selectGoal(vo);
				if (originalVO == null) {
					result = reportMapper.insertGoal(vo);
				} else {
					result = reportMapper.updateGoal(vo);
				}
			} catch (Exception e) {
				new Exception();
			}
		}
		return result;
	}

	@Override
	public List<HashMap<String, Object>> getGoalStats(final ReportSearchVO reportSearchVO) throws Exception {
		return reportMapper.getGoalStats(reportSearchVO);
	}

	@Override
	public List<HashMap<String, Object>> getGoalStatsPlace(final ReportSearchVO reportSearchVO) throws Exception {
		return reportMapper.getGoalStatsPlace(reportSearchVO);
	}	
}
