<?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.ebts.generator.dao.RelationalMapDao">

    <resultMap type="com.ebts.generator.entity.RelationalMap" id="RelationalMapResult">
        <result property="mainId"    column="main_id"    />
        <result property="childId"    column="child_id"    />
        <result property="mainKey"    column="main_key"    />
        <result property="childKey"    column="child_key"    />
        <result property="type"    column="type"    />
    </resultMap>
    <sql id="selectRelationalMapVo">
        select main_id, child_id, main_key, child_key, type from gen_relational_mapping
    </sql>


    <select id="selectRelationalMapList" parameterType="RelationalMap" resultMap="RelationalMapResult">
        <include refid="selectRelationalMapVo"/>
        <where>
            <if test="mainId != null "> and main_id = #{mainId}</if>
            <if test="childId != null "> and child_id = #{childId}</if>
            <if test="mainKey != null  and mainKey != ''"> and main_key like concat('%', #{mainKey}, '%')</if>
            <if test="childKey != null  and childKey != ''"> and child_key like concat('%', #{childKey}, '%')</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
        </where>
    </select>

    <select id="selectRelationalMapById" parameterType="Long" resultMap="RelationalMapResult">
        <include refid="selectRelationalMapVo"/>
        where main_id = #{mainId}
    </select>

    <insert id="insertRelationalMap" parameterType="RelationalMap">
        insert into gen_relational_mapping
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mainId != null">main_id,</if>
            <if test="childId != null">child_id,</if>
            <if test="mainKey != null">main_key,</if>
            <if test="childKey != null">child_key,</if>
            <if test="type != null">type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mainId != null">#{mainId},</if>
            <if test="childId != null">#{childId},</if>
            <if test="mainKey != null">#{mainKey},</if>
            <if test="childKey != null">#{childKey},</if>
            <if test="type != null">#{type},</if>
        </trim>
    </insert>

    <update id="updateRelationalMap" parameterType="RelationalMap">
        update gen_relational_mapping
        <trim prefix="SET" suffixOverrides=",">
            <if test="childId != null">child_id = #{childId},</if>
            <if test="mainKey != null">main_key = #{mainKey},</if>
            <if test="childKey != null">child_key = #{childKey},</if>
            <if test="type != null">type = #{type},</if>
        </trim>
        where main_id = #{mainId}
    </update>

    <delete id="deleteRelationalMapById" parameterType="Long">
        delete from gen_relational_mapping where main_id = #{mainId}
    </delete>

    <delete id="deleteRelationalMapByIds" parameterType="String">
        delete from gen_relational_mapping where main_id in
        <foreach item="mainId" collection="array" open="(" separator="," close=")">
            #{mainId}
        </foreach>
    </delete>

    <insert id="batchRelationalMap">
        insert into gen_relational_mapping(main_id, child_id, main_key, child_key, type) values
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.mainId},#{item.childId},#{item.mainKey},#{item.childKey},#{item.type})
        </foreach>
    </insert>

</mapper>