<?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.suelec.permission.dao.SysUserDao">
  <resultMap id="BaseResultMap" type="com.suelec.permission.pojo.SysUser">
    <result column="id" jdbcType="VARCHAR" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="real_name" jdbcType="VARCHAR" property="realName" />
    <result column="nick_name" jdbcType="VARCHAR" property="nickName" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="sex" jdbcType="TINYINT" property="sex" />
    <result column="deleted" jdbcType="TINYINT" property="deleted" />
    <result column="create_id" jdbcType="VARCHAR" property="createId" />
    <result column="update_id" jdbcType="VARCHAR" property="updateId" />
    <result column="create_where" jdbcType="TINYINT" property="createWhere" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <insert id="insert" parameterType="com.suelec.permission.pojo.SysUser">
    insert into sys_user (id, username,
      `password`, phone,
      real_name, nick_name, email, 
      `status`, sex, deleted, 
      create_id, update_id, create_where, 
      create_time, update_time)
    values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR},
      #{password,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
      #{realName,jdbcType=VARCHAR}, #{nickName,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, 
      #{status,jdbcType=TINYINT}, #{sex,jdbcType=TINYINT}, #{deleted,jdbcType=TINYINT}, 
      #{createId,jdbcType=VARCHAR}, #{updateId,jdbcType=VARCHAR}, #{createWhere,jdbcType=TINYINT}, 
      #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.suelec.permission.pojo.SysUser">
    insert into sys_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="phone != null">
        phone,
      </if>
      <if test="realName != null">
        real_name,
      </if>
      <if test="nickName != null">
        nick_name,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="sex != null">
        sex,
      </if>
      <if test="deleted != null">
        deleted,
      </if>
      <if test="createId != null">
        create_id,
      </if>
      <if test="updateId != null">
        update_id,
      </if>
      <if test="createWhere != null">
        create_where,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="realName != null">
        #{realName,jdbcType=VARCHAR},
      </if>
      <if test="nickName != null">
        #{nickName,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=TINYINT},
      </if>
      <if test="sex != null">
        #{sex,jdbcType=TINYINT},
      </if>
      <if test="deleted != null">
        #{deleted,jdbcType=TINYINT},
      </if>
      <if test="createId != null">
        #{createId,jdbcType=VARCHAR},
      </if>
      <if test="updateId != null">
        #{updateId,jdbcType=VARCHAR},
      </if>
      <if test="createWhere != null">
        #{createWhere,jdbcType=TINYINT},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>


  <sql id="base_sysuser_sql">
    id, username, `password`, phone, real_name, nick_name, email, `status`, sex, deleted,
    create_id, update_id, create_where,create_time, update_time
  </sql>

  <select id="findByUsername" resultType="Tuser" parameterType="string">
    select * from tuser where regname=#{username}
  </select>

  <select id="selectByPrimaryKey" resultType="SysUser" parameterType="string">
    select *
    from sys_user
    where id=#{id}
    and deleted=1
  </select>

  <!-- 分页查询用户（包括搜索条件） -->
  <select id="selectAll" resultType="com.suelec.permission.vo.resp.UserTableRespVo" parameterType="com.suelec.permission.vo.req.UserPageReqVo">
    SELECT
    su_a.id,
    su_a.regname username,
    su_a.phone,
    su_a.name real_name,
    su_a.name nick_name,
    su_a.email,
    su_a. `status`,
    su_a.sex
    FROM
    tuser su_a
    <where>
      <if test="username != null and username != ''">
        and su_a.regname like concat('%',#{username},'%')
      </if>
      <if test="nickName != null and nickName != ''">
        and su_a.name like concat('%',#{nickName},'%')
      </if>
    </where>
  </select>

  <!-- 更新用户信息 -->
  <update id="updateSelective" parameterType="com.suelec.permission.pojo.SysUser">
    update sys_user
    <set>
        <if test="username != null and username != ''">
          username=#{username},
        </if>
        <if test="password != null and password != ''">
          `password`=#{password},
        </if>
        <if test="phone != null and phone != ''">
          phone=#{phone},
        </if>
        <if test="realName != null and realName != ''">
          real_name=#{realName},
        </if>
        <if test="nickName != null and nickName != ''">
          nick_name=#{nickName},
        </if>
        <if test="email != null and email != ''">
          email=#{email},
        </if>
        <if test="status != null and status != ''">
          `status`=#{status},
        </if>
        <if test="sex != null and sex != ''">
          sex=#{sex},
        </if>
        <if test="deleted != null and deleted != ''">
          deleted=#{deleted},
        </if>
        <if test="createId != null and createId != ''">
          create_id=#{createId},
        </if>
        <if test="updateId != null and updateId != ''">
          update_id=#{updateId},
        </if>
        <if test="createWhere != null and createWhere != ''">
          create_where=#{createWhere},
        </if>
        <if test="createTime != null">
          create_time=#{createTime},
        </if>
        <if test="updateTime != null">
          update_time=#{updateTime},
        </if>
    </set>
    where id=#{id}
  </update>


  <!-- 批量删除用户 -->
  <update id="deletedUsers" parameterType="com.suelec.permission.pojo.SysUser">
    update sys_user
    <set>
      deleted=0,
      <if test="sysUser.updateId != null and sysUser.updateId != ''">
        update_id=#{sysUser.updateId},
      </if>
      <if test="sysUser.updateTime != null">
        update_time=#{sysUser.updateTime},
      </if>
    </set>
    where id in
    <foreach collection="list" separator="," open="(" close=")" item="item">
        #{item}
    </foreach>
  </update>
</mapper>