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

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.dto.ComtsrcdataDTO;
import egovframework.com.mtdata.comtsrcdata.vo.ComtsrcdataVO;
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.json.simple.parser.JSONParser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
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;


	@Override
	public int insertComtrlsMetadataInfo(ComtrlsMetaDataVO vo) throws Exception {
		return comtrlsdataMapper.insertComtrlsMetadataInfo(vo);
	}

	@Override
	public int deleteComtrlsColAndData(Map<String, Object> param) throws Exception {
		int colCnt = comtrlsdataMapper.deleteComtrlsCol(param);
		int colDataCnt = 0;
		if(colCnt>0){
			colDataCnt = comtrlsdataMapper.deleteComtrlsData(param);
		}

		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);
		}

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