<?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.energy.manage.service.mappers.system.SysPermissionMapper">

    <sql id="BASE_PERMISSION_SQL">
      p.permission_id AS permissionId,
      p.permission_name AS permissionName,
      p.parent_id AS parentId,
      p.permission_code AS permissionCode,
      p.permission_type AS permissionType,
      p.permission_url AS permissionUrl,
      p.permission_state AS permissionState,
      p.permission_sort AS permissionSort,
      p.permission_icon_url AS permissionIconUrl,
      p.permission_depth AS permissionDepth,
      p.permission_show as permissionShow,
      p.permission_code as permissionCode
    </sql>

    <select id="getMaxDepth" resultType="java.lang.Integer">
        SELECT MAX(permission_depth) FROM sys_permission
    </select>

    <select id="getAllPermission" resultType="com.energy.manage.service.domain.vo.system.SystemPermissionVo">
        SELECT <include refid="BASE_PERMISSION_SQL"/>
        FROM sys_permission p
        where 1=1
        <if test="permissionListDto.permissionState != null">
            and p.permission_state=#{permissionListDto.permissionState}
        </if>
        <if test="permissionListDto.permissionName != null and permissionListDto.permissionName != ''">
            and p.permission_name like CONCAT("%", #{permissionListDto.permissionName} ,"%")
        </if>
        ORDER BY p.permission_sort ASC, p.permission_id ASC
    </select>

    <select id="getRolePermissionByUserId" resultType="com.energy.manage.service.domain.vo.system.PermissionVo">
        SELECT <include refid="BASE_PERMISSION_SQL"/>
        FROM sys_user_role ur
        INNER JOIN sys_role_permission rp ON rp.role_id = ur.role_id
        LEFT JOIN sys_permission p ON p.permission_id = rp.permission_id
        WHERE ur.user_id = #{userId}
        ORDER BY p.permission_sort ASC, p.permission_id ASC
    </select>

    <select id="getRolePermissionMaxDepth" resultType="java.lang.Integer">
        SELECT MAX(p.permission_depth)
        FROM sys_user_role ur
        LEFT JOIN sys_role_permission rp ON rp.role_id = ur.role_id
        LEFT JOIN sys_permission p ON p.permission_id = rp.permission_id
        WHERE ur.user_id = #{userId}
    </select>

    <update id="updatePermissionSelective" parameterType="com.energy.manage.common.po.system.SysPermissionPO">
        UPDATE sys_permission
        <set>
            <if test="null != permissionName and '' != permissionName">
                permission_name = #{permissionName,jdbcType=VARCHAR},
            </if>
            <if test="null != permissionCode and '' != permissionCode">
                permission_code = #{permissionCode,jdbcType=VARCHAR},
            </if>
            <if test="null != permissionType">
                permission_type = #{permissionType,jdbcType=INTEGER},
            </if>
            <if test="null != permissionUrl and '' != permissionUrl">
                permission_url = #{permissionUrl,jdbcType=VARCHAR},
            </if>
            <if test="null != permissionState">
                permission_state = #{permissionState,jdbcType=VARCHAR},
            </if>
            <if test="null != permissionSort">
                permission_sort = #{permissionSort,jdbcType=VARCHAR},
            </if>
            <if test="null != permissionIconUrl and '' != permissionIconUrl">
                permission_icon_url = #{permissionIconUrl,jdbcType=VARCHAR},
            </if>
            <if test="null != permissionShow and '' != permissionShow">
                permission_show = #{permissionShow},
            </if>
        </set>
        WHERE permission_id = #{permissionId,jdbcType=INTEGER}
    </update>
</mapper>
