<?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.manage.dao.Power_RoleMapper">
    <resultMap id="BaseResultMap" type="com.manage.vo.Power_RoleVo" >
        <id column="role_id" property="roleId" jdbcType="INTEGER" />
        <result column="role_name" property="roleName" jdbcType="VARCHAR" />
        <result column="show_record" property="showRecord" jdbcType="SMALLINT" />
        <result column="downLoad_record" property="downloadRecord" jdbcType="SMALLINT" />
        <result column="show_print" property="showPrint" jdbcType="SMALLINT" />
        <result column="remark" property="remark" jdbcType="VARCHAR" />
        <result column="effective" property="effective" jdbcType="INTEGER" />
        <result column="create_date" property="createDate" jdbcType="CHAR" />
        <result column="creater" property="creater" jdbcType="VARCHAR" />
        <result column="update_date" property="updateDate" jdbcType="CHAR" />
        <result column="updater" property="updater" jdbcType="VARCHAR" />
    </resultMap>
    <sql id="Base_Column_List" >
    role_id, role_name, show_record, downLoad_record, show_print, remark, effective,
    create_date, creater, update_date, updater
  </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select
        <include refid="Base_Column_List" />
        from power_role
        where role_id = #{roleId,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from power_role
    where role_id = #{roleId,jdbcType=INTEGER}
  </delete>
    <insert id="insert" parameterType="com.manage.entity.Power_Role" >
    insert into power_role (role_id, role_name, show_record,
      downLoad_record, show_print, remark,
      effective, create_date, creater,
      update_date, updater)
    values (#{roleId,jdbcType=INTEGER}, #{roleName,jdbcType=VARCHAR}, #{showRecord,jdbcType=SMALLINT},
      #{downloadRecord,jdbcType=SMALLINT}, #{showPrint,jdbcType=SMALLINT}, #{remark,jdbcType=VARCHAR},
      #{effective,jdbcType=INTEGER}, #{createDate,jdbcType=CHAR}, #{creater,jdbcType=VARCHAR},
      #{updateDate,jdbcType=CHAR}, #{updater,jdbcType=VARCHAR})
  </insert>
    <insert id="insertSelective" parameterType="com.manage.entity.Power_Role" >
        insert into power_role
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="roleId != null" >
                role_id,
            </if>
            <if test="roleName != null" >
                role_name,
            </if>
            <if test="showRecord != null" >
                show_record,
            </if>
            <if test="downloadRecord != null" >
                downLoad_record,
            </if>
            <if test="showPrint != null" >
                show_print,
            </if>
            <if test="remark != null" >
                remark,
            </if>
            <if test="effective != null" >
                effective,
            </if>
            <if test="createDate != null" >
                create_date,
            </if>
            <if test="creater != null" >
                creater,
            </if>
            <if test="updateDate != null" >
                update_date,
            </if>
            <if test="updater != null" >
                updater,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="roleId != null" >
                #{roleId,jdbcType=INTEGER},
            </if>
            <if test="roleName != null" >
                #{roleName,jdbcType=VARCHAR},
            </if>
            <if test="showRecord != null" >
                #{showRecord,jdbcType=SMALLINT},
            </if>
            <if test="downloadRecord != null" >
                #{downloadRecord,jdbcType=SMALLINT},
            </if>
            <if test="showPrint != null" >
                #{showPrint,jdbcType=SMALLINT},
            </if>
            <if test="remark != null" >
                #{remark,jdbcType=VARCHAR},
            </if>
            <if test="effective != null" >
                #{effective,jdbcType=INTEGER},
            </if>
            <if test="createDate != null" >
                #{createDate,jdbcType=CHAR},
            </if>
            <if test="creater != null" >
                #{creater,jdbcType=VARCHAR},
            </if>
            <if test="updateDate != null" >
                #{updateDate,jdbcType=CHAR},
            </if>
            <if test="updater != null" >
                #{updater,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.manage.entity.Power_Role" >
        update power_role
        <set >
            <if test="roleName != null" >
                role_name = #{roleName,jdbcType=VARCHAR},
            </if>
            <if test="showRecord != null" >
                show_record = #{showRecord,jdbcType=SMALLINT},
            </if>
            <if test="downloadRecord != null" >
                downLoad_record = #{downloadRecord,jdbcType=SMALLINT},
            </if>
            <if test="showPrint != null" >
                show_print = #{showPrint,jdbcType=SMALLINT},
            </if>
            <if test="remark != null" >
                remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="effective != null" >
                effective = #{effective,jdbcType=INTEGER},
            </if>
            <if test="createDate != null" >
                create_date = #{createDate,jdbcType=CHAR},
            </if>
            <if test="creater != null" >
                creater = #{creater,jdbcType=VARCHAR},
            </if>
            <if test="updateDate != null" >
                update_date = #{updateDate,jdbcType=CHAR},
            </if>
            <if test="updater != null" >
                updater = #{updater,jdbcType=VARCHAR},
            </if>
        </set>
        where role_id = #{roleId,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.manage.entity.Power_Role" >
    update power_role
    set role_name = #{roleName,jdbcType=VARCHAR},
      show_record = #{showRecord,jdbcType=SMALLINT},
      downLoad_record = #{downloadRecord,jdbcType=SMALLINT},
      show_print = #{showPrint,jdbcType=SMALLINT},
      remark = #{remark,jdbcType=VARCHAR},
      effective = #{effective,jdbcType=INTEGER},
      create_date = #{createDate,jdbcType=CHAR},
      creater = #{creater,jdbcType=VARCHAR},
      update_date = #{updateDate,jdbcType=CHAR},
      updater = #{updater,jdbcType=VARCHAR}
    where role_id = #{roleId,jdbcType=INTEGER}
  </update>
  <select id="findSomeByMore" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List" />
    FROM power_role
    WHERE 1 = 1
    <if test="roleId != null and roleId != 0">
      AND role_id != -100
      AND creater IN (
      SELECT
      user_name
      FROM
      power_user
      WHERE
      user_id IN (
      SELECT
      user_id
      FROM
      power_user_dict
      WHERE
      dict_id = (
      SELECT
      dict_id
      FROM
      power_user_dict
      WHERE
      user_id = ${userId}
      )
      )
      OR role_id = 0
      )
    </if>
    AND role_id != 0
    <if test="record.roleName!=null and record.roleName!=''">
      and role_name LIKE '%${record.roleName}%'
    </if>
    <if test="record.effective!=null">
      and effective=${record.effective}
    </if>
    <if test="record.creater!=null and record.creater!=''">
      and creater LIKE '%${record.creater}%'
    </if>
    <if test="record.checks != null and record.checks != ''">
      AND role_id IN (${record.checks})
    </if>
    ORDER BY create_date DESC,role_id DESC
    <if test="record.offset != null and record.limit != null">
      LIMIT #{record.offset},#{record.limit}
    </if>
  </select>

  <select id="getTotal" resultType="int">
    SELECT
    count(1)
    FROM power_role
    WHERE 1 = 1
    <if test="roleId != null and roleId != 0">
      AND role_id != -100
      AND creater IN (
      SELECT
      user_name
      FROM
      power_user
      WHERE
      user_id IN (
      SELECT
      user_id
      FROM
      power_user_dict
      WHERE
      dict_id = (
      SELECT
      dict_id
      FROM
      power_user_dict
      WHERE
      user_id = ${userId}
      )
      )
      OR role_id = 0
      )
    </if>
    AND role_id != 0
    <if test="record.roleName!=null and record.roleName!=''">
      and role_name LIKE '%${record.roleName}%'
    </if>
    <if test="record.effective!=null">
      and effective=${record.effective}
    </if>
    <if test="record.creater!=null and record.creater!=''">
      and creater LIKE '%${record.creater}%'
    </if>
  </select>

  <select id="selectList" resultMap="BaseResultMap">
      SELECT
      <include refid="Base_Column_List" />
      FROM power_role
  </select>

  <!--验证角色名是否重复-->
  <select id="checkRoleName" parameterType="java.lang.String" resultType="com.manage.entity.Power_Role">
     SELECT
          role_id
     FROM
          power_role
     WHERE
          role_name = #{roleName}
  </select>
  <!--按权限查询角色结合-->
  <select id="selectListByPower" parameterType="java.lang.Integer" resultMap="BaseResultMap">
      SELECT
      <include refid="Base_Column_List" />
      FROM power_role
      WHERE
          effective = 1
          <if test="roleId != null and roleId != 0">
             AND role_id != -100
            AND creater IN (
            SELECT
                user_name
            FROM
                power_user
            WHERE
                user_id IN (
                  SELECT
                      user_id
                  FROM
                      power_user_dict
                  WHERE
                      dict_id = (
                        SELECT
                            dict_id
                        FROM
                            power_user_dict
                        WHERE
                            user_id = ${userId}
                        )
                  )
                  OR role_id = 0
            )
          </if>
        AND role_id != 0
  </select>

  <!--批量插入-->
  <insert id="SimpleInsert" parameterType="java.util.List">
    INSERT INTO power_role(
    role_id,
    role_name,
    show_record,
    downLoad_record,
    effective,
    create_date,
    creater,
    update_date,
    updater,
    remark
    )
    VALUES
    <foreach collection ="list" item="item" index= "index" separator =",">
      (
      #{item.roleId,jdbcType=INTEGER},
      #{item.roleName,jdbcType=VARCHAR},
      #{item.showRecord,jdbcType=SMALLINT},
      #{item.downloadRecord,jdbcType=SMALLINT},
      #{item.effective,jdbcType=INTEGER},
      #{item.createDate,jdbcType=CHAR},
      #{item.creater,jdbcType=VARCHAR},
      #{item.updateDate,jdbcType=CHAR},
      #{item.updater,jdbcType=VARCHAR},
      #{item.remark,jdbcType=VARCHAR}
      )
    </foreach >
  </insert>
</mapper>