package com.eactive.eai.common.usedate;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.usedate.loader.UseDateControlLoader;
import com.eactive.eai.common.usedate.mapper.UseDateControlMapper;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.useDate.UseDateControl;

@Service
@Transactional
public class UseDateControlDAO extends BaseDAO {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	@Autowired
	private UseDateControlLoader useDateControlLoader;
	
	@Autowired
	private UseDateControlMapper useDateControlMapper;

	public List<UseDateControlVO> getUseDateContorlList() throws DAOException {
		try {
			List<UseDateControl> useDateControls = useDateControlLoader.findUseDateByTypeDSL("01");
			List<UseDateControlVO> vos = new ArrayList<>();
			useDateControls.forEach( e -> vos.add(useDateControlMapper.toVo(e)) );
			return vos;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
		}
	}

	public UseDateControlVO getUseDateContorlListByInterface(String inter) throws DAOException {
		ResultSet rs = null;
		UseDateControlVO useDateControlVO = null;
		try {
// TODO :  아래의 로직이 정상이 아니므로 확인이 핗요함

//		    // 사용일자제어 전체 인터페이스 조회
//		    public static final String SELECT_USE_DATE_CONTROL_BY_ALL_INTERFACE = 
//		        "SELECT NAME, STARTDATE, ENDDATE, USEYN\r\n" + 
//		        "FROM " + Keys.TABLE_OWNER + "TSEAIFR10\r\n" + 
//		        "WHERE TYPE='01'\r\n" + 
//		        "ORDER BY NAME, USEYN";

//		    // 사용일자제어 인터페이스 조회
//		    public static final String SELECT_USE_DATE_CONTROL_BY_INTERFACE = 
//				"SELECT NAME, STARTDATE, ENDDATE, USEYN\r\n" + 
//				"FROM " + Keys.TABLE_OWNER + "TSEAIFR10\r\n" + 
//		        "WHERE TYPE='01'\r\n" + 
//				"ORDER BY NAME, USEYN";
			
//			this.connect(SELECT_USE_DATE_CONTROL_BY_INTERFACE);
//
//			rs = this.executeQuery();
//			if (rs.next()) {
//
//				useDateControlVO = new UseDateControlVO();
//				SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
//				useDateControlVO.setName(rs.getString(1)); // NAME 인터페이스
//				Date start = null;
//				Date end = null;
//				try {
//					start = format.parse(rs.getString(2) + "000000");
//					end = format.parse(rs.getString(3) + "235959");
//				} catch (Exception e) {
//					useDateControlVO.setActivate(false);
//
//				}
//				useDateControlVO.setStartDate(start); // 시작일자
//				useDateControlVO.setEndDate(end); // 종료일자
//				useDateControlVO.setActivate("0".equals(rs.getString(4)) ? false : true);
//
//			}
			return useDateControlVO;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
		} finally {
			this.disconnect();
		}
	}

}
