<?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.usky.data.mapper.DataLinkMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.data.domain.DataLink">
        <id column="id" property="id" />
        <result column="full_name" property="fullName" />
        <result column="db_type" property="dbType" />
        <result column="host" property="host" />
        <result column="port" property="port" />
        <result column="db_name" property="dbName" />
        <result column="password" property="password" />
        <result column="service_name" property="serviceName" />
        <result column="description" property="description" />
        <result column="db_schema" property="dbSchema" />
        <result column="table_space" property="tableSpace" />
        <result column="oracle_param" property="oracleParam" />
        <result column="oracle_extend" property="oracleExtend" />
        <result column="sort_code" property="sortCode" />
        <result column="create_time" property="createTime" />
        <result column="create_by" property="createBy" />
        <result column="update_time" property="updateTime" />
        <result column="update_by" property="updateBy" />
        <result column="delete_time" property="deleteTime" />
        <result column="delete_by" property="deleteBy" />
        <result column="is_delete" property="isDelete" />
        <result column="tenant_id" property="tenantId" />
    </resultMap>

    <!-- 根据连接名称统计数量（排除指定ID） -->
    <select id="countByFullName" resultType="int">
        SELECT COUNT(*)
        FROM data_link
        WHERE full_name = #{fullName}
        AND is_delete = 0
        <if test="id != null">
            AND id != #{id}
        </if>
    </select>

    <!-- 分页查询数据源列表 -->
    <select id="selectPageList" resultMap="BaseResultMap">
        SELECT *
        FROM data_link
        WHERE is_delete = 0
        <if test="keyword != null and keyword != ''">
            AND (full_name LIKE CONCAT('%', #{keyword}, '%')
            OR host LIKE CONCAT('%', #{keyword}, '%')
            OR db_name LIKE CONCAT('%', #{keyword}, '%'))
        </if>
        ORDER BY create_time DESC
        LIMIT #{startIndex}, #{pageSize}
    </select>

</mapper>
