<?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_Role_MenuMapper">
  <resultMap id="BaseResultMap" type="com.manage.entity.Power_Role_Menu">
    <id column="role_menu_id" jdbcType="INTEGER" property="roleMenuId" />
    <result column="role_id" jdbcType="INTEGER" property="roleId" />
    <result column="menu_id" jdbcType="INTEGER" property="menuId" />
    <result column="create_date" jdbcType="CHAR" property="createDate" />
    <result column="creater" jdbcType="VARCHAR" property="creater" />
    <result column="update_date" jdbcType="CHAR" property="updateDate" />
    <result column="updater" jdbcType="VARCHAR" property="updater" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
  </resultMap>
  <sql id="Base_Column_List">
    role_menu_id, role_id, menu_id, create_date, creater, update_date, updater, remark
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from power_role_menu
    where role_menu_id = #{roleMenuId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from power_role_menu
    where role_menu_id = #{roleMenuId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.manage.entity.Power_Role_Menu">
    insert into power_role_menu (role_menu_id, role_id, menu_id, 
      create_date, creater, update_date, 
      updater, remark)
    values (#{roleMenuId,jdbcType=INTEGER}, #{roleId,jdbcType=INTEGER}, #{menuId,jdbcType=INTEGER}, 
      #{createDate,jdbcType=CHAR}, #{creater,jdbcType=VARCHAR}, #{updateDate,jdbcType=CHAR}, 
      #{updater,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.manage.entity.Power_Role_Menu">
    insert into power_role_menu
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="roleMenuId != null">
        role_menu_id,
      </if>
      <if test="roleId != null">
        role_id,
      </if>
      <if test="menuId != null">
        menu_id,
      </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>
      <if test="remark != null">
        remark,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="roleMenuId != null">
        #{roleMenuId,jdbcType=INTEGER},
      </if>
      <if test="roleId != null">
        #{roleId,jdbcType=INTEGER},
      </if>
      <if test="menuId != null">
        #{menuId,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>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.manage.entity.Power_Role_Menu">
    update power_role_menu
    <set>
      <if test="roleId != null">
        role_id = #{roleId,jdbcType=INTEGER},
      </if>
      <if test="menuId != null">
        menu_id = #{menuId,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>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
    </set>
    where role_menu_id = #{roleMenuId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.manage.entity.Power_Role_Menu">
    update power_role_menu
    set role_id = #{roleId,jdbcType=INTEGER},
      menu_id = #{menuId,jdbcType=INTEGER},
      create_date = #{createDate,jdbcType=CHAR},
      creater = #{creater,jdbcType=VARCHAR},
      update_date = #{updateDate,jdbcType=CHAR},
      updater = #{updater,jdbcType=VARCHAR},
      remark = #{remark,jdbcType=VARCHAR}
    where role_menu_id = #{roleMenuId,jdbcType=INTEGER}
  </update>
  <!--根据系统id和角色id查出对应该系统在用户菜单表的记录-->
  <select id="selectRoleMenuByRoleId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from
    power_role_menu
    where role_id = ${roleId}
  </select>
  <!--根据系统id和角色id查出对应该系统在用户菜单表的记录-->
  <select id="selectRoleMenuPower" parameterType="string" resultMap="BaseResultMap">
    SELECT
	    b.role_menu_id,b.menu_Id
    FROM
        power_menu a,
        power_role_menu b
    WHERE
        a.sys_flag = #{sysFlag}
    AND b.role_id = ${roleId}
    AND a.menu_id = b.menu_id
  </select>

  <!--根据系统标识和角色id删除记录-->
  <delete id="deleteRoleMenuByRoleAndSysFlag">
    DELETE
    FROM
        power_role_menu
    WHERE
        role_id = #{roleId}
    AND menu_id IN (
        SELECT
            c.menu_id
        FROM
            (
                SELECT
                    b.menu_Id
                FROM
                    power_menu a,
                    power_role_menu b
                WHERE
                    a.sys_flag = #{sysFlag}
                AND b.role_id = #{roleId}
                AND a.menu_id = b.menu_id
            ) c
    )
  </delete>

  <!--2019-06-28 ljx-->
  <!--根据用户ID查询角色菜单表-->
  <select id="getRoleMenuByUserId"  resultMap="BaseResultMap" parameterType="java.lang.Integer">
    SELECT
	    *
    FROM
	    power_role_menu
    WHERE
	    role_id IN (
		    SELECT
			    role_id
		    FROM
			    power_user
		    WHERE
			    user_id = ${userId}
	);
  </select>
  <!--批量插入-->
    <insert id="simpleInsertRoleMenu" parameterType="java.util.List">
      INSERT INTO power_role_menu(
      role_menu_id,
      role_id,
      menu_id,
      create_date,
      creater,
      update_date,
      updater,
      remark)
      VALUES
      <foreach collection ="list" item="item" index= "index" separator =",">
        (
        #{item.roleMenuId,jdbcType=INTEGER},
        #{item.roleId,jdbcType=INTEGER},
        #{item.menuId,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>