<?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="cn.iocoder.mall.user.biz.dao.UserMapper">

    <sql id="FIELDS">
      id, mobile, nickname, avatar, status,
      create_time, deleted
    </sql>

    <insert id="insert" parameterType="UserDO" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO users (
          id, mobile, status, create_time, deleted
        ) VALUES (
          #{id}, #{mobile}, #{status}, #{createTime}, #{deleted}
        )
    </insert>

    <update id="update" parameterType="UserDO">
        UPDATE users
        <set>
            <if test="mobile != null">
                , mobile = #{mobile}
            </if>
            <if test="nickname != null">
                , nickname = #{nickname}
            </if>
            <if test="avatar != null">
                , avatar = #{avatar}
            </if>
            <if test="status != null">
                , status = #{status}
            </if>
            <if test="deleted != null">
                , deleted = #{deleted}
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <select id="selectById" parameterType="Integer" resultType="UserDO">
        SELECT
           <include refid="FIELDS" />
        FROM users
        WHERE id = #{id}
        AND deleted = 0
    </select>

    <select id="selectByMobile" parameterType="String" resultType="UserDO">
      SELECT
          <include refid="FIELDS" />
      FROM users
      WHERE mobile = #{mobile}
      AND deleted = 0
    </select>

    <select id="selectListByNicknameLike" resultType="UserDO">
        SELECT
            <include refid="FIELDS" />
        FROM users
        <where>
            <if test="nickname != null">
                nickname LIKE "%"#{nickname}"%"
            </if>
            <if test="status != null">
                status = #{status}
            </if>
            AND deleted = 0
        </where>
        LIMIT #{offset}, #{limit}
    </select>

    <select id="selectCountByNicknameLike" resultType="Integer">
        SELECT
            COUNT(1)
        FROM users
        <where>
            <if test="nickname != null">
                nickname LIKE "%"#{nickname}"%"
            </if>
            <if test="status != null">
                status = #{status}
            </if>
            AND deleted = 0
        </where>
    </select>


</mapper>
