package egovframework.com.mtdata.comtsrcdata.web;

import egovframework.com.mtdata.comtsrcdata.dto.ComtsrcdataDTO;
import egovframework.com.mtdata.comtsrcdata.service.ComtsrcdataService;
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.*;

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

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

	/** Service */
	@Resource(name = "ComtsrcdataService")
	private ComtsrcdataService service;


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

	// jsonb 데이터 업데이트 테스트 샘플
	@RequestMapping("/comtsrcdataTest.do")
	@Transactional(rollbackFor = Exception.class)
	public String insertDbConn(@ModelAttribute DatabaseInfoVO vo) throws Exception {

		try {
			ComtsrcdataVO comtvo = new ComtsrcdataVO();
			comtvo.setSrcDataVl("{\"id\":\"1\", \"field1\":\"ccc\"}");
				int test = service.updateDatasrc(comtvo);
				return "원천데이터 컨트롤러 ::"+test;
		} catch (Exception e) {
			log.error("Exception {}", e);
			throw new RuntimeException(e.getMessage());
		}


	}

	// jsonb 데이터 insert 테스트 샘플
	@RequestMapping("/insertComtsrcdata.do")
	@Transactional(rollbackFor = Exception.class)
	public String selectComtsrcdata( ) throws Exception {

		try {
			String url = "jdbc:mysql://" + "223.130.160.210" + ":" + "3306" + "/";
			Connection conn = DriverManager.getConnection(url, "replica_user", "dpcPW12!@");
			String selectTableSql = "SELECT * FROM smku.test_table1";
			Statement stmt = conn.createStatement();
			ResultSet datas = stmt.executeQuery(selectTableSql);
			JSONObject obj = new JSONObject();

			while (datas.next()){
				Map<String, String> resultMap = new HashMap<>();
				ResultSetMetaData rsmd = datas.getMetaData();
				int colCnt = rsmd.getColumnCount();
				for (int i=0; i<colCnt; i++) {
					String columnName = rsmd.getColumnLabel(i+1);
					String value = datas.getString(i+1);
					resultMap.put(columnName, value);
				}
				obj.putAll(resultMap);
				ComtsrcdataVO vo = new ComtsrcdataVO();
				vo.setSrcDataVl(obj.toString());
//				service.insertComtSrcdata(vo);
			}

			return "원천데이터 컨트롤러 ::";

		} catch (Exception e) {
			log.error("Exception {}", e);
			throw new RuntimeException(e.getMessage());
		}


	}

	@GetMapping(value="/get/comsrcdata.do")
	public ResponseEntity<?> getComsrcdata(ComtsrcdataVO comtsrcdataVO) {
		try{
			return ResponseEntity.ok().body(service.selectComtSrcData(comtsrcdataVO));
		} catch(Exception e){
			log.error("Exception", e);
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
		}
	}

}
