<?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="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="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 member_id, mobile, member_code, nick_name, password, member_level, status, order_count, total_amount,
               register_time, avatar, del_flag, create_by, create_time, update_by, update_time, remark
        from biz_member
    </sql>

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

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

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

    <select id="countByMobile" resultType="int">
        select count(1) from biz_member
        where mobile = #{mobile} and del_flag = '0'
        <if test="excludeId != null">
            and member_id != #{excludeId}
        </if>
    </select>

    <select id="countByByMemberName" resultType="int">
        select count(1) from biz_member
        where member_code = #{memberName} and 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" useGeneratedKeys="true" keyProperty="memberId">
        insert into biz_member (
            mobile, member_code, nick_name, password,
            <if test="memberLevel != null">member_level,</if>
            status, order_count, total_amount,
            register_time, avatar, del_flag, create_by, create_time
        ) values (
            #{mobile}, #{memberCode}, #{nickName}, #{password},
            <if test="memberLevel != null">#{memberLevel},</if>
            #{status}, #{orderCount}, #{totalAmount},
            #{registerTime}, #{avatar}, '0', #{createBy}, sysdate()
        )
    </insert>

    <update id="update">
        update biz_member
        <set>
            <if test="memberCode != null">member_code = #{memberCode},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <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} and del_flag = '0'
    </update>

    <update id="updateStatus">
        update biz_member
        set status = #{status}, update_by = #{updateBy}, update_time = sysdate()
        where member_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} and del_flag = '0'
    </update>

</mapper>
