package egovframework.com.mtdata.comtrls.web;

import egovframework.com.mtdata.comtrls.dto.ComtrlsSrcdataDTO;
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.service.ComtsrcdataService;
import egovframework.com.mtdata.comtsrcdata.vo.ComtTableVO;
import egovframework.com.mtdata.comtsrcdata.vo.ComtsrcdataVO;
import egovframework.com.mtdata.database.vo.DatabaseInfoVO;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class ComtrlsdataController {

	/** Service */
	@Resource(name = "ComtsrcdataService")
	private ComtsrcdataService comtsrcdataService;
	@Resource(name = "ComtrlsdataService")
	private ComtrlsdataService comtrlsdataService;

	private static final Logger log = LoggerFactory.getLogger(ComtrlsdataController.class);


	// 공유데이터 요청
//	@PostMapping(value="/insert/getComtrls.do")
//	@ResponseBody
////	@Transactional(rollbackFor = Exception.class)
//	public ResponseEntity<?> comtrls(@RequestBody ComtTableVO comtTbVO) throws Exception {
//
//		try {
//			comtTbVO.setSrcDataMetaId(3);
//			ComtTableVO tableVo =  comtsrcdataService.selectSrcMetaData(comtTbVO);
//
//			ComtrlsMetaDataVO metaVo = new ComtrlsMetaDataVO();
//			metaVo.setRlsDataNm("공유데이터 가져오기 테스트 DATA NAME");
//			metaVo.setRlsDataAplcntId(1);  // 공유데이터 요청자 ID
//			metaVo.setRlsDataRvwId(3);	   // 공유데이터 검토자 ID
//			metaVo.setRlsDataMngDeptId(tableVo.getSysId()); // 관리기관 ID
//			metaVo.setRlsDataSrcInstId(tableVo.getSysId()); // 공유데이터 출처기관 ID
//			metaVo.setDwnldType("JSON");  // 제공방식
//			metaVo.setRlsDataSttsChg("PROGRESS");   // 현재상태
//			metaVo.setRlsYn("Y"); // 공개여부
//
//			return ResponseEntity.ok().build();
//		} catch (Exception e) {
//			log.error("Exception {}", e);
//			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
//
//		}
//
//	}




	// 공유데이터 컬럼삭제
	@PostMapping(value="/delete/deleteComtrlsColAndData.do")
	@ResponseBody
	public ResponseEntity<?> deleteComtrlsCol(@RequestBody Map<String, Object> param) throws Exception {
		try {
			Map<String, Object> returnMap = new HashMap<>();
			int cnt  = comtrlsdataService.deleteComtrlsColAndData(param);
			if (cnt>0){
				returnMap.put("message", "성공");
			}else{
				returnMap.put("message", "삭제할 내용이 없습니다.");

			}
			return ResponseEntity.ok().body(returnMap);
		} catch (Exception e) {
			log.error("Exception {}", e);
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());

		}


	}

	/**
	 * 공유데이터의 메타데이터 리스트 조회
	 *
	 * @param comtrlsMetaDataVO
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value="/get/comtrlsMetaDataList.do")
	public ResponseEntity<?> getComtrlsMetaDataList(ComtrlsMetaDataVO comtrlsMetaDataVO) {
		try{
			return ResponseEntity.ok().body(comtrlsdataService.selectComtrlsMetaDataList(comtrlsMetaDataVO));
		} catch(Exception e){
			log.error("Exception", e);
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
		}
	}

	/**
	 * 공유데이터의 메타데이터 상세 조회
	 *
	 * @param comtrlsMetaDataVO
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value="/get/comtrlsMetaData.do")
	public ResponseEntity<?> getComtrlsMetaData(ComtrlsMetaDataVO comtrlsMetaDataVO) {
		try{
			return ResponseEntity.ok().body(comtrlsdataService.selectComtrlsMetaDataInfo(comtrlsMetaDataVO));
		} catch(Exception e){
			log.error("Exception", e);
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
		}
	}


	/**
	 * 공유데이터의 컬럼정보 상세 조회
	 *
	 * @param comtrlsColVO
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value="/get/comtrlsCol.do")
	public ResponseEntity<?> getComtrlsCol(ComtrlsColVO comtrlsColVO) {
		try{
			return ResponseEntity.ok().body(comtrlsdataService.selectComtrlsCol(comtrlsColVO));
		} catch(Exception e){
			log.error("Exception", e);
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
		}
	}

	/**
	 * 공유데이터의 데이터 상세 조회
	 *
	 * @param comtrlsSrcdataVO
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value="/get/comsrcdata.do")
	public ResponseEntity<?> getComsrcdata(ComtrlsSrcdataVO comtrlsSrcdataVO) {
		try{
			return ResponseEntity.ok().body(comtrlsdataService.selectComtrlsSrcData(comtrlsSrcdataVO));
		} catch(Exception e){
			log.error("Exception", e);
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
		}
	}
}
