<?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.example.mapper.UserMapper">

    <sql id="Base_Column_List">
        id,username,password,name,avatar,role,sex,phone,account,openid,address_ids
    </sql>

    <!--    用户名查询 -->
    <select id="selectByUserName" resultType="com.example.entity.Account">
        select
        <include refid="Base_Column_List" />
        from user
        where username = #{username}
    </select>
    <!--    根据openid查找查找用户信息 -->
    <select id="selectByOpenid" resultType="com.example.entity.User">
        select
        <include refid="Base_Column_List" />
        from user
        where openid = #{openid}
    </select>
    <select id="selectAll" resultType="com.example.entity.User">
        select
        <include refid="Base_Column_List" />
        from user
        <where>
            <if test="id != null"> and id = #{id}</if>
            <if test="username != null"> and username like concat('%', #{username}, '%')</if>
            <if test="password != null"> and password like concat('%', #{password}, '%')</if>
            <if test="name != null"> and name like concat('%', #{name}, '%')</if>
            <if test="avatar != null"> and avatar like concat('%', #{avatar}, '%')</if>
            <if test="role != null"> and role like concat('%', #{role}, '%')</if>
            <if test="sex != null"> and sex like concat('%', #{sex}, '%')</if>
            <if test="phone != null"> and phone like concat('%', #{phone}, '%')</if>
            <if test="account != null"> and account = #{account}</if>
            <if test="openid != null"> and openid like concat('%', #{openid}, '%')</if>
            <if test="addressIds != null"> and address_ids like concat('%', #{addressIds}, '%')</if>
        </where>
        order by id desc
    </select>

    <select id="selectById" resultType="com.example.entity.User">
        select
        <include refid="Base_Column_List" />
        from user
        where id = #{id}
    </select>

    <delete id="deleteById">
        delete from user
        where  id = #{id}
    </delete>

    <insert id="insert" parameterType="com.example.entity.User" useGeneratedKeys="true">
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="username != null">username,</if>
            <if test="password != null">password,</if>
            <if test="name != null">name,</if>
            <if test="avatar != null">avatar,</if>
            <if test="role != null">role,</if>
            <if test="sex != null">sex,</if>
            <if test="phone != null">phone,</if>
            <if test="account != null">account,</if>
            <if test="openid != null">openid,</if>
            <if test="addressIds != null">address_ids,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="username != null">#{username},</if>
            <if test="password != null">#{password},</if>
            <if test="name != null">#{name},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="role != null">#{role},</if>
            <if test="sex != null">#{sex},</if>
            <if test="phone != null">#{phone},</if>
            <if test="account != null">#{account},</if>
            <if test="openid != null">#{openid},</if>
            <if test="addressIds != null">#{addressIds},</if>
        </trim>
    </insert>

    <update id="updateById" parameterType="com.example.entity.User">
        update user
        <set>
            <if test="username != null">
                username = #{username},
            </if>
            <if test="password != null">
                password = #{password},
            </if>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="avatar != null">
                avatar = #{avatar},
            </if>
            <if test="role != null">
                role = #{role},
            </if>
            <if test="sex != null">
                sex = #{sex},
            </if>
            <if test="phone != null">
                phone = #{phone},
            </if>
            <if test="account != null">
                account = #{account},
            </if>
            <if test="openid != null">
                openid = #{openid},
            </if>
            <if test="addressIds != null">
                address_ids = #{addressIds},
            </if>
        </set>
        where id = #{id} 
    </update>

</mapper>