<?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.user.service.mapper.SysUserMapper">

    <resultMap type="SysUser" id="SysUserResult">
        <id property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="sex" column="sex"/>
        <result property="avatar" column="avatar"/>
        <result property="phoneNumber" column="phone_number"/>
        <result property="passWord" column="pass_word"/>
        <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="remark" column="remark"/>
        <result property="email" column="email"/>
        <result property="userType" column="user_type"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <sql id="selectUserVo">
        user_id
        ,user_name,nick_name,sex,avatar,phone_number,pass_word,status,login_ip,login_date,create_time,create_by,update_by,
			update_time,remark,email,user_type,del_flag
    </sql>

    <delete id="deleteUserByIds" parameterType="Long">
        delete from sys_user where user_id in
        <foreach collection="array" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
        select
        <include refid="selectUserVo"/>
        from sys_user
        where user_id = #{userId}
    </select>


    <select id="selectUserByVo" parameterType="SysUser" resultMap="SysUserResult">
        select
        <include refid="selectUserVo"/>
        from sys_user
        where del_flag='0'
        <if test="sex != null and sex != ''">
            and sex = #{sex}
        </if>
        <if test="nickName != null and nickName != ''">
            and nick_name like concat('%', #{nickName}, '%')
        </if>
        <if test="userName != null and userName != ''">
            and user_name like concat('%', #{userName}, '%')
        </if>
        <if test="phoneNumber != null and phoneNumber != ''">
            and phone_number like concat('%', #{phoneNumber}, '%')
        </if>
        <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
            and date_format(create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
        </if>
        <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
            and date_format(create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
        </if>
    </select>

    <select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
        select
        <include refid="selectUserVo"/>
        from sys_user
        where user_name = #{userName}
    </select>

    <insert id="add" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
        INSERT INTO sys_user (user_id,user_name, nick_name, sex, pass_word, status, email, create_by, create_time, del_flag,phone_number)
        values (
                #{userId},
                #{userName},
                #{nickName},
                #{sex},
                #{passWord},
                #{status},
                #{email},
                #{createBy},
                #{createTime},
                #{delFlag},
                #{phoneNumber}
                )
    </insert>

    <update id="updateUser" parameterType="SysUser">
        update sys_user
        <set>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
            <if test="email != null ">email = #{email},</if>
            <if test="phoneNumber != null and phoneNumber!='' ">phone_number = #{phoneNumber},</if>
            <if test="sex != null and sex != ''">sex = #{sex},</if>
            <if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
            <if test="passWord != null and passWord != ''">pass_word = #{passWord},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>
            <if test="loginDate != null">login_date = #{loginDate},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </set>
        where user_id = #{userId}
    </update>
</mapper> 