<?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.nbclass.system.mapper.PermissionMapper" >
  <resultMap id="BaseResultMap" type="com.nbclass.system.model.Permission" >
    <!--
      WARNING - @mbggenerated
    -->
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="permission_id" property="permissionId" jdbcType="VARCHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="description" property="description" jdbcType="VARCHAR" />
    <result column="url" property="url" jdbcType="VARCHAR" />
    <result column="perms" property="perms" jdbcType="VARCHAR" />
    <result column="parent_id" property="parentId" jdbcType="INTEGER" />
    <result column="type" property="type" jdbcType="INTEGER" />
    <result column="order_num" property="orderNum" jdbcType="INTEGER" />
    <result column="icon" property="icon" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    <!--
      WARNING - @mbggenerated
    -->
    id, permission_id, name, description, url, perms, parent_id, type, order_num, icon, status, create_time, update_time
  </sql>

  <select id="selectAllPerms" parameterType="integer" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>  FROM  permission WHERE status=#{status} ORDER BY order_num
  </select>

  <select id="selectAllMenuName" parameterType="integer" resultMap="BaseResultMap">
    SELECT id, permission_id, `name`, parent_id, `type`  FROM  permission WHERE status=#{status} AND `type` in (0,1) ORDER BY order_num
  </select>

  <select id="findPermsByUserId" resultType="java.lang.String">
    SELECT DISTINCT p.perms FROM permission p LEFT JOIN role_permission rp ON
    p.permission_id = rp.permission_id LEFT JOIN user_role ur on rp.role_id = ur.role_id
    WHERE ur.user_id = #{userId} and p.perms is not null and p.perms!='' and p.status=1
  </select>

  <select id="findByRoleId" parameterType="string" resultMap="BaseResultMap">
    SELECT  a.permission_id,b.name,b.parent_id FROM role_permission a LEFT JOIN permission b on a.permission_id = b.permission_id WHERE a.role_id = #{roleId} and b.status=1
  </select>

  <select id="selectByUserId" parameterType="string" resultMap="BaseResultMap">
    SELECT DISTINCT p.id, p.permission_id, p.name, p.description, p.url, p.perms, p.parent_id FROM permission p
    LEFT JOIN role_permission rp ON p.permission_id = rp.permission_id
    LEFT JOIN user_role ur on rp.role_id = ur.role_id
    WHERE ur.user_id = #{userId} and p.status=1
  </select>

  <select id="selectMenuByUserId" parameterType="string" resultMap="BaseResultMap">
    SELECT DISTINCT p.id, p.permission_id, p.name, p.description, p.url, p.parent_id, p.icon, p.order_num FROM permission p LEFT JOIN role_permission rp ON
    p.permission_id = rp.permission_id
    LEFT JOIN user_role ur on rp.role_id = ur.role_id
    WHERE ur.user_id = #{userId,jdbcType=VARCHAR}
    and p.type in(0,1) and p.status=1
    ORDER BY p.order_num
  </select>

  <update id="updateStatusByPermissionId">
    UPDATE
    permission
    SET
    status=#{status}, update_time=now()
    where
    permission_id = #{permissionId}
  </update>

  <select id="selectByPermissionId" parameterType="string" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/> FROM permission
    WHERE permission_id = #{permissionId}
  </select>

  <update id="updateByPermissionId" parameterType="com.nbclass.system.model.Permission">
    UPDATE
    permission
    SET
    name=#{name},description=#{description},url=#{url},perms=#{perms}, parent_id=#{parentId}, order_num=#{orderNum}, icon=#{icon}, update_time=now()
    where
    permission_id = #{permissionId}
  </update>

  <select id="selectSubPermsByPermissionId" parameterType="string" resultType="integer">
    SELECT  count(b.id) FROM permission a  LEFT JOIN permission b on a.id = b.parent_id and b.status=1 WHERE a.permission_id = #{permissionId}
  </select>

</mapper>