<?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.ydd.oms.mapper.sys.AdminMapper">
  <resultMap id="BaseResultMap" type="com.ydd.oms.entity.sys.Admin">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="mobile" jdbcType="CHAR" property="mobile" />
    <result column="password" jdbcType="CHAR" property="password" />
    <result column="salt" jdbcType="VARCHAR" property="salt" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="gender" jdbcType="TINYINT" property="gender" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="created_id" jdbcType="INTEGER" property="createdId" />
    <result column="modified_id" jdbcType="INTEGER" property="modifiedId" />
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime" />
    <result column="modified_time" jdbcType="TIMESTAMP" property="modifiedTime" />
    <result column="deleted" jdbcType="TINYINT" property="deleted" />
  </resultMap>
  <sql id="Base_Column_List">
    id, status, mobile, password, salt, username, gender, email, created_id,
    modified_id, created_time, modified_time, deleted
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from sys_admin
    where id = #{id,jdbcType=INTEGER} AND deleted = 0
  </select>

  <!--查询管理员-->
  <select id="findByAccount" parameterType="String" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List" />
    FROM
      sys_admin
    WHERE
      deleted = 0 AND (mobile = #{account, jdbcType=CHAR} OR email = #{account, jdbcType=VARCHAR})
  </select>

  <select id="findAll" resultMap="BaseResultMap">
    SELECT
      <include refid="Base_Column_List" />
    FROM
      sys_admin
    WHERE
      deleted = 0
  </select>

  <!--查询管理员数量-->
  <select id="countByMobileAndExcludeId" resultType="INTEGER">
    SELECT
      COUNT(1)
    FROM
      sys_admin
    WHERE
      deleted = 0 AND mobile = #{mobile, jdbcType=VARCHAR}
    <if test="excludeId != null and excludeId != 0">
      AND id != #{excludeId, jdbcType=INTEGER}
    </if>
  </select>

  <!--查询管理员数量-->
  <select id="countByEmailAndExcludeId" resultType="INTEGER">
    SELECT
      COUNT(1)
    FROM
      sys_admin
    WHERE
      deleted = 0 AND email = #{email, jdbcType=VARCHAR}
    <if test="excludeId != null and excludeId != 0">
      AND id != #{excludeId, jdbcType=INTEGER}
    </if>
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    UPDATE
      sys_admin
    SET
      deleted = 1
    WHERE
      id = #{id, jdbcType=INTEGER}
  </delete>

  <!--删除管理员-->
  <update id="deleteByIds">
    UPDATE
      sys_admin
    SET
      deleted = 1
    WHERE
      id
    IN
      <foreach collection="array" item="id" index="index"
               open="(" close=")" separator=",">
        #{id}
      </foreach>
  </update>

  <!--更新管理员状态-->
  <update id="updateStatus">
    UPDATE
      sys_admin
    SET
      status = #{status, jdbcType=TINYINT}
    WHERE
      id
    IN
    <foreach collection="ids" item="id" index="index"
             open="(" close=")" separator=",">
      #{id}
    </foreach>
  </update>

  <insert id="insert" parameterType="com.ydd.oms.entity.sys.Admin">
    insert into sys_admin (id, status, mobile,
      password, salt,
      username, gender, email,
      created_id, modified_id, created_time, 
      modified_time, deleted)
    values (#{id,jdbcType=INTEGER}, #{status,jdbcType=TINYINT}, #{mobile,jdbcType=CHAR}, 
      #{password,jdbcType=CHAR}, #{salt,jdbcType=VARCHAR},
      #{username,jdbcType=VARCHAR}, #{gender,jdbcType=TINYINT}, #{email,jdbcType=VARCHAR},
      #{createdId,jdbcType=INTEGER}, #{modifiedId,jdbcType=INTEGER}, #{createdTime,jdbcType=TIMESTAMP}, 
      #{modifiedTime,jdbcType=TIMESTAMP}, #{deleted,jdbcType=TINYINT})
  </insert>
  <insert id="insertSelective" parameterType="com.ydd.oms.entity.sys.Admin" useGeneratedKeys="true" keyProperty="id">
    insert into sys_admin
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="mobile != null">
        mobile,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="salt != null">
        salt,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="gender != null">
        gender,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="createdId != null">
        created_id,
      </if>
      <if test="modifiedId != null">
        modified_id,
      </if>
      <if test="createdTime != null">
        created_time,
      </if>
      <if test="modifiedTime != null">
        modified_time,
      </if>
      <if test="deleted != null">
        deleted,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="status != null">
        #{status,jdbcType=TINYINT},
      </if>
      <if test="mobile != null">
        #{mobile,jdbcType=CHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=CHAR},
      </if>
      <if test="salt != null">
        #{salt,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="gender != null">
        #{gender,jdbcType=TINYINT},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="createdId != null">
        #{createdId,jdbcType=INTEGER},
      </if>
      <if test="modifiedId != null">
        #{modifiedId,jdbcType=INTEGER},
      </if>
      <if test="createdTime != null">
        #{createdTime,jdbcType=TIMESTAMP},
      </if>
      <if test="modifiedTime != null">
        #{modifiedTime,jdbcType=TIMESTAMP},
      </if>
      <if test="deleted != null">
        #{deleted,jdbcType=TINYINT},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.ydd.oms.entity.sys.Admin">
    update sys_admin
    <set>
      <if test="status != null">
        status = #{status,jdbcType=TINYINT},
      </if>
      <if test="mobile != null">
        mobile = #{mobile,jdbcType=CHAR},
      </if>
      <if test="password != null and password != ''">
        password = #{password,jdbcType=CHAR},
      </if>
      <if test="salt != null">
        salt = #{salt,jdbcType=VARCHAR},
      </if>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="gender != null">
        gender = #{gender,jdbcType=TINYINT},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="createdId != null">
        created_id = #{createdId,jdbcType=INTEGER},
      </if>
      <if test="modifiedId != null">
        modified_id = #{modifiedId,jdbcType=INTEGER},
      </if>
      <if test="createdTime != null">
        created_time = #{createdTime,jdbcType=TIMESTAMP},
      </if>
      <if test="modifiedTime != null">
        modified_time = #{modifiedTime,jdbcType=TIMESTAMP},
      </if>
      <if test="deleted != null">
        deleted = #{deleted,jdbcType=TINYINT},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.ydd.oms.entity.sys.Admin">
    update sys_admin
    set status = #{status,jdbcType=TINYINT},
      mobile = #{mobile,jdbcType=CHAR},
      password = #{password,jdbcType=CHAR},
      salt = #{salt,jdbcType=VARCHAR},
      username = #{username,jdbcType=VARCHAR},
      gender = #{gender,jdbcType=TINYINT},
      email = #{email,jdbcType=VARCHAR},
      created_id = #{createdId,jdbcType=INTEGER},
      modified_id = #{modifiedId,jdbcType=INTEGER},
      created_time = #{createdTime,jdbcType=TIMESTAMP},
      modified_time = #{modifiedTime,jdbcType=TIMESTAMP},
      deleted = #{deleted,jdbcType=TINYINT}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <!--查询角色编号集合-->
  <select id="findRoleIdList" parameterType="Integer" resultType="Integer">
    SELECT
      role_id
    FROM
      sys_admin_role
    WHERE
      admin_id = #{adminId, jdbcType=INTEGER}
  </select>
</mapper>