package com.ejie.ad74a.dao.iml;

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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.dao.InteropsNisaeDao;
import com.ejie.ad74a.model.DatosInterop;
import com.ejie.ad74a.util.UtilSQL;

/**
 * @author Ayesa
 *
 */
@Repository
@Transactional
public class InteropsNisaeDaoImpl implements InteropsNisaeDao {

	private static final Logger logger = LoggerFactory.getLogger(InteropsNisaeDaoImpl.class);

	private JdbcTemplate jdbcTemplate;

	public static final String SEQUENCE_DATOS_INTEROP = "SEQ_DATOS_INTEROP";
	
	/**
	 * Method use to set the datasource.
	 *
	 * @param dataSource DataSource
	 * @return
	 */
	@Resource
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	
	/*
	 * ROW_MAPPERS
	 */
	private RowMapper<DatosInterop> rwMap = new RowMapper<DatosInterop>() {
		public DatosInterop mapRow(ResultSet resultSet, int rowNum) throws SQLException {
			return new DatosInterop(UtilSQL.getLong(resultSet, "IDINTEROP19"),
					UtilSQL.getLong(resultSet, "IDEXPEDIENTE19"),
					resultSet.getDate("FCONIMSERSO19"), resultSet.getString("CODRESULCONIMSERSO19"),
					resultSet.getString("RESULCONIMSERSO19"), resultSet.getDate("FINVOKECONIMSERSO19"),
					resultSet.getString("IDTRACONIMSERSO19"),
					resultSet.getDate("FCONDISCAPA19"), resultSet.getString("CODRESULDISCAPA19"),
					resultSet.getString("RESULDISCAPA19"), resultSet.getDate("FINVOKEDISCAPA19"),
					resultSet.getString("IDTRADISCAPA19"));
		}
	};

	/*
	 * OPERACIONES CRUD
	 */

