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

    <resultMap id="BizMemberResult" type="com.ruoyi.web.modules.account.domain.BizMember">
        <id property="memberId" column="member_id"/>
        <result property="mobile" column="mobile"/>
        <result property="memberCode" column="member_code"/>
        <result property="nickName" column="nick_name"/>
        <result property="email" column="email"/>
        <result property="sex" column="sex"/>
        <result property="password" column="password"/>
        <result property="memberLevel" column="member_level"/>
        <result property="status" column="status"/>
        <result property="orderCount" column="order_count"/>
        <result property="totalAmount" column="total_amount"/>
        <result property="registerTime" column="register_time"/>
        <result property="wxOpenid" column="wx_openid"/>
        <result property="avatar" column="avatar"/>
        <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"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectVo">
        select m.member_id,
               u.phonenumber as mobile,
               u.user_name as member_code,
               u.nick_name,
               u.email,
               u.sex,
               u.password,
               m.member_level,
               u.status,
               m.order_count,
               m.total_amount,
               m.register_time,
               m.wx_openid,
               u.avatar,
               u.del_flag,
               m.create_by,
               m.create_time,
               m.update_by,
               m.update_time,
               m.remark
        from biz_member m
        inner join sys_user u on m.member_id = u.user_id
    </sql>

    <sql id="memberRoleExists">
        and exists (
            select 1
            from sys_user_role ur
            inner join sys_role r on ur.role_id = r.role_id and r.del_flag = '0' and r.status = '0'
            where ur.user_id = u.user_id
              and r.role_key = #{memberRoleKey}
        )
    </sql>

    <select id="selectById" resultMap="BizMemberResult">
        <include refid="selectVo"/>
        where m.member_id = #{memberId} and u.del_flag = '0'
    </select>

    <select id="selectByMobile" resultMap="BizMemberResult">
        <include refid="selectVo"/>
        where u.phonenumber = #{mobile} and u.del_flag = '0'
    </select>

    <select id="selectByMemberCode" resultMap="BizMemberResult">
        <include refid="selectVo"/>
        where u.user_name = #{memberCode} and u.del_flag = '0'
    </select>

    <select id="selectList" resultMap="BizMemberResult">
        <include refid="selectVo"/>
        where u.del_flag = '0'
        <include refid="memberRoleExists"/>
        <if test="status != null and status != ''">
            and u.status = #{status}
        </if>
        <if test="keyword != null and keyword != ''">
            and (u.user_name like concat('%', #{keyword}, '%')
                or u.nick_name like concat('%', #{keyword}, '%')
                or u.phonenumber like concat('%', #{keyword}, '%'))
        </if>
        order by m.register_time desc
    </select>

    <select id="countByMobile" resultType="int">
        select count(1)
        from biz_member m
        inner join sys_user u on m.member_id = u.user_id
        where u.phonenumber = #{mobile} and u.del_flag = '0'
        <if test="excludeId != null">
            and m.member_id != #{excludeId}
        </if>
    </select>

    <select id="countByByMemberName" resultType="int">
        select count(1)
        from biz_member m
        inner join sys_user u on m.member_id = u.user_id
        where u.user_name = #{memberName} and u.del_flag = '0'
    </select>

    <select id="selectConsumptionFromOrder" resultType="java.util.HashMap">
        select count(1) as orderCount, ifnull(sum(pay_amount), 0) as totalAmount
        from biz_order
        where member_id = #{memberId} and order_status = '3' and del_flag = '0'
    </select>

    <insert id="insert">
        insert into biz_member (
            member_id,
            <if test="memberLevel != null">member_level,</if>
            order_count, total_amount, register_time, create_by, create_time
        ) values (
            #{memberId},
            <if test="memberLevel != null">#{memberLevel},</if>
            #{orderCount}, #{totalAmount}, #{registerTime}, #{createBy}, sysdate()
        )
    </insert>

    <update id="update">
        update biz_member
        <set>
            <if test="memberLevel != null">member_level = #{memberLevel},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where member_id = #{memberId}
    </update>

    <update id="updateAccount">
        update sys_user
        <set>
            <if test="memberCode != null and memberCode != ''">user_name = #{memberCode},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="email != null">email = #{email},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where user_id = #{memberId} and del_flag = '0'
    </update>

    <update id="updateStatus">
        update sys_user
        set status = #{status}, update_by = #{updateBy}, update_time = sysdate()
        where user_id = #{memberId} and del_flag = '0'
    </update>

    <update id="updateConsumption">
        update biz_member
        set order_count = #{orderCount}, total_amount = #{totalAmount}, update_time = sysdate()
        where member_id = #{memberId}
    </update>

    <select id="selectWxOpenid" resultType="String">
        select wx_openid from biz_member where member_id = #{memberId}
    </select>

    <update id="updateWxOpenid">
        update biz_member set wx_openid = #{wxOpenid}, update_time = sysdate() where member_id = #{memberId}
    </update>

</mapper>
