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

    <resultMap id="SysUserResult" type="com.amms.domain.SysUser">
        <id column="id" property="id"/>
        <result column="username" property="username" />
        <result column="nickname" property="nickname" />
        <result column="avatar" property="avatar" />
        <result column="password" property="password" />
        <result column="status" property="status" />
        <result column="role" property="role" />
        <result column="creator" property="creator" />
        <result column="create_time" property="createTime" />
        <result column="updater" property="updater" />
        <result column="update_time" property="updateTime" />
        <result column="remark" property="remark" />
        <result column="deleted" property="deleted" />
        <association property="tourist" column="id" select="com.amms.mapper.TouristMapper.selectTouristById"/>
    </resultMap>

    <sql id="selectSql">
        select id, username, nickname, avatar, password, status, role, creator, create_time, updater, update_time, remark, deleted from sys_user
    </sql>

    <select id="selectSysUserList" parameterType="com.amms.domain.SysUser" resultMap="SysUserResult">
        <include refid="selectSql" />
        <where>
            deleted != 1
            <if test="username != null and username != ''"> and username like concat('%', #{username}, '%')</if>
            <if test="nickname != null and nickname != ''"> and nickname like concat('%', #{nickname}, '%')</if>
            <if test="role != null and role != ''"> and role = #{role}</if>
        </where>
    </select>

    <select id="selectByUsername" parameterType="String" resultMap="SysUserResult">
        <include refid="selectSql" />
        where username = #{username} and deleted != 1
    </select>

    <select id="selectById" parameterType="Long" resultMap="SysUserResult">
        <include refid="selectSql" />
        where id = #{id} and deleted != 1
    </select>

    <insert id="insertSysUser" parameterType="com.amms.domain.SysUser" useGeneratedKeys="true" keyProperty="id">
        insert into sys_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null and username != ''">username,</if>
            <if test="nickname != null and nickname != ''">nickname,</if>
            <if test="avatar != null and avatar != ''">avatar,</if>
            <if test="password != null and password != ''">password,</if>
            <if test="status != null">status,</if>
            <if test="role != null and role != ''">role,</if>
            <if test="creator != null">creator,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updater != null">updater,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="deleted != null">deleted,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="username != null and username != ''">#{username},</if>
            <if test="nickname != null and nickname != ''">#{nickname},</if>
            <if test="avatar != null and avatar != ''">#{avatar},</if>
            <if test="password != null and password != ''">#{password},</if>
            <if test="status != null">#{status},</if>
            <if test="role != null and role != ''">#{role},</if>
            <if test="creator != null">#{creator},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updater != null">#{updater},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="deleted != null">#{deleted},</if>
        </trim>
    </insert>

    <update id="updateSysUser" parameterType="com.amms.domain.SysUser">
        update sys_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="username != null and username != ''">username = #{username},</if>
            <if test="nickname != null and nickname != ''">nickname = #{nickname},</if>
            <if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="status != null">status = #{status},</if>
            <if test="role != null and role != ''">role = #{role},</if>
            <if test="updater != null">updater = #{updater},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysUserById" parameterType="Long">
        delete from sys_user where id = #{id}
    </delete>
</mapper>
