<?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.easyproject.service.mapper.member.ClientMemberMapper">

    <resultMap type="ClientMember" id="ClientMemberResult">
        <result property="id"    column="id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="memberNum"    column="member_num"    />
        <result property="memberName"    column="member_name"    />
        <result property="password"    column="password"    />
        <result property="memberAddress"    column="member_address"    />
        <result property="avatarUrl"    column="avatar_url"    />
        <result property="inviteCode"    column="invite_code"    />
        <result property="status"    column="status"    />
        <result property="loginIp"    column="login_ip"    />
        <result property="loginDate"    column="login_date"    />
        <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="delFlag"    column="del_flag"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectClientMemberVo">
        select id, nick_name, member_num, member_name, password, member_address, avatar_url, invite_code, status, login_ip, login_date, create_by, create_time, update_by, update_time, del_flag, remark from client_member
    </sql>

    <select id="selectClientMemberList" parameterType="ClientMember" resultMap="ClientMemberResult">
        <include refid="selectClientMemberVo"/>
        <where>
            <if test="memberNum != null  and memberNum != ''"> and member_num = #{memberNum}</if>
            <if test="memberName != null  and memberName != ''"> and member_name like concat('%', #{memberName}, '%')</if>
            <if test="memberAddress != null  and memberAddress != ''"> and member_address like concat('%', #{memberAddress}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''"> and create_time between #{params.beginCreateTime} and #{params.endCreateTime}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectClientMemberById" parameterType="Long" resultMap="ClientMemberResult">
        <include refid="selectClientMemberVo"/>
        where id = #{id}
    </select>

    <update id="updateClientMember" parameterType="ClientMember">
        update client_member
        <trim prefix="SET" suffixOverrides=",">
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="memberNum != null">member_num = #{memberNum},</if>
            <if test="memberName != null">member_name = #{memberName},</if>
            <if test="password != null">password = #{password},</if>
            <if test="memberAddress != null">member_address = #{memberAddress},</if>
            <if test="avatarUrl != null">avatar_url = #{avatarUrl},</if>
            <if test="inviteCode != null">invite_code = #{inviteCode},</if>
            <if test="status != null">status = #{status},</if>
            <if test="loginIp != null">login_ip = #{loginIp},</if>
            <if test="loginDate != null">login_date = #{loginDate},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>



    <resultMap id="MemberResult" type="ClientMember" >
        <result column="id" property="id" />
        <result column="nick_name" property="nickName" />
        <result column="member_num" property="memberNum" />
        <result column="member_name" property="memberName" />
        <result column="password" property="password" />
        <result column="member_address" property="memberAddress" />
        <result column="avatar_url" property="avatarUrl" />
        <result column="invite_code" property="inviteCode" />
        <result column="status" property="status" />
        <result column="login_ip" property="loginIp" />
        <result column="login_date" property="loginDate" />
        <result column="create_by" property="createBy" />
        <result column="create_time" property="createTime" />
        <result column="update_by" property="updateBy" />
        <result column="update_time" property="updateTime" />
        <result column="remark" property="remark" />
    </resultMap>

    <sql id="selectMemberVo">
        select id,
               nick_name,
               member_num,
               member_name,
               password,
               member_address,
               avatar_url,
               invite_code,
               status,
               login_ip,
               login_date,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from client_member
    </sql>

    <!--修改用户信息-->
    <update id="updateMember" parameterType="ClientMember">
        UPDATE client_member
        <set>
            <if test="null != nickName and '' != nickName">nick_name = #{nickName},</if>
            <if test="null != memberNum and '' != memberNum">member_num = #{memberNum},</if>
            <if test="null != memberName and '' != memberName">member_name = #{memberName},</if>
            <if test="null != password and '' != password">password = #{password},</if>
            <if test="null != memberAddress and '' != memberAddress">member_address = #{memberAddress},</if>
            <if test="null != avatarUrl and '' != avatarUrl">avatar_url = #{avatarUrl},</if>
            <if test="null != inviteCode and '' != inviteCode">invite_code = #{inviteCode},</if>
            <if test="null != status and '' != status">status = #{status},</if>
            <if test="null != loginIp and '' != loginIp">login_ip = #{loginIp},</if>
            <if test="null != loginDate">login_date = #{loginDate},</if>
            <if test="null != remark and '' != remark">remark = #{remark},</if>
            update_time = sysdate()
        </set>
        WHERE id = #{id}
    </update>

    <!--校验用户名称是否唯一-->
    <select id="checkUserNameUnique" parameterType="String" resultMap="MemberResult">
        select id, member_name from client_member where member_name = #{userName} and del_flag = '0' limit 1
    </select>

    <!--注册用户信息-->
    <insert id="insertMember" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="ClientMember">
        INSERT INTO client_member
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != nickName and '' != nickName">
                nick_name,
            </if>
            <if test="null != memberNum and '' != memberNum">
                member_num,
            </if>
            <if test="null != memberName and '' != memberName">
                member_name,
            </if>
            <if test="null != password and '' != password">
                password,
            </if>
            <if test="null != memberAddress and '' != memberAddress">
                member_address,
            </if>
            <if test="null != avatarUrl and '' != avatarUrl">
                avatar_url,
            </if>
            <if test="null != inviteCode and '' != inviteCode">
                invite_code,
            </if>
            <if test="null != status and '' != status">
                status,
            </if>
            <if test="null != loginIp and '' != loginIp">
                login_ip,
            </if>
            <if test="null != loginDate">
                login_date,
            </if>
            <if test="null != createBy and '' != createBy">
                create_by,
            </if>
            <if test="null != createTime and '' != createTime">
                create_time,
            </if>
            <if test="null != updateBy and '' != updateBy">
                update_by,
            </if>
            <if test="null != updateTime and '' != updateTime">
                update_time,
            </if>
            <if test="null != remark and '' != remark">
                remark
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != nickName and '' != nickName">
                #{nickName},
            </if>
            <if test="null != memberNum and '' != memberNum">
                #{memberNum},
            </if>
            <if test="null != memberName and '' != memberName">
                #{memberName},
            </if>
            <if test="null != password and '' != password">
                #{password},
            </if>
            <if test="null != memberAddress and '' != memberAddress">
                #{memberAddress},
            </if>
            <if test="null != avatarUrl and '' != avatarUrl">
                #{avatarUrl},
            </if>
            <if test="null != inviteCode and '' != inviteCode">
                #{inviteCode},
            </if>
            <if test="null != status and '' != status">
                #{status},
            </if>
            <if test="null != loginIp and '' != loginIp">
                #{loginIp},
            </if>
            <if test="null != loginDate">
                #{loginDate},
            </if>
            <if test="null != createBy and '' != createBy">
                #{createBy},
            </if>
            <if test="null != createTime and '' != createTime">
                #{createTime},
            </if>
            <if test="null != updateBy and '' != updateBy">
                #{updateBy},
            </if>
            <if test="null != updateTime and '' != updateTime">
                #{updateTime},
            </if>
            <if test="null != remark and '' != remark">
                #{remark}
            </if>
        </trim>
    </insert>

    <!--通过用户名查询用户-->
    <select id="selectMemberByMemberName" parameterType="String" resultMap="MemberResult">
        <include refid="selectMemberVo"/>
        where member_name = #{memberName} and del_flag = '0'
    </select>

    <!--根据用户ID查询用户信息-->
    <select id="selectMemberById" parameterType="Long" resultMap="MemberResult">
        <include refid="selectMemberVo"/>
        where id = #{memberId}
    </select>

    <!--根据地址查询用户信息-->
    <select id="selectMemberByAddr" parameterType="String" resultMap="MemberResult">
        <include refid="selectMemberVo"/>
        where member_address = #{address}
    </select>
</mapper>
