package incheon.ags.aip.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.stereotype.Service;

import incheon.ags.aip.mapper.AipIntprMapper;
import incheon.ags.aip.service.AipIntprService;
import incheon.ags.aip.util.AipAuthUtil.AipAuthInfo;
import incheon.ags.aip.vo.AipIntprSearchVO;
import incheon.ags.aip.vo.AipIntprVO;
import lombok.RequiredArgsConstructor;

@Service("AipIntprService")
@RequiredArgsConstructor
public class AipIntprServiceImpl extends EgovAbstractServiceImpl implements AipIntprService {

	private final AipIntprMapper aipIntprMapper;
	
	/**
	 * 판독 count
	 * @param vo
	 * @return 판독 count
	 * @exception Exception
	 */
	@Override
	public int getIntprCount(AipIntprSearchVO vo) throws Exception {
		return aipIntprMapper.getIntprCount(vo);
	}
	
	/**
	 * 판독 리스트 조회
	 * @param vo
	 * @return 판독 List
	 * @exception Exception
	 */
	@Override
	public List<Map<String, Object>> getIntprList(AipIntprSearchVO vo) throws Exception {
		return aipIntprMapper.getIntprList(vo);
	}
	
	
	
	/**
	 * 판독 요청 상세
	 * @param vo
	 * @return 판독 상세
	 * @exception Exception
	 */
	@Override
	public Map<String, Object> getRqstMap(Map<String, Object> param) throws Exception {
		return aipIntprMapper.getRqstMap(param);
	}
	
	/**
	 * 판독 요청 상태 변경
	 * @param param
	 * @return 판독 File List
	 * @exception Exception
	 */
	@Override
	public void updateStts(Map<String, Object> param) throws Exception {
		aipIntprMapper.updateStts(param);
	}
	
	/**
	 * 판독 요청 상태코드변경
	 * @param String
	 * @return 
	 * @exception Exception
	 */
	@Override
	public List<Map<String, Object>> getRqstFileList(Map<String, Object> param) throws Exception {
		return aipIntprMapper.getRqstFileList(param);
	}
	
	/**
	 * 판독 결과 상세
	 * @param vo
	 * @return 판독 상세
	 * @exception Exception
	 */
	@Override
	public Map<String, Object> getRsltMap(Map<String, Object> param) throws Exception {
		return aipIntprMapper.getRsltMap(param);
	}
	
	/**
	 * 판독 결과 File List
	 * @param vo
	 * @return 판독 File List
	 * @exception Exception
	 */
	@Override
	public List<Map<String, Object>> getRsltFileList(Map<String, Object> param) throws Exception {
		return aipIntprMapper.getRsltFileList(param);
	}
	
	
	
	/**
	 * 판독 요청 등록
	 * @param vo
	 * @return 판독 요청 등록
	 * @exception Exception
	 */
	@Override
	public int insertRqst(AipIntprVO vo) throws Exception {
		return aipIntprMapper.insertRqst(vo);
	}
	
	/**
	 * 판독 요청 파일 삭제
	 * @param file
	 * @return
	 * @exception Exception
	 */
	@Override
	public List<Map<String, Object>> deleteRqstFiles(List<String> uuid) throws Exception {
		if(uuid==null || uuid.isEmpty()) return Collections.emptyList();
		return aipIntprMapper.deleteRqstFiles(uuid);
	}
	
	/**
	 * 판독 요청 파일 업로드
	 * @param file
	 * @return
	 * @exception Exception
	 */
	@Override
	public int insertRqstFile(Map<String, Object> file) throws Exception {
		return aipIntprMapper.insertRqstFile(file);
	}
	
	/**
	 * 판독 결과 등록
	 * @param vo
	 * @return 판독 결과 등록
	 * @exception Exception
	 */
	@Override
	public int insertRslt(AipIntprVO vo) throws Exception {
		return aipIntprMapper.insertRslt(vo);
	}
	
	/**
	 * 판독 결과 파일 삭제
	 * @param file
	 * @return
	 * @exception Exception
	 */
	@Override
	public List<Map<String, Object>> deleteRsltFiles(List<String> uuid) throws Exception {
		if(uuid==null || uuid.isEmpty()) return Collections.emptyList();
		return aipIntprMapper.deleteRsltFiles(uuid);
	}
	
	/**
	 * 판독 결과 파일 업로드
	 * @param file
	 * @return
	 * @exception Exception
	 */
	@Override
	public int insertRsltFile(Map<String, Object> file) throws Exception {
		return aipIntprMapper.insertRsltFile(file);
	}
	
	/**
	 * 의뢰 수정
	 * @param vo
	 * @return
	 * @exception Exception
	 */
	@Override
	public int updateRqst(AipIntprVO vo) throws Exception {
		return aipIntprMapper.updateRqst(vo);
	}

	/**
	 * 결과 수정
	 * @param vo
	 * @return
	 * @exception Exception
	 */
	@Override
	public int updateRslt(AipIntprVO vo) throws Exception {
		return aipIntprMapper.updateRslt(vo);
	}

	/**
	 * 결과 삭제
	 * @param rqstId
	 * @return
	 * @exception Exception
	 */
	@Override
	public List<Map<String, Object>> deleteRsltCascadeByRqstId(Long rqstId) throws Exception {
		if(rqstId == null) return Collections.emptyList();
	
		Map<String, Object> param = new HashMap<>();
		param.put("rqstId", rqstId);
	
		List<Map<String, Object>> files = aipIntprMapper.deleteRsltFilesByRqstId(param);
		aipIntprMapper.deleteRsltInfoByRqstId(param);
	
		return (files == null) ? Collections.emptyList() : files;
	}
	
	/**
	 * 의뢰삭제
	 * @param rqstId
	 * @return
	 * @exception Exception
	 */
	@Override
	public List<Map<String, Object>> deleteRqstCascadeByRqstId(Long rqstId, AipAuthInfo auth) throws Exception {
		if(rqstId == null) return Collections.emptyList();
	
		Map<String, Object> param = new HashMap<>();
		param.put("rqstId", rqstId);
	
		if(auth != null && auth.isTask() && !auth.isAdmin()){
			param.put("user_id", auth.getLoginUserId());
		}
	
		List<Map<String, Object>> files = new ArrayList<>();
	
		List<Map<String, Object>> rsFiles = aipIntprMapper.deleteRsltFilesByRqstId(param);
		if(rsFiles != null && !rsFiles.isEmpty()) files.addAll(rsFiles);
	
		aipIntprMapper.deleteRsltInfoByRqstId(param);
	
		List<Map<String, Object>> rqFiles = aipIntprMapper.deleteRqstFilesByRqstId(param);
		if(rqFiles != null && !rqFiles.isEmpty()) files.addAll(rqFiles);
	
		int del = aipIntprMapper.deleteRqstInfoByRqstId(param);
		if(auth != null && auth.isTask() && !auth.isAdmin()){
			if(del <= 0) return Collections.emptyList();
		}
	
		return files;
	}

}