<?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.xintong.visualinspection.dao.master.PermissionDao">

  <resultMap id="BaseResultMap" type="com.xintong.visualinspection.bean.Permission" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="description" property="description" jdbcType="VARCHAR" />
        <result column="pid" property="pid" jdbcType="INTEGER" />
        <result column="type" property="type" jdbcType="INTEGER" />     
        <result column="resourceid" property="resourceid" jdbcType="INTEGER" />  
        <result column="status" property="status" jdbcType="INTEGER" />     
         <result column="permission_type" property="permission_type" jdbcType="INTEGER" />
    </resultMap>


    <select id="findAll"  resultMap="BaseResultMap">

        SELECT * from sys_permission ;
    </select>
    
    
     <select id="getPermission" parameterType="com.xintong.visualinspection.dao.master.PermissionDao" resultMap="BaseResultMap">
        SELECT 
        id,name,description,pid,type,resourceid,status 
         FROM sys_permission
         where 1=1
         <if test="id != null and id != 0">and id = #{id}</if>
         <if test="name != null">and name = #{name}</if>
         <if test="description != null">and description = #{description}</if>
         <if test="pid != null and pid != 0">and pid = #{pid}</if>
         <if test="type != null and type != 0">and type = #{type}</if>
         <if test="resourceid != null and resourceid != 0">and resourceid = #{resourceid}</if>
         <if test="status != null">and status = #{status}</if>
    </select>
    
     <insert id="insert" parameterType="com.xintong.visualinspection.dao.master.PermissionDao" >
        INSERT INTO
        sys_permission
        (name,description,pid,type,resourceid,status)
        VALUES
        (#{name},#{description},#{pid},#{type},#{resourceid},#{status})
    </insert>
    
    
     <update id="update" parameterType="com.xintong.visualinspection.dao.master.PermissionDao" >
        UPDATE
        sys_permission
        SET
         id = #{id}
         <if test="name != null">and name = #{name}</if>
         <if test="descritpion != null">and descritpion = #{descritpion}</if>
         <if test="pid != null and pid != 0">and pid = #{pid}</if>
         <if test="type != null and type != 0">and type = #{type}</if>
         <if test="resourceid != null and resourceid != 0">and resourceid = #{resourceid}</if>
        WHERE
        id = #{id}
    </update>
    
     <delete id="delete" parameterType="java.lang.Integer" >
        delete from sys_permission
        WHERE
        id =#{id}
    </delete>

    <select id="findByAdminUserId" parameterType="int" resultType="com.xintong.visualinspection.bean.Permission">
      select p.*,spr.permission_type 
        from sys_permission p
        LEFT JOIN sys_role_user sru on sru.sys_user_id = #{userId}
        LEFT JOIN sys_role r on sru.sys_role_id=r.id
        LEFT JOIN sys_permission_role spr on spr.role_id=r.id
        where p.id =spr.permission_id
    </select>
    
    
     <select id="getByRoleId" parameterType="java.lang.Integer"  resultMap="BaseResultMap">
        SELECT p.* from sys_permission p  where id in (select permission_id from sys_permission_role where role_id=#{roleId});
    </select>
    
    
    
     <insert id="addrolepermision" parameterType="com.xintong.visualinspection.pojo.RolePermision" >
        INSERT INTO
        sys_permission_role
        (role_id,permission_id,permission_type)
        VALUES
        (#{role_id},#{permission_id},#{permission_type})
      </insert>
      
       <delete id="deleterolepermision" parameterType="com.xintong.visualinspection.pojo.RolePermision" >
        delete from
        sys_permission_role
        where role_id=#{role_id} and permission_id=#{permission_id}
      </delete>
    
    
       <update id="updaterolepermision" parameterType="com.xintong.visualinspection.pojo.RolePermision" >
        update  
        sys_permission_role
        set permission_type = #{permission_type} 
        where role_id=#{role_id} and permission_id=#{permission_id}
      </update>
</mapper>