<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cloudhandson.vpdbackoffice.mapper.PermissionMapper">
  <select id="findRoles" resultType="com.cloudhandson.vpdbackoffice.domain.permission.AppRole">
    SELECT role_id,
           role_name,
           description,
           max_sensitivity_level
    FROM sg_app_role
    ORDER BY role_name
  </select>

  <select id="findRole" resultType="com.cloudhandson.vpdbackoffice.domain.permission.AppRole">
    SELECT role_id,
           role_name,
           description,
           max_sensitivity_level
    FROM sg_app_role
    WHERE role_id = #{roleId}
  </select>

  <select id="nextRoleId" resultType="long">
    SELECT NVL(MAX(role_id), 0) + 1 FROM sg_app_role
  </select>

  <insert id="insertRole">
    INSERT INTO sg_app_role (role_id, role_name, description, max_sensitivity_level)
    VALUES (
      #{roleId,jdbcType=NUMERIC},
      UPPER(#{roleName,jdbcType=VARCHAR}),
      #{description,jdbcType=VARCHAR},
      #{maxSensitivityLevel,jdbcType=VARCHAR}
    )
  </insert>

  <update id="updateRoleMaxSensitivity">
    UPDATE sg_app_role
    SET max_sensitivity_level = #{maxSensitivityLevel,jdbcType=VARCHAR}
    WHERE role_id = #{roleId,jdbcType=NUMERIC}
  </update>

  <delete id="deleteRole">
    DELETE FROM sg_app_role
    WHERE role_id = #{roleId,jdbcType=NUMERIC}
  </delete>

  <select id="countUserRolesByRoleId" resultType="int">
    SELECT COUNT(*)
    FROM sg_user_role
    WHERE role_id = #{roleId,jdbcType=NUMERIC}
  </select>

  <select id="countGroupRolesByRoleId" resultType="int">
    SELECT COUNT(*)
    FROM sg_group_role
    WHERE role_id = #{roleId,jdbcType=NUMERIC}
  </select>

  <select id="countPermissionsByRoleId" resultType="int">
    SELECT COUNT(*)
    FROM dual
    WHERE 1 = 0
  </select>

  <select id="findPermissionViews" resultType="com.cloudhandson.vpdbackoffice.domain.permission.PermissionView">
    SELECT CAST(NULL AS NUMBER) AS permission_id,
           CAST(NULL AS NUMBER) AS role_id,
           CAST(NULL AS VARCHAR2(80)) AS role_name,
           CAST(NULL AS NUMBER) AS object_id,
           CAST(NULL AS VARCHAR2(200)) AS object_name,
           CAST(NULL AS VARCHAR2(30)) AS action,
           CAST(NULL AS VARCHAR2(20)) AS permission_effect,
           CAST(NULL AS VARCHAR2(4000)) AS rules,
           CAST(NULL AS VARCHAR2(4000)) AS visible_columns,
           CAST(NULL AS VARCHAR2(4000)) AS filter_preview,
           CAST(NULL AS VARCHAR2(4000)) AS null_policy_preview
    FROM dual
    WHERE 1 = 0
  </select>

  <select id="findPermissionId" resultType="long">
    SELECT p.perm_id
    FROM cb_permission p
    JOIN cb_protected_object o ON o.object_name = p.target_name
    WHERE p.role_id = #{roleId}
      AND o.object_id = #{objectId}
  </select>

  <select id="findObjectIdByPermissionId" resultType="long">
    SELECT o.object_id
    FROM cb_permission p
    JOIN cb_protected_object o ON o.object_name = p.target_name
    WHERE p.perm_id = #{permissionId}
  </select>

  <select id="countPermissionsByObjectId" resultType="int">
    SELECT COUNT(*)
    FROM cb_permission p
    JOIN cb_protected_object o ON o.object_name = p.target_name
    WHERE o.object_id = #{objectId}
  </select>

  <select id="findPermissionSet" resultType="com.cloudhandson.vpdbackoffice.domain.permission.PermissionSet">
    SELECT p.perm_id AS permission_id,
           p.role_id,
           o.object_id,
           p.action_name AS action,
           NVL(p.permission_effect, 'ALLOW') AS permission_effect
    FROM cb_permission p
    JOIN cb_protected_object o ON o.object_name = p.target_name
    WHERE p.role_id = #{roleId}
      AND o.object_id = #{objectId}
  </select>

  <select id="nextPermissionId" resultType="long">
    SELECT cb_permission_seq.NEXTVAL FROM dual
  </select>

  <select id="nextRuleId" resultType="long">
    SELECT cb_permission_rule_seq.NEXTVAL FROM dual
  </select>

  <insert id="insertPermission">
    INSERT INTO cb_permission (perm_id, role_id, target_name, action_name, permission_effect)
    SELECT #{permissionId}, #{roleId}, object_name, #{action}, #{permissionEffect}
    FROM cb_protected_object
    WHERE object_id = #{objectId}
  </insert>

  <update id="updatePermissionAction">
    UPDATE cb_permission
    SET action_name = #{action}
    WHERE perm_id = #{permissionId}
  </update>

  <update id="updatePermissionEffect">
    UPDATE cb_permission
    SET permission_effect = #{permissionEffect}
    WHERE perm_id = #{permissionId}
  </update>

  <delete id="deleteRules">
    DELETE FROM cb_permission_rule
    WHERE perm_id = #{permissionId}
  </delete>

  <insert id="insertRule" parameterType="com.cloudhandson.vpdbackoffice.domain.permission.PermissionRule">
    INSERT INTO cb_permission_rule (rule_id, perm_id, rule_column, rule_type, rule_value)
    VALUES (
      #{ruleId,jdbcType=NUMERIC},
      #{permissionId,jdbcType=NUMERIC},
      #{ruleColumn,jdbcType=VARCHAR},
      #{ruleType,jdbcType=VARCHAR},
      #{ruleValue,jdbcType=VARCHAR}
    )
  </insert>

  <delete id="deleteVisibleColumns">
    DELETE FROM cb_permission_column
    WHERE permission_id = #{permissionId}
  </delete>

  <insert id="insertVisibleColumn">
    INSERT INTO cb_permission_column (permission_id, column_name)
    VALUES (#{permissionId}, #{columnName})
  </insert>

  <delete id="deletePermission">
    DELETE FROM cb_permission
    WHERE perm_id = #{permissionId}
  </delete>
</mapper>
