<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.mx.dla.dda.contrato.deal.daos.DealDAO">

	<resultMap id="resultsTransFilialByContract" type="FilialDTO">
		<result property="idFilial" column="ID_FILIAL" />
		<result property="descFilial" column="DESC_FILIAL" />	
		<result property="estatus" column="ESTATUS" />
	</resultMap>
	
	<resultMap id="resultsTransFilial" type="FilialDTO">	
		<result property="idFilial" column="ID_FILIAL" />
		<result property="descFilial" column="DESC_FILIAL" />	
		<result property="estatus" column="ESTATUS" />
	</resultMap>
	
	<insert id="creaContratoFilialRelacion" parameterType="ContratoFilialDTO" >

		INSERT INTO DDA_T_CONTRATO_DEAL_FILIAL 
		  (ID_CONTRATO, ID_FILIAL) 
		     values(
		        #{idContrato, javaType=Long, jdbcType=NUMERIC},
		        #{idFilial, javaType=Long, jdbcType=NUMERIC}
		      )    
	</insert>

	<select id="consultaDealFilialesByContrato"  resultMap="resultsTransFilialByContract" 
		parameterType="map">
		SELECT 
           fl.ID_FILIAL, 
           fl.DESC_FILIAL, 
           fl.ESTATUS 
        FROM DDA_T_CONTRATO_DEAL_FILIAL cdf
          INNER JOIN DDA_C_FILIAL fl
          ON fl.ID_FILIAL = cdf.ID_FILIAL
        WHERE 
           cdf.ID_CONTRATO = #{contratoFilial.idContrato}
        AND 
           fl.ESTATUS = 1
	</select>
	
	<select id="consultaDealFiliales" resultMap="resultsTransFilial"
		parameterType="map">
        SELECT 
           fl.ID_FILIAL, 
           fl.DESC_FILIAL, 
           fl.ESTATUS 
        FROM DDA_C_FILIAL fl 
	</select>
	
	<delete id="eliminaContratoFilialRelacion" parameterType="ContratoFilialDTO">
		DELETE DDA_T_CONTRATO_DEAL_FILIAL
		WHERE
		    ID_CONTRATO = #{idContrato, javaType=Long, jdbcType=NUMERIC}
		    AND
		    ID_FILIAL = #{idFilial, javaType=Long, jdbcType=NUMERIC}
	</delete>

</mapper>