package com.ejie.ad74a.dao.iml;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ad74a.dao.SoporteDao;
import com.ejie.ad74a.model.Soporte;
import com.ejie.ad74a.util.UtilSQL;

/**
 * SoporteDaoImpl generated by UDA, 22-may-2023 13:12:39.
 * 
 * @author UDA
 */

@Repository
@Transactional
public class SoporteDaoImpl implements SoporteDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDSOPORTE22", "IDNIVELSOPORTE22",
			"IDVALORACION22", "IDSOLICITANTE22", "USUHISTORICO22", "FHISTORICO22", "FALTA22", "USUALTA22" };

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_SOPORTE = "SEQ_SOPORTE";

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<Soporte> rwMap = new RowMapper<Soporte>() {
		public Soporte mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			Soporte soporte = new Soporte();
			soporte.setIdSoporte22(UtilSQL.getLong(resultSet, "IDSOPORTE22"));
			soporte.setIdNivelSoporte22(UtilSQL.getLong(resultSet, "IDNIVELSOPORTE22"));
			soporte.setfAlta22(resultSet.getDate("FALTA22"));
			soporte.setfHistorico22(resultSet.getDate("FHISTORICO22"));
			return soporte;			
		}
	};	

	/**
	 * Method use to set the datasource.
	 *
	 * @param dataSource DataSource
	 * @return
	 */
	@Resource
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	
	/**
	 * StringBuilder initilization value
	 */
	public static final int STRING_BUILDER_INIT = 4096;

	@Override
	public List<Soporte> findSoporte(Soporte soporte, Date fDesde, Date fHasta) {
		String query = "SELECT ID_SOPORTE_22 IDSOPORTE22, ID_NIVEL_SOPORTE_22 IDNIVELSOPORTE22, F_ALTA_22 FALTA22, " + 
				"f_historico_22 fhistorico22 " +
				"FROM AD74A22T00 " + 
				"WHERE ID_SOLICITANTE_22 = ? " +  
				" AND (trunc(f_alta_22) <= ? OR trunc(f_alta_22) BETWEEN ? AND ?) " +
				" AND (NVL(TRUNC(f_historico_22), to_date('31/12/2500','dd/mm/yyyy')) >= ? OR trunc(f_historico_22) BETWEEN ? AND ?)";
								
				//"AND (f_historico_22 IS NULL OR (f_historico_22 - 1) >=?) ";

		return this.jdbcTemplate.query(query, this.rwMap, soporte.getIdSolicitante22(), fDesde, fDesde, fHasta, fHasta, fDesde, fHasta);
	}
}
