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

    <sql id="tableSql">
        select table_id, data_source_id, data_source_type, table_name, table_comment, create_by, create_time, update_by, update_time
        from query_table
    </sql>

    <select id="selectById" resultType="cn.fateverse.query.entity.Table">
        <include refid="tableSql"/>
        where table_id = #{tableId}
    </select>

    <select id="selectTableNameByDataSourceId" resultType="java.lang.String">
        select table_name
        from query_table
        where data_source_id = #{dataSourceId}
    </select>

    <select id="selectListByDataSourceId" resultType="cn.fateverse.query.entity.Table">
        <include refid="tableSql"/>
        where data_source_id = #{dataSourceId}
    </select>

    <select id="selectList" resultType="cn.fateverse.query.entity.Table">
        <include refid="tableSql"/>
        <where>
            <if test="dataSourceId != null ">
                and data_source_id = #{dataSourceId}
            </if>
            <if test="tableName != null and tableName != ''">
                and lower(`table_name`) like lower(concat('%', #{tableName}, '%'))
            </if>
            <if test="tableComment != null and tableComment != ''">
                and lower(table_comment) like lower(concat('%', #{tableComment}, '%'))
            </if>
            <if test="dataSourceType != null">
                and data_source_type = #{dataSourceType}
            </if>
            <if test="startTime != null and endTime != null">
                and create_time between #{startTime} and #{endTime}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectCountByDataSourceIds" resultType="cn.fateverse.query.entity.DataSourceManageCount">
        SELECT dsm.id as data_source_id,COUNT(*) as count, dsm.ds_name as data_source_name
        FROM query_table qt
            LEFT JOIN data_source_manage dsm on qt.data_source_id = dsm.id
        where dsm.id in
        <foreach collection="list" item="dataSourceId" open="(" separator="," close=")">
            #{dataSourceId}
        </foreach>
        GROUP BY dsm.id
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="tableId" keyColumn="table_id">
        insert into query_table
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tableName != null">table_name,</if>
            <if test="dataSourceId != null">data_source_id,</if>
            <if test="dataSourceType != null ">data_source_type,</if>
            <if test="tableComment != null and tableComment != ''">table_comment,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tableName != null">#{tableName},</if>
            <if test="dataSourceId != null">#{dataSourceId},</if>
            <if test="dataSourceType != null ">#{dataSourceType},</if>
            <if test="tableComment != null and tableComment != ''">#{tableComment},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="update">
        update query_table
        <set>
            <if test="tableName != null and tableName != ''">table_name = #{tableName},</if>
            <if test="tableComment != null and tableComment != ''">table_comment = #{tableComment},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </set>
        where table_id = #{tableId}
    </update>

    <delete id="deleteById">
        delete from query_table where table_id = #{tableId}
    </delete>

    <delete id="deleteByIds">
        delete from query_table where table_id in
        <foreach collection="array" item="tableId" open="(" separator="," close=")">
            #{tableId}
        </foreach>
    </delete>

</mapper>