	/**
	 * Inserts a single row in the InteropsNisae table.
	 *
	 * @param interopsNisae InteropsNisae
	 * @return InteropsNisae
	 */
	@Override
	public DatosInterop add(DatosInterop datosinterop, Boolean modoPrueba) {
		
		
		if(Boolean.FALSE.equals(modoPrueba)) {
			InteropsNisaeDaoImpl.logger.info("Add DatosInterop : INSERT INTO DATOS_INTEROP (ID_INTEROP_19, ID_EXPEDIENTE_19,  F_CON_IMSERSO_19, CODRESUL_CON_IMSERSO_19, RESUL_CON_IMSERSO_19, FINVOKE_CON_IMSERSO_19, IDTRA_CON_IMSERSO_19,  F_CON_DISCAPA_19, CODRESUL_DISCAPA_19, RESUL_DISCAPA_19, FINVOKE_DISCAPA_19, IDTRA_DISCAPA_19)  VALUES (\" + SEQUENCE_DATOS_INTEROP  + \".NEXTVAL, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})",datosinterop.getIdExpediente19(), datosinterop.getfConImserso19(), datosinterop.getCodresulConImserso19(), datosinterop.getResulConImserso19() , datosinterop.getfinvokeConImserso19(), datosinterop.getIdtraConImserso19(), datosinterop.getfConDiscapa19(), datosinterop.getCodresulDiscapa19(), datosinterop.getResulDiscapa19(), datosinterop.getFinvokeDiscapa19(), datosinterop.getIdtraDiscapa19() );
			
	
			GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
	
			String query = "INSERT INTO DATOS_INTEROP (ID_INTEROP_19, ID_EXPEDIENTE_19,  F_CON_IMSERSO_19, CODRESUL_CON_IMSERSO_19, RESUL_CON_IMSERSO_19, FINVOKE_CON_IMSERSO_19, IDTRA_CON_IMSERSO_19,  F_CON_DISCAPA_19, CODRESUL_DISCAPA_19, RESUL_DISCAPA_19, FINVOKE_DISCAPA_19, IDTRA_DISCAPA_19) "
					+ "VALUES (" + SEQUENCE_DATOS_INTEROP
					+ ".NEXTVAL,:idExpediente, :fConImserso,:codResulImserso,:resulConImserso,:fInvokeImserso,:idtraConImserso,   :fConDiscapa, :codResulDiscapa, :resulDiscapa, :fInvokeDiscapa, :idtraDiscapa)";
	
			MapSqlParameterSource parameters = new MapSqlParameterSource()
					.addValue("idExpediente", datosinterop.getIdExpediente19())
					
					
					.addValue("fConImserso", datosinterop.getfConImserso19())
					.addValue("codResulImserso", datosinterop.getCodresulConImserso19())
					.addValue("resulConImserso", datosinterop.getResulConImserso19())
					.addValue("fInvokeImserso", datosinterop.getfinvokeConImserso19())
					.addValue("idtraConImserso", datosinterop.getIdtraConImserso19())
	
					.addValue("fConDiscapa", datosinterop.getfConDiscapa19())
					.addValue("codResulDiscapa", datosinterop.getCodresulDiscapa19())
					.addValue("resulDiscapa", datosinterop.getResulDiscapa19())
					.addValue("fInvokeDiscapa", datosinterop.getFinvokeDiscapa19())
					.addValue("idtraDiscapa", datosinterop.getIdtraDiscapa19());
	
			NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
			namedParameterJdbcTemplate.update(query, parameters, generatedKeyHolder, new String[] { "ID_INTEROP_19" });
			datosinterop.setIdInterop19(generatedKeyHolder.getKey().longValue());
		}else {
			InteropsNisaeDaoImpl.logger.info("Add DatosInterop MODO PRUEBA : INSERT INTO DATOS_INTEROP (ID_INTEROP_19, ID_EXPEDIENTE_19,  F_CON_IMSERSO_19, CODRESUL_CON_IMSERSO_19, RESUL_CON_IMSERSO_19, FINVOKE_CON_IMSERSO_19, IDTRA_CON_IMSERSO_19,  F_CON_DISCAPA_19, CODRESUL_DISCAPA_19, RESUL_DISCAPA_19, FINVOKE_DISCAPA_19, IDTRA_DISCAPA_19)  VALUES (\" + SEQUENCE_DATOS_INTEROP  + \".NEXTVAL, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})",datosinterop.getIdExpediente19(), datosinterop.getfConImserso19(), datosinterop.getCodresulConImserso19(), datosinterop.getResulConImserso19() , datosinterop.getfinvokeConImserso19(), datosinterop.getIdtraConImserso19(), datosinterop.getfConDiscapa19(), datosinterop.getCodresulDiscapa19(), datosinterop.getResulDiscapa19(), datosinterop.getFinvokeDiscapa19(), datosinterop.getIdtraDiscapa19() );
		}
		return datosinterop;
	}
	@Override
	public DatosInterop updateDatosDiscapacidad(DatosInterop datosinterop, Boolean modoPrueba) {
		if(Boolean.FALSE.equals(modoPrueba)) {
			InteropsNisaeDaoImpl.logger.info("Update DatosDiscapacidad : UPDATE DATOS_INTEROP SET F_CON_DISCAPA_19 = {}, CODRESUL_DISCAPA_19 = {}, RESUL_DISCAPA_19 = {}, FINVOKE_DISCAPA_19 = {}, IDTRA_DISCAPA_19 = {} WHERE ID_INTEROP_19={} ", datosinterop.getfConDiscapa19(), datosinterop.getCodresulDiscapa19(),datosinterop.getResulDiscapa19(), datosinterop.getFinvokeDiscapa19(), datosinterop.getIdtraDiscapa19(),datosinterop.getIdInterop19() );
		
			String query = "UPDATE DATOS_INTEROP SET F_CON_DISCAPA_19 = ?, CODRESUL_DISCAPA_19 = ?, RESUL_DISCAPA_19 = ?, FINVOKE_DISCAPA_19 = ?, IDTRA_DISCAPA_19 = ? WHERE ID_INTEROP_19=?";
			this.jdbcTemplate.update(query, datosinterop.getfConDiscapa19(), datosinterop.getCodresulDiscapa19(),
					datosinterop.getResulDiscapa19(), datosinterop.getFinvokeDiscapa19(), datosinterop.getIdtraDiscapa19(),
					datosinterop.getIdInterop19());
		}else {
			InteropsNisaeDaoImpl.logger.info("Update DatosDiscapacidad MODO PRUEBA : UPDATE DATOS_INTEROP SET F_CON_DISCAPA_19 = {}, CODRESUL_DISCAPA_19 = {}, RESUL_DISCAPA_19 = {}, FINVOKE_DISCAPA_19 = {}, IDTRA_DISCAPA_19 = {} WHERE ID_INTEROP_19={} ", datosinterop.getfConDiscapa19(), datosinterop.getCodresulDiscapa19(),datosinterop.getResulDiscapa19(), datosinterop.getFinvokeDiscapa19(), datosinterop.getIdtraDiscapa19(),datosinterop.getIdInterop19() );
		}
		return datosinterop;
	}
	
