<?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.cool.store.mapper.SysRoleMapper">
    <resultMap id="BaseResultMap" type="com.cool.store.entity.SysRoleDO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="role_id" jdbcType="VARCHAR" property="roleId"/>
        <result column="role_name" jdbcType="VARCHAR" property="roleName"/>
        <result column="role_type" jdbcType="TINYINT" property="roleType"/>
        <result column="third_unique_id" jdbcType="VARCHAR" property="thirdUniqueId"/>
        <result column="deleted" jdbcType="BIT" property="deleted"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="create_user" jdbcType="VARCHAR" property="createUser"/>
        <result column="update_user" jdbcType="VARCHAR" property="updateUser"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, role_id, role_name, role_type, third_unique_id, deleted, create_time, update_time, create_user,
        update_user
    </sql>
    <insert id="batchInsertSelective">
        <foreach collection="insertOrUpdateList" separator=";" item="record">
            insert into sys_role
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="record.roleId != null">
                    role_id,
                </if>
                <if test="record.roleName != null">
                    role_name,
                </if>
                <if test="record.roleType != null">
                    role_type,
                </if>
                <if test="record.thirdUniqueId != null">
                    third_unique_id,
                </if>
                <if test="record.deleted != null">
                    deleted,
                </if>
                <if test="record.createTime != null">
                    create_time,
                </if>
                <if test="record.updateTime != null">
                    update_time,
                </if>
                <if test="record.createUser != null">
                    create_user,
                </if>
                <if test="record.updateUser != null">
                    update_user,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="record.roleId != null">
                    #{record.roleId},
                </if>
                <if test="record.roleName != null">
                    #{record.roleName},
                </if>
                <if test="record.roleType != null">
                    #{record.roleType},
                </if>
                <if test="record.thirdUniqueId != null">
                    #{record.thirdUniqueId},
                </if>
                <if test="record.deleted != null">
                    #{record.deleted},
                </if>
                <if test="record.createTime != null">
                    #{record.createTime},
                </if>
                <if test="record.updateTime != null">
                    #{record.updateTime},
                </if>
                <if test="record.createUser != null">
                    #{record.createUser},
                </if>
                <if test="record.updateUser != null">
                    #{record.updateUser},
                </if>
            </trim>
            ON DUPLICATE KEY UPDATE role_name = values(role_name), deleted = values(deleted)
        </foreach>
    </insert>
    <update id="updateByPrimaryKeySelective">
        update sys_role
        <set>
            <if test="roleId != null">
                role_id = #{roleId},
            </if>
            <if test="roleName != null">
                role_name = #{roleName},
            </if>
            <if test="roleType != null">
                role_type = #{roleType},
            </if>
            <if test="thirdUniqueId != null">
                third_unique_id = #{thirdUniqueId},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="createUser != null">
                create_user = #{createUser},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser},
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="deleteRole">
        update sys_role set deleted = 1 where role_type = #{roleType} and role_id not in
        <foreach collection="roleIds" open="(" close=")" separator="," item="roleId">#{roleId}</foreach>
    </update>

    <select id="getHighestPriorityRoleByUserId" resultMap="BaseResultMap">
        select
            r.*
        from
            sys_role r inner join enterprise_user_role e on r.role_id = e.role_id
        where
            e.user_id = #{userId} and r.deleted = 0 and e.deleted = 0
        limit 1
    </select>
    
    <select id="getRoleByName" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List"/>
        from
            sys_role
        where
            role_name = #{roleName} and role_type = #{roleType} and deleted = 0 limit 1
    </select>

    <select id="getRolePage" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List"/>
        from
            sys_role
        where deleted = 0
    </select>

    <select id="getRoleDetail" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List"/>
        from
            sys_role
        where
            role_id = #{roleId} and deleted = 0
    </select>
</mapper>