package com.ejie.ad74a.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ad74a.model.MaestroBenefFactu;
import com.ejie.ad74a.util.UtilSQL;
import com.ejie.x38.dao.RowNumResultSetExtractor;
import com.ejie.x38.dto.JerarquiaDto;
import com.ejie.x38.dto.TableManager;
import com.ejie.x38.dto.TableManagerJerarquia;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * MaestroBenefFactuDaoImpl generated by UDA, 01-jun-2023 13:17:33.
 * 
 * @author UDA
 */

@Repository
@Transactional
public class MaestroBenefFactuDaoImpl implements MaestroBenefFactuDao {

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDBENEFFACTURACION43", "DESCES43", "DESCEU43",
			"COSTEFACTURA43", "USUALTA43", "FALTA43", "FBAJA43" };

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_BENEF_FACTURA = "SEQ_BENEF_FACTURA";

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<MaestroBenefFactu> rwMap = new RowMapper<MaestroBenefFactu>() {
		public MaestroBenefFactu mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new MaestroBenefFactu(UtilSQL.getLong(resultSet, "IDBENEFFACTURACION43"),
					resultSet.getString("DESCES43"), resultSet.getString("DESCEU43"),
					resultSet.getBigDecimal("COSTEFACTURA43"), resultSet.getString("USUALTA43"),
					resultSet.getDate("FALTA43"), resultSet.getDate("FBAJA43"));
		}
	};

	private RowMapper<MaestroBenefFactu> rwMapPK = new RowMapper<MaestroBenefFactu>() {
		public MaestroBenefFactu mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new MaestroBenefFactu(UtilSQL.getLong(resultSet, "IDBENEFFACTURACION43"));
		}
	};

	/**
	 * Rowmapper para Jerarquía
	 *
	 * @param dataSource DataSource
	 * @return
	 */
	private RowMapper<JerarquiaDto<MaestroBenefFactu>> rwMapJerarquia = new RowMapper<JerarquiaDto<MaestroBenefFactu>>() {
		public JerarquiaDto<MaestroBenefFactu> mapRow(ResultSet resultSet, int rowNum) throws SQLException {

			MaestroBenefFactu maestrobeneffactu = new MaestroBenefFactu(
					UtilSQL.getLong(resultSet, "IDBENEFFACTURACION43"), resultSet.getString("DESCES43"),
					resultSet.getString("DESCEU43"), resultSet.getBigDecimal("COSTEFACTURA43"),
					resultSet.getString("USUALTA43"), resultSet.getDate("FALTA43"), resultSet.getDate("FBAJA43"));

			JerarquiaDto<MaestroBenefFactu> jerarquia = new JerarquiaDto<MaestroBenefFactu>();
			jerarquia.setModel(maestrobeneffactu);
			jerarquia.setLevel(resultSet.getBigDecimal("LEVEL").intValue());
			jerarquia.setParentNodes(resultSet.getString("PARENTNODES"));
			jerarquia.setIsLeaf(Boolean.parseBoolean(resultSet.getString("ISLEAF")));
			jerarquia.setFilter(Boolean.parseBoolean(resultSet.getString("FILTER")));
			return jerarquia;
		}
	};

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

	/*
	 * OPERACIONES CRUD
	 */

	/**
	 * Inserts a single row in the MaestroBenefFactu table.
	 *
	 * @param maestrobeneffactu MaestroBenefFactu
	 * @return MaestroBenefFactu
	 */
	public MaestroBenefFactu add(MaestroBenefFactu maestrobeneffactu) {

		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();

		String query = "INSERT INTO MAESTRO_BENEF_FACTU (ID_BENEF_FACTURACION_43, DESC_ES_43, DESC_EU_43, COSTE_FACTURA_43, USU_ALTA_43, F_ALTA_43, F_BAJA_43) "
				+ "VALUES (" + SEQUENCE_BENEF_FACTURA + ".NEXTVAL,:descEs,:descEu,:coste,:usuAlta,sysdate,:fBaja)";

		MapSqlParameterSource parameters = new MapSqlParameterSource()
				.addValue("descEs", maestrobeneffactu.getDescEs43()).addValue("descEu", maestrobeneffactu.getDescEu43())
				.addValue("coste", maestrobeneffactu.getCosteFactura43())
				.addValue("usuAlta", maestrobeneffactu.getUsuAlta43())
				.addValue("fBaja", maestrobeneffactu.getfBaja43());

		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
		namedParameterJdbcTemplate.update(query, parameters, generatedKeyHolder,
				new String[] { "ID_BENEF_FACTURACION_43" });
		maestrobeneffactu.setIdBenefFacturacion43(generatedKeyHolder.getKey().longValue());

		return maestrobeneffactu;
	}

	/**
	 * Updates a single row in the MaestroBenefFactu table.
	 *
	 * @param maestrobeneffactu MaestroBenefFactu
	 * @return MaestroBenefFactu
	 */
	public MaestroBenefFactu update(MaestroBenefFactu maestrobeneffactu) {
		String query = "UPDATE MAESTRO_BENEF_FACTU SET DESC_ES_43=?, DESC_EU_43=?, COSTE_FACTURA_43=?, USU_ALTA_43=?, F_ALTA_43=?, F_BAJA_43=? WHERE ID_BENEF_FACTURACION_43=?";
		this.jdbcTemplate.update(query, maestrobeneffactu.getDescEs43(), maestrobeneffactu.getDescEu43(),
				maestrobeneffactu.getCosteFactura43(), maestrobeneffactu.getUsuAlta43(), maestrobeneffactu.getfAlta43(),
				maestrobeneffactu.getfBaja43(), maestrobeneffactu.getIdBenefFacturacion43());
		return maestrobeneffactu;
	}

	/**
	 * Finds a single row in the MaestroBenefFactu table.
	 *
	 * @param maestrobeneffactu MaestroBenefFactu
	 * @return MaestroBenefFactu
	 */
	@Transactional(readOnly = true)
	public MaestroBenefFactu find(MaestroBenefFactu maestrobeneffactu) {
		String query = "SELECT t1.ID_BENEF_FACTURACION_43 IDBENEFFACTURACION43, t1.DESC_ES_43 DESCES43, t1.DESC_EU_43 DESCEU43, t1.COSTE_FACTURA_43 COSTEFACTURA43, t1.USU_ALTA_43 USUALTA43, t1.F_ALTA_43 FALTA43, t1.F_BAJA_43 FBAJA43 FROM MAESTRO_BENEF_FACTU t1  WHERE t1.ID_BENEF_FACTURACION_43 = ?  ";

		List<MaestroBenefFactu> maestrobeneffactuList = this.jdbcTemplate.query(query, this.rwMap,
				maestrobeneffactu.getIdBenefFacturacion43());
		return (MaestroBenefFactu) DataAccessUtils.uniqueResult(maestrobeneffactuList);
	}

	/**
	 * Removes a single row in the MaestroBenefFactu table.
	 *
	 * @param maestrobeneffactu MaestroBenefFactu
	 * @return
	 */
	public void remove(MaestroBenefFactu maestrobeneffactu) {
		String query = "DELETE FROM MAESTRO_BENEF_FACTU WHERE ID_BENEF_FACTURACION_43=?";
		this.jdbcTemplate.update(query, maestrobeneffactu.getIdBenefFacturacion43());
	}

	/**
	 * Exporta Datos al clipBoard
	 *
	 */
	public List<MaestroBenefFactu> getMultiple(MaestroBenefFactu filterMaestroBenefFactu,
			TableRequestDto tableRequestDto, Boolean startsWith) {

		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_BENEF_FACTURACION_43 IDBENEFFACTURACION43, t1.DESC_ES_43 DESCES43, t1.DESC_EU_43 DESCEU43, t1.COSTE_FACTURA_43 COSTEFACTURA43, t1.USU_ALTA_43 USUALTA43, t1.F_ALTA_43 FALTA43, t1.F_BAJA_43 FBAJA43");
		query.append(" FROM MAESTRO_BENEF_FACTU t1  ");
		// Where clause & Params
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterMaestroBenefFactu, startsWith);
		StringBuilder where = new StringBuilder(query + " WHERE 1=1 ");
		where.append(mapaWhere.get("query"));

		@SuppressWarnings("unchecked")
		List<Object> params = (List<Object>) mapaWhere.get("params");

		StringBuilder sbMultipleSQL = TableManager.getSelectMultipleQuery(tableRequestDto, MaestroBenefFactu.class,
				params, "ID_BENEF_FACTURACION_43");
		where.append(sbMultipleSQL);

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

	/**
	 * Finds a list of rows in the MaestroBenefFactu table.
	 *
	 * @param maestrobeneffactu MaestroBenefFactu
	 * @param tableRequestDto   TableRequestDto
	 * @return List<MaestroBenefFactu>
	 */
	@Transactional(readOnly = true)
	public List<MaestroBenefFactu> findAll(MaestroBenefFactu maestrobeneffactu, TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_BENEF_FACTURACION_43 IDBENEFFACTURACION43,t1.DESC_ES_43 DESCES43,t1.DESC_EU_43 DESCEU43,t1.COSTE_FACTURA_43 COSTEFACTURA43,t1.USU_ALTA_43 USUALTA43,t1.F_ALTA_43 FALTA43,t1.F_BAJA_43 FBAJA43 ");
		query.append("FROM MAESTRO_BENEF_FACTU t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(maestrobeneffactu);
		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,
					MaestroBenefFactuDaoImpl.ORDER_BY_WHITE_LIST);
		}

		return (List<MaestroBenefFactu>) this.jdbcTemplate.query(query.toString(), this.rwMap, params.toArray());
	}

	/**
	 * Finds rows in the MaestroBenefFactu table using like.
	 *
	 * @param maestrobeneffactu MaestroBenefFactu
	 * @param tableRequestDto   TableRequestDto
	 * @param startsWith        Boolean
	 * @return List<MaestroBenefFactu>
	 */
	@Transactional(readOnly = true)
	public List<MaestroBenefFactu> findAllLike(MaestroBenefFactu maestrobeneffactu, TableRequestDto tableRequestDto,
			Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_BENEF_FACTURACION_43 IDBENEFFACTURACION43,t1.DESC_ES_43 DESCES43,t1.DESC_EU_43 DESCEU43,t1.COSTE_FACTURA_43 COSTEFACTURA43,t1.USU_ALTA_43 USUALTA43,t1.F_ALTA_43 FALTA43,t1.F_BAJA_43 FBAJA43 ");
		query.append("FROM MAESTRO_BENEF_FACTU t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrobeneffactu, 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,
					MaestroBenefFactuDaoImpl.ORDER_BY_WHITE_LIST);
		}

		return (List<MaestroBenefFactu>) this.jdbcTemplate.query(query.toString(), this.rwMap, params.toArray());
	}

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereMap(maestrobeneffactu);
		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 MaestroBenefFactu table using like.
	 *
	 * @param maestrobeneffactu MaestroBenefFactu
	 * @param startsWith        Boolean
	 * @return Long
	 */
	@Transactional(readOnly = true)
	public Long findAllLikeCount(MaestroBenefFactu maestrobeneffactu, Boolean startsWith) {
		StringBuilder query = new StringBuilder("SELECT COUNT(1) FROM MAESTRO_BENEF_FACTU t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrobeneffactu, 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);
	}

	/**
	 * Reorder the data list of MaestroBenefFactu selected for rup_table
	 *
	 * @param maestrobeneffactu MaestroBenefFactu
	 * @param tableRequestDto   TableRequestDto
	 * @param startsWith        Boolean
	 * @return List<TableRowDto<MaestroBenefFactu>>
	 */
	@Override
	public List<TableRowDto<MaestroBenefFactu>> reorderSelection(MaestroBenefFactu maestrobeneffactu,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_BENEF_FACTURACION_43 IDBENEFFACTURACION43,t1.DESC_ES_43 DESCES43,t1.DESC_EU_43 DESCEU43,t1.COSTE_FACTURA_43 COSTEFACTURA43,t1.USU_ALTA_43 USUALTA43,t1.F_ALTA_43 FALTA43,t1.F_BAJA_43 FBAJA43 ");
		// FROM
		query.append("FROM MAESTRO_BENEF_FACTU t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrobeneffactu, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhere.get("query"));
		query.append(where);

		// Parámetros de filtrado
		@SuppressWarnings("unchecked")
		List<Object> filterParamList = (List<Object>) mapaWhere.get("params");

		// SQL para la reordenación
		StringBuilder sbReorderSelectionSQL = TableManager.getReorderQuery(query, tableRequestDto,
				MaestroBenefFactu.class, filterParamList, "IDBENEFFACTURACION43");

		return this.jdbcTemplate.query(sbReorderSelectionSQL.toString(),
				new RowNumResultSetExtractor<MaestroBenefFactu>(this.rwMapPK, tableRequestDto),
				filterParamList.toArray());
	}

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams    MaestroBenefFactu
	 * @param searchParams    MaestroBenefFactu
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<MaestroBenefFactu>>
	 */
	@Override
	public List<TableRowDto<MaestroBenefFactu>> search(MaestroBenefFactu filterParams, MaestroBenefFactu searchParams,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_BENEF_FACTURACION_43 IDBENEFFACTURACION43,t1.DESC_ES_43 DESCES43,t1.DESC_EU_43 DESCEU43,t1.COSTE_FACTURA_43 COSTEFACTURA43,t1.USU_ALTA_43 USUALTA43,t1.F_ALTA_43 FALTA43,t1.F_BAJA_43 FBAJA43 ");
		// FROM
		query.append("FROM MAESTRO_BENEF_FACTU t1 ");
		// TABLAS_ALIAS
		List<String> from_alias = new ArrayList<String>();
		from_alias.add(" t1 ");

		// FILTRADO
		Map<String, ?> mapaWhereFilter = this.getWhereLikeMap(filterParams, startsWith);
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		where.append(mapaWhereFilter.get("query"));
		query.append(where);
		// Filter params
		@SuppressWarnings("unchecked")
		List<Object> filterParamList = (List<Object>) mapaWhereFilter.get("params");

		// BUSQUEDA
		Map<String, Object> mapaWhereSearch = this.getWhereLikeMap(searchParams, startsWith);
		String searchSQL = ((StringBuilder) mapaWhereSearch.get("query")).toString();
		// Search params
		@SuppressWarnings("unchecked")
		List<Object> searchParamList = (List<Object>) mapaWhereSearch.get("params");

		// SQL
		StringBuilder sbReorderSelectionSQL = TableManager.getSearchQuery(query, tableRequestDto,
				MaestroBenefFactu.class, filterParamList, searchSQL, searchParamList, from_alias,
				"IDBENEFFACTURACION43");

		return this.jdbcTemplate.query(sbReorderSelectionSQL.toString(),
				new RowNumResultSetExtractor<MaestroBenefFactu>(this.rwMapPK, tableRequestDto),
				filterParamList.toArray());
	}

	/**
	 * Remove multiple method for rup_table
	 *
	 * @param tableRequestDto TableRequestDto
	 */
	@Override
	public void removeMultiple(TableRequestDto tableRequestDto) {
		StringBuilder sbRemoveMultipleSQL = TableManager.getRemoveMultipleQuery(tableRequestDto,
				MaestroBenefFactu.class, "MAESTRO_BENEF_FACTU", new String[] { "IDBENEFFACTURACION43" });

		List<String> params = tableRequestDto.getMultiselection().getSelectedIds();

		this.jdbcTemplate.update(sbRemoveMultipleSQL.toString(), params.toArray());
	}

	/**
	 * Filter in the hierarchical MaestroBenefFactu table.
	 *
	 * @param filterMaestroBenefFactu MaestroBenefFactu
	 * @param tableRequestDto         TableRequestDto
	 * @return List<JerarquiaDto<MaestroBenefFactu>>
	 */
	@Override
	public List<JerarquiaDto<MaestroBenefFactu>> findAllLikeJerarquia(MaestroBenefFactu filterMaestroBenefFactu,
			TableRequestDto tableRequestDto) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_BENEF_FACTURACION_43 IDBENEFFACTURACION43,t1.DESC_ES_43 DESCES43,t1.DESC_EU_43 DESCEU43,t1.COSTE_FACTURA_43 COSTEFACTURA43,t1.USU_ALTA_43 USUALTA43,t1.F_ALTA_43 FALTA43,t1.F_BAJA_43 FBAJA43 ");
		// FROM
		// StringBuilder from = new StringBuilder("FROM MAESTRO_BENEF_FACTU t1 ");
		List<String> from = new ArrayList<String>();
		from.add("MAESTRO_BENEF_FACTU");
		// TABLAS_ALIAS
		List<String> from_alias = new ArrayList<String>();
		from_alias.add(" t1 ");

		// JOINS TABLAS
		/*
		 * Ejemplo de como indicar joins entre las tablas de la query
		 */
		/*
		 * StringBuilder joins = new StringBuilder("");
		 * joins.append("AND T1.ID=T2.ID_T1");
		 */

		// CONDICIONES (negocio)
		/*
		 * Ejemplo de como incluir condiciones de negocio en la consulta de la jerarquía
		 */
		/*
		 * StringBuilder businessFilters = new StringBuilder(); List<Object>
		 * businessParams = new ArrayList<Object>();
		 * businessFilters.append("   AND t1.EJIE = ?  "); businessParams.add("1");
		 */

		// FILTRO
		Map<String, ?> mapaWhere = this.getWhereLikeMap(filterMaestroBenefFactu, false);

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada por
		// defecto (ID_PADRE)
		query = TableManagerJerarquia.getQuery(tableRequestDto, query, mapaWhere, "IDBENEFFACTURACION43", "ID_PADRE",
				"NOMBRE", from, from_alias);

		/*
		 * Ejemplo de llamada en caso de indicar joins entre tablas y condiciones de
		 * negocio
		 */
//		query = TableManagerJerarquia.getQuery(tableRequestDto, query, mapaWhere, "ID", "ID_PADRE", "NOMBRE", from, from_alias, joins, businessFilters, businessParams);

		// PAGINACION
		if (tableRequestDto != null) {
			query = TableManagerJerarquia.getPaginationQuery(tableRequestDto, query,
					MaestroBenefFactuDaoImpl.ORDER_BY_WHITE_LIST);
		}

		List<?> params = (List<?>) mapaWhere.get("params");
		return this.jdbcTemplate.query(query.toString(), this.rwMapJerarquia, params.toArray());
	}

	/**
	 * Count rows in hierarchy
	 *
	 * @param filterMaestroBenefFactu MaestroBenefFactu
	 * @param tableRequestDto         TableRequestDto
	 * @return Long
	 */
	@Override
	public Long findAllLikeCountJerarquia(MaestroBenefFactu filterMaestroBenefFactu, TableRequestDto tableRequestDto) {
		// FROM
		// StringBuilder from = new StringBuilder("FROM MAESTRO_BENEF_FACTU t1 ");
		List<String> from = new ArrayList<String>();
		from.add("MAESTRO_BENEF_FACTU");

		// TABLAS_ALIAS
		List<String> from_alias = new ArrayList<String>();
		from_alias.add(" t1 ");

		// JOINS TABLAS
		/*
		 * Ejemplo de como indicar joins entre las tablas de la query
		 */
		/*
		 * StringBuilder joins = new StringBuilder("");
		 * joins.append("AND T1.ID=T2.ID_T1");
		 */

		// CONDICIONES (negocio)
		/*
		 * Ejemplo de como incluir condiciones de negocio en la consulta de la jerarquía
		 */
		/*
		 * StringBuilder businessFilters = new StringBuilder(); List<Object>
		 * businessParams = new ArrayList<Object>();
		 * businessFilters.append("   AND t1.EJIE = ?  "); businessParams.add("1");
		 */

		// FILTRO
		Map<String, ?> mapaWhere = this.getWhereLikeMap(filterMaestroBenefFactu, false);

		// JERARQUIA
		// Especificar la columna padre correspondiente en lugar de la generada por
		// defecto (ID_PADRE)
		StringBuilder query = TableManagerJerarquia.getQueryCount(tableRequestDto, mapaWhere, "IDBENEFFACTURACION43",
				"ID_PADRE", from, from_alias);

		/*
		 * Ejemplo de llamada en caso de indicar joins entre tablas y condiciones de
		 * negocio
		 */
//		StringBuilder query = TableManagerJerarquia.getQueryCount(tableRequestDto, mapaWhere, "ID", "ID_PADRE", from, from_alias, joins, businessFilters, businessParams);

		List<?> params = (List<?>) mapaWhere.get("params");
		return this.jdbcTemplate.queryForObject(query.toString(), params.toArray(), Long.class);
	}

	/**
	 * Find dependent hierarchical rows
	 *
	 * @param filterMaestroBenefFactu MaestroBenefFactu
	 * @param tableRequestDto         TableRequestDto
	 * @return List<TableRowDto<MaestroBenefFactu>>
	 */
	@Override
	public List<TableRowDto<MaestroBenefFactu>> findAllChild(MaestroBenefFactu filterMaestroBenefFactu,
			TableRequestDto tableRequestDto) {

		// FROM
		// StringBuilder from = new StringBuilder("FROM MAESTRO_BENEF_FACTU t1 ");
		List<String> from = new ArrayList<String>();
		from.add("MAESTRO_BENEF_FACTU");

		// TABLAS_ALIAS
		List<String> from_alias = new ArrayList<String>();
		from_alias.add(" t1 ");

		// JOINS TABLAS
		/*
		 * Ejemplo de como indicar joins entre las tablas de la query
		 */
		/*
		 * StringBuilder joins = new StringBuilder("");
		 * joins.append("AND T1.ID=T2.ID_T1");
		 */

		// CONDICIONES (negocio)
		/*
		 * Ejemplo de como incluir condiciones de negocio en la consulta de la jerarquía
		 */
		/*
		 * StringBuilder businessFilters = new StringBuilder(); List<Object>
		 * businessParams = new ArrayList<Object>();
		 * businessFilters.append("   AND t1.EJIE = ?  "); businessParams.add("1");
		 */

		// FILTRO
		Map<String, ?> mapaWhere = this.getWhereLikeMap(filterMaestroBenefFactu, false);

		// MULTISELECCION
		// Especificar la columna padre correspondiente en lugar de la generada por
		// defecto (ID_PADRE)
		StringBuilder query = TableManagerJerarquia.getQueryChildren(tableRequestDto, mapaWhere, "IDBENEFFACTURACION43",
				"ID_PADRE", from, from_alias);

		/*
		 * Ejemplo de llamada en caso de indicar joins entre tablas y condiciones de
		 * negocio
		 */
//		StringBuilder query = TableManagerJerarquia.getQueryChildren(tableRequestDto, mapaWhere, "ID", "ID_PADRE", from, from_alias, joins, businessFilters, businessParams);

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

		return this.jdbcTemplate.query(query.toString(),
				new RowNumResultSetExtractor<MaestroBenefFactu>(this.rwMapPK, "IDBENEFFACTURACION43"),
				params.toArray());
	}

	/*
	 * MÉTODOS PRIVADOS
	 */

	/**
	 * Returns a map with the needed value to create the conditions to filter by the
	 * MaestroBenefFactu entity
	 *
	 * @param maestrobeneffactu MaestroBenefFactu 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.
	 */
	// CHECKSTYLE:OFF CyclomaticComplexity - Generación de código de UDA
	private Map<String, ?> getWhereMap(MaestroBenefFactu maestrobeneffactu) {

		StringBuilder where = new StringBuilder(MaestroBenefFactuDaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (maestrobeneffactu != null && maestrobeneffactu.getIdBenefFacturacion43() != null) {
			where.append(" AND t1.ID_BENEF_FACTURACION_43 = ?");
			params.add(maestrobeneffactu.getIdBenefFacturacion43());
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getDescEs43() != null) {
			where.append(" AND t1.DESC_ES_43 = ?");
			params.add(maestrobeneffactu.getDescEs43());
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getDescEu43() != null) {
			where.append(" AND t1.DESC_EU_43 = ?");
			params.add(maestrobeneffactu.getDescEu43());
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getCosteFactura43() != null) {
			where.append(" AND t1.COSTE_FACTURA_43 = ?");
			params.add(maestrobeneffactu.getCosteFactura43());
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getUsuAlta43() != null) {
			where.append(" AND t1.USU_ALTA_43 = ?");
			params.add(maestrobeneffactu.getUsuAlta43());
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getfAlta43() != null) {
			where.append(" AND t1.F_ALTA_43 = ?");
			params.add(maestrobeneffactu.getfAlta43());
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getfBaja43() != null) {
			where.append(" AND t1.F_BAJA_43 = ?");
			params.add(maestrobeneffactu.getfBaja43());
		}

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

		return mapWhere;
	}
	// CHECKSTYLE:ON CyclomaticComplexity - Generación de código de UDA

	/**
	 * Returns a map with the needed value to create the conditions to filter by the
	 * MaestroBenefFactu entity
	 *
	 * @param maestrobeneffactu MaestroBenefFactu 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.
	 */
	// CHECKSTYLE:OFF CyclomaticComplexity - Generación de código de UDA
	private Map<String, Object> getWhereLikeMap(MaestroBenefFactu maestrobeneffactu, Boolean startsWith) {

		StringBuilder where = new StringBuilder(MaestroBenefFactuDaoImpl.STRING_BUILDER_INIT);
		List<Object> params = new ArrayList<Object>();

		if (maestrobeneffactu != null && maestrobeneffactu.getIdBenefFacturacion43() != null) {
			where.append(" AND t1.ID_BENEF_FACTURACION_43 = ?");
			params.add(maestrobeneffactu.getIdBenefFacturacion43());
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getDescEs43() != null) {
			where.append(" AND UPPER(t1.DESC_ES_43) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrobeneffactu.getDescEs43().toUpperCase() + "%");
			} else {
				params.add("%" + maestrobeneffactu.getDescEs43().toUpperCase() + "%");
			}
			where.append(" AND t1.DESC_ES_43 IS NOT NULL");
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getDescEu43() != null) {
			where.append(" AND UPPER(t1.DESC_EU_43) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrobeneffactu.getDescEu43().toUpperCase() + "%");
			} else {
				params.add("%" + maestrobeneffactu.getDescEu43().toUpperCase() + "%");
			}
			where.append(" AND t1.DESC_EU_43 IS NOT NULL");
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getCosteFactura43() != null) {
			where.append(" AND t1.COSTE_FACTURA_43 = ?");
			params.add(maestrobeneffactu.getCosteFactura43());
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getUsuAlta43() != null) {
			where.append(" AND UPPER(t1.USU_ALTA_43) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrobeneffactu.getUsuAlta43().toUpperCase() + "%");
			} else {
				params.add("%" + maestrobeneffactu.getUsuAlta43().toUpperCase() + "%");
			}
			where.append(" AND t1.USU_ALTA_43 IS NOT NULL");
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getfAlta43() != null) {
			where.append(" AND t1.F_ALTA_43 = ?");
			params.add(maestrobeneffactu.getfAlta43());
		}
		if (maestrobeneffactu != null && maestrobeneffactu.getfBaja43() != null) {
			where.append(" AND t1.F_BAJA_43 = ?");
			params.add(maestrobeneffactu.getfBaja43());
		}

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

		return mapWhere;
	}
	// CHECKSTYLE:ON CyclomaticComplexity - Generación de código de UDA

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