<?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="resources/permission.WbMenuMapper">
  <resultMap id="BaseResultMap" type="com.wb.permission.pojo.WbMenu">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="ID" jdbcType="VARCHAR" property="id" />
    <result column="MENU_ID" jdbcType="VARCHAR" property="menuId" />
    <result column="TENANCY_ID" jdbcType="VARCHAR" property="tenancyId" />
    <result column="MENU_NAME" jdbcType="VARCHAR" property="menuName" />
    <result column="MENU_URL" jdbcType="VARCHAR" property="menuUrl" />
    <result column="MENU_TYPE" jdbcType="CHAR" property="menuType" />
    <result column="PARENT_MENU" jdbcType="VARCHAR" property="parentMenu" />
    <result column="ORDER_INDEX" jdbcType="DECIMAL" property="orderIndex" />
    <result column="NOTES" jdbcType="VARCHAR" property="notes" />
    <result column="CREATED_BY" jdbcType="VARCHAR" property="createdBy" />
    <result column="CREATED_ON" jdbcType="TIMESTAMP" property="createdOn" />
    <result column="UPDATED_BY" jdbcType="VARCHAR" property="updatedBy" />
    <result column="UPDATED_ON" jdbcType="TIMESTAMP" property="updatedOn" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    ID, MENU_ID, TENANCY_ID, MENU_NAME, MENU_URL, MENU_TYPE, PARENT_MENU, ORDER_INDEX, 
    NOTES, CREATED_BY, CREATED_ON, UPDATED_BY, UPDATED_ON
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select 
    <include refid="Base_Column_List" />
    from WB_MENU
    where ID = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    delete from WB_MENU
    where ID = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.wb.permission.pojo.WbMenu">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into WB_MENU (ID, MENU_ID, TENANCY_ID, 
      MENU_NAME, MENU_URL, MENU_TYPE, 
      PARENT_MENU, ORDER_INDEX, NOTES, 
      CREATED_BY, CREATED_ON, UPDATED_BY, 
      UPDATED_ON)
    values (#{id,jdbcType=VARCHAR}, #{menuId,jdbcType=VARCHAR}, #{tenancyId,jdbcType=VARCHAR}, 
      #{menuName,jdbcType=VARCHAR}, #{menuUrl,jdbcType=VARCHAR}, #{menuType,jdbcType=CHAR}, 
      #{parentMenu,jdbcType=VARCHAR}, #{orderIndex,jdbcType=DECIMAL}, #{notes,jdbcType=VARCHAR}, 
      #{createdBy,jdbcType=VARCHAR}, #{createdOn,jdbcType=TIMESTAMP}, #{updatedBy,jdbcType=VARCHAR}, 
      #{updatedOn,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.wb.permission.pojo.WbMenu">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    insert into WB_MENU
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        ID,
      </if>
      <if test="menuId != null">
        MENU_ID,
      </if>
      <if test="tenancyId != null">
        TENANCY_ID,
      </if>
      <if test="menuName != null">
        MENU_NAME,
      </if>
      <if test="menuUrl != null">
        MENU_URL,
      </if>
      <if test="menuType != null">
        MENU_TYPE,
      </if>
      <if test="parentMenu != null">
        PARENT_MENU,
      </if>
      <if test="orderIndex != null">
        ORDER_INDEX,
      </if>
      <if test="notes != null">
        NOTES,
      </if>
      <if test="createdBy != null">
        CREATED_BY,
      </if>
      <if test="createdOn != null">
        CREATED_ON,
      </if>
      <if test="updatedBy != null">
        UPDATED_BY,
      </if>
      <if test="updatedOn != null">
        UPDATED_ON,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="menuId != null">
        #{menuId,jdbcType=VARCHAR},
      </if>
      <if test="tenancyId != null">
        #{tenancyId,jdbcType=VARCHAR},
      </if>
      <if test="menuName != null">
        #{menuName,jdbcType=VARCHAR},
      </if>
      <if test="menuUrl != null">
        #{menuUrl,jdbcType=VARCHAR},
      </if>
      <if test="menuType != null">
        #{menuType,jdbcType=CHAR},
      </if>
      <if test="parentMenu != null">
        #{parentMenu,jdbcType=VARCHAR},
      </if>
      <if test="orderIndex != null">
        #{orderIndex,jdbcType=DECIMAL},
      </if>
      <if test="notes != null">
        #{notes,jdbcType=VARCHAR},
      </if>
      <if test="createdBy != null">
        #{createdBy,jdbcType=VARCHAR},
      </if>
      <if test="createdOn != null">
        #{createdOn,jdbcType=TIMESTAMP},
      </if>
      <if test="updatedBy != null">
        #{updatedBy,jdbcType=VARCHAR},
      </if>
      <if test="updatedOn != null">
        #{updatedOn,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.wb.permission.pojo.WbMenu">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update WB_MENU
    <set>
      <if test="menuId != null">
        MENU_ID = #{menuId,jdbcType=VARCHAR},
      </if>
      <if test="tenancyId != null">
        TENANCY_ID = #{tenancyId,jdbcType=VARCHAR},
      </if>
      <if test="menuName != null">
        MENU_NAME = #{menuName,jdbcType=VARCHAR},
      </if>
      <if test="menuUrl != null">
        MENU_URL = #{menuUrl,jdbcType=VARCHAR},
      </if>
      <if test="menuType != null">
        MENU_TYPE = #{menuType,jdbcType=CHAR},
      </if>
      <if test="parentMenu != null">
        PARENT_MENU = #{parentMenu,jdbcType=VARCHAR},
      </if>
      <if test="orderIndex != null">
        ORDER_INDEX = #{orderIndex,jdbcType=DECIMAL},
      </if>
      <if test="notes != null">
        NOTES = #{notes,jdbcType=VARCHAR},
      </if>
      <if test="createdBy != null">
        CREATED_BY = #{createdBy,jdbcType=VARCHAR},
      </if>
      <if test="createdOn != null">
        CREATED_ON = #{createdOn,jdbcType=TIMESTAMP},
      </if>
      <if test="updatedBy != null">
        UPDATED_BY = #{updatedBy,jdbcType=VARCHAR},
      </if>
      <if test="updatedOn != null">
        UPDATED_ON = #{updatedOn,jdbcType=TIMESTAMP},
      </if>
    </set>
    where ID = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.wb.permission.pojo.WbMenu">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    update WB_MENU
    set MENU_ID = #{menuId,jdbcType=VARCHAR},
      TENANCY_ID = #{tenancyId,jdbcType=VARCHAR},
      MENU_NAME = #{menuName,jdbcType=VARCHAR},
      MENU_URL = #{menuUrl,jdbcType=VARCHAR},
      MENU_TYPE = #{menuType,jdbcType=CHAR},
      PARENT_MENU = #{parentMenu,jdbcType=VARCHAR},
      ORDER_INDEX = #{orderIndex,jdbcType=DECIMAL},
      NOTES = #{notes,jdbcType=VARCHAR},
      CREATED_BY = #{createdBy,jdbcType=VARCHAR},
      CREATED_ON = #{createdOn,jdbcType=TIMESTAMP},
      UPDATED_BY = #{updatedBy,jdbcType=VARCHAR},
      UPDATED_ON = #{updatedOn,jdbcType=TIMESTAMP}
    where ID = #{id,jdbcType=VARCHAR}
  </update>

  <!-- 查询授权数据 -->
  <select id="selectGrantMenu" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from WB_MENU
    START WITH MENU_ID =  #{menuId,jdbcType=VARCHAR}

    CONNECT BY PRIOR MENU_ID = PARENT_MENU

    ORDER SIBLINGS BY ORDER_INDEX
  </select>

  <!--插入数据-->
  <insert id="insertMenu" parameterType="com.wb.permission.pojo.WbMenu">
    <selectKey keyProperty="countMenu" order="BEFORE" resultType="int">
      select count(*) countMenu from wb_menu
      where  MENU_NAME = #{menuName,jdbcType=VARCHAR}
    </selectKey>
    <if test="countMenu == 0">
      insert into WB_MENU (ID,MENU_ID, TENANCY_ID, MENU_NAME,
      MENU_URL, MENU_TYPE, PARENT_MENU,
      ORDER_INDEX, NOTES, CREATED_BY,
      CREATED_ON, UPDATED_BY, UPDATED_ON)
      values (#{id,jdbcType=VARCHAR},#{menuId,jdbcType=VARCHAR}, #{tenancyId,jdbcType=VARCHAR}, #{menuName,jdbcType=VARCHAR},
      #{menuUrl,jdbcType=VARCHAR}, #{menuType,jdbcType=CHAR}, #{parentMenu,jdbcType=VARCHAR},
      #{orderIndex,jdbcType=DECIMAL}, #{notes,jdbcType=VARCHAR}, #{createdBy,jdbcType=VARCHAR},
      #{createdOn,jdbcType=TIMESTAMP}, #{updatedBy,jdbcType=VARCHAR}, #{updatedOn,jdbcType=TIMESTAMP})
    </if>
    <if test="countMenu > 0">
      select * from dual
    </if>
  </insert>



  <select id="selectWbMenu" parameterType="map" resultMap="BaseResultMap">
    select
    distinct <include refid="Base_Column_List" />
    from (
    select WM.ID, wm.MENU_ID, WM.TENANCY_ID, WM.MENU_NAME, WM.MENU_URL, WM.MENU_TYPE, WM.PARENT_MENU, WM.ORDER_INDEX,
    NULL NOTES,NULL CREATED_BY,NULL CREATED_ON,NULL UPDATED_BY,NULL UPDATED_ON
    FROM WB_MENU WM
    INNER JOIN WB_ROLE_RESOURCE WRR ON WM.MENU_ID = WRR.MENU_ID

    INNER JOIN WB_USER_ROLE WUR ON  WRR.ROLE_ID= WUR.ROLE_ID AND WUR.USER_ID=#{userId,jdbcType=VARCHAR}

    WHERE WM.MENU_TYPE= #{menuType,jdbcType=CHAR} AND WM.PARENT_MENU=#{parentMenu,jdbcType=VARCHAR}

    ) a order by ORDER_INDEX
  </select>

<!--  <select id="selectWbMenu" parameterType="map" resultMap="BaseResultMap">-->
<!--    select-->
<!--    <include refid="Base_Column_List" />-->
<!--    from (-->
<!--    select WM.ID, wm.MENU_ID, WM.TENANCY_ID, WM.MENU_NAME, WM.MENU_URL, WM.MENU_TYPE, WM.PARENT_MENU, WM.ORDER_INDEX,-->
<!--    NULL NOTES,NULL CREATED_BY,NULL CREATED_ON,NULL UPDATED_BY,NULL UPDATED_ON-->
<!--    FROM WB_MENU WM-->
<!--    INNER JOIN WB_ROLE_RESOURCE WRR ON WM.MENU_ID = WRR.MENU_ID-->
<!--     AND WRR.TENANCY_ID=#{tenancyId,jdbcType=VARCHAR}-->
<!--    INNER JOIN WB_USER_ROLE WUR ON  WRR.ROLE_ID= WUR.ROLE_ID AND WUR.USER_ID=#{userId,jdbcType=VARCHAR}-->
<!--     AND  WUR.TENANCY_ID=#{tenancyId,jdbcType=VARCHAR}-->
<!--    WHERE WM.MENU_TYPE= #{menuType,jdbcType=CHAR} AND WM.PARENT_MENU=#{parentMenu,jdbcType=VARCHAR}-->
<!--     AND WM.TENANCY_ID=#{tenancyId,jdbcType=VARCHAR}-->
<!--    )-->
<!--  </select>-->
</mapper>