<?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_User_MenuMapper">
  <resultMap id="BaseResultMap" type="com.manage.entity.Power_User_Menu">
    <id column="user_menu_id" jdbcType="INTEGER" property="userMenuId" />
    <result column="user_id" jdbcType="INTEGER" property="userId" />
    <result column="menu_id" jdbcType="INTEGER" property="menuId" />
    <result column="flag" jdbcType="INTEGER" property="flag" />
    <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">
    user_menu_id, user_id, menu_id, flag, 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_user_menu
    where user_menu_id = #{userMenuId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from power_user_menu
    where user_menu_id = #{userMenuId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.manage.entity.Power_User_Menu">
    insert into power_user_menu (user_menu_id, user_id, menu_id, 
      flag, create_date, creater, 
      update_date, updater, remark
      )
    values (#{userMenuId,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{menuId,jdbcType=INTEGER}, 
      #{flag,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_User_Menu">
    insert into power_user_menu
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="userMenuId != null">
        user_menu_id,
      </if>
      <if test="userId != null">
        user_id,
      </if>
      <if test="menuId != null">
        menu_id,
      </if>
      <if test="flag != null">
        flag,
      </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="userMenuId != null">
        #{userMenuId,jdbcType=INTEGER},
      </if>
      <if test="userId != null">
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="menuId != null">
        #{menuId,jdbcType=INTEGER},
      </if>
      <if test="flag != null">
        #{flag,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_User_Menu">
    update power_user_menu
    <set>
      <if test="userId != null">
        user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="menuId != null">
        menu_id = #{menuId,jdbcType=INTEGER},
      </if>
      <if test="flag != null">
        flag = #{flag,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 user_menu_id = #{userMenuId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.manage.entity.Power_User_Menu">
    update power_user_menu
    set user_id = #{userId,jdbcType=INTEGER},
      menu_id = #{menuId,jdbcType=INTEGER},
      flag = #{flag,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 user_menu_id = #{userMenuId,jdbcType=INTEGER}
  </update>
  <!--根据系统id和用户id查出对应该系统在用户菜单表的记录-->
  <select id="selectUserMenuPower" parameterType="string" resultMap="BaseResultMap">
    SELECT
	    b.user_menu_id,b.menu_Id,b.flag
    FROM
        power_menu a,
        power_user_menu b
    WHERE
        a.sys_flag = #{sysFlag}
    AND b.user_id = ${userId}
    AND a.menu_id = b.menu_id
  </select>
  <!--根据系统标识和用户id删除记录-->
  <delete id="deleteUserMenuByUserIdAndSysFlag">
    DELETE FROM power_user_menu WHERE user_id = #{userId} AND menu_id in (
        SELECT c.menu_id FROM (
            SELECT
                b.user_menu_id,b.menu_Id,b.flag
            FROM
                power_menu a,
                power_user_menu b
            WHERE
                a.sys_flag = #{sysFlag}
            AND b.user_id = #{userId}
            AND a.menu_id = b.menu_id
        ) c
    )
  </delete>

  <!--根据系统id和用户id查出对应该系统在用户菜单表和角色菜单表的综合表-->
  <select id="selectUserAndRoleMenuPower" parameterType="string" resultMap="BaseResultMap">
    SELECT
		m.menu_id
    FROM
        power_menu m
    WHERE
        m.sys_flag = #{sysFlag}
    AND m.menu_id IN (
        SELECT
            r.menu_id
        FROM
            power_role_menu r
        WHERE
            r.role_id = (
                SELECT
                    u.role_id
                FROM
                    power_user u
                WHERE
                    u.user_id = ${userId}
            )
        AND r.menu_id NOT IN (
            SELECT
                m.menu_id
            FROM
                power_menu m
            WHERE
                m.sys_flag = #{sysFlag}
            AND m.menu_id IN (
                SELECT
                    pu.menu_id
                FROM
                    power_user_menu pu
                WHERE
                    pu.user_id = ${userId}
                AND pu.flag = 0
            )
        )
    )
    UNION ALL
        SELECT
            m.menu_id
        FROM
            power_menu m
        WHERE
            m.sys_flag = #{sysFlag}
        AND m.menu_id IN (
            SELECT
                pu.menu_id menuId1
            FROM
                power_user_menu pu
            WHERE
                pu.user_id = ${userId}
            AND pu.flag = 1
        )
  </select>

    <!--批量插入-->
    <insert id="simpleInsertUserMenu" parameterType="java.util.List">
        INSERT INTO power_user_menu(
        user_menu_id,
        user_id,
        menu_id,
        flag,
        create_date,
        creater,
        update_date,
        updater,
        remark)
        VALUES
        <foreach collection ="list" item="item" index= "index" separator =",">
            (
            #{item.userMenuId,jdbcType=INTEGER},
            #{item.userId,jdbcType=INTEGER},
            #{item.menuId,jdbcType=INTEGER},
            #{item.flag,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>