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

    <sql id="uniQueryWhere">
        <where>
            1 = 1
            <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('%', #{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('%', #{uniCon.ucMock}, '%')</if>
                    <if test="uniCon.ucCon == 'BETWEEN'">and ${uniCon.ucKey} between #{uniCon.begin} and #{uniCon.end}
                    </if>
                </if>
            </foreach>
        </where>
    </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}
        <include refid="uniQueryWhere"/>
        limit #{page.startNum},#{page.endNum}
    </select>

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

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

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


    <select id="uniQuerySql" resultType="java.util.Map">
        ${select.selectSql}
        <include refid="uniQueryWhere"/>
        <if test="page != null">
            limit #{page.startNum},#{page.endNum}
        </if>
    </select>

    <select id="uniQuerySqlCount" resultType="java.lang.Long">
        ${select.selectSql}
        <include refid="uniQueryWhere"/>
    </select>

    <select id="selectTableList" resultMap="cn.fateverse.query.mapper.DynamicDataSourceMapper.DynamicTableResult">
        select table_name, table_comment, create_time, update_time
        from information_schema.tables
        where table_schema = (select database())
        <if test="table.tableName != null and table.tableName != ''">
            and table_name like concat('%', #{query.tableName}, '%')
        </if>
        <if test="table.tableComment != null and table.tableComment != ''">
            and table_comment like concat('%', #{table.tableComment}, '%')
        </if>
        <if test="table.startTime != null and table.endTime != null"><!-- 开始时间检索 -->
            and create_time between #{table.startTime} and #{table.endTime}
        </if>
        <if test="list !=null and list.size!=0">
            and table_name not in
            <foreach collection="list" item="name" separator="," open="(" close=")">
                #{name}
            </foreach>
        </if>
        limit #{page.startNum},#{page.endNum}
    </select>

    <select id="selectTableListCount" resultType="java.lang.Long">
        select count(1)
        from information_schema.tables
        where table_schema = (select database())
        <if test="table.tableName != null and table.tableName != ''">
            and table_name like concat('%', #{table.tableName}, '%')
        </if>
        <if test="table.tableComment != null and table.tableComment != ''">
            and table_comment like concat('%', #{table.tableComment}, '%')
        </if>
        <if test="table.startTime != null and table.endTime != null"><!-- 开始时间检索 -->
            and create_time between #{table.startTime} and #{table.endTime}
        </if>
        <if test="list !=null and list.size!=0">
            and table_name not in
            <foreach collection="list" item="name" separator="," open="(" close=")">
                #{name}
            </foreach>
        </if>
    </select>

    <select id="selectDynamicTableListByNameList"
            resultMap="cn.fateverse.query.mapper.DynamicDataSourceMapper.TableResult">
        select table_name, table_comment, create_time, update_time, 'MYSQL' as data_source_type
        from information_schema.tables
        where table_schema = (select database())
        and table_name in
        <foreach collection="list" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
    </select>

    <select id="selectDynamicTableColumnsByNameList"
            resultMap="cn.fateverse.query.mapper.DynamicDataSourceMapper.TableColumnResult">
        select table_name as remark, column_name, (case when column_key = 'PRI' then 1 else 0 end) as pk,
        ordinal_position as sort, column_comment, column_type
        from information_schema.columns
        where table_schema = (select database()) and table_name in
        <foreach collection="list" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
        order by ordinal_position
    </select>

</mapper>