/*
* Copyright 2021 E.J.I.E., S.A.
*
* Licencia con arreglo a la EUPL, Versión 1.1 exclusivamente (la «Licencia»);
* Solo podrá usarse esta obra si se respeta la Licencia.
* Puede obtenerse una copia de la Licencia en
*
* http://ec.europa.eu/idabc/eupl.html
*
* Salvo cuando lo exija la legislación aplicable o se acuerde por escrito,
* el programa distribuido con arreglo a la Licencia se distribuye «TAL CUAL»,
* SIN GARANT�?AS NI CONDICIONES DE NINGÚN TIPO, ni expresas ni implícitas.
* Véase la Licencia en el idioma concreto que rige los permisos y limitaciones
* que establece la Licencia.
*/
package com.ejie.ad74a.dao.ae01b;

import com.ejie.x38.dao.RowNumResultSetExtractor;
import com.ejie.x38.dto.TableManager;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableRowDto;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
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.model.ae01b.CopagosGestionImpagos;
import com.ejie.ad74a.util.UtilSQL;
import com.ejie.ad74a.util.constantes.MesesEnum;
import com.ejie.ad74a.util.constantes.modelos.TipoSolicitantes;

/**
 * CopagosGestionImpagosDaoImpl generated by UDA, 28-jun-2023 10:59:26.
 * 
 * @author UDA
 */

@Repository
@Transactional
public class CopagosGestionImpagosDaoImpl implements CopagosGestionImpagosDao {

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

