<?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.dgtis.dorm.manager.db.dao.AdminMapper">
  <resultMap id="BaseResultMap" type="com.dgtis.dorm.manager.db.domain.Admin">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="nickname" jdbcType="VARCHAR" property="nickname" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="last_login_ip" jdbcType="VARCHAR" property="lastLoginIp" />
    <result column="last_login_time" jdbcType="TIMESTAMP" property="lastLoginTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="avatar" jdbcType="VARCHAR" property="avatar" />
    <result column="add_time" jdbcType="TIMESTAMP" property="addTime" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="userId" jdbcType="INTEGER" property="userId" />
    <result column="userEmail" jdbcType="VARCHAR" property="userEmail" />
    <result column="employeNo" jdbcType="VARCHAR" property="employeNo" />
    <result column="post" jdbcType="VARCHAR" property="post" />
  </resultMap>

  <sql id="Base_Column_List">
    id, username, nickname, `password`, last_login_ip, last_login_time, update_time, avatar, add_time, 
    deleted, phone,userId,userEmail,employeNo,post
  </sql>

  <select id="selectByExample" parameterType="com.dgtis.dorm.manager.db.domain.Admin" resultMap="BaseResultMap">
    select distinct
    <include refid="Base_Column_List" />
    from zhll_sys_admin
    <where>
      <if test="username != null and username != ''">
        username = #{username}
      </if>
      <if test="employeNo != null and employeNo != ''">
        employeNo = #{employeNo}
      </if>
      and deleted = #{deleted}
    </where>
  </select>

  <select id="selectByExampleSelective" parameterType="map" resultMap="BaseResultMap">
    select distinct
    <foreach collection="selective" item="column" separator=",">
      ${column.value}
    </foreach>
    from zhll_sys_admin
    <where>
      <if test="example.username != null">
      username like concat('%', #{example.username}, '%')
      </if>
      and deleted = #{example.deleted}
    </where>
  </select>

  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from zhll_sys_admin
    where id = #{id,jdbcType=INTEGER}
  </select>

  <select id="selectByPrimaryKeySelective" parameterType="map" resultMap="BaseResultMap">
    select
    <foreach collection="selective" item="column" separator=",">
      ${column.value}
    </foreach>
    from zhll_sys_admin
    where id = #{id,jdbcType=INTEGER}
  </select>

  <select id="countByExample" parameterType="com.dgtis.dorm.manager.db.domain.Admin" resultType="java.lang.Long">
    select count(*) from zhll_sys_admin
    <where>
      <if test="username != null">
      username like concat('%', #{username}, '%')
      </if>
      and deleted = #{deleted}
    </where>
  </select>
  
  <select id="findByIdList" parameterType="map" resultMap="BaseResultMap">
    select distinct
    <foreach collection="selective" item="column" separator=",">
      ${column.value}
    </foreach>
    from zhll_sys_admin
    <where>
      id in 
      <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
        #{id}
      </foreach>
    </where>
  </select>

  <insert id="insertSelective" parameterType="com.dgtis.dorm.manager.db.domain.Admin">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into zhll_sys_admin
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="username != null">
        username,
      </if>
      <if test="nickname != null">
        nickname,
      </if>
      <if test="password != null">
        `password`,
      </if>
      <if test="lastLoginIp != null">
        last_login_ip,
      </if>
      <if test="lastLoginTime != null">
        last_login_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="avatar != null">
        avatar,
      </if>
      <if test="addTime != null">
        add_time,
      </if>
      <if test="deleted != null">
        deleted,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="userId != null">
        userId,
      </if>
      <if test="userEmail != null">
        userEmail,
      </if>
      <if test="employeNo != null">
        employeNo,
      </if>
      <if test="post != null">
        post,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="nickname != null">
        #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="lastLoginIp != null">
        #{lastLoginIp,jdbcType=VARCHAR},
      </if>
      <if test="lastLoginTime != null">
        #{lastLoginTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="avatar != null">
        #{avatar,jdbcType=VARCHAR},
      </if>
      <if test="addTime != null">
        #{addTime,jdbcType=TIMESTAMP},
      </if>
      <if test="deleted != null">
        #{deleted,jdbcType=BIT},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="userId != null">
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="userEmail != null">
        #{userEmail,jdbcType=VARCHAR},
      </if>
      <if test="employeNo != null">
        #{employeNo,jdbcType=VARCHAR},
      </if>
      <if test="post != null">
        #{post,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
	
  <!-- 登录名不可改 username = #{username,jdbcType=VARCHAR}, -->
  <update id="updateByPrimaryKey" parameterType="com.dgtis.dorm.manager.db.domain.Admin">
    update zhll_sys_admin
    set
      username = #{username,jdbcType=VARCHAR},
      nickname = #{nickname,jdbcType=VARCHAR},
      `password` = #{password,jdbcType=VARCHAR},
      last_login_ip = #{lastLoginIp,jdbcType=VARCHAR},
      last_login_time = #{lastLoginTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      avatar = #{avatar,jdbcType=VARCHAR},
      add_time = #{addTime,jdbcType=TIMESTAMP},
      deleted = #{deleted,jdbcType=BIT},
      phone = #{phone,jdbcType=VARCHAR},
      userId = #{userId,jdbcType=INTEGER},
      userEmail= #{userEmail,jdbcType=VARCHAR},
      employeNo= #{employeNo,jdbcType=VARCHAR},
      post = #{post,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <update id="updateByPrimaryKeySelective" parameterType="com.dgtis.dorm.manager.db.domain.Admin">
    update zhll_sys_admin
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="nickname != null">
        nickname = #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        `password` = #{password,jdbcType=VARCHAR},
      </if>
      <if test="lastLoginIp != null">
        last_login_ip = #{lastLoginIp,jdbcType=VARCHAR},
      </if>
      <if test="lastLoginTime != null">
        last_login_time = #{lastLoginTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="avatar != null">
        avatar = #{avatar,jdbcType=VARCHAR},
      </if>
      <if test="addTime != null">
        add_time = #{addTime,jdbcType=TIMESTAMP},
      </if>
      <if test="deleted != null">
        deleted = #{deleted,jdbcType=BIT},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="userId != null">
        userId = #{userId,jdbcType=INTEGER},
      </if>
      <if test="userEmail != null">
        userEmail = #{userEmail,jdbcType=VARCHAR},
      </if>
      <if test="post != null">
        post = #{post,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from zhll_sys_admin
    where id = #{id,jdbcType=INTEGER}
  </delete>

  <select id="selectByUserId" parameterType="int" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from zhll_sys_admin
    where userId = #{userId,jdbcType=INTEGER}
  </select>

  <select id="getUserInfoByUserId" parameterType="java.lang.String" resultType="java.util.Map" >
    select
       d.DEPT_UNID deptUnid,
       p.post_code postCode
    from user_info u
    left join  department_info d on d.deptId =u.department
    left join sys_post p on p.id=u.post
    where u.id = #{userId}
  </select>
</mapper>