<?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.ytpm.middle.dao.RoleMapper">
    <insert id="insertOne">
        insert into yt_middle_role
        (
         role_name,
         role_desc,
         `range`,
         sort,
         create_time,
         create_user_id,
         available
        )
        values
        (
            #{roleName},
            #{roleDesc},
            #{range},
            #{sort},
            #{createTime},
            #{createUserId},
            #{available}
        )
    </insert>
    <insert id="grantRoleToUsers">
        insert into yt_middle_user_role (user_id, role_id, create_time, create_user_id)
        values
        <foreach collection="userIds.split(',')" separator="," item="item">
            (#{item},#{roleId}, now(),#{operatorId})
        </foreach>
    </insert>
    <update id="updateById">
        update yt_middle_role
        <set>
            <if test="roleName != null">
                role_name = #{roleName},
            </if>
            <if test="roleDesc != null">
                role_desc = #{roleDesc},
            </if>
            <if test="range != null">
                `range` = #{range},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateUserId != null">
                update_user_id = #{updateUserId},
            </if>
            <if test="available != null">
                available = #{available}
            </if>
        </set>
        where role_id = #{roleId}
    </update>
    <delete id="delUserRoleById">
        delete from yt_middle_user_role where role_id = #{roleId}
    </delete>

    <select id="queryList" resultType="com.ytpm.middle.view.MiddleRoleVO">
        select
            role_id, role_name, role_desc, `range`, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_role
        <where>
            <if test="roleName != null and roleName != ''">
                and role_name like concat('%',#{roleName},'%')
            </if>
        </where>
    </select>
    <select id="selectByName" resultType="com.ytpm.middle.model.YtMiddleRole">
        select
            role_id, role_name, role_desc, `range`, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_role
        where role_name = #{roleName}
    </select>
    <select id="selectPrimary" resultType="com.ytpm.middle.model.YtMiddleRole">
        select
            role_id, role_name, role_desc, `range`, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_role
        where role_id = #{roleId}
    </select>
    <select id="getRoleUserIds" resultType="java.lang.String">
        select distinct
            user_id
        from yt_middle_role r
        left join yt_middle_user_role ymur on r.role_id = ymur.role_id
        where r.role_id = #{roleId}
    </select>
</mapper>