	protected static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDLIQUIDACION", "IDBENEFICIARIO",
			"NUMIDENTIFICACION", "NOMBRE", "APELLIDO1", "APELLIDO2", "IDTERRITORIO", "FECINSTALACION",
			"CANTIDADMENSUALIDADES", "DEUDA", "IDENTIFICADOR", "TERRITORIO", "FECALTA", "FECBAJA",
			"CANTIDADMENSUALIDADES", "IDSOLICITANTE", "DEUDA" };

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_GRUPO_IMPAGO = "SEQ_GRUPO_IMPAGO";

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<CopagosGestionImpagos> rwMap = new RowMapper<CopagosGestionImpagos>() {
		public CopagosGestionImpagos mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			CopagosGestionImpagos copagosGestionImpagos = new CopagosGestionImpagos();
			copagosGestionImpagos.setIdSolicitante(resultSet.getString("IDSOLICITANTE"));
			copagosGestionImpagos.setIdentificador(resultSet.getString("IDENTIFICADOR"));
			copagosGestionImpagos.setNombre(resultSet.getString("NOMBRE"));
			copagosGestionImpagos.setApellido1(resultSet.getString("APELLIDO1"));
			copagosGestionImpagos.setApellido2(resultSet.getString("APELLIDO2"));
			copagosGestionImpagos.setTerritorio(resultSet.getString("TERRITORIO"));
			copagosGestionImpagos.setFecDeAlta(resultSet.getDate("FECALTA"));
			copagosGestionImpagos.setFecDeBaja(resultSet.getDate("FECBAJA"));
			copagosGestionImpagos.setTipoIdentificador(resultSet.getString("TIPOIDENTIFICADOR"));
			copagosGestionImpagos.setIdTipoIngreso(resultSet.getLong("TIPOINGRESO"));

			copagosGestionImpagos.setCantidadMensualidades(UtilSQL.getInt(resultSet, "CANTIDADMENSUALIDADES"));
			copagosGestionImpagos.setDeuda(resultSet.getBigDecimal("DEUDA"));

			return copagosGestionImpagos;
		}
	};

	private RowMapper<CopagosGestionImpagos> rwMapMinMaxFechas = new RowMapper<CopagosGestionImpagos>() {
		public CopagosGestionImpagos mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			CopagosGestionImpagos copagosGestionImpagos = new CopagosGestionImpagos();
			copagosGestionImpagos.setMaxMes(resultSet.getInt("MAXMES"));
			copagosGestionImpagos.setMaxAnyo(resultSet.getInt("MAXANYO"));
			copagosGestionImpagos.setMinMes(resultSet.getInt("MINMES"));
			copagosGestionImpagos.setMinAnyo(resultSet.getInt("MINANYO"));

			return copagosGestionImpagos;
		}
	};

	private RowMapper<CopagosGestionImpagos> rwMapPK = new RowMapper<CopagosGestionImpagos>() {
		public CopagosGestionImpagos mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new CopagosGestionImpagos(resultSet.getString("IDBENEFICIARIO"));
		}
	};

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

	/*
	 * OPERACIONES CRUD
	 */

	/**
	 * Finds a list of rows in the CopagosGestionImpagos table.
	 *
	 * @param copagosGestionImpagos CopagosGestionImpagos
	 * @param tableRequestDto       TableRequestDto
	 *
	 * @return List<CopagosGestionImpagos>
	 */
	@Transactional(readOnly = true)
	public List<CopagosGestionImpagos> findAll(CopagosGestionImpagos copagosGestionImpagos,
			TableRequestDto tableRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SOLICITANTE IDSOLICITANTE, t1.IDENTIFICADOR IDENTIFICADOR, t1.TIPO_IDENTIFICADOR TIPOIDENTIFICADOR, t1.NOMBRE NOMBRE, t1.APELLIDO1 APELLIDO1, t1.APELLIDO2 APELLIDO2, t1.TERRITORIO TERRITORIO, t1.F_ALTA FECALTA, t1.F_BAJA FECBAJA, t1.TIPO_INGRESO TIPOINGRESO, t1.CANTIDAD_MENSUALIDADES CANTIDADMENSUALIDADES, t1.DEUDA DEUDA ");

		// FROM
		query.append(" FROM COPAGOS_GESTION_IMPAGOS t1 ");

		// WHERE clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(copagosGestionImpagos);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		List<?> params = (List<?>) mapaWhere.get("params");

		if (tableRequestDto != null) {
			query = TableManager.getPaginationQuery(tableRequestDto, query,
					CopagosGestionImpagosDaoImpl.ORDER_BY_WHITE_LIST);
		}

		return this.jdbcTemplate.query(query.toString(), this.rwMap, params.toArray());
	}

	/*
	 * OPERACIONES CRUD
	 */

	/**
	 * Finds a list of rows in the CopagosGestionImpagos table.
	 *
	 * @param copagosGestionImpagos CopagosGestionImpagos
	 * @param tableRequestDto       TableRequestDto
	 *
	 * @return List<CopagosGestionImpagos>
	 */
	@Transactional(readOnly = true)
	public List<CopagosGestionImpagos> findAllExcel(CopagosGestionImpagos copagosGestionImpagos,
			TableRequestDto tableRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SOLICITANTE IDSOLICITANTE, t1.IDENTIFICADOR IDENTIFICADOR, t1.TIPO_IDENTIFICADOR TIPOIDENTIFICADOR, t1.NOMBRE NOMBRE, t1.APELLIDO1 APELLIDO1, t1.APELLIDO2 APELLIDO2, t1.TERRITORIO TERRITORIO, t1.F_ALTA FECALTA, t1.F_BAJA FECBAJA, t1.TIPO_INGRESO TIPOINGRESO, t1.CANTIDAD_MENSUALIDADES CANTIDADMENSUALIDADES, t1.DEUDA DEUDA ");

		// FROM
		query.append(" FROM COPAGOS_GESTION_IMPAGOS t1 ");

		// WHERE clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(copagosGestionImpagos);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);
		query.append(" ORDER BY t1.IDENTIFICADOR ASC ");

		List<?> params = (List<?>) mapaWhere.get("params");

		if (tableRequestDto != null) {
			query = TableManager.getPaginationQuery(tableRequestDto, query,
					CopagosGestionImpagosDaoImpl.ORDER_BY_WHITE_LIST);
		}

		return this.jdbcTemplate.query(query.toString(), this.rwMap, params.toArray());
	}

	/**
	 * Finds rows in the CopagosGestionImpagos table using like.
	 *
	 * @param copagosGestionImpagos CopagosGestionImpagos
	 * @param tableRequestDto       TableRequestDto
	 * @param startsWith            Boolean
	 *
	 * @return List<CopagosGestionImpagos>
	 */
	@Transactional(readOnly = true)
	public List<CopagosGestionImpagos> findAllLike(CopagosGestionImpagos copagosGestionImpagos,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SOLICITANTE IDSOLICITANTE, t1.IDENTIFICADOR IDENTIFICADOR, t1.TIPO_IDENTIFICADOR TIPOIDENTIFICADOR, t1.NOMBRE NOMBRE, t1.APELLIDO1 APELLIDO1, t1.APELLIDO2 APELLIDO2, t1.TERRITORIO TERRITORIO, t1.F_ALTA FECALTA, t1.F_BAJA FECBAJA, t1.TIPO_INGRESO TIPOINGRESO, t1.CANTIDAD_MENSUALIDADES CANTIDADMENSUALIDADES, t1.DEUDA DEUDA");
		// FROM
		query.append(" FROM COPAGOS_GESTION_IMPAGOS t1 ");

		// WHERE clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(copagosGestionImpagos, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		List<?> params = (List<?>) mapaWhere.get("params");

		if (tableRequestDto != null) {
			query = TableManager.getPaginationQuery(tableRequestDto, query,
					CopagosGestionImpagosDaoImpl.ORDER_BY_WHITE_LIST);
		}

		return this.jdbcTemplate.query(query.toString(), this.rwMap, params.toArray());
	}

	/**
	 * Finds rows in the CopagosGestionImpagos table using like.
	 *
	 * @param copagosGestionImpagos CopagosGestionImpagos
	 * @param tableRequestDto       TableRequestDto
	 * @param startsWith            Boolean
	 *
	 * @return List<CopagosGestionImpagos>
	 */

	@Transactional(readOnly = true)
	public List<CopagosGestionImpagos> findAllLikeExcel(CopagosGestionImpagos copagosGestionImpagos,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SOLICITANTE IDSOLICITANTE, t1.IDENTIFICADOR IDENTIFICADOR, t1.TIPO_IDENTIFICADOR TIPOIDENTIFICADOR, t1.NOMBRE NOMBRE, t1.APELLIDO1 APELLIDO1, t1.APELLIDO2 APELLIDO2, t1.TERRITORIO TERRITORIO, t1.F_ALTA FECALTA, t1.F_BAJA FECBAJA, t1.TIPO_INGRESO TIPOINGRESO, t1.CANTIDAD_MENSUALIDADES CANTIDADMENSUALIDADES, t1.DEUDA DEUDA");
		// FROM
		query.append(" FROM COPAGOS_GESTION_IMPAGOS t1 ");

		// WHERE clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(copagosGestionImpagos, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);
		query.append(" ORDER BY t1.IDENTIFICADOR ASC ");

		List<?> params = (List<?>) mapaWhere.get("params");

		if (tableRequestDto != null) {
			query = TableManager.getPaginationQuery(tableRequestDto, query,
					CopagosGestionImpagosDaoImpl.ORDER_BY_WHITE_LIST);
		}

		return this.jdbcTemplate.query(query.toString(), this.rwMap, params.toArray());
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

	/**
	 * Counts rows in the CopagosGestionImpagos table.
	 *
	 * @param copagosGestionImpagos CopagosGestionImpagos
	 *
	 * @return Long
	 */
	@Transactional(readOnly = true)
	public Long findAllCount(CopagosGestionImpagos copagosGestionImpagos) {
		// SELECT
		StringBuilder query = new StringBuilder("SELECT COUNT(1) FROM COPAGOS_GESTION_IMPAGOS t1 ");

		// WHERE clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(copagosGestionImpagos);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		List<?> params = (List<?>) mapaWhere.get("params");

		return this.jdbcTemplate.queryForObject(query.toString(), params.toArray(), Long.class);
	}

	/**
	 * Counts rows in the CopagosGestionImpagos table using like.
	 *
	 * @param copagosGestionImpagos CopagosGestionImpagos
	 * @param startsWith            Boolean
	 *
	 * @return Long
	 */
	@Transactional(readOnly = true)
	public Long findAllLikeCount(CopagosGestionImpagos copagosGestionImpagos, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder("SELECT COUNT(1) FROM COPAGOS_GESTION_IMPAGOS t1 ");

		// WHERE clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(copagosGestionImpagos, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		List<?> params = (List<?>) mapaWhere.get("params");

		return this.jdbcTemplate.queryForObject(query.toString(), params.toArray(), Long.class);
	}

	public String obtenerDesdeHastaMesAnyo(CopagosGestionImpagos copagosGestionImpagos) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT  t1.id_solicitante_89, MIN(t1.anyo_89) AS MINANYO, MIN(t1.mes_89) KEEP (DENSE_RANK FIRST ORDER BY t1.anyo_89, t1.mes_89) AS MINMES, MAX(t1.anyo_89) AS MAXANYO, MAX(t1.mes_89) KEEP (DENSE_RANK LAST ORDER BY t1.anyo_89, t1.mes_89) AS MAXMES FROM mensualidades t1");
		query.append(" JOIN solicitantes           t3 ON t1.id_solicitante_89 = t3.id_solicitante_01 ");
		query.append(" JOIN direcciones            t4 ON t4.id_direccion_06 = t3.id_direccion_01 ");
		query.append(
				" JOIN (SELECT id_mensualidad_89,IMPORTE_00,COD_ORIGEN_00, SUM(1) OVER (PARTITION BY COD_ORIGEN_00) AS DIVISOR, estado_liq_00, f_limite_pago_00,id_tipo_ingreso_00 FROM (SELECT LIQMAX.id_mensualidad_89,LIQMAX.id_liquidacion_00,id_tipo_ingreso_00,COD_ORIGEN_00,importe_00,estado_liq_00,f_limite_pago_00 FROM ( SELECT t1.id_mensualidad_89,max(tlm.id_liquidacion_00)id_liquidacion_00 FROM mensualidades t1 JOIN solicitantes t3 ON t1.id_solicitante_89 = t3.id_solicitante_01 JOIN AE01B.LIQUIDACIONES tlm on (tlm.cod_origen_00 =t1.id_mensualidad_89 and tlm.id_tipo_ingreso_00  = 2 OR tlm.cod_origen_00 =t1.grupo_impago_89 and tlm.id_tipo_ingreso_00 = 1) group by(t1.id_mensualidad_89)) LIQMAX,AE01B.LIQUIDACIONES LIQTIPOING WHERE liqtipoing.id_liquidacion_00=LIQMAX.id_liquidacion_00))  IMPORTES ON t1.id_mensualidad_89=importes.id_mensualidad_89");
		query.append(" where (t1.exoneracion_89 = 0 or t1.exoneracion_89 is null) ");
		query.append(" AND ( ( t1.anyo_89 = to_number(to_char(t3.f_baja_01, 'YYYY')) ");
		query.append(" AND t1.mes_89 <= to_number(to_char(t3.f_baja_01, 'MM')) ) ");
		query.append(" OR t1.anyo_89 < to_number(to_char(t3.f_baja_01, 'YYYY')) OR t3.f_baja_01 is null ) ");
		query.append(" AND ( IMPORTES.estado_liq_00 = 2 ");
		query.append(
				" OR ( IMPORTES.estado_liq_00 = 1 AND IMPORTES.f_limite_pago_00 < sysdate AND IMPORTES.id_tipo_ingreso_00 = 1)) ");
		query.append(" and t1.id_solicitante_89 = ? ");
		query.append(" GROUP BY t1.id_solicitante_89 ");
		List<CopagosGestionImpagos> lista = this.jdbcTemplate.query(query.toString(), this.rwMapMinMaxFechas,
				copagosGestionImpagos.getIdSolicitante());
		CopagosGestionImpagos copago = lista.get(0);
		String textoDesdeHasta = "";
		if (copago.getMinAnyo().equals(copago.getMaxAnyo()) && copago.getMinMes().equals(copago.getMaxMes())) {
			textoDesdeHasta = this.obtenerMesString(copago.getMinMes()) + "  " + copago.getMinAnyo();
		} else {
			textoDesdeHasta = this.obtenerMesString(copago.getMinMes()) + "  " + copago.getMinAnyo() + " a "
					+ this.obtenerMesString(copago.getMaxMes()) + "  " + copago.getMaxAnyo();
		}

		return textoDesdeHasta;
	}

	private String obtenerMesString(Integer mes) {

		Locale locale = new Locale("es");

		return MesesEnum.getLabelDesc(String.valueOf(mes), locale);

	}

	/**
	 * Reorder the data list of CopagosGestionImpagos selected for rup_table
	 *
	 * @param copagosGestionImpagos CopagosGestionImpagos
	 * @param tableRequestDto       TableRequestDto
	 * @param startsWith            Boolean
	 *
	 * @return List<TableRowDto<CopagosGestionImpagos>>
	 */
