<?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="cn.fateverse.query.mapper.dynamic.OracleDynamicDataSourceMapper">

    <sql id="uniQueryWhere">
        <foreach collection="select.uniCons" item="uniCon">
            <if test="uniCon.ucMock != null and uniCon.ucMock != '' and uniCon.ucType == 'input'">
                <if test="uniCon.ucCon == 'EQ'">and ${uniCon.ucKey} = #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'NE'">and ${uniCon.ucKey} != #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'GT'">and ${uniCon.ucKey} > #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'GTE'">and ${uniCon.ucKey} >= #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'LT'">and ${uniCon.ucKey} &lt; #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'LTE'">and ${uniCon.ucKey} &lt;= #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'LIKE'">and ${uniCon.ucKey} like concat(concat('%', #{uniCon.ucMock}),
                    '%')
                </if>
            </if>
            <if test="uniCon.ucMock != null and uniCon.ucType != 'input'">
                <if test="uniCon.ucCon == 'EQ'">and ${uniCon.ucKey} = #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'NE'">and ${uniCon.ucKey} != #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'GT'">and ${uniCon.ucKey} > #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'GTE'">and ${uniCon.ucKey} >= #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'LT'">and ${uniCon.ucKey} &lt; #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'LTE'">and ${uniCon.ucKey} &lt;= #{uniCon.ucMock}</if>
                <if test="uniCon.ucCon == 'LIKE'">and ${uniCon.ucKey} like concat(concat('%', #{uniCon.ucMock}),
                    '%')
                </if>
                <if test="uniCon.ucCon == 'BETWEEN'">and ${uniCon.ucKey} between #{uniCon.begin} and #{uniCon.end}
                </if>
            </if>
        </foreach>
    </sql>

    <select id="checkSource" resultType="java.lang.Integer">
        select 1 from dual
    </select>

    <select id="uniSingleQueryTopology" resultType="java.util.Map">
        select ${select.selectKey} from ${select.tableInfo}
        <where>
            rownum between #{page.startNum} and #{page.endNum}
            <include refid="uniQueryWhere"/>
        </where>
    </select>

    <select id="uniSingleQueryTopologyCount" resultType="java.lang.Long">
        select count(1) from ${select.tableInfo}
        <where>
            <include refid="uniQueryWhere"/>
        </where>
    </select>


    <select id="uniMultiListQueryTopology" resultType="java.util.Map">
        select ${select.selectKey} from ${select.tableInfo}
        <foreach collection="select.relevance" item="item">
            ${item}
        </foreach>
        <where>
            rownum between #{page.startNum} and #{page.endNum}
            <include refid="uniQueryWhere"/>
        </where>
    </select>

    <select id="uniMultiListQueryTopologyCount" resultType="java.lang.Long">
        select count(1) from ${select.tableInfo}
        <foreach collection="select.relevance" item="item">
            ${item}
        </foreach>
        <where>
            <include refid="uniQueryWhere"/>
        </where>
    </select>

    <select id="selectTableList" resultMap="cn.fateverse.query.mapper.DynamicDataSourceMapper.DynamicTableResult">
        select ut.table_name,utc.comments as table_comment,ut.last_analyzed as update_time
        from user_tables ut
        left join user_tab_comments utc on ut.table_name = utc.table_name
        <where>
            rownum between #{page.startNum} and #{page.endNum}
            <if test="table.tableName != null and table.tableName != ''">
                and  ut.table_name like  concat(concat('%', #{table.tableName}), '%')
            </if>
            <if test="table.tableComment != null and table.tableComment != ''">
                and  utc.comments like  concat(concat('%', #{table.tableComment}, '%'))
            </if>
            <if test="table.startTime != null and table.endTime != null"><!-- 开始时间检索 -->
                and update_time between #{table.startTime} and #{table.endTime}
            </if>
            <if test="list !=null and list.size!=0">
                and  ut.table_name not in
                <foreach collection="list" item="name" separator="," open="(" close=")">
                     #{name}
                </foreach>
            </if>
        </where>
</select>

    <select id="selectTableListCount" resultType="java.lang.Long">
        select count(1)
        from user_tables ut
            left join user_tab_comments utc on ut.table_name = utc.table_name
        <where>
            <if test="table.tableName != null and table.tableName != ''">
                and  ut.table_name like  concat(concat('%', #{table.tableName}), '%')
            </if>
            <if test="table.tableComment != null and table.tableComment != ''">
                and  utc.comments) like  concat(concat('%', #{table.tableComment}), '%')
            </if>
            <if test="table.startTime != null and table.endTime != null"><!-- 开始时间检索 -->
                and update_time between #{table.startTime} and #{table.endTime}
            </if>
            <if test="list !=null and list.size!=0">
                and  ut.table_name not in
                <foreach collection="list" item="name" separator="," open="(" close=")">
                     #{name}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectDynamicTableListByNameList"
            resultMap="cn.fateverse.query.mapper.DynamicDataSourceMapper.TableResult">
        select ut.table_name,utc.comments as table_comment,ut.last_analyzed as update_time,'ORACLE' as data_source_type
        from user_tables ut
        left join user_tab_comments utc on ut.table_name = utc.table_name
        where  ut.table_name in
        <foreach collection="list" item="name" separator="," open="(" close=")">
             #{name}
        </foreach>
    </select>

    <select id="selectDynamicTableColumnsByNameList"
            resultMap="cn.fateverse.query.mapper.DynamicDataSourceMapper.TableColumnResult">
        select distinct utc.column_name,
            utc.table_name as remark,
            CONCAT(CONCAT(CONCAT(lower(utc.data_type),'('), utc.data_length),')') as column_type,
            utc.data_scale as column_scale,
            utc.column_id as sort,
            (case when uc.constraint_type = 'P' then '1' else '0' end) as pk ,
            ucc.comments as column_comment
        from user_tab_columns utc
            inner join user_col_comments ucc on ucc.column_name = utc.column_name and ucc.table_name = utc.table_name
            left join user_cons_columns uccs on uccs.column_name = utc.column_name
            left join user_constraints uc on uc.constraint_name = uccs.constraint_name
        where uc.search_condition is null
            and utc.table_name in
        <foreach collection="list" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
        order by utc.column_id
    </select>

</mapper>