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

import java.util.List;

import javax.annotation.Resource;

import egovframework.com.mtdata.database.vo.*;
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.stereotype.Service;

import egovframework.com.mtdata.database.mapper.DatabaseMapper;
import egovframework.com.mtdata.database.service.DatabaseService;
import org.springframework.transaction.annotation.Transactional;

@Service("DatabaseService")
public class DatabaseServiceImpl extends EgovAbstractServiceImpl implements DatabaseService {

	@Resource(name = "DatabaseMapper")
	private DatabaseMapper databaseMapper;

	@Override
	public int insertDbInfo(DatabaseInfoVO vo) throws Exception {
		return databaseMapper.insertDbInfo(vo);
	}

	@Override
	public int insertDbConnInfo(DatabaseInfoVO vo) throws Exception {
		return databaseMapper.insertDbConnInfo(vo);
	}

	@Override
	public DatabaseInfoVO selectSysDb(DatabaseInfoVO vo) {
		return databaseMapper.selectSysDb(vo);
	}

	@Override
	public List<DatabaseInfoVO> selectSysDbConnList(DatabaseInfoVO vo) {
		return databaseMapper.selectSysDbConnList(vo);
	}

	@Override
	public int checkDbConnInfo(DatabaseInfoVO vo) {
		return databaseMapper.checkDbConnInfo(vo);
	}

	@Override
	public int insertDbTblInfo(TableInfoVO tbVo) throws Exception {
		return databaseMapper.insertDbTblInfo(tbVo);
	}

	@Override
	public int insertTblColInfo(ColumnInfoVO colVo) throws Exception {
		return databaseMapper.insertTblColInfo(colVo);
	}

	@Override
	public List<DatabaseInfoVO> selectRegisterdDBInfoList(DatabaseInfoVO vo) {
		return databaseMapper.selectRegisterdDBInfoList(vo);
	}

	@Override
	@Transactional
	public void updateRegisterdDBInfoList(DatabaseInfoVO vo) throws Exception{

		//TODO: 연결체크 로직 추가
		if (true) {
			DBConnInfoVO dbConnInfoVO = new DBConnInfoVO();
			dbConnInfoVO.setSysId(vo.getSysId());
			dbConnInfoVO.setDbConnUser(vo.getDbConnUser());
			dbConnInfoVO.setDbConnPw(vo.getDbConnPw());
			dbConnInfoVO.setDbType(vo.getDbType());
			dbConnInfoVO.setDbConnIp(vo.getDbConnIp());
			dbConnInfoVO.setRegisterId(vo.getRegisterId());

			DBInfoVO dbInfoVO = new DBInfoVO();
			dbInfoVO.setDbLinkId(vo.getDbLinkId());
			dbInfoVO.setDbNm(vo.getDbNm());

			int checkDbConnCnt = databaseMapper.checkDbConnInfo(vo);

			// DB 연결정보 미존재 시 DB 연결 정보 insert
			if (checkDbConnCnt < 1) {
				databaseMapper.insertInstDbConnInfo(dbConnInfoVO);

				dbInfoVO.setDbConnId(dbConnInfoVO.getDbConnId());
				databaseMapper.updateInstDbInfo(dbInfoVO);
			} else { // 존재 시 update
				dbConnInfoVO.setDbConnId(vo.getDbConnId());
				databaseMapper.updateInstDbConnInfo(dbConnInfoVO);

				dbInfoVO.setDbConnId(vo.getDbConnId());
				databaseMapper.updateInstDbInfo(dbInfoVO);
			}

		} else {
			throw new Exception();
		}
	}
	@Override
	@Transactional
	public void deleteRegisterdDBInfoList(DatabaseInfoVO vo) throws Exception{

		DBInfoVO dbInfoVO = new DBInfoVO();
		dbInfoVO.setDbLinkId(vo.getDbLinkId());

		databaseMapper.deleteInstDbInfo(dbInfoVO);
	}

	@Override
	public List<DBConnInfoVO> selectInstDbConnInfoList(DBConnInfoVO vo) {
		return databaseMapper.selectInstDbConnInfoList(vo);
	}

	@Override
	public DBConnInfoVO selectInstDbConnInfo(DBConnInfoVO vo) {
		return databaseMapper.selectInstDbConnInfo(vo);
	}

	@Override
	public void insertInstDbConnInfo(DBConnInfoVO vo) throws Exception {
		databaseMapper.insertInstDbConnInfo(vo);
	}

	@Override
	public void updateInstDbConnInfo(DBConnInfoVO vo) throws Exception {
		databaseMapper.updateInstDbConnInfo(vo);
	}

	@Override
	public void deleteInstDbConnInfo(DBConnInfoVO vo) throws Exception {
		databaseMapper.deleteInstDbConnInfo(vo);
	}


	@Override
	public List<DBInfoVO> selectInstDbInfoList(DBInfoVO vo) {
		return databaseMapper.selectInstDbInfoList(vo);
	}

	@Override
	public DBInfoVO selectInstDbInfo(DBInfoVO vo) {
		return databaseMapper.selectInstDbInfo(vo);
	}

	@Override
	public void insertInstDbInfo(DBInfoVO vo) throws Exception {
		databaseMapper.insertInstDbInfo(vo);
	}

	@Override
	public void updateInstDbInfo(DBInfoVO vo) throws Exception {
		databaseMapper.updateInstDbInfo(vo);
	}

	@Override
	public void deleteInstDbInfo(DBInfoVO vo) throws Exception {
		databaseMapper.deleteInstDbInfo(vo);
	}

	@Override
	public List<DBTblInfoVO> selectInstDbTblInfoList(DBTblInfoVO vo) {
		return databaseMapper.selectInstDbTblInfoList(vo);
	}

	@Override
	public DBTblInfoVO selectInstDbTblInfo(DBTblInfoVO vo) {
		return databaseMapper.selectInstDbTblInfo(vo);
	}

	@Override
	public void insertInstDbTblInfo(DBTblInfoVO vo) throws Exception {
		databaseMapper.insertInstDbTblInfo(vo);
	}

	@Override
	public void updateInstDbTblInfo(DBTblInfoVO vo) throws Exception {
		databaseMapper.updateInstDbTblInfo(vo);
	}

	@Override
	public void deleteInstDbTblInfo(DBTblInfoVO vo) throws Exception {
		databaseMapper.deleteInstDbTblInfo(vo);
	}


	@Override
	public List<DBTblColInfoVO> selectInstDbTblColInfoList(DBTblColInfoVO vo) {
		return databaseMapper.selectInstDbTblColInfoList(vo);
	}

	@Override
	public DBTblColInfoVO selectInstDbTblColInfo(DBTblColInfoVO vo) {
		return databaseMapper.selectInstDbTblColInfo(vo);
	}

	@Override
	public void insertInstDbTblColInfo(DBTblColInfoVO vo) throws Exception {
		databaseMapper.insertInstDbTblColInfo(vo);
	}

	@Override
	public void updateInstDbTblColInfo(DBTblColInfoVO vo) throws Exception {
		databaseMapper.updateInstDbTblColInfo(vo);
	}

	@Override
	public void deleteInstDbTblColInfo(DBTblColInfoVO vo) throws Exception {
		databaseMapper.deleteInstDbTblColInfo(vo);
	}

}
