<?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="org.xyzh.system.mapper.view.TbSysViewPermissionMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbSysViewPermissionDTO">
        <!-- 视图权限关系字段 -->
        <id column="view_id" property="viewId" jdbcType="VARCHAR"/>
        <id column="permission_id" property="permissionId" jdbcType="VARCHAR"/>
        <!-- 基础字段 -->
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="DATE"/>
        <result column="update_time" property="updateTime" jdbcType="DATE"/>
        <result column="delete_time" property="deleteTime" jdbcType="DATE"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- VO结果映射（用于前端展示） -->
    <resultMap id="PermissionVOResultMap" type="org.xyzh.api.system.vo.PermissionVO">
        <!-- 视图字段 -->
        <result column="view_id" property="viewId" jdbcType="VARCHAR"/>
        <result column="view_name" property="viewName" jdbcType="VARCHAR"/>
        <result column="view_parent_id" property="viewParentId" jdbcType="VARCHAR"/>
        <result column="view_url" property="viewUrl" jdbcType="VARCHAR"/>
        <result column="view_component" property="viewComponent" jdbcType="VARCHAR"/>
        <result column="view_icon" property="viewIcon" jdbcType="VARCHAR"/>
        <result column="view_type" property="viewType" jdbcType="INTEGER"/>
        <result column="view_layout" property="viewLayout" jdbcType="VARCHAR"/>
        <result column="view_order_num" property="viewOrderNum" jdbcType="INTEGER"/>
        <result column="view_description" property="viewDescription" jdbcType="VARCHAR"/>
        <!-- 权限字段 -->
        <result column="permission_id" property="permissionId" jdbcType="VARCHAR"/>
        <result column="permission_name" property="permissionName" jdbcType="VARCHAR"/>
        <result column="permission_code" property="permissionCode" jdbcType="VARCHAR"/>
        <result column="permission_description" property="permissionDescription" jdbcType="VARCHAR"/>
        <result column="permission_status" property="permissionStatus" jdbcType="BOOLEAN"/>
        <!-- 模块关联字段 -->
        <result column="module_id" property="moduleId" jdbcType="VARCHAR"/>
        <result column="module_name" property="moduleName" jdbcType="VARCHAR"/>
        <result column="module_description" property="moduleDescription" jdbcType="VARCHAR"/>
        <!-- BaseVO 基础字段 -->
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="DATE"/>
        <result column="update_time" property="updateTime" jdbcType="DATE"/>
        <result column="delete_time" property="deleteTime" jdbcType="DATE"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础列 -->
    <sql id="Base_Column_List">
        view_id, permission_id,
        optsn, creator, updater, dept_path, remark, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 插入系统视图权限关系（必填 + 可选字段动态列，仅修改 insert） -->
    <insert id="insertViewPermission" parameterType="org.xyzh.common.dto.sys.TbSysViewPermissionDTO">
        INSERT INTO sys.tb_sys_view_permission
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段：view_id, permission_id, optsn -->
            view_id,
            permission_id,
            optsn,
            <!-- 可选字段：基础审计字段 -->
            <if test="creator != null and creator != ''">creator,</if>
            <if test="deptPath != null and deptPath != ''">dept_path,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deleteTime != null">delete_time,</if>
            <if test="deleted != null">deleted,</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段值 -->
            #{viewId},
            #{permissionId},
            #{optsn},
            <!-- 可选字段值 -->
            <if test="creator != null and creator != ''">#{creator},</if>
            <if test="deptPath != null and deptPath != ''">#{deptPath},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deleteTime != null">#{deleteTime},</if>
            <if test="deleted != null">#{deleted},</if>
        </trim>
    </insert>

    <!-- 更新系统视图权限关系 -->
    <update id="updateViewPermission" parameterType="org.xyzh.common.dto.sys.TbSysViewPermissionDTO">
        UPDATE sys.tb_sys_view_permission
        <set>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="deptPath != null and deptPath != ''">
                dept_path = #{deptPath},
            </if>
            <if test="remark != null">
                remark = #{remark},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE view_id = #{viewId} AND permission_id = #{permissionId}
        <if test="deleted != null">
            AND deleted = #{deleted}
        </if>
    </update>

    <!-- 删除系统视图权限关系 -->
    <update id="deleteViewPermission" parameterType="org.xyzh.common.dto.sys.TbSysViewPermissionDTO">
        UPDATE sys.tb_sys_view_permission
        SET deleted = true,
            delete_time = NOW()
        WHERE view_id = #{viewId} AND permission_id = #{permissionId}
    </update>

    <!-- 根据视图ID和权限ID查询系统视图权限关系 -->
    <select id="getViewPermissionByViewId" resultMap="PermissionVOResultMap">
        SELECT
            vp.view_id, vp.permission_id,
            v.view_id, v.name AS view_name, v.parent_id AS view_parent_id, v.url AS view_url, v.component AS view_component,
            v.icon AS view_icon, v.type AS view_type, v.layout AS view_layout, v.order_num AS view_order_num, v.description AS view_description,
            p.permission_id, p.name AS permission_name, p.code AS permission_code, p.description AS permission_description, p.status AS permission_status,
            p.module_id, m.name AS module_name, m.description AS module_description,
            vp.optsn, vp.creator, vp.updater, vp.dept_path, vp.remark, vp.create_time, vp.update_time, vp.delete_time, vp.deleted
        FROM sys.tb_sys_view_permission vp
        LEFT JOIN sys.tb_sys_view v ON vp.view_id = v.view_id AND (v.deleted IS NULL OR v.deleted = false)
        LEFT JOIN sys.tb_sys_permission p ON vp.permission_id = p.permission_id AND (p.deleted IS NULL OR p.deleted = false)
        LEFT JOIN sys.tb_sys_module m ON p.module_id = m.module_id AND (m.deleted IS NULL OR m.deleted = false)
        WHERE vp.view_id = #{viewId} AND vp.permission_id = #{permissionId}
        AND (vp.deleted IS NULL OR vp.deleted = false)
    </select>

    <!-- 根据条件查询系统视图权限关系列表 -->
    <select id="getViewPermissionByFilter" resultMap="PermissionVOResultMap" parameterType="org.xyzh.common.dto.sys.TbSysViewPermissionDTO">
        SELECT
            vp.view_id, vp.permission_id,
            v.view_id, v.name AS view_name, v.parent_id AS view_parent_id, v.url AS view_url, v.component AS view_component,
            v.icon AS view_icon, v.type AS view_type, v.layout AS view_layout, v.order_num AS view_order_num, v.description AS view_description,
            p.permission_id, p.name AS permission_name, p.code AS permission_code, p.description AS permission_description, p.status AS permission_status,
            p.module_id, m.name AS module_name, m.description AS module_description,
            vp.optsn, vp.creator, vp.updater, vp.dept_path, vp.remark, vp.create_time, vp.update_time, vp.delete_time, vp.deleted
        FROM sys.tb_sys_view_permission vp
        LEFT JOIN sys.tb_sys_view v ON vp.view_id = v.view_id AND (v.deleted IS NULL OR v.deleted = false)
        LEFT JOIN sys.tb_sys_permission p ON vp.permission_id = p.permission_id AND (p.deleted IS NULL OR p.deleted = false)
        LEFT JOIN sys.tb_sys_module m ON p.module_id = m.module_id AND (m.deleted IS NULL OR m.deleted = false)
        <where>
            <if test="filter.viewId != null and filter.viewId != ''">
                AND vp.view_id = #{filter.viewId}
            </if>
            <if test="filter.permissionId != null and filter.permissionId != ''">
                AND vp.permission_id = #{filter.permissionId}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND vp.dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (vp.deleted IS NULL OR vp.deleted = false)
        </where>
        ORDER BY vp.create_time DESC
    </select>

    <!-- 根据条件查询系统视图权限关系分页列表 -->
    <select id="getViewPermissionPageByFilter" resultMap="PermissionVOResultMap">
        SELECT
            vp.view_id, vp.permission_id,
            v.view_id, v.name AS view_name, v.parent_id AS view_parent_id, v.url AS view_url, v.component AS view_component,
            v.icon AS view_icon, v.type AS view_type, v.layout AS view_layout, v.order_num AS view_order_num, v.description AS view_description,
            p.permission_id, p.name AS permission_name, p.code AS permission_code, p.description AS permission_description, p.status AS permission_status,
            p.module_id, m.name AS module_name, m.description AS module_description,
            vp.optsn, vp.creator, vp.updater, vp.dept_path, vp.remark, vp.create_time, vp.update_time, vp.delete_time, vp.deleted
        FROM sys.tb_sys_view_permission vp
        LEFT JOIN sys.tb_sys_view v ON vp.view_id = v.view_id AND (v.deleted IS NULL OR v.deleted = false)
        LEFT JOIN sys.tb_sys_permission p ON vp.permission_id = p.permission_id AND (p.deleted IS NULL OR p.deleted = false)
        LEFT JOIN sys.tb_sys_module m ON p.module_id = m.module_id AND (m.deleted IS NULL OR m.deleted = false)
        <where>
            <if test="filter.viewId != null and filter.viewId != ''">
                AND vp.view_id = #{filter.viewId}
            </if>
            <if test="filter.permissionId != null and filter.permissionId != ''">
                AND vp.permission_id = #{filter.permissionId}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND vp.dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (vp.deleted IS NULL OR vp.deleted = false)
        </where>
        ORDER BY vp.create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 根据条件查询系统视图权限关系数量 -->
    <select id="getViewPermissionCount" resultType="java.lang.Integer" parameterType="org.xyzh.common.dto.sys.TbSysViewPermissionDTO">
        SELECT COUNT(1)
        FROM sys.tb_sys_view_permission
        <where>
            <if test="filter.viewId != null and filter.viewId != ''">
                AND view_id = #{filter.viewId}
            </if>
            <if test="filter.permissionId != null and filter.permissionId != ''">
                AND permission_id = #{filter.permissionId}
            </if>
            <if test="filter.deptPath != null and filter.deptPath != ''">
                AND dept_path LIKE CONCAT(#{filter.deptPath}, '%')
            </if>
            AND (deleted IS NULL OR deleted = false)
        </where>
    </select>

</mapper>

