<?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.fjs.scenic.mapper.system.PermissionMapper">
    <resultMap id="BaseResultMap" type="com.fjs.scenic.entity.system.Permission">
        <constructor>
            <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer"/>
            <arg column="name" jdbcType="VARCHAR" javaType="java.lang.String"/>
            <arg column="url" jdbcType="VARCHAR" javaType="java.lang.String"/>
            <arg column="lever" jdbcType="TINYINT" javaType="java.lang.Byte"/>
            <arg column="seq" jdbcType="INTEGER" javaType="java.lang.Integer"/>
            <arg column="pid" jdbcType="INTEGER" javaType="java.lang.Integer"/>
            <arg column="icon" jdbcType="VARCHAR" javaType="java.lang.String"/>
        </constructor>
    </resultMap>
    <sql id="Base_Column_List">
    id, name, url, lever, seq, pid, icon
  </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from permission
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from permission
    where id = #{id,jdbcType=INTEGER}
  </delete>
    <insert id="insert" parameterType="com.fjs.scenic.entity.system.Permission">
    insert into permission (id, name, url, 
      lever, seq, pid, icon
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR}, 
      #{lever,jdbcType=TINYINT}, #{seq,jdbcType=INTEGER}, #{pid,jdbcType=INTEGER}, #{icon,jdbcType=VARCHAR}
      )
  </insert>
    <insert id="insertSelective" parameterType="com.fjs.scenic.entity.system.Permission">
        insert into permission
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="url != null">
                url,
            </if>
            <if test="lever != null">
                lever,
            </if>
            <if test="seq != null">
                seq,
            </if>
            <if test="pid != null">
                pid,
            </if>
            <if test="icon != null">
                icon,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="url != null">
                #{url,jdbcType=VARCHAR},
            </if>
            <if test="lever != null">
                #{lever,jdbcType=TINYINT},
            </if>
            <if test="seq != null">
                #{seq,jdbcType=INTEGER},
            </if>
            <if test="pid != null">
                #{pid,jdbcType=INTEGER},
            </if>
            <if test="icon != null">
                #{icon,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.fjs.scenic.entity.system.Permission">
        update permission
        <set>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="url != null">
                url = #{url,jdbcType=VARCHAR},
            </if>
            <if test="lever != null">
                lever = #{lever,jdbcType=TINYINT},
            </if>
            <if test="seq != null">
                seq = #{seq,jdbcType=INTEGER},
            </if>
            <if test="pid != null">
                pid = #{pid,jdbcType=INTEGER},
            </if>
            <if test="icon != null">
                icon = #{icon,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.fjs.scenic.entity.system.Permission">
    update permission
    set name = #{name,jdbcType=VARCHAR},
      url = #{url,jdbcType=VARCHAR},
      lever = #{lever,jdbcType=TINYINT},
      seq = #{seq,jdbcType=INTEGER},
      pid = #{pid,jdbcType=INTEGER},
      icon = #{icon,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

    <!--自定义-->
    <resultMap id="TreeMap" type="com.fjs.scenic.dto.PermissionDto">
        <id property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer"/>
        <result property="name" column="name" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="url" column="url" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="lever" column="lever" jdbcType="TINYINT" javaType="java.lang.Byte"/>
        <result property="seq" column="seq" jdbcType="INTEGER" javaType="java.lang.Integer"/>
        <result property="pid" column="pid" jdbcType="INTEGER" javaType="java.lang.Integer"/>
        <result property="icon" column="icon" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <collection property="children" ofType="com.fjs.scenic.dto.PermissionDto">
            <id property="id" column="id1" jdbcType="INTEGER" javaType="java.lang.Integer"/>
            <result property="name" column="name1" jdbcType="VARCHAR" javaType="java.lang.String"/>
            <result property="url" column="url1" jdbcType="VARCHAR" javaType="java.lang.String"/>
            <result property="lever" column="lever1" jdbcType="TINYINT" javaType="java.lang.Byte"/>
            <result property="seq" column="seq1" jdbcType="INTEGER" javaType="java.lang.Integer"/>
            <result property="pid" column="pid1" jdbcType="INTEGER" javaType="java.lang.Integer"/>
            <result property="icon" column="icon1" jdbcType="VARCHAR" javaType="java.lang.String"/>
            <collection property="children" ofType="com.fjs.scenic.dto.PermissionDto">
                <id property="id" column="id2" jdbcType="INTEGER" javaType="java.lang.Integer"/>
                <result property="name" column="name2" jdbcType="VARCHAR" javaType="java.lang.String"/>
                <result property="url" column="url2" jdbcType="VARCHAR" javaType="java.lang.String"/>
                <result property="lever" column="lever2" jdbcType="TINYINT" javaType="java.lang.Byte"/>
                <result property="seq" column="seq2" jdbcType="INTEGER" javaType="java.lang.Integer"/>
                <result property="pid" column="pid2" jdbcType="INTEGER" javaType="java.lang.Integer"/>
                <result property="icon" column="icon2" jdbcType="VARCHAR" javaType="java.lang.String"/>
            </collection>
        </collection>
    </resultMap>

    <resultMap id="TreeMap2" type="com.fjs.scenic.dto.PermissionDto">
        <id property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer"/>
        <result property="name" column="name" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="url" column="url" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="lever" column="lever" jdbcType="TINYINT" javaType="java.lang.Byte"/>
        <result property="seq" column="seq" jdbcType="INTEGER" javaType="java.lang.Integer"/>
        <result property="pid" column="pid" jdbcType="INTEGER" javaType="java.lang.Integer"/>
        <result property="icon" column="icon" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <collection property="children" column="id" select="selectByPid"></collection>
    </resultMap>

    <select id="selectByPid" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from permission
        where pid = #{id,jdbcType=INTEGER}
    </select>

    <!--自定义sql-->
    <select id="selectTree" resultMap="TreeMap" parameterType="com.fjs.scenic.dto.PermissionDto">
    SELECT p.*,p1.id id1,p1.name name1,p1.url url1,p1.lever lever1,
          p1.seq seq1,p1.pid pid1,p1.icon icon1,
          p2.id id2,p2.name name2,p2.url url2,p2.lever lever2,
          p2.seq seq2,p2.pid pid2,p2.icon icon2
          FROM permission p
          LEFT JOIN permission p1 ON
          p.id=p1.pid LEFT JOIN permission p2 ON
          p2.pid=p1.id
      WHERE p.pid=0
      ORDER BY p.seq,p1.seq,p2.seq
  </select>

    <select id="getParent" resultMap="BaseResultMap">
    select * from permission WHERE pid=0
  </select>

    <select id="getAll" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from permission
    </select>

    <select id="getListByManageId" resultMap="TreeMap2">
        SELECT
            distinct p.*
        FROM
            manage m
        LEFT JOIN manage_role mr ON m.id = mr.manage_id
        LEFT JOIN role r ON r.id = mr.role_id
        LEFT JOIN role_permission rp ON r.id = rp.role_id
        LEFT JOIN permission p ON p.id = rp.permission_id
        WHERE m.id = #{id,jdbcType=INTEGER} and p.pid = 0
    </select>
  
  <select id="getPermissionAll" resultType="java.lang.String">
       SELECT distinct url from permission
  </select>
  
  <select id="getPermissionsByManageId" resultType="java.lang.String">
  SELECT distinct p.url FROM (
	SELECT role_id FROM manage_role WHERE manage_id=#{managerId}) m
	LEFT JOIN role_permission rp ON m.role_id=rp.role_id
	LEFT JOIN (SELECT id,url FROM permission) p
	ON rp.permission_id=p.id WHERE p.url is NOT NULL
  </select>

    <!--通过账号获取登录用户权限-->
    <select id="getListByManageAccount" resultMap="TreeMap2">
        SELECT distinct p.* FROM manage m
        LEFT JOIN manage_role mr ON m.id = mr.manage_id
        LEFT JOIN role r ON r.id = mr.role_id
        LEFT JOIN role_permission rp ON r.id = rp.role_id
        LEFT JOIN permission p ON p.id = rp.permission_id
        WHERE  p.pid = 0
        <if test="account != null and account != '' ">
           AND m.account = #{account}
        </if>
        ORDER BY p.seq
    </select>
</mapper>