//	public List<CopagosGestionImpagos> reorderSelection(CopagosGestionImpagos copagosGestionImpagos,
//			// SELECT
//			StringBuilder query = new StringBuilder(
//					"SELECT t1.ID_SOLICITANTE IDSOLICITANTE, t1.IDENTIFICADOR IDENTIFICADOR, t1.TIPO_IDENTIFICADOR TIPOIDENTIFICADOR, t1.NOMBRE NOMBRE, t1.APELLIDO1 APELLIDO1, t1.APELLIDO2 APELLIDO2, t1.TERRITORIO TERRITORIO, t1.F_ALTA FECALTA, t1.F_BAJA FECBAJA, t1.TIPO_INGRESO TIPOINGRESO, t1.CANTIDAD_MENSUALIDADES CANTIDADMENSUALIDADES, t1.DEUDA DEUDA");
//			// FROM
//			query.append(" FROM COPAGOS_GESTION_IMPAGOS t1 ");
//
//			// WHERE clause & Params
//			Map<String, ?> mapaWhere = this.getWhereLikeMap(copagosGestionImpagos, startsWith);
//			StringBuilder where = new StringBuilder(" WHERE 1=1 ");
//			where.append(mapaWhere.get("query"));
//			query.append(where);
//
//			List<?> params = (List<?>) mapaWhere.get("params");
//
//			if (tableRequestDto != null) {
//				query = TableManager.getPaginationQuery(tableRequestDto, query,
//						CopagosGestionImpagosDaoImpl.ORDER_BY_WHITE_LIST);
//			}
//
//			return this.jdbcTemplate.query(query.toString(), this.rwMap, params.toArray());
//	}

	/*
	 * MÉTODOS PRIVADOS
	 */

	/**
	 * Returns a map with the needed value to create the conditions to filter by the
	 * CopagosGestionImpagos entity
	 *
	 * @param copagosGestionImpagos CopagosGestionImpagos Bean with the criteria
	 *                              values to filter by.
	 *
	 * @return Map created with two keys key query stores the sql query syntax key
	 *         params stores the parameter values to be used in the condition
	 *         sentence.
	 */
	private Map<String, ?> getWhereMap(CopagosGestionImpagos copagosGestionImpagos) {
		StringBuilder where = new StringBuilder(CopagosGestionImpagosDaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (copagosGestionImpagos != null && copagosGestionImpagos.getIdSolicitante() != null) {
			where.append(" AND t1.IDBENEFICIARIO = ?");
			params.add(copagosGestionImpagos.getIdSolicitante());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getIdentificador() != null) {
			where.append(" AND t1.NUMIDENTIFICACION = ?");
			params.add(copagosGestionImpagos.getIdentificador());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getNombre() != null) {
			where.append(" AND t1.NOMBRE = ?");
			params.add(copagosGestionImpagos.getNombre());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getApellido1() != null) {
			where.append(" AND t1.APELLIDO1 = ?");
			params.add(copagosGestionImpagos.getApellido1());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getApellido2() != null) {
			where.append(" AND t1.APELLIDO2 = ?");
			params.add(copagosGestionImpagos.getApellido2());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getTerritorio() != null) {
			where.append(" AND t1.IDTERRITORIO = ?");
			params.add(copagosGestionImpagos.getTerritorio());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecAlta() != null) {
			where.append(" AND t1.FECINSTALACION = ?");
			params.add(copagosGestionImpagos.getFecAlta());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecBaja() != null) {
			where.append(" AND t1.FECBAJA = ?");
			params.add(copagosGestionImpagos.getFecBaja());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getCantidadMensualidades() != null) {
			where.append(" AND t1.NUMMESESPENDPAGO = ?");
			params.add(copagosGestionImpagos.getCantidadMensualidades());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getDeuda() != null) {
			where.append(" AND t1.DEUDA = ?");
			params.add(copagosGestionImpagos.getDeuda());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecInstalacionDesde() != null) {
			where.append(" AND t1.FINSTALACION > ?");
			params.add(copagosGestionImpagos.getFecInstalacionDesde());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecInstalacionHasta() != null) {
			where.append(" AND TRUNC(t1.FINSTALACION) <= ?");
			params.add(copagosGestionImpagos.getFecInstalacionHasta());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecBajaDesde() != null) {
			where.append(" AND t1.FBAJA > ?");
			params.add(copagosGestionImpagos.getFecBajaDesde());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecBajaHasta() != null) {
			where.append(" AND TRUNC(t1.FBAJA) <= ?");
			params.add(copagosGestionImpagos.getFecBajaHasta());
		}

		Map<String, Object> mapWhere = new HashMap<String, Object>();
		mapWhere.put("query", where);
		mapWhere.put("params", params);

		return mapWhere;
	}

	/**
	 * Returns a map with the needed value to create the conditions to filter by the
	 * CopagosGestionImpagos entity
	 *
	 * @param copagosGestionImpagos CopagosGestionImpagos Bean with the criteria
	 *                              values to filter by.
	 * @param startsWith            Boolean
	 *
	 * @return Map created with two keys key query stores the sql query syntax key
	 *         params stores the parameter values to be used in the condition
	 *         sentence.
	 */
	private Map<String, Object> getWhereLikeMap(CopagosGestionImpagos copagosGestionImpagos, Boolean startsWith) {
		StringBuilder where = new StringBuilder(CopagosGestionImpagosDaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (copagosGestionImpagos != null && copagosGestionImpagos.getIdSolicitante() != null) {
			where.append(" AND t1.ID_SOLICITANTE = ?");
			params.add(copagosGestionImpagos.getIdSolicitante());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getIdentificador() != null) {
			where.append(" AND UPPER(t1.IDENTIFICADOR) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(copagosGestionImpagos.getIdentificador().toUpperCase() + "%");
			} else {
				params.add("%" + copagosGestionImpagos.getIdentificador().toUpperCase() + "%");
			}
			where.append(" AND t1.IDENTIFICADOR IS NOT NULL");
		}

		if (copagosGestionImpagos != null && copagosGestionImpagos.getNombre() != null) {
			where.append(" AND UPPER(t1.NOMBRE) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(copagosGestionImpagos.getNombre().toUpperCase() + "%");
			} else {
				params.add("%" + copagosGestionImpagos.getNombre().toUpperCase() + "%");
			}
			where.append(" AND t1.NOMBRE IS NOT NULL");
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getApellido1() != null) {
			where.append(" AND UPPER(t1.APELLIDO1) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(copagosGestionImpagos.getApellido1().toUpperCase() + "%");
			} else {
				params.add("%" + copagosGestionImpagos.getApellido1().toUpperCase() + "%");
			}
			where.append(" AND t1.APELLIDO1 IS NOT NULL");
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getApellido2() != null) {
			where.append(" AND UPPER(t1.APELLIDO2) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(copagosGestionImpagos.getApellido2().toUpperCase() + "%");
			} else {
				params.add("%" + copagosGestionImpagos.getApellido2().toUpperCase() + "%");
			}
			where.append(" AND t1.APELLIDO2 IS NOT NULL");
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getTerritorio() != null) {
			where.append(" AND t1.TERRITORIO = ?");
			params.add(copagosGestionImpagos.getTerritorio());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecAlta() != null) {
			where.append(" AND t1.F_ALTA = ?");
			params.add(copagosGestionImpagos.getFecAlta());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecBaja() != null) {
			where.append(" AND t1.F_BAJA = ?");
			params.add(copagosGestionImpagos.getFecBaja());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getCantidadMensualidades() != null) {
			where.append(" AND t1.NUMMESESPENDPAGO = ?");
			params.add(copagosGestionImpagos.getCantidadMensualidades());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getDeuda() != null) {
			where.append(" AND t1.DEUDA = ?");
			params.add(copagosGestionImpagos.getDeuda());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecInstalacionDesde() != null) {
			where.append(" AND TRUNC(t1.F_ALTA) >= ?");
			params.add(copagosGestionImpagos.getFecInstalacionDesde());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecInstalacionHasta() != null) {
			where.append(" AND TRUNC(t1.F_ALTA) <= ?");
			params.add(copagosGestionImpagos.getFecInstalacionHasta());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecBajaDesde() != null) {
			where.append(" AND TRUNC(t1.F_BAJA) >= ?");
			params.add(copagosGestionImpagos.getFecBajaDesde());
		}
		if (copagosGestionImpagos != null && copagosGestionImpagos.getFecBajaHasta() != null) {
			where.append(" AND TRUNC(t1.F_BAJA) <= ?");
			params.add(copagosGestionImpagos.getFecBajaHasta());
		}
		if (copagosGestionImpagos != null && TipoSolicitantes.ACTIVO_SI.equals(copagosGestionImpagos.getActivo())) {
			where.append(" AND t1.F_BAJA is null ");
		} else if (copagosGestionImpagos != null
				&& TipoSolicitantes.ACTIVO_NO.equals(copagosGestionImpagos.getActivo())) {
			where.append(" AND t1.F_BAJA is not null ");
		}
		Map<String, Object> mapWhere = new HashMap<String, Object>();
		mapWhere.put("query", where);
		mapWhere.put("params", params);

		return mapWhere;
	}

	@Transactional(readOnly = true)
	public Long getLastGrupoImpagoMensualidades() {
		// SELECT
		StringBuilder query = new StringBuilder("SELECT " + SEQUENCE_GRUPO_IMPAGO + ".NEXTVAL FROM DUAL ");

		return this.jdbcTemplate.queryForObject(query.toString(), null, Long.class);
	}

}