	@Override
	public DatosInterop updateDatosDependencia(DatosInterop datosinterop, Boolean modoPrueba) {
		
		if(Boolean.FALSE.equals(modoPrueba)) {
			InteropsNisaeDaoImpl.logger.info("Update DatosDependencia : UPDATE DATOS_INTEROP SET F_CON_IMSERSO_19= {} , CODRESUL_CON_IMSERSO_19= {} , RESUL_CON_IMSERSO_19 = {} , FINVOKE_CON_IMSERSO_19 = {}, IDTRA_CON_IMSERSO_19 = {} WHERE ID_INTEROP_19={} ", datosinterop.getfConImserso19(), datosinterop.getCodresulConImserso19(),datosinterop.getResulConImserso19(), datosinterop.getfinvokeConImserso19(), datosinterop.getIdtraConImserso19(),datosinterop.getIdInterop19());
			String query = "UPDATE DATOS_INTEROP SET  F_CON_IMSERSO_19= ? , CODRESUL_CON_IMSERSO_19= ? , RESUL_CON_IMSERSO_19 = ? , FINVOKE_CON_IMSERSO_19 = ?, IDTRA_CON_IMSERSO_19 = ? WHERE ID_INTEROP_19=?";
			this.jdbcTemplate.update(query, datosinterop.getfConImserso19(), datosinterop.getCodresulConImserso19(),
					datosinterop.getResulConImserso19(), datosinterop.getfinvokeConImserso19(), datosinterop.getIdtraConImserso19(),
					datosinterop.getIdInterop19());
		}else {
			InteropsNisaeDaoImpl.logger.info("Update DatosDependencia : UPDATE DATOS_INTEROP SET F_CON_IMSERSO_19= {} , CODRESUL_CON_IMSERSO_19= {} , RESUL_CON_IMSERSO_19 = {} , FINVOKE_CON_IMSERSO_19 = {}, IDTRA_CON_IMSERSO_19 = {} WHERE ID_INTEROP_19={} ",datosinterop.getfConImserso19(), datosinterop.getCodresulConImserso19(),datosinterop.getResulConImserso19(), datosinterop.getfinvokeConImserso19(), datosinterop.getIdtraConImserso19(),datosinterop.getIdInterop19());
		}
		return datosinterop;
	}
	
	/**
	 * Finds a single row in the DatosInterop table.
	 *
	 * @param datosinterop DatosInterop
	 * @return DatosInterop
	 */
	@Transactional(readOnly = true)
	@Override
	public DatosInterop findIdExpediente(DatosInterop datosinterop) {
		String query = "SELECT t1.ID_INTEROP_19 IDINTEROP19, t1.ID_EXPEDIENTE_19 IDEXPEDIENTE19,  t1.F_CON_IMSERSO_19 FCONIMSERSO19, t1.CODRESUL_CON_IMSERSO_19 CODRESULCONIMSERSO19, t1.RESUL_CON_IMSERSO_19 RESULCONIMSERSO19, t1.FINVOKE_CON_IMSERSO_19 FINVOKECONIMSERSO19, t1.IDTRA_CON_IMSERSO_19 IDTRACONIMSERSO19,  t1.F_CON_DISCAPA_19 FCONDISCAPA19, t1.CODRESUL_DISCAPA_19 CODRESULDISCAPA19, t1.RESUL_DISCAPA_19 RESULDISCAPA19, t1.FINVOKE_DISCAPA_19 FINVOKEDISCAPA19, t1.IDTRA_DISCAPA_19 IDTRADISCAPA19, t1.DATOS_RETORNO_DGP_19 DATOSRETORNODGP19, t1.DATOS_RETORNO_IRPF_CONY_19 DATOSRETORNOIRPFCONY19 FROM DATOS_INTEROP t1  WHERE t1.ID_EXPEDIENTE_19 = ?  ";

		List<DatosInterop> datosinteropList = this.jdbcTemplate.query(query, this.rwMap,
				datosinterop.getIdExpediente19());
		return (DatosInterop) DataAccessUtils.uniqueResult(datosinteropList);
	}
}