<?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.hchyun.test.dao.RelADao">
    
    <resultMap type="com.hchyun.test.entity.RelA" id="RelAResult">
        <result property="aid"    column="aid"    />
        <result property="infoa"    column="infoa"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>
    <resultMap id="RelAAllResult" type="com.hchyun.test.entity.RelA" extends="RelAResult">
        <collection property="relB" notNullColumn="unicon_id" javaType="com.hchyun.test.entity.RelB" resultMap="RelBResult" />
        <collection property="relC" notNullColumn="unicon_id" javaType="com.hchyun.test.entity.RelC" resultMap="RelCResult" />
    </resultMap>
    <resultMap id="RelBResult" type="com.hchyun.test.entity.RelB">
        <result property="createTime"    column="create_time"    />
        <result property="infob"    column="infob"    />
        <result property="uniconId"    column="unicon_id"    />
        <result property="bid"    column="bid"    />
    </resultMap>
    <resultMap id="RelCResult" type="com.hchyun.test.entity.RelC">
        <result property="infoc"    column="infoc"    />
        <result property="uniconId"    column="unicon_id"    />
        <result property="cid"    column="cId"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectRelAVo">
        select aid, infoa, create_time from test_rel_a
    </sql>


    <select id="selectRelAList" parameterType="RelA" resultMap="RelAAllResult">
        select  tra.aid, tra.infoa, tra.create_time,
             trb.create_time, trb.infob, trb.unicon_id, trb.bid,
             trc.infoc, trc.unicon_id, trc.cId, trc.create_time
        from test_rel_a tra
            left join test_rel_b trb on tra.aid = trb.unicon_id
            left join test_rel_c trc on trb.bid = trc.unicon_id
        <where>
            <if test="infoa != null  and infoa != ''"> and tra.infoa like concat('%', #{infoa}, '%')</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''"> and tra.create_time between #{params.beginCreateTime} and #{params.endCreateTime}</if>
            <if test="relB.params.beginCreateTime != null and relB.params.beginCreateTime != '' and relB.params.endCreateTime != null and relB.params.endCreateTime != ''"> and trb.create_time between #{relB.params.beginCreateTime} and #{relB.params.endCreateTime}</if>
            <if test="relB.infob != null  and relB.infob != ''"> and trb.infob like concat('%', #{relB.infob}, '%')</if>
            <if test="relC.infoc != null  and relC.infoc != ''"> and trc.infoc like concat('%', #{relC.infoc}, '%')</if>
            <if test="relC.params.beginCreateTime != null and relC.params.beginCreateTime != '' and relC.params.endCreateTime != null and relC.params.endCreateTime != ''"> and trc.create_time between #{relC.params.beginCreateTime} and #{relC.params.endCreateTime}</if>
        </where>
    </select>
    
    <select id="selectRelAById" parameterType="Long" resultMap="RelAResult">
        <include refid="selectRelAVo"/>
        where aid = #{aid}
    </select>
    <select id="selectRelAAllById" parameterType="Long" resultMap="RelAAllResult">
        select  tra.aid, tra.infoa, tra.create_time,
             trb.create_time, trb.infob, trb.unicon_id, trb.bid,
             trc.infoc, trc.unicon_id, trc.cId, trc.create_time
        from test_rel_a tra
            left join test_rel_b trb on tra.aid = trb.unicon_id
            left join test_rel_c trc on trb.bid = trc.unicon_id
        where tra.aid = #{aid}
    </select>
        
    <insert id="insertRelA" parameterType="RelA">
        insert into test_rel_a
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="aid != null">aid,</if>
            <if test="infoa != null">infoa,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="aid != null">#{aid},</if>
            <if test="infoa != null">#{infoa},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateRelA" parameterType="RelA">
        update test_rel_a
        <trim prefix="SET" suffixOverrides=",">
            <if test="infoa != null">infoa = #{infoa},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where aid = #{aid}
    </update>

    <delete id="deleteRelAById" parameterType="Long">
        delete from test_rel_a where aid = #{aid}
    </delete>

    <delete id="deleteRelAByIds" parameterType="String">
        delete from test_rel_a where aid in 
        <foreach item="aid" collection="array" open="(" separator="," close=")">
            #{aid}
        </foreach>
    </delete>
</mapper>