<?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.ssm.mapper.system.SysMenuMapper" >
  
  <resultMap id="ResultMapWithBLOBs" type="com.ssm.model.system.SysMenu"  >
    <result column="MenuID" property="menuid" jdbcType="INTEGER" />
    <result column="MenuCnName" property="menucnname" jdbcType="VARCHAR" />
    <result column="MenuEnName" property="menuenname" jdbcType="VARCHAR" />
    <result column="MenuUrl" property="menuurl" jdbcType="VARCHAR" />
    <result column="ActionName" property="actionname" jdbcType="VARCHAR" />
    <result column="ParentID" property="parentid" jdbcType="INTEGER" />
    <result column="IsEnable" property="isenable" jdbcType="BIT" />
    <result column="CreatedUserID" property="createduserid" jdbcType="INTEGER" />
    <result column="CreatedDatetime" property="createddatetime" jdbcType="TIMESTAMP" />
    <result column="LastUpdateUserID" property="lastupdateuserid" jdbcType="INTEGER" />
    <result column="LastUpdateDatetime" property="lastupdatedatetime" jdbcType="TIMESTAMP" />
    <result column="IsDelete" property="isdelete" jdbcType="BIT" />
    <result column="Level" property="level" jdbcType="INTEGER" />
    <result column="MenuType" property="menutype" jdbcType="VARCHAR" />
    <result column="pmenucnname" property="pmenucnname" jdbcType="VARCHAR" />
    <result column="createdusername" property="createdusername" jdbcType="VARCHAR" />
    <result column="OrderBy" property="orderby" jdbcType="CLOB" />
    <result column="Icon" property="icon" jdbcType="CLOB" />
    <result column="MenuState" property="menustate" jdbcType="CLOB" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause" >
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    MenuID, MenuCnName, MenuEnName, MenuUrl, ActionName, ParentID, IsEnable, CreatedUserID, 
    CreatedDatetime, LastUpdateUserID, LastUpdateDatetime, IsDelete, Level, MenuType
  </sql>
  <sql id="Blob_Column_List" >
    OrderBy, Icon, MenuState
  </sql>
  <select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="com.ssm.model.system.SysMenuExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from SysMenu
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
  
  <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from SysMenu
    where MenuID = #{menuid, jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from SysMenu
    where ID = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.ssm.model.system.SysMenuExample" >
    delete from SysMenu
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.ssm.model.system.SysMenu" >
    insert into SysMenu (ID, MenuID, MenuCnName, 
      MenuEnName, MenuUrl, ActionName, 
      ParentID, IsEnable, CreatedUserID, 
      CreatedDatetime, LastUpdateUserID, LastUpdateDatetime, 
      IsDelete, Level, MenuType, 
      OrderBy, Icon, MenuState)
    values (#{id,jdbcType=INTEGER}, #{menuid,jdbcType=INTEGER}, #{menucnname,jdbcType=VARCHAR}, 
      #{menuenname,jdbcType=VARCHAR}, #{menuurl,jdbcType=VARCHAR}, #{actionname,jdbcType=VARCHAR}, 
      #{parentid,jdbcType=INTEGER}, #{isenable,jdbcType=BIT}, #{createduserid,jdbcType=INTEGER}, 
      #{createddatetime,jdbcType=TIMESTAMP}, #{lastupdateuserid,jdbcType=INTEGER}, #{lastupdatedatetime,jdbcType=TIMESTAMP}, 
      #{isdelete,jdbcType=BIT}, #{level,jdbcType=INTEGER}, #{menutype,jdbcType=VARCHAR}, 
      #{orderby,jdbcType=CLOB}, #{icon,jdbcType=CLOB}, #{menustate,jdbcType=CLOB})
  </insert>
  <insert id="insertSelective" parameterType="com.ssm.model.system.SysMenu" >
    insert into SysMenu
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="menuid != null" >
        MenuID,
      </if>
      <if test="menucnname != null" >
        MenuCnName,
      </if>
      <if test="menuenname != null" >
        MenuEnName,
      </if>
      <if test="menuurl != null" >
        MenuUrl,
      </if>
      <if test="actionname != null" >
        ActionName,
      </if>
      <if test="parentid != null" >
        ParentID,
      </if>
      <if test="isenable != null" >
        IsEnable,
      </if>
      <if test="createduserid != null" >
        CreatedUserID,
      </if>
      <if test="createddatetime != null" >
        CreatedDatetime,
      </if>
      <if test="lastupdateuserid != null" >
        LastUpdateUserID,
      </if>
      <if test="lastupdatedatetime != null" >
        LastUpdateDatetime,
      </if>
      <if test="isdelete != null" >
        IsDelete,
      </if>
      <if test="level != null" >
        Level,
      </if>
      <if test="menutype != null" >
        MenuType,
      </if>
      <if test="orderby != null" >
        OrderBy,
      </if>
      <if test="icon != null" >
        Icon,
      </if>
      <if test="menustate != null" >
        MenuState,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="menuid != null" >
        #{menuid,jdbcType=INTEGER},
      </if>
      <if test="menucnname != null" >
        #{menucnname,jdbcType=VARCHAR},
      </if>
      <if test="menuenname != null" >
        #{menuenname,jdbcType=VARCHAR},
      </if>
      <if test="menuurl != null" >
        #{menuurl,jdbcType=VARCHAR},
      </if>
      <if test="actionname != null" >
        #{actionname,jdbcType=VARCHAR},
      </if>
      <if test="parentid != null" >
        #{parentid,jdbcType=INTEGER},
      </if>
      <if test="isenable != null" >
        #{isenable,jdbcType=BIT},
      </if>
      <if test="createduserid != null" >
        #{createduserid,jdbcType=INTEGER},
      </if>
      <if test="createddatetime != null" >
        #{createddatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="lastupdateuserid != null" >
        #{lastupdateuserid,jdbcType=INTEGER},
      </if>
      <if test="lastupdatedatetime != null" >
        #{lastupdatedatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="isdelete != null" >
        #{isdelete,jdbcType=BIT},
      </if>
      <if test="level != null" >
        #{level,jdbcType=INTEGER},
      </if>
      <if test="menutype != null" >
        #{menutype,jdbcType=VARCHAR},
      </if>
      <if test="orderby != null" >
        #{orderby,jdbcType=CLOB},
      </if>
      <if test="icon != null" >
        #{icon,jdbcType=CLOB},
      </if>
      <if test="menustate != null" >
        #{menustate,jdbcType=CLOB},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.ssm.model.system.SysMenuExample" resultType="java.lang.Integer" >
    select count(*) from SysMenu
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    update SysMenu
    <set >
      <if test="record.id != null" >
        ID = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.menuid != null" >
        MenuID = #{record.menuid,jdbcType=INTEGER},
      </if>
      <if test="record.menucnname != null" >
        MenuCnName = #{record.menucnname,jdbcType=VARCHAR},
      </if>
      <if test="record.menuenname != null" >
        MenuEnName = #{record.menuenname,jdbcType=VARCHAR},
      </if>
      <if test="record.menuurl != null" >
        MenuUrl = #{record.menuurl,jdbcType=VARCHAR},
      </if>
      <if test="record.actionname != null" >
        ActionName = #{record.actionname,jdbcType=VARCHAR},
      </if>
      <if test="record.parentid != null" >
        ParentID = #{record.parentid,jdbcType=INTEGER},
      </if>
      <if test="record.isenable != null" >
        IsEnable = #{record.isenable,jdbcType=BIT},
      </if>
      <if test="record.createduserid != null" >
        CreatedUserID = #{record.createduserid,jdbcType=INTEGER},
      </if>
      <if test="record.createddatetime != null" >
        CreatedDatetime = #{record.createddatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.lastupdateuserid != null" >
        LastUpdateUserID = #{record.lastupdateuserid,jdbcType=INTEGER},
      </if>
      <if test="record.lastupdatedatetime != null" >
        LastUpdateDatetime = #{record.lastupdatedatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.isdelete != null" >
        IsDelete = #{record.isdelete,jdbcType=BIT},
      </if>
      <if test="record.level != null" >
        Level = #{record.level,jdbcType=INTEGER},
      </if>
      <if test="record.menutype != null" >
        MenuType = #{record.menutype,jdbcType=VARCHAR},
      </if>
      <if test="record.orderby != null" >
        OrderBy = #{record.orderby,jdbcType=CLOB},
      </if>
      <if test="record.icon != null" >
        Icon = #{record.icon,jdbcType=CLOB},
      </if>
      <if test="record.menustate != null" >
        MenuState = #{record.menustate,jdbcType=CLOB},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExampleWithBLOBs" parameterType="map" >
    update SysMenu
    set ID = #{record.id,jdbcType=INTEGER},
      MenuID = #{record.menuid,jdbcType=INTEGER},
      MenuCnName = #{record.menucnname,jdbcType=VARCHAR},
      MenuEnName = #{record.menuenname,jdbcType=VARCHAR},
      MenuUrl = #{record.menuurl,jdbcType=VARCHAR},
      ActionName = #{record.actionname,jdbcType=VARCHAR},
      ParentID = #{record.parentid,jdbcType=INTEGER},
      IsEnable = #{record.isenable,jdbcType=BIT},
      CreatedUserID = #{record.createduserid,jdbcType=INTEGER},
      CreatedDatetime = #{record.createddatetime,jdbcType=TIMESTAMP},
      LastUpdateUserID = #{record.lastupdateuserid,jdbcType=INTEGER},
      LastUpdateDatetime = #{record.lastupdatedatetime,jdbcType=TIMESTAMP},
      IsDelete = #{record.isdelete,jdbcType=BIT},
      Level = #{record.level,jdbcType=INTEGER},
      MenuType = #{record.menutype,jdbcType=VARCHAR},
      OrderBy = #{record.orderby,jdbcType=CLOB},
      Icon = #{record.icon,jdbcType=CLOB},
      MenuState = #{record.menustate,jdbcType=CLOB}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    update SysMenu
    set ID = #{record.id,jdbcType=INTEGER},
      MenuID = #{record.menuid,jdbcType=INTEGER},
      MenuCnName = #{record.menucnname,jdbcType=VARCHAR},
      MenuEnName = #{record.menuenname,jdbcType=VARCHAR},
      MenuUrl = #{record.menuurl,jdbcType=VARCHAR},
      ActionName = #{record.actionname,jdbcType=VARCHAR},
      ParentID = #{record.parentid,jdbcType=INTEGER},
      IsEnable = #{record.isenable,jdbcType=BIT},
      CreatedUserID = #{record.createduserid,jdbcType=INTEGER},
      CreatedDatetime = #{record.createddatetime,jdbcType=TIMESTAMP},
      LastUpdateUserID = #{record.lastupdateuserid,jdbcType=INTEGER},
      LastUpdateDatetime = #{record.lastupdatedatetime,jdbcType=TIMESTAMP},
      IsDelete = #{record.isdelete,jdbcType=BIT},
      Level = #{record.level,jdbcType=INTEGER},
      MenuType = #{record.menutype,jdbcType=VARCHAR}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.ssm.model.system.SysMenu" >
    update SysMenu
    <set >
      <if test="menucnname != null" >
        MenuCnName = #{menucnname,jdbcType=VARCHAR},
      </if>
      <if test="menuenname != null" >
        MenuEnName = #{menuenname,jdbcType=VARCHAR},
      </if>
      <if test="menuurl != null" >
        MenuUrl = #{menuurl,jdbcType=VARCHAR},
      </if>
      <if test="actionname != null" >
        ActionName = #{actionname,jdbcType=VARCHAR},
      </if>
      <if test="parentid != null" >
        ParentID = #{parentid,jdbcType=INTEGER},
      </if>
      <if test="isenable != null" >
        IsEnable = #{isenable,jdbcType=BIT},
      </if>
      <if test="createduserid != null" >
        CreatedUserID = #{createduserid,jdbcType=INTEGER},
      </if>
      <if test="createddatetime != null" >
        CreatedDatetime = #{createddatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="lastupdateuserid != null" >
        LastUpdateUserID = #{lastupdateuserid,jdbcType=INTEGER},
      </if>
      <if test="lastupdatedatetime != null" >
        LastUpdateDatetime = #{lastupdatedatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="isdelete != null" >
        IsDelete = #{isdelete,jdbcType=BIT},
      </if>
      <if test="level != null" >
        Level = #{level,jdbcType=INTEGER},
      </if>
      <if test="menutype != null" >
        MenuType = #{menutype,jdbcType=VARCHAR},
      </if>
      <if test="orderby != null" >
        OrderBy = #{orderby,jdbcType=CLOB},
      </if>
      <if test="icon != null" >
        Icon = #{icon,jdbcType=CLOB},
      </if>
      <if test="menustate != null" >
        MenuState = #{menustate,jdbcType=CLOB},
      </if>
    </set>
    where MenuId = #{menuid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.ssm.model.system.SysMenu" >
    update SysMenu
    set MenuID = #{menuid,jdbcType=INTEGER},
      MenuCnName = #{menucnname,jdbcType=VARCHAR},
      MenuEnName = #{menuenname,jdbcType=VARCHAR},
      MenuUrl = #{menuurl,jdbcType=VARCHAR},
      ActionName = #{actionname,jdbcType=VARCHAR},
      ParentID = #{parentid,jdbcType=INTEGER},
      IsEnable = #{isenable,jdbcType=BIT},
      CreatedUserID = #{createduserid,jdbcType=INTEGER},
      CreatedDatetime = #{createddatetime,jdbcType=TIMESTAMP},
      LastUpdateUserID = #{lastupdateuserid,jdbcType=INTEGER},
      LastUpdateDatetime = #{lastupdatedatetime,jdbcType=TIMESTAMP},
      IsDelete = #{isdelete,jdbcType=BIT},
      Level = #{level,jdbcType=INTEGER},
      MenuType = #{menutype,jdbcType=VARCHAR},
      OrderBy = #{orderby,jdbcType=CLOB},
      Icon = #{icon,jdbcType=CLOB},
      MenuState = #{menustate,jdbcType=CLOB}
    where ID = #{id,jdbcType=INTEGER}
  </update>
  
  <select id = "selectSysMenuWithBLOBsByPid" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
  	 select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from SysMenu
    where isDelete = 0 and sm.isenable = 0 and ParentID = #{parentid,jdbcType=INTEGER}
  </select>
  
  <select id = "selectBySysMenuWithBLOBs" resultMap="ResultMapWithBLOBs"  parameterType="com.ssm.model.system.SysMenu">
  	 select
    sm.*,
    psm.MenuCnName as pmenucnname,
    sus.UserName as createdusername
    from SysMenu sm
    
    LEFT JOIN SysMenu psm on sm.ParentID = psm.menuid
    
    LEFT JOIN SysUser sus on sm.CreatedUserID = sus.UserID  
    
    where sm.isDelete = 0
    <if test="menucnname != null" >
       and   sm.MenuCnName like '%${menucnname}%'
    </if>   
    Order By  
    cast(sm.OrderBy as UNSIGNED INTEGER)
    
  </select>
  
    <select id = "selectBySysMenuWithBLOBs2" resultMap="ResultMapWithBLOBs"  parameterType="com.ssm.model.system.SysMenu">
  	 select
    sm.*,
    psm.MenuCnName as pmenucnname,
    sus.UserName as createdusername
    from SysMenu sm
    
    LEFT JOIN SysMenu psm on sm.ParentID = psm.menuid
    
    LEFT JOIN SysUser sus on sm.CreatedUserID = sus.UserID  
    
    where sm.isDelete = 0 AND sm.IsEnable = 0
    <if test="menucnname != null" >
       and   sm.MenuCnName like '%${menucnname}%'
      </if>
     
    Order By sm.MenuID 
    
  </select>
  
  
  
  <select id = "selectCountByParentid" parameterType="java.lang.Integer" resultType="java.lang.Integer">
  	  select count(1) from SysMenu
   
     <where >
     		 ParentID = #{parentid,jdbcType=INTEGER}
     </where>
  </select>
  <select id = "selectMaxmenuIdByParentid" parameterType="java.lang.Integer" resultType="java.lang.Integer">
  	 select max(menuid) from SysMenu
   
     <where >
     		 ParentID = #{parentid,jdbcType=INTEGER}
     </where>
     
  </select>
  
  <select id = "selectAllpageMenus" resultMap="ResultMapWithBLOBs">
    select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from SysMenu
    <![CDATA[
     	where isDelete = 0 and Level < 4
     ]]>
	And MenuType = 'Page'
     Order By  
     cast(OrderBy as UNSIGNED INTEGER)
     </select>
  <select id = "selectFirstMenu" resultMap="ResultMapWithBLOBs">
   select
  		*
  	from 
  		SysMenu  su
  	where 
  		su.ParentID = -1
  	AND su.isDelete = 0
  	AND su.IsEnable = 0
    AND su.menutype = 'page'
  </select>
  
  <select id = "selectByMenuId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
  	 select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from SysMenu
    where MenuID = #{menuid,jdbcType=INTEGER}
  </select>
  <select id = "selectFirstMenuByParentId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
   select
  		*
  	from 
  		SysMenu  su
  	where 
  		su.ParentID = #{parentId}
  </select>
  
  <select id = "selectSysUserMenus" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
  	
  	select 
  		distinct
  		sm.* 
 	from 
 		SysUser su ,
 		SysUserRole sur ,
 		SysRoleMenu srm ,
 		SysMenu sm

	where  
		su.UserID = sur.UserID 
		and sur.RoleID = srm.RoleID 
		and srm.MenuID = sm.MenuID 
		and su.UserID = #{userid,jdbcType=INTEGER}
		and sm.isDelete = 0 
		and sm.isenable = 0
 </select>
 
  <select id = "selectMenusByPage" resultMap="ResultMapWithBLOBs" parameterType="com.ssm.model.system.SysMenu">
  	 select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from SysMenu
    
    where isDelete = 0
    AND IsEnable = 0
    AND ( menutype = 'page'   or menutype = 'group' )
    ANd `Level`  <![CDATA[ < 4 ]]> 
     ORDER BY cast(OrderBy as UNSIGNED INTEGER)
  </select>
	
	
  <select id="selectPageMenusByRoleId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
   	  	SELECT  sm.* from  SysMenu sm , SysRoleMenu srm , SysRole sr
		where sm.MenuID = srm.MenuID AND sr.RoleID = srm.RoleID
		AND srm.RoleID= #{roleId}  AND sm.IsDelete=0 AND sm.IsEnable = 0 
		AND sm.`Level` <![CDATA[ <  ]]>  4 AND (sm.MenuType ='page' or sm.MenuType ='group')
		ORDER BY cast(sm.OrderBy as UNSIGNED INTEGER)  ASC
  </select>	

  <select id="selectButtonMenusByRoleId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
   	    SELECT  sm.* from  SysMenu sm , SysRoleMenu srm , SysRole sr
		where sm.MenuID = srm.MenuID AND sr.RoleID = srm.RoleID
		AND srm.RoleID= #{roleId}  AND sm.IsDelete=0 AND sm.IsEnable = 0 
		AND sm.MenuType != 'page' AND sm.MenuType != 'group'
		ORDER BY cast(sm.OrderBy as UNSIGNED INTEGER)  ASC
  </select>				

  <select id="selectMenusByRoleId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
    SELECT
		su.*
		FROM
			SysRoleMenu sm , SysMenu su 
		WHERE	
			sm.RoleID = #{roleId}
		AND sm.MenuID = su.MenuID
		AND ( su.MenuType = 'page'  or  su.MenuType='Group' )	
	<![CDATA[	AND  su.level < 4 ]]> 
     ORDER BY cast(su.OrderBy as UNSIGNED INTEGER)
  </select>
  
  <select id="selectMenusLevelLT2ByRoleId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
    SELECT
		DISTINCT	su.*
		FROM
			SysRoleMenu sm , SysMenu su 
		WHERE	
			sm.RoleID = #{roleId}
		AND sm.MenuID = su.MenuID
	AND su.MenuType != 'page'
	AND su.MenuType != 'group'
		
     ORDER BY su.OrderBy
  </select>

  <select id="selectButtonByRoleId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
   	    SELECT
			su.*
		FROM
			SysRoleMenu sm
		LEFT JOIN SysMenu su ON sm.MenuID = su.MenuID
		WHERE
			sm.RoleID = #{roleId}
		AND IsDelete = 0
		AND su.level = 3
     	ORDER BY su.OrderBy
  </select>
  
  <select id="selectPageMenusByRole" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
   	   SELECT
			*
		FROM
			SysRoleMenu sm,
			SysMenu su
		WHERE
			sm.MenuID = su.MenuID
		AND su.MenuType = 'page'
		AND su.IsDelete = 0
		AND sm.RoleID = #{roleId}
  </select>
  
   <select id="selectChildMenusByPageMenuId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
   	  SELECT
			*
		FROM
			SysMenu su
		WHERE
			su.ParentID = #{menuId}
		ORDER BY cast(su.OrderBy as UNSIGNED INTEGER)  ASC
  </select>
  
   <select id="selectButtonMenusByRole" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
   		 SELECT
			*
		FROM
			SysRoleMenu sm,
			SysMenu su
		WHERE
			sm.MenuID = su.MenuID
		AND su.MenuType = 'button'
		AND su.IsDelete = 0
		AND sm.RoleID = #{roleId}
  </select>
  
  <select id="selectMaxOrder" resultType="java.lang.Integer" >
  	SELECT MAX(cast(s.orderby as UNSIGNED INTEGER))+1 from SysMenu s 
  	
  	
  	
  </select>
  <select id="selectMaxOrderUnderParent"  resultType="java.lang.Integer" parameterType="java.lang.Integer">
  	SELECT MAX(cast(s.orderby as UNSIGNED INTEGER)) from SysMenu s where s.ParentID= #{parentId}
  	
  </select>
  
  <select id="selectMaxOrderIdUnderParent"  resultType="java.lang.Integer" parameterType="java.lang.Integer">
  	SELECT TOP 1 s.MenuId from SysMenu s where s.ParentID= #{parentId} ORDER BY cast(s.orderby as UNSIGNED INTEGER) DESC 
  </select>
  
  <update id="updateOrderBy"  parameterType="java.lang.Integer">
  	UPDATE SysMenu set OrderBy = cast(OrderBy as UNSIGNED INTEGER)+1 WHERE cast(OrderBy as UNSIGNED INTEGER)
   <![CDATA[	>=  ]]>
  	#{orderBy}
  </update>
  
  <update id="updateOrderByCount"  parameterType="java.lang.Integer">
  	UPDATE SysMenu set OrderBy = cast(OrderBy as UNSIGNED INTEGER) + #{addNum} WHERE cast(OrderBy as UNSIGNED INTEGER)
   <![CDATA[	>  ]]> 
  	#{orderBy}
  </update>
  
  
  <select id="countSonMenu" parameterType="java.lang.Integer" resultType="java.lang.Integer" >
  	SELECT count(s.MenuID) from SysMenu s where s.ParentID = #{menuId} and s.IsDelete = 0
  </select>
  <!-- 查询同一级  前一个菜单 -->
  <select id="selectPrevOrder"  parameterType="java.lang.Integer" resultType="java.lang.Integer">
  	SELECT MAX(cast(s.OrderBy as UNSIGNED INTEGER)) from SysMenu s where s.`Level` = #{level}
  		AND cast(s.OrderBy as UNSIGNED INTEGER)
  	 <![CDATA[ < #{orderBy}  ]]> 
  	  AND s.ParentID= #{parentId}
  </select>
  
  <select id="selectNextOrder"  parameterType="java.lang.Integer" resultType="java.lang.Integer">
 	SELECT MIN(cast(s.OrderBy as UNSIGNED INTEGER)) from SysMenu s where s.`Level` = #{level}
  		AND cast(s.OrderBy as UNSIGNED INTEGER)
  	 > #{orderBy}  
  	  AND s.ParentID= #{parentId} 
  </select>
  
  <select id="selectNextLevel"  parameterType="java.lang.Integer" resultType="java.lang.Integer">
 	SELECT MIN(cast(s.OrderBy as UNSIGNED INTEGER)) from SysMenu s where s.`Level` = #{level}
  		AND cast(s.OrderBy as UNSIGNED INTEGER)
  	 > #{orderBy}  
  </select>
  
  <select id="selectMenuByOrderAndParent" parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs" >
  	SELECT s.* from SysMenu s where s.ParentID=#{parentId} AND s.OrderBy=#{orderBy}
  </select>
  
  <select id="selectMenusByOrderBy" parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs">
  	SELECT s.* from SysMenu s where cast(s.OrderBy as UNSIGNED INTEGER) > #{orderBy}
  </select>
  
  <select id="countAdd" parameterType="java.lang.Integer" resultType="java.lang.Integer" >
  	SELECT count(s.MenuID) from SysMenu s where cast(s.OrderBy as UNSIGNED INTEGER) >= #{from}
	AND cast(s.OrderBy as UNSIGNED INTEGER) <![CDATA[ < ]]>  ${to}
  </select>
  
  <select id="addOrderBy"  parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs">
  	select * from SysMenu 
	WHERE cast(OrderBy as UNSIGNED INTEGER) >= #{to} AND cast(OrderBy as UNSIGNED INTEGER) <![CDATA[ < ]]>  #{from}
	order by  cast(OrderBy as UNSIGNED INTEGER) asc
  </select>
  <select id="deleteOrderBy"  parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs" >
  	select * from SysMenu 
	WHERE cast(OrderBy as UNSIGNED INTEGER) >= #{from} AND cast(OrderBy as UNSIGNED INTEGER) <![CDATA[ < ]]> #{next}
	order by  cast(OrderBy as UNSIGNED INTEGER) asc
  </select>
  
  <select id="selectMenuByUrl" parameterType="java.lang.String" resultMap="ResultMapWithBLOBs">
  	 SELECT * from SysMenu s where s.MenuUrl = #{url} 
  </select>
  
  <select id="selectReByMenuIdAndRoleId" parameterType="java.lang.Integer"  resultType="java.lang.Integer" >
  	SELECT count(*) from SysRoleMenu s where s.MenuID=#{menuId}  AND s.RoleID=#{roleId}
  </select>
</mapper>