<?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="cn.superdesk.uniorder.dao.mapper.sys.role.SysRoleMenuMapperEx">
	<resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.sys.role.SysRoleMenuKey">
		<id column="role_id" property="roleId" jdbcType="BIGINT"/>
		<id column="menu_id" property="menuId" jdbcType="BIGINT"/>
	</resultMap>
	<resultMap id="RoleMenuResultMap" type="cn.superdesk.uniorder.dto.vo.sys.role.ViewRoleMenu">
		<result column="role_id" property="roleId" jdbcType="BIGINT"/>
		<result column="menu_id" property="menuId" jdbcType="BIGINT"/>
		<result column="pid" property="pid" jdbcType="BIGINT"/>
		<result column="menu_name" property="menuName" jdbcType="VARCHAR"/>
		<result column="menu_url" property="menuUrl" jdbcType="VARCHAR"/>
		<result column="perms" property="perms" jdbcType="VARCHAR"/>
		<result column="menu_type" property="menuType" jdbcType="INTEGER"/>
		<result column="menu_icon" property="menuIcon" jdbcType="VARCHAR"/>
		<result column="order_num" property="orderNum" jdbcType="INTEGER"/>
	</resultMap>

	<select id="getDataByRoleId" resultMap="RoleMenuResultMap">
		select
			srm.role_id,srm.menu_id,
			sm.pid,sm.menu_name,sm.menu_url,sm.perms,sm.menu_type,sm.menu_icon,sm.order_num
		from
			sys_role_menu srm
			LEFT JOIN sys_menu sm on srm.menu_id=sm.id
		<where>
		<if test="_parameter != null ">
			and srm.role_id=#{_parameter,jdbcType=BIGINT}
		</if>
		</where>
		order by sm.order_num
	</select>

	<select id="findAllView" resultMap="RoleMenuResultMap">
		select
			*
		from
			view_role_menu
		<where>
		<if test="roleId != null ">
			and role_id=#{roleId,jdbcType=BIGINT}
		</if>
		<if test="pid != null ">
			and pid=#{pid,jdbcType=BIGINT}
		</if>
		</where>
		order by order_num
	</select>

	<resultMap id="TreeResultMap" type="cn.superdesk.uniorder.dto.vo.sys.role.ViewMenuTree">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="role_id" property="roleId" jdbcType="BIGINT"/>
		<result column="pid" property="pid" jdbcType="BIGINT"/>
		<result column="menu_name" property="menuName" jdbcType="VARCHAR"/>
		<result column="menu_url" property="menuUrl" jdbcType="VARCHAR"/>
		<result column="perms" property="perms" jdbcType="VARCHAR"/>
		<result column="menu_type" property="menuType" jdbcType="VARCHAR"/>
		<result column="menu_icon" property="menuIcon" jdbcType="VARCHAR"/>
		<result column="order_num" property="orderNum" jdbcType="INTEGER"/>
		<collection property="childList" ofType="cn.superdesk.uniorder.dto.vo.sys.role.ViewMenuTree"
		            javaType="java.util.List" column="{pid=id,roleId=role_id}" select="GetChildById">
		</collection>
	</resultMap>
	<resultMap id="TreeResultMapNew" type="cn.superdesk.uniorder.dto.vo.sys.role.ViewMenuTree">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="role_id" property="roleId" jdbcType="BIGINT"/>
		<result column="pid" property="pid" jdbcType="BIGINT"/>
		<result column="menu_name" property="menuName" jdbcType="VARCHAR"/>
		<result column="menu_url" property="menuUrl" jdbcType="VARCHAR"/>
		<result column="perms" property="perms" jdbcType="VARCHAR"/>
		<result column="menu_type" property="menuType" jdbcType="VARCHAR"/>
		<result column="menu_icon" property="menuIcon" jdbcType="VARCHAR"/>
		<result column="order_num" property="orderNum" jdbcType="INTEGER"/>

	</resultMap>
	<select id="getTreeByRoleIdNew" resultMap="TreeResultMapNew" parameterType="java.lang.Long">
		select
			srm.role_id,
			sm.id,
			sm.pid,
			sm.menu_name,
			sm.menu_url,
			sm.perms,
			sm.menu_type,
			sm.menu_icon,
			sm.order_num
		from
			sys_role_menu srm
			inner join sys_menu sm on srm.menu_id=sm.id
		<where>
			and sm.enabled = 1
			and sm.deleted = 0
			and srm.role_id=#{_parameter,jdbcType=BIGINT}
		</where>
		order by sm.menu_type,sm.order_num
  	</select>
	<select id="getTreeByRoleId" resultMap="TreeResultMap" parameterType="java.lang.Long">
		select
			srm.role_id,
			sm.id,sm.pid,sm.menu_name,sm.menu_url,sm.perms,sm.menu_type,sm.menu_icon,sm.order_num
		from
			sys_role_menu srm
			inner join sys_menu sm on srm.menu_id=sm.id
		<where>
			and srm.role_id = #{_parameter,jdbcType=BIGINT}
			and pid is null
		</where>
		order by b.order_num
  	</select>
	<select id="GetChildById" resultMap="TreeResultMap">
		select
			srm.role_id,
			sm.id,sm.pid,sm.menu_name,sm.menu_url,sm.perms,sm.menu_type,sm.menu_icon,sm.order_num
		from
			sys_role_menu srm
			inner join sys_menu sm on srm.menu_id=sm.id
		<where>
			and srm.role_id=#{roleId,jdbcType=BIGINT}
			and pid =#{pid,jdbcType=BIGINT}
		</where>
		order by sm.order_num
	</select>
	<delete id="deleteByRoleId" parameterType="java.lang.Long">
		delete from
			sys_role_menu
		<where>
			role_id = #{roleId,jdbcType=BIGINT}
		</where>
	</delete>

	<delete id="deleteByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.sys.role.SysRoleMenuKey">
    delete from sys_role_menu
    where role_id = #{roleId,jdbcType=BIGINT}
      and menu_id = #{menuId,jdbcType=BIGINT}
  </delete>
	<insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.sys.role.SysRoleMenuKey">
    insert into sys_role_menu (role_id, menu_id)
    values (#{roleId,jdbcType=BIGINT}, #{menuId,jdbcType=BIGINT})
  </insert>
	<insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.sys.role.SysRoleMenuKey">
		insert into sys_role_menu
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="roleId != null">
				role_id,
			</if>
			<if test="menuId != null">
				menu_id,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="roleId != null">
				#{roleId,jdbcType=BIGINT},
			</if>
			<if test="menuId != null">
				#{menuId,jdbcType=BIGINT},
			</if>
		</trim>
	</insert>
</mapper>