<?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.dgtly.system.mapper.SysUserAuthorRelMapper">
    
    <resultMap type="SysUserAuthorRel" id="SysUserAuthorRelResult">
        <result property="userId"    column="user_id"    />
        <result property="authorId"    column="author_id"    />
    </resultMap>

    <sql id="selectSysUserAuthorRelVo">
        select user_id, author_id from sys_user_author_rel
    </sql>

    <select id="selectSysUserAuthorRelList" parameterType="SysUserAuthorRel" resultMap="SysUserAuthorRelResult">
        <include refid="selectSysUserAuthorRelVo"/>
        <where>  
            <if test="authorId != null "> and author_id = #{authorId}</if>
        </where>
    </select>
    
    <select id="selectSysUserAuthorRelById" parameterType="Long" resultMap="SysUserAuthorRelResult">
        <include refid="selectSysUserAuthorRelVo"/>
        where user_id = #{userId}
    </select>
        
    <insert id="insertSysUserAuthorRel" parameterType="SysUserAuthorRel">
        insert into sys_user_author_rel
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null ">user_id,</if>
            <if test="authorId != null ">author_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null ">#{userId},</if>
            <if test="authorId != null ">#{authorId},</if>
         </trim>
    </insert>


    <update id="updateSysUserAuthorRel" parameterType="SysUserAuthorRel">
        update sys_user_author_rel
        <trim prefix="SET" suffixOverrides=",">
            <if test="authorId != null ">author_id = #{authorId},</if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteSysUserAuthorRelById" parameterType="Long">
        delete from sys_user_author_rel where user_id = #{userId}
    </delete>

    <delete id="deleteSysUserAuthorRelByIds" parameterType="String">
        delete from sys_user_author_rel where user_id in 
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <insert id="batchRel">
        insert into sys_user_author_rel(user_id, author_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId},#{item.authorId})
        </foreach>
    </insert>

    <select id="selectSysUserAuthorRelByUIdAndAId"  resultMap="SysUserAuthorRelResult">
        <include refid="selectSysUserAuthorRelVo"/>
        where user_id = #{userId} and author_id =#{authorId}
    </select>

    <select id="selectSysUserAuthorRelListById" resultType="java.lang.Long">
        select  author_id from sys_user_author_rel
        where user_id = #{userId}
    </select>
</mapper>