<?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.admin.dao.AdminMapper">

    <sql id="FIELDS">
      id, username, nickname, password, status,
      create_time
    </sql>

    <select id="selectByUsername" parameterType="String" resultType="AdminDO">
        SELECT
            <include refid="FIELDS" />
        FROM admin
        WHERE username = #{username}
        AND deleted = 0
    </select>

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

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

    <insert id="insert" parameterType="AdminDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO admin (
          username, nickname, password, status,
          create_time, deleted
        ) VALUES (
          #{username}, #{nickname}, #{password}, #{status},
          #{createTime}, #{deleted}
        )
    </insert>

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

    <update id="update" parameterType="RoleDO">
        UPDATE admin
        <set>
            <if test="username != null">
                , username = #{username}
            </if>
            <if test="nickname != null">
                , nickname = #{nickname}
            </if>
            <if test="password != null">
                , password = #{password}
            </if>
            <if test="status != null">
                , status = #{status}
            </if>
            <if test="deleted != null">
                , deleted = #{deleted}
            </if>
        </set>
        WHERE id = #{id}
    </update>

</mapper>
