<?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.ruoyi.project.business.mapper.TcAccountRelationMapper">
    
    <resultMap type="TcAccountRelation" id="TcAccountRelationResult">
        <result property="id"    column="id"    />
        <result property="accountId"    column="account_id"    />
        <result property="otherAccountId"    column="other_account_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
        <result property="agentName"    column="agentName"    />
    </resultMap>

    <sql id="selectTcAccountRelationVo">
        select id, account_id, other_account_id, del_flag, create_time, create_by, update_time, update_by, remark from tc_account_relation
    </sql>

    <select id="selectTcAccountRelationList" parameterType="TcAccountRelation" resultMap="TcAccountRelationResult">
        <include refid="selectTcAccountRelationVo"/>
        <where>  
            <if test="accountId != null "> and account_id = #{accountId}</if>
            <if test="otherAccountId != null "> and other_account_id = #{otherAccountId}</if>
        </where>
    </select>
    
    <select id="selectTcAccountRelationById" parameterType="Long" resultMap="TcAccountRelationResult">
        <include refid="selectTcAccountRelationVo"/>
        where id = #{id}
    </select>

    <select id="selectAllTcAccountRelation" resultMap="TcAccountRelationResult">
        select r.*, IF(a.owner_agent = 0, 'common.head', b.agent_name) as agentName
        from tc_account_relation r
        left join tc_account a on r.other_account_id = a.account_id
        left join tb_agent b on a.owner_agent = b.agent_id
        where r.del_flag = '0'
    </select>
    <select id="selectTcAccountRelationListByList"
            resultType="com.ruoyi.project.business.domain.TcAccountRelation">
        select * from tc_account_relation 
        where 
            <foreach collection="list" item="item" index="index" separator="or">
                (account_id = #{item.accountId} and other_account_id = #{item.otherAccountId})
            </foreach>
    </select>

    <insert id="insertTcAccountRelation" parameterType="TcAccountRelation" useGeneratedKeys="true" keyProperty="id">
        insert into tc_account_relation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="accountId != null">account_id,</if>
            <if test="otherAccountId != null">other_account_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="accountId != null">#{accountId},</if>
            <if test="otherAccountId != null">#{otherAccountId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <insert id="batchInsertTcAccountRelation">
        insert into tc_account_relation
        (account_id, other_account_id, del_flag, create_time, create_by, update_time, update_by) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.accountId}, #{item.otherAccountId}, '0', #{item.createTime}, #{item.createBy}, #{item.updateTime}, #{item.updateBy})
        </foreach>
    </insert>

    <update id="updateTcAccountRelation" parameterType="TcAccountRelation">
        update tc_account_relation
        <trim prefix="SET" suffixOverrides=",">
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="otherAccountId != null">other_account_id = #{otherAccountId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTcAccountRelationById" parameterType="Long">
        delete from tc_account_relation where id = #{id}
    </delete>

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