<?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.AccountManageMapper">

    <resultMap id="AccountManageRowResult" type="com.ruoyi.web.modules.account.domain.AccountManageRow">
        <id property="userId" column="user_id"/>
        <result property="merchantId" column="merchant_id"/>
        <result property="nickName" column="nick_name"/>
        <result property="userName" column="user_name"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="email" column="email"/>
        <result property="shopCount" column="shop_count"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectAccountVo">
        select u.user_id,
               a.merchant_id,
               u.nick_name,
               u.user_name,
               u.phonenumber,
               u.email,
               u.create_time,
               case
                   when a.account_id is not null then (
                       select count(1) from biz_shop s
                        where s.merchant_id = a.merchant_id and s.del_flag = '0'
                   )
                   else null
               end as shop_count
        from sys_user u
        left join biz_merchant_account a
               on u.user_id = a.account_id and a.del_flag = '0' and a.shop_id is null
        where u.del_flag = '0'
    </sql>

    <select id="selectList" resultMap="AccountManageRowResult">
        <include refid="selectAccountVo"/>
        <if test="accountName != null and accountName != ''">
            and u.nick_name like concat('%', #{accountName}, '%')
        </if>
        order by u.create_time desc
    </select>

    <select id="selectByUserId" resultMap="AccountManageRowResult">
        <include refid="selectAccountVo"/>
        and u.user_id = #{userId}
        limit 1
    </select>
</mapper>
