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.MaestroSubtiposAviso;
import com.ejie.ad74a.model.MaestroTiposAviso;
import com.ejie.ad74a.util.UtilSQL;
import com.ejie.ad74a.util.constantes.OrigenAvisosEnum;
import com.ejie.x38.dao.RowNumResultSetExtractor;
import com.ejie.x38.dto.TableManager;
import com.ejie.x38.dto.TableRequestDto;
import com.ejie.x38.dto.TableRowDto;

/**
 * MaestroSubtiposAvisoDaoImpl generated by UDA, 12-may-2023 11:16:38.
 * 
 * @author UDA
 */

@Repository
@Transactional
public class MaestroSubtiposAvisoDaoImpl implements MaestroSubtiposAvisoDao {

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

	public static final String[] ORDER_BY_WHITE_LIST = new String[] { "IDSUBTIPOAVISO41", "IDTIPOAVISO41", "DESCES41",
			"DESCEU41", "FBAJA41" };

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_MAESTRO_SUBTIPOS_AVISO = "SEQ_MAESTRO_SUBTIPOS_AVISO";

	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<MaestroSubtiposAviso> rwMap = new RowMapper<MaestroSubtiposAviso>() {
		public MaestroSubtiposAviso mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			MaestroSubtiposAviso maestroSubtiposAviso = new MaestroSubtiposAviso(
					UtilSQL.getLong(resultSet, "IDSUBTIPOAVISO41"));
			maestroSubtiposAviso
					.setMaestroTiposAviso(new MaestroTiposAviso(UtilSQL.getLong(resultSet, "IDTIPOAVISO41")));
			maestroSubtiposAviso.setDescEs41(resultSet.getString("DESCES41"));
			maestroSubtiposAviso.setDescEu41(resultSet.getString("DESCEU41"));
			maestroSubtiposAviso.setfBaja41(resultSet.getDate("FBAJA41"));
			return maestroSubtiposAviso;
		}
	};

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

	/**
	 * 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 MaestroSubtiposAviso table.
	 *
	 * @param maestrosubtiposaviso MaestroSubtiposAviso
	 * @return MaestroSubtiposAviso
	 */
	public MaestroSubtiposAviso add(MaestroSubtiposAviso maestrosubtiposaviso) {

		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();

		String query = "INSERT INTO MAESTRO_SUBTIPOS_AVISO (ID_SUB_TIPO_AVISO_41, ID_TIPO_AVISO_41, DESC_ES_41, DESC_EU_41, F_BAJA_41) "
				+ "VALUES (" + SEQUENCE_MAESTRO_SUBTIPOS_AVISO + ".NEXTVAL,:idTipoAviso,:descEs,:descEu,:fBaja)";

		Object getMaestroTipoAvisoIdTipoAviso40Aux = null;
		if (maestrosubtiposaviso.getMaestroTiposAviso() != null
				&& maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40() != null) {
			getMaestroTipoAvisoIdTipoAviso40Aux = maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40();
		}

		MapSqlParameterSource parameters = new MapSqlParameterSource()
				.addValue("idTipoAviso", getMaestroTipoAvisoIdTipoAviso40Aux)
				.addValue("descEs", maestrosubtiposaviso.getDescEs41())
				.addValue("descEu", maestrosubtiposaviso.getDescEu41())
				.addValue("fBaja", maestrosubtiposaviso.getfBaja41());

		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
		namedParameterJdbcTemplate.update(query, parameters, generatedKeyHolder,
				new String[] { "ID_SUB_TIPO_AVISO_41" });
		maestrosubtiposaviso.setIdSubTipoAviso41(generatedKeyHolder.getKey().longValue());

		return maestrosubtiposaviso;
	}

	/**
	 * Updates a single row in the MaestroSubtiposAviso table.
	 *
	 * @param maestrosubtiposaviso MaestroSubtiposAviso
	 * @return MaestroSubtiposAviso
	 */
	public MaestroSubtiposAviso update(MaestroSubtiposAviso maestrosubtiposaviso) {
		String query = "UPDATE MAESTRO_SUBTIPOS_AVISO SET ID_TIPO_AVISO_41=?, DESC_ES_41=?, DESC_EU_41=?, F_BAJA_41=? WHERE ID_SUB_TIPO_AVISO_41=?";
		Object getMaestroTipoAvisoIdTipoAviso40Aux = null;
		if (maestrosubtiposaviso.getMaestroTiposAviso() != null
				&& maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40() != null) {
			getMaestroTipoAvisoIdTipoAviso40Aux = maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40();
		}
		this.jdbcTemplate.update(query, getMaestroTipoAvisoIdTipoAviso40Aux, maestrosubtiposaviso.getDescEs41(),
				maestrosubtiposaviso.getDescEu41(), maestrosubtiposaviso.getfBaja41(),
				maestrosubtiposaviso.getIdSubTipoAviso41());
		return maestrosubtiposaviso;
	}

	/**
	 * Finds a single row in the MaestroSubtiposAviso table.
	 *
	 * @param maestrosubtiposaviso MaestroSubtiposAviso
	 * @return MaestroSubtiposAviso
	 */
	@Transactional(readOnly = true)
	public MaestroSubtiposAviso find(MaestroSubtiposAviso maestrosubtiposaviso) {
		String query = "SELECT t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41, t1.ID_TIPO_AVISO_41 IDTIPOAVISO41, t1.DESC_ES_41 DESCES41, t1.DESC_EU_41 DESCEU41, t1.F_BAJA_41 FBAJA41 FROM MAESTRO_SUBTIPOS_AVISO t1  WHERE t1.ID_SUB_TIPO_AVISO_41 = ?  ";

		List<MaestroSubtiposAviso> maestrosubtiposavisoList = this.jdbcTemplate.query(query, this.rwMap,
				maestrosubtiposaviso.getIdSubTipoAviso41());
		return (MaestroSubtiposAviso) DataAccessUtils.uniqueResult(maestrosubtiposavisoList);
	}

	/**
	 * Removes a single row in the MaestroSubtiposAviso table.
	 *
	 * @param maestrosubtiposaviso MaestroSubtiposAviso
	 * @return
	 */
	public void remove(MaestroSubtiposAviso maestrosubtiposaviso) {
		String query = "DELETE FROM MAESTRO_SUBTIPOS_AVISO WHERE ID_SUB_TIPO_AVISO_41=?";
		this.jdbcTemplate.update(query, maestrosubtiposaviso.getIdSubTipoAviso41());
	}

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

		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41, t1.ID_TIPO_AVISO_41 IDTIPOAVISO41, t1.DESC_ES_41 DESCES41, t1.DESC_EU_41 DESCEU41, t1.F_BAJA_41 FBAJA41");
		query.append(" FROM MAESTRO_SUBTIPOS_AVISO t1  ");
		// Where clause & Params
		Map<String, Object> mapaWhere = this.getWhereLikeMap(filterMaestroSubtiposAviso, 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, MaestroSubtiposAviso.class,
				params, "ID_SUB_TIPO_AVISO_41");
		where.append(sbMultipleSQL);

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

	/**
	 * Finds a list of rows in the MaestroSubtiposAviso table.
	 *
	 * @param maestrosubtiposaviso MaestroSubtiposAviso
	 * @param tableRequestDto      TableRequestDto
	 * @return List<MaestroSubtiposAviso>
	 */
	@Transactional(readOnly = true)
	public List<MaestroSubtiposAviso> findAll(MaestroSubtiposAviso maestrosubtiposaviso,
			TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO t1 ");

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

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

	@Transactional(readOnly = true)
	public List<MaestroSubtiposAviso> findPorUsuarios(MaestroSubtiposAviso maestrosubtiposaviso,
			TableRequestDto tableRequestDto, String usuario) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereUsuMap(maestrosubtiposaviso, usuario);
		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,
					MaestroSubtiposAvisoDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	@Transactional(readOnly = true)
	public List<MaestroSubtiposAviso> findManuales(MaestroSubtiposAviso maestrosubtiposaviso,
			TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO t1 ");

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

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

	/**
	 * Finds rows in the MaestroSubtiposAviso table using like.
	 *
	 * @param maestrosubtiposaviso MaestroSubtiposAviso
	 * @param tableRequestDto      TableRequestDto
	 * @param startsWith           Boolean
	 * @return List<MaestroSubtiposAviso>
	 */
	@Transactional(readOnly = true)
	public List<MaestroSubtiposAviso> findAllLike(MaestroSubtiposAviso maestrosubtiposaviso,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO t1 ");

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

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

	/*
	 * OPERACIONES RUP_TABLE
	 */

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

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

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrosubtiposaviso, 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 MaestroSubtiposAviso selected for rup_table
	 *
	 * @param maestrosubtiposaviso MaestroSubtiposAviso
	 * @param tableRequestDto      TableRequestDto
	 * @param startsWith           Boolean
	 * @return List<TableRowDto<MaestroSubtiposAviso>>
	 */
	@Override
	public List<TableRowDto<MaestroSubtiposAviso>> reorderSelection(MaestroSubtiposAviso maestrosubtiposaviso,
			TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		// FROM
		query.append("FROM MAESTRO_SUBTIPOS_AVISO t1 ");
		// FILTRADO
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereLikeMap(maestrosubtiposaviso, 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,
				MaestroSubtiposAviso.class, filterParamList, "IDSUBTIPOAVISO41");

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

	/**
	 * Search method for rup_table
	 *
	 * @param filterParams    MaestroSubtiposAviso
	 * @param searchParams    MaestroSubtiposAviso
	 * @param tableRequestDto TableRequestDto
	 * @param startsWith      Boolean
	 * @return List<TableRowDto<MaestroSubtiposAviso>>
	 */
	@Override
	public List<TableRowDto<MaestroSubtiposAviso>> search(MaestroSubtiposAviso filterParams,
			MaestroSubtiposAviso searchParams, TableRequestDto tableRequestDto, Boolean startsWith) {
		// SELECT
		StringBuilder query = new StringBuilder(
				"SELECT t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		// FROM
		query.append("FROM MAESTRO_SUBTIPOS_AVISO 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,
				MaestroSubtiposAviso.class, filterParamList, searchSQL, searchParamList, from_alias,
				"IDSUBTIPOAVISO41");

		return this.jdbcTemplate.query(sbReorderSelectionSQL.toString(),
				new RowNumResultSetExtractor<MaestroSubtiposAviso>(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,
				MaestroSubtiposAviso.class, "MAESTRO_SUBTIPOS_AVISO", new String[] { "IDSUBTIPOAVISO41" });

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

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

	/*
	 * MÉTODOS PRIVADOS
	 */

	/**
	 * Returns a map with the needed value to create the conditions to filter by the
	 * MaestroSubtiposAviso entity
	 *
	 * @param maestrosubtiposaviso MaestroSubtiposAviso 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(MaestroSubtiposAviso maestrosubtiposaviso) {

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

		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getIdSubTipoAviso41() != null) {
			where.append(" AND t1.ID_SUB_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getIdSubTipoAviso41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getMaestroTiposAviso() != null
				&& maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEs41() != null) {
			where.append(" AND t1.DESC_ES_41 = ?");
			params.add(maestrosubtiposaviso.getDescEs41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEu41() != null) {
			where.append(" AND t1.DESC_EU_41 = ?");
			params.add(maestrosubtiposaviso.getDescEu41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getfBaja41() != null) {
			where.append(" AND t1.F_BAJA_41 = ?");
			params.add(maestrosubtiposaviso.getfBaja41());
		}

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

		return mapWhere;
	}

	// CHECKSTYLE:OFF CyclomaticComplexity - Generación de código de UDA
	private Map<String, ?> getWhereUsuMap(MaestroSubtiposAviso maestrosubtiposaviso, String usuario) {

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

		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getIdSubTipoAviso41() != null) {
			where.append(" AND t1.ID_SUB_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getIdSubTipoAviso41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getMaestroTiposAviso() != null
				&& maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEs41() != null) {
			where.append(" AND t1.DESC_ES_41 = ?");
			params.add(maestrosubtiposaviso.getDescEs41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEu41() != null) {
			where.append(" AND t1.DESC_EU_41 = ?");
			params.add(maestrosubtiposaviso.getDescEu41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getfBaja41() != null) {
			where.append(" AND t1.F_BAJA_41 = ?");
			params.add(maestrosubtiposaviso.getfBaja41());
		}

		if (usuario.equals("DSS")) {
			where.append(" AND t1.ORIGEN_DSS_41 = 1");
		}
		if (usuario.equals("PS")) {
			where.append(" AND t1.ORIGEN_PS_41 = 1");
		}
		if (usuario.equals("ADMIN")) {
			where.append(" AND t1.ORIGEN_AUTOMATICO_41 = 0");

		}

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

		return mapWhere;
	}

	// CHECKSTYLE:OFF CyclomaticComplexity - Generación de código de UDA
	private Map<String, ?> getWhereDssMap(MaestroSubtiposAviso maestrosubtiposaviso) {

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

		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getIdSubTipoAviso41() != null) {
			where.append(" AND t1.ID_SUB_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getIdSubTipoAviso41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getMaestroTiposAviso() != null
				&& maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEs41() != null) {
			where.append(" AND t1.DESC_ES_41 = ?");
			params.add(maestrosubtiposaviso.getDescEs41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEu41() != null) {
			where.append(" AND t1.DESC_EU_41 = ?");
			params.add(maestrosubtiposaviso.getDescEu41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getfBaja41() != null) {
			where.append(" AND t1.F_BAJA_41 = ?");
			params.add(maestrosubtiposaviso.getfBaja41());
		}

		where.append(" AND t1.ORIGEN_DSS_41 = 1");

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

		return mapWhere;
	}

	// CHECKSTYLE:OFF CyclomaticComplexity - Generación de código de UDA
	private Map<String, ?> getWhereManualMap(MaestroSubtiposAviso maestrosubtiposaviso) {

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

		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getIdSubTipoAviso41() != null) {
			where.append(" AND t1.ID_SUB_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getIdSubTipoAviso41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getMaestroTiposAviso() != null
				&& maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEs41() != null) {
			where.append(" AND t1.DESC_ES_41 = ?");
			params.add(maestrosubtiposaviso.getDescEs41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEu41() != null) {
			where.append(" AND t1.DESC_EU_41 = ?");
			params.add(maestrosubtiposaviso.getDescEu41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getfBaja41() != null) {
			where.append(" AND t1.F_BAJA_41 = ?");
			params.add(maestrosubtiposaviso.getfBaja41());
		}

		// where.append(" AND t1.ORIGEN_AUTOMATICO_41 = 0");

		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
	 * MaestroSubtiposAviso entity
	 *
	 * @param maestrosubtiposaviso MaestroSubtiposAviso 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(MaestroSubtiposAviso maestrosubtiposaviso, Boolean startsWith) {

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

		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getIdSubTipoAviso41() != null) {
			where.append(" AND t1.ID_SUB_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getIdSubTipoAviso41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getMaestroTiposAviso() != null
				&& maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEs41() != null) {
			where.append(" AND UPPER(t1.DESC_ES_41) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrosubtiposaviso.getDescEs41().toUpperCase() + "%");
			} else {
				params.add("%" + maestrosubtiposaviso.getDescEs41().toUpperCase() + "%");
			}
			where.append(" AND t1.DESC_ES_41 IS NOT NULL");
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEu41() != null) {
			where.append(" AND UPPER(t1.DESC_EU_41) like ? ESCAPE  '\\'");
			if (startsWith) {
				params.add(maestrosubtiposaviso.getDescEu41().toUpperCase() + "%");
			} else {
				params.add("%" + maestrosubtiposaviso.getDescEu41().toUpperCase() + "%");
			}
			where.append(" AND t1.DESC_EU_41 IS NOT NULL");
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getfBaja41() != null) {
			where.append(" AND t1.F_BAJA_41 = ?");
			params.add(maestrosubtiposaviso.getfBaja41());
		}

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

		return mapWhere;
	}

	// CHECKSTYLE:OFF CyclomaticComplexity - Generación de código de UDA
	private Map<String, ?> getWhereUsuMap(MaestroSubtiposAviso maestrosubtiposaviso, Long idActor42) {

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

		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getIdSubTipoAviso41() != null) {
			where.append(" AND t1.ID_SUB_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getIdSubTipoAviso41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getMaestroTiposAviso() != null
				&& maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40() != null) {
			where.append(" AND t1.ID_TIPO_AVISO_41 = ?");
			params.add(maestrosubtiposaviso.getMaestroTiposAviso().getIdTipoAviso40());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEs41() != null) {
			where.append(" AND t1.DESC_ES_41 = ?");
			params.add(maestrosubtiposaviso.getDescEs41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getDescEu41() != null) {
			where.append(" AND t1.DESC_EU_41 = ?");
			params.add(maestrosubtiposaviso.getDescEu41());
		}
		if (maestrosubtiposaviso != null && maestrosubtiposaviso.getfBaja41() != null) {
			where.append(" AND t1.F_BAJA_41 = ?");
			params.add(maestrosubtiposaviso.getfBaja41());
		}

		if (idActor42 != null && idActor42.toString().equals(OrigenAvisosEnum.DSS.getId())) {
			where.append(" AND t1.ORIGEN_DSS_41 = 1");
		}
		if (idActor42 != null && idActor42.toString().equals(OrigenAvisosEnum.PS.getId())) {
			where.append(" AND t1.ORIGEN_PS_41 = 1");
		}
		if (idActor42 == null) {
			where.append(" AND t1.ORIGEN_AUTOMATICO_41 = 0");

		}

		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

	@Override
	public List<MaestroSubtiposAviso> findSubtiposDss(MaestroSubtiposAviso maestrosubtiposaviso,
			TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO t1 ");

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

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

	@Override
	public List<MaestroSubtiposAviso> findSubtiposPs(MaestroSubtiposAviso maestrosubtiposaviso,
			TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO t1 ");

		String usuario = "PS";
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereUsuMap(maestrosubtiposaviso, usuario);
		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,
					MaestroSubtiposAvisoDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	@Override
	public List<MaestroSubtiposAviso> findSubtiposSsb(MaestroSubtiposAviso maestrosubtiposaviso,
			TableRequestDto tableRequestDto) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO t1 ");

		String usuario = "ADMIN";
		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereUsuMap(maestrosubtiposaviso, usuario);
		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,
					MaestroSubtiposAvisoDaoImpl.ORDER_BY_WHITE_LIST);
		}

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

	@Transactional(readOnly = true)
	public List<MaestroSubtiposAviso> findPorUsuarios(MaestroSubtiposAviso maestrosubtiposaviso,
			TableRequestDto tableRequestDto, Long idActor42) {
		StringBuilder query = new StringBuilder(
				"SELECT  t1.ID_SUB_TIPO_AVISO_41 IDSUBTIPOAVISO41,t1.ID_TIPO_AVISO_41 IDTIPOAVISO41,t1.DESC_ES_41 DESCES41,t1.DESC_EU_41 DESCEU41,t1.F_BAJA_41 FBAJA41 ");
		query.append("FROM MAESTRO_SUBTIPOS_AVISO t1 ");

		// Where clause & Params
		Map<String, ?> mapaWhere = this.getWhereUsuMap(maestrosubtiposaviso, idActor42);
		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,
					MaestroSubtiposAvisoDaoImpl.ORDER_BY_WHITE_LIST);
		}

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