package kcg.imis.report.service;

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

import javax.annotation.Resource;

import kcg.imis.cmmn.egov.file.EgovFileMngUtil;
import kcg.imis.cmmn.service.CodeService;
import kcg.imis.report.mapper.ReportMapper;
import kcg.imis.report.vo.ReportGoalVO;
import kcg.imis.report.vo.ReportSearchVO;
import kcg.imis.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;

/**
  * @FileName : ReportServiceImpl.java
  * @Project : 국제해양프로젝트
  * @Date : 2018. 3. 29. 
  * @작성자 : Moon
  * @변경이력 : 
  * @프로그램 설명 :
 */
@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);
		
		/*20210222 최형석 견문 수정시 파일이름을 기존 파일명으로 가져온다.*/
		dataVO.setInFile1(vo.getInFile1());
		dataVO.setInFile2(vo.getInFile2());
		dataVO.setInFile3(vo.getInFile3());
		
//		dataVO.setInFile1(vo.getInFileName1());
//		dataVO.setInFile2(vo.getInFileName2());
//		dataVO.setInFile3(vo.getInFileName3());
		
		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"};
		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 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);
	}	
}
