<?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.web.modules.role.mapper.MerchantRoleMapper">

    <resultMap id="MerchantRoleRowResult" type="com.ruoyi.web.modules.role.domain.MerchantRoleRow">
        <id property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <result property="roleDesc" column="role_desc"/>
        <result property="status" column="status"/>
        <result property="bindCount" column="bind_count"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="bindCountSubquery">
        (
            select count(distinct ur.user_id)
            from sys_user_role ur
            inner join biz_merchant_account a
                    on ur.user_id = a.account_id
                   and a.shop_id = #{shopId}
                   and a.del_flag = '0'
            where ur.role_id = r.role_id
        )
    </sql>

    <select id="selectList" resultMap="MerchantRoleRowResult">
        select r.role_id,
               r.role_name,
               r.remark as role_desc,
               r.status,
               r.create_time,
               <include refid="bindCountSubquery"/> as bind_count
        from sys_role r
        inner join biz_shop_role sr on r.role_id = sr.role_id
        where sr.shop_id = #{shopId}
          and r.del_flag = '0'
        <if test="roleName != null and roleName != ''">
            and r.role_name like concat('%', #{roleName}, '%')
        </if>
        order by r.create_time desc
    </select>

    <select id="selectByRoleId" resultMap="MerchantRoleRowResult">
        select r.role_id,
               r.role_name,
               r.remark as role_desc,
               r.status,
               r.create_time,
               <include refid="bindCountSubquery"/> as bind_count
        from sys_role r
        inner join biz_shop_role sr on r.role_id = sr.role_id
        where sr.shop_id = #{shopId}
          and r.role_id = #{roleId}
          and r.del_flag = '0'
        limit 1
    </select>

    <select id="countRoleNameInShop" resultType="int">
        select count(1)
        from sys_role r
        inner join biz_shop_role sr on r.role_id = sr.role_id
        where sr.shop_id = #{shopId}
          and r.role_name = #{roleName}
          and r.del_flag = '0'
        <if test="excludeRoleId != null">
            and r.role_id &lt;&gt; #{excludeRoleId}
        </if>
    </select>

    <select id="countEmployeeBindByRole" resultType="int">
        select count(distinct ur.user_id)
        from sys_user_role ur
        inner join biz_merchant_account a
                on ur.user_id = a.account_id
               and a.shop_id = #{shopId}
               and a.del_flag = '0'
        inner join biz_shop_role sr
                on sr.role_id = ur.role_id
               and sr.shop_id = #{shopId}
        where ur.role_id = #{roleId}
    </select>

    <select id="selectMenuIdsByRoleId" resultType="long">
        select menu_id
        from sys_role_menu
        where role_id = #{roleId}
    </select>

    <insert id="insertShopRole">
        insert into biz_shop_role (shop_id, role_id)
        values (#{shopId}, #{roleId})
    </insert>

    <delete id="deleteShopRole">
        delete from biz_shop_role
        where shop_id = #{shopId}
          and role_id = #{roleId}
    </delete>
</mapper>
