<?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.TbSysViewMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbSysViewDTO">
        <!-- 视图字段 -->
        <id column="view_id" property="viewId" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="parent_id" property="parentId" jdbcType="VARCHAR"/>
        <result column="url" property="url" jdbcType="VARCHAR"/>
        <result column="component" property="component" jdbcType="VARCHAR"/>
        <result column="icon" property="icon" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="view_type" property="viewType" jdbcType="VARCHAR"/>
        <result column="iframe_url" property="iframeUrl" jdbcType="VARCHAR"/>
        <result column="service" property="service" jdbcType="VARCHAR"/>
        <result column="layout" property="layout" jdbcType="VARCHAR"/>
        <result column="order_num" property="orderNum" jdbcType="INTEGER"/>
        <result column="description" property="description" 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>

    <!-- 基础列 -->
    <sql id="Base_Column_List">
        view_id, name, parent_id, url, component, icon, type, view_type, iframe_url, service, layout, order_num, description,
        optsn, creator, updater, dept_path, remark, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 插入系统视图（必填 + 可选字段动态列，仅修改 insert） -->
    <insert id="insertView" parameterType="org.xyzh.common.dto.sys.TbSysViewDTO">
        INSERT INTO sys.tb_sys_view
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 必填字段：view_id, name, type, layout, order_num, optsn -->
            view_id,
            name,
            type,
            layout,
            order_num,
            optsn,
            <!-- 可选字段：parent_id, url, component, icon, view_type, iframe_url, service, description 及基础字段 -->
            <if test="parentId != null and parentId != ''">parent_id,</if>
            <if test="url != null and url != ''">url,</if>
            <if test="component != null and component != ''">component,</if>
            <if test="icon != null and icon != ''">icon,</if>
            <if test="viewType != null and viewType != ''">view_type,</if>
            <if test="iframeUrl != null and iframeUrl != ''">iframe_url,</if>
            <if test="service != null and service != ''">service,</if>
            <if test="description != null and description != ''">description,</if>
            <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},
            #{name},
            #{type},
            #{layout},
            #{orderNum},
            #{optsn},
            <!-- 可选字段对应的值 -->
            <if test="parentId != null and parentId != ''">#{parentId},</if>
            <if test="url != null and url != ''">#{url},</if>
            <if test="component != null and component != ''">#{component},</if>
            <if test="icon != null and icon != ''">#{icon},</if>
            <if test="viewType != null and viewType != ''">#{viewType},</if>
            <if test="iframeUrl != null and iframeUrl != ''">#{iframeUrl},</if>
            <if test="service != null and service != ''">#{service},</if>
            <if test="description != null and description != ''">#{description},</if>
            <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="updateView" parameterType="org.xyzh.common.dto.sys.TbSysViewDTO">
        UPDATE sys.tb_sys_view
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            <if test="url != null">
                url = #{url},
            </if>
            <if test="component != null">
                component = #{component},
            </if>
            <if test="icon != null">
                icon = #{icon},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="viewType != null">
                view_type = #{viewType},
            </if>
            <if test="iframeUrl != null">
                iframe_url = #{iframeUrl},
            </if>
            <if test="service != null">
                service = #{service},
            </if>
            <if test="layout != null">
                layout = #{layout},
            </if>
            <if test="orderNum != null">
                order_num = #{orderNum},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            <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}
        <if test="deleted != null">
            AND deleted = #{deleted}
        </if>
    </update>

    <!-- 删除系统视图 -->
    <update id="deleteView" parameterType="org.xyzh.common.dto.sys.TbSysViewDTO">
        UPDATE sys.tb_sys_view
        SET deleted = true,
            delete_time = NOW()
        WHERE view_id = #{viewId}
    </update>

    <!-- 根据ID查询系统视图 -->
    <select id="getViewById" resultMap="BaseResultMap" parameterType="java.lang.String">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_view
        WHERE view_id = #{viewId}
        AND (deleted IS NULL OR deleted = false)
    </select>

    <!-- 根据条件查询系统视图列表 -->
    <select id="getViewByFilter" resultMap="BaseResultMap" parameterType="org.xyzh.common.dto.sys.TbSysViewDTO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_view
        <where>
            <if test="filter.viewId != null and filter.viewId != ''">
                AND view_id = #{filter.viewId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.parentId != null and filter.parentId != ''">
                AND parent_id = #{filter.parentId}
            </if>
            <if test="filter.type != null">
                AND type = #{filter.type}
            </if>
            <if test="filter.viewType != null and filter.viewType != ''">
                AND view_type = #{filter.viewType}
            </if>
            <if test="filter.service != null and filter.service != ''">
                AND service = #{filter.service}
            </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>
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 根据条件查询系统视图分页列表 -->
    <select id="getViewPageByFilter" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sys.tb_sys_view
        <where>
            <if test="filter.viewId != null and filter.viewId != ''">
                AND view_id = #{filter.viewId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.parentId != null and filter.parentId != ''">
                AND parent_id = #{filter.parentId}
            </if>
            <if test="filter.type != null">
                AND type = #{filter.type}
            </if>
            <if test="filter.viewType != null and filter.viewType != ''">
                AND view_type = #{filter.viewType}
            </if>
            <if test="filter.service != null and filter.service != ''">
                AND service = #{filter.service}
            </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>
        ORDER BY order_num ASC, create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 根据条件查询系统视图数量 -->
    <select id="getViewCount" resultType="java.lang.Integer" parameterType="org.xyzh.common.dto.sys.TbSysViewDTO">
        SELECT COUNT(1)
        FROM sys.tb_sys_view
        <where>
            <if test="filter.viewId != null and filter.viewId != ''">
                AND view_id = #{filter.viewId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.parentId != null and filter.parentId != ''">
                AND parent_id = #{filter.parentId}
            </if>
            <if test="filter.type != null">
                AND type = #{filter.type}
            </if>
            <if test="filter.viewType != null and filter.viewType != ''">
                AND view_type = #{filter.viewType}
            </if>
            <if test="filter.service != null and filter.service != ''">
                AND service = #{filter.service}
            </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>

