<?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.account.mapper.MerchantEmployeeMapper">

    <resultMap id="MerchantEmployeeRowResult" type="com.ruoyi.web.modules.account.domain.MerchantEmployeeRow">
        <id property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="email" column="email"/>
        <result property="status" column="status"/>
        <result property="avatar" column="avatar"/>
        <result property="createTime" column="create_time"/>
        <result property="merchantId" column="merchant_id"/>
        <result property="shopId" column="shop_id"/>
        <result property="accountType" column="account_type"/>
    </resultMap>

    <select id="selectList" resultMap="MerchantEmployeeRowResult">
        select u.user_id,
               u.user_name,
               u.nick_name,
               u.phonenumber,
               u.email,
               u.status,
               u.avatar,
               u.create_time,
               a.merchant_id,
               a.shop_id,
               'EMPLOYEE' as account_type
        from sys_user u
        inner join biz_merchant_account a
                on u.user_id = a.account_id and a.del_flag = '0'
        where a.merchant_id = #{merchantId}
          and a.shop_id = #{shopId}
          and u.del_flag = '0'
        <if test="employeeName != null and employeeName != ''">
            and u.nick_name like concat('%', #{employeeName}, '%')
        </if>
        order by u.create_time desc
    </select>

    <select id="selectByUserId" resultMap="MerchantEmployeeRowResult">
        select u.user_id,
               u.user_name,
               u.nick_name,
               u.phonenumber,
               u.email,
               u.status,
               u.avatar,
               u.create_time,
               a.merchant_id,
               a.shop_id,
               'EMPLOYEE' as account_type
        from sys_user u
        inner join biz_merchant_account a
                on u.user_id = a.account_id and a.del_flag = '0'
        where a.merchant_id = #{merchantId}
          and a.shop_id = #{shopId}
          and u.user_id = #{userId}
          and u.del_flag = '0'
        limit 1
    </select>

    <select id="selectRoleOptions" resultType="com.ruoyi.web.modules.account.vo.MerchantEmployeeRoleOptionVO">
        select r.role_id as roleId,
               r.role_name as roleName
        from biz_shop_role sr
        inner join sys_role r on sr.role_id = r.role_id and r.del_flag = '0' and r.status = '0'
        where sr.shop_id = #{shopId}
          and r.role_key not in ('admin', 'merchant')
        order by r.role_sort
    </select>

    <select id="countValidShopRoles" resultType="int">
        select count(1)
        from biz_shop_role sr
        inner join sys_role r on sr.role_id = r.role_id and r.del_flag = '0' and r.status = '0'
        where sr.shop_id = #{shopId}
          and r.role_key not in ('admin', 'merchant')
          and sr.role_id in
        <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </select>

    <select id="selectRoleIdsByUserId" resultType="long">
        select ur.role_id
        from sys_user_role ur
        inner join biz_shop_role sr on ur.role_id = sr.role_id
        where ur.user_id = #{userId}
          and sr.shop_id = #{shopId}
    </select>
</mapper>
