<?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.store.mapper.BizMerchantAccountMapper">

    <resultMap id="BizMerchantAccountResult" type="com.ruoyi.web.modules.store.domain.BizMerchantAccount">
        <id property="accountId" column="account_id"/>
        <result property="merchantId" column="merchant_id"/>
        <result property="shopId" column="shop_id"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="selectByMerchantId" resultMap="BizMerchantAccountResult">
        select account_id, merchant_id, shop_id, del_flag,
               create_by, create_time, update_by, update_time
        from biz_merchant_account
        where merchant_id = #{merchantId} and shop_id is null and del_flag = '0'
    </select>

    <select id="selectMerchantAdminByUserId" resultMap="BizMerchantAccountResult">
        select account_id, merchant_id, shop_id, del_flag,
               create_by, create_time, update_by, update_time
        from biz_merchant_account
        where account_id = #{userId} and shop_id is null and del_flag = '0'
    </select>

    <select id="selectByUserId" resultMap="BizMerchantAccountResult">
        select account_id, merchant_id, shop_id, del_flag,
               create_by, create_time, update_by, update_time
        from biz_merchant_account
        where account_id = #{userId} and del_flag = '0'
    </select>

    <select id="selectMerchantAdminWithUser" resultType="com.ruoyi.web.modules.store.vo.MerchantAccountUserVO">
        select a.account_id as accountId,
               a.merchant_id as merchantId,
               a.shop_id as shopId,
               u.user_name as loginName,
               u.nick_name as adminName
        from biz_merchant_account a
        inner join sys_user u on a.account_id = u.user_id and u.del_flag = '0'
        where a.merchant_id = #{merchantId} and a.shop_id is null and a.del_flag = '0'
    </select>

    <select id="countByAccountId" resultType="int">
        select count(1) from biz_merchant_account
        where account_id = #{accountId} and del_flag = '0'
    </select>

    <insert id="insert">
        insert into biz_merchant_account (
            account_id, merchant_id, shop_id, del_flag, create_by, create_time
        ) values (
            #{accountId}, #{merchantId}, #{shopId}, #{delFlag}, #{createBy}, sysdate()
        )
    </insert>

    <update id="update">
        update biz_merchant_account
        <set>
            <if test="shopId != null">shop_id = #{shopId},</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where account_id = #{accountId} and del_flag = '0'
    </update>

    <update id="updateSysUserProfile">
        update sys_user
        <set>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="email != null">email = #{email},</if>
            <if test="phonenumber != null and phonenumber != ''">phonenumber = #{phonenumber},</if>
            <if test="password != null and password != ''">password = #{password}, pwd_update_date = sysdate(),</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where user_id = #{userId} and del_flag = '0'
    </update>

    <update id="logicDeleteByUserId">
        update biz_merchant_account
        set del_flag = '2',
            update_by = #{updateBy},
            update_time = sysdate()
        where account_id = #{userId} and shop_id is null and del_flag = '0'
    </update>

    <select id="countEmployeesByMerchantId" resultType="int">
        select count(1)
        from biz_merchant_account
        where merchant_id = #{merchantId}
          and shop_id is not null
          and del_flag = '0'
    </select>

    <select id="selectEmployeeByUserIdAndMerchant" resultMap="BizMerchantAccountResult">
        select account_id, merchant_id, shop_id, del_flag,
               create_by, create_time, update_by, update_time
        from biz_merchant_account
        where account_id = #{userId}
          and merchant_id = #{merchantId}
          and shop_id is not null
          and del_flag = '0'
    </select>

    <update id="logicDeleteEmployeeByUserId">
        update biz_merchant_account
        set del_flag = '2',
            update_by = #{updateBy},
            update_time = sysdate()
        where account_id = #{userId}
          and shop_id is not null
          and del_flag = '0'
    </update>
</mapper>
