package egovframework.com.mtdata.comtrls.service.impl;

import egovframework.com.mtdata.comtrls.dto.ComtrlsMultiColDTO;
import egovframework.com.mtdata.comtrls.dto.ComtrlsSrcdataDTO;
import egovframework.com.mtdata.comtrls.mapper.ComtrlsdataMapper;
import egovframework.com.mtdata.comtrls.service.ComtrlsdataService;
import egovframework.com.mtdata.comtrls.vo.ComtrlsColVO;
import egovframework.com.mtdata.comtrls.vo.ComtrlsMetaDataVO;
import egovframework.com.mtdata.comtrls.vo.ComtrlsSrcdataVO;
import egovframework.com.mtdata.comtsrcdata.mapper.ComtsrcdataMapper;
import egovframework.com.mtdata.comtsrcdata.vo.ComtTableVO;
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.json.simple.parser.JSONParser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;

@Service("ComtrlsdataService")
public class ComtrlsdataServiceImpl extends EgovAbstractServiceImpl implements ComtrlsdataService {

	@Resource(name = "ComtrlsdataMapper")
	private ComtrlsdataMapper comtrlsdataMapper;
	@Resource(name = "ComtsrcdataMapper")
	private ComtsrcdataMapper comtsrcdataMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertComtrlsMetadataInfo(ComtrlsMetaDataVO vo) throws Exception {
		// 공유데이터 메타데이터 저장
		ComtTableVO comtTableVO = new ComtTableVO();
		comtTableVO.setSrcDataMetaId(vo.getSrcDataMetaId());
		ComtTableVO tableVo =  comtsrcdataMapper.selectSrcMetaData(comtTableVO);

		if (tableVo == null) {
			throw new RuntimeException("저장정보 없음");
		}

//			vo.setRlsDataNm("공유데이터 가져오기 테스트 DATA NAME");
		vo.setRlsDataAplcntId(1);  // 공유데이터 요청자 ID (임시)
		vo.setRlsDataRvwId(3);	   // 공유데이터 검토자 ID (임시)
		vo.setRlsDataMngDeptId(tableVo.getSysId()); // 관리기관 ID
		vo.setRlsDataSrcInstId(tableVo.getSysId()); // 공유데이터 출처기관 ID
//			vo.setDwnldType("JSON");  // 제공방식 (임시)
//			vo.setRlsDataSttsChg("PROGRESS");   // 현재상태 (임시)
			vo.setRlsYn("Y"); // 공개여부
		comtrlsdataMapper.insertComtrlsMetadataInfo(vo);

		// 공유데이터 데이터 저장
		ComtrlsSrcdataVO comtrlsSrcdataVO = new ComtrlsSrcdataVO();
		comtrlsSrcdataVO.setSrcDataMetaId(vo.getSrcDataMetaId());
		comtrlsSrcdataVO.setRlsDataId(vo.getRlsDataId());
		comtrlsdataMapper.insertComtrlsSrcData(comtrlsSrcdataVO);


		// 공유데이터 컬럼 저장
		ComtrlsColVO comtrlsColVO = new ComtrlsColVO();
		comtrlsColVO.setSrcDataMetaId(vo.getSrcDataMetaId());
		comtrlsColVO.setRlsDataId(vo.getRlsDataId());
		comtrlsdataMapper.insertComtrlsCol(comtrlsColVO);
	}

	@Override
	public int deleteComtrlsColAndData(ComtrlsMultiColDTO colDTO) throws Exception {
		List<String> colNmList = comtrlsdataMapper.selectComtrlsColNm(colDTO);
		int colCnt = comtrlsdataMapper.deleteComtrlsCol(colDTO);
		int colDataCnt = 0;
		colDTO.setColsNmArr(colNmList);
		if(colCnt>0){
			colDataCnt = comtrlsdataMapper.deleteComtrlsData(colDTO);
		}
		return colDataCnt;
	}


	@Override
	public List<ComtrlsMetaDataVO> selectComtrlsMetaDataList(ComtrlsMetaDataVO vo) {
		return comtrlsdataMapper.selectComtrlsMetaDataList(vo);
	}

	@Override
	public ComtrlsMetaDataVO selectComtrlsMetaDataInfo(ComtrlsMetaDataVO vo) {
		return comtrlsdataMapper.selectComtrlsMetaDataInfo(vo);
	}

	@Override
	public ComtrlsColVO selectComtrlsCol(ComtrlsColVO vo) {
		return comtrlsdataMapper.selectComtrlsCol(vo);
	}

	@Override
	public ComtrlsSrcdataDTO selectComtrlsSrcData(ComtrlsSrcdataVO vo) throws Exception{
		List <ComtrlsSrcdataVO> resultVO = comtrlsdataMapper.selectComtrlsSrcData(vo);
		JSONParser jsonParser = new JSONParser();
		List<Object> dataList = new ArrayList<>();

		int size = resultVO.size();

		for(int i = 0; i < size; i++) {
			String rlsDataVl = resultVO.get(i).getRlsDataVl();
			Object data = jsonParser.parse(rlsDataVl);
			dataList.add(data);
		}

		Integer rlsDataId = null;
		if(size > 0) {
			rlsDataId = resultVO.get(0).getRlsDataId();
		}

		return ComtrlsSrcdataDTO.builder()
				.rlsDataId(rlsDataId)
				.data(dataList)
				.build();
	}

	@Override
	public void deIdentificationMasking(ComtrlsColVO vo) throws Exception{
		comtrlsdataMapper.deIdentificationMasking(vo);
	}
}
