<?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.ebts.generator.dao.TopSearchTableDao">
    
    <resultMap type="com.ebts.generator.entity.TopSearchTable" id="TopSearchTableResult">
        <result property="id"    column="id"    />
        <result property="tableName"    column="table_name"    />
        <result property="tableComment"    column="table_comment"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap id="TopSearchTableTopSearchColumnResult" type="com.ebts.generator.entity.TopSearchTable" extends="TopSearchTableResult">
        <collection property="topSearchColumnList" notNullColumn="id" javaType="java.util.List" resultMap="TopSearchColumnResult" />
        <collection property="relationalMapList" notNullColumn="id" javaType="java.util.List" resultMap="RelationalMappingResult" />
    </resultMap>

    <resultMap type="com.ebts.generator.entity.TopSearchColumn" id="TopSearchColumnResult">
        <result property="id"    column="column_id"    />
        <result property="searchTableId"    column="search_table_id"    />
        <result property="columnName"    column="column_name"    />
        <result property="columnComment"    column="column_comment"    />
        <result property="columnType"    column="column_type"    />
        <result property="isPk"    column="is_pk"    />
        <result property="isUse"    column="is_use"    />
        <result property="sort"    column="sort"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap type="com.ebts.generator.entity.RelationalMap" id="RelationalMappingResult">
        <result property="mainId"    column="main_id"    />
        <result property="childId"    column="child_id"    />
        <result property="mainKey"    column="main_key"    />
        <result property="childKey"    column="child_key"    />
        <result property="type"    column="type"    />
    </resultMap>



    <sql id="selectTopSearchTableVo">
        select id, table_name, table_comment, remark, create_by, create_time, update_by, update_time from gen_top_search_table
    </sql>

    <select id="selectTopSearchTableInfo" resultMap="TopSearchTableResult">
        select id, table_name ,table_comment from gen_top_search_table where 1=1
    </select>

    <select id="selectTableColumns" resultMap="TopSearchColumnResult" parameterType="Long">
        select column_name ,column_comment from gen_top_search_column where search_table_id = #{id}
    </select>


    <select id="selectTopSearchTableList" parameterType="TopSearchTable" resultMap="TopSearchTableResult">
        <include refid="selectTopSearchTableVo"/>
        <where>  
            <if test="tableName != null  and tableName != ''"> and table_name like concat('%', #{tableName}, '%')</if>
            <if test="tableComment != null  and tableComment != ''"> and table_comment like concat('%', #{tableComment}, '%')</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''"> and create_time between #{params.beginCreateTime} and #{params.endCreateTime}</if>
            <if test="params.beginUpdateTime != null and params.beginUpdateTime != '' and params.endUpdateTime != null and params.endUpdateTime != ''"> and update_time between #{params.beginUpdateTime} and #{params.endUpdateTime}</if>
        </where>
    </select>
    
    <select id="selectTopSearchTableById" parameterType="Long" resultMap="TopSearchTableTopSearchColumnResult">
        select gtst.id, gtst.table_name, gtst.table_comment, gtst.remark, gtst.create_by, gtst.create_time, gtst.update_by, gtst.update_time,
            gtsc.id as column_id, gtsc.search_table_id, gtsc.column_name, gtsc.column_comment, gtsc.column_type, gtsc.is_use, gtsc.create_by, gtsc.create_time, gtsc.update_by, gtsc.update_time,
            grm.main_id, grm.child_id, grm.main_key, grm.child_key, grm.type
        from gen_top_search_table gtst
            left join gen_top_search_column gtsc on gtsc.search_table_id = gtst.id
            left join gen_relational_mapping grm on grm.main_id = gtst.id
        where gtst.id = #{id}
    </select>

    <select id="selectTopSearchTables" resultMap="TopSearchTableTopSearchColumnResult">
        select gtst.id, gtst.table_name, gtst.table_comment, gtst.remark, gtst.create_by, gtst.create_time, gtst.update_by, gtst.update_time,
               gtsc.id as column_id, gtsc.search_table_id, gtsc.column_name, gtsc.column_comment, gtsc.column_type, gtsc.is_use, gtsc.create_by, gtsc.create_time, gtsc.update_by, gtsc.update_time,
               grm.main_id, grm.child_id, grm.main_key, grm.child_key, grm.type
        from gen_top_search_table gtst
                 left join gen_top_search_column gtsc on gtsc.search_table_id = gtst.id
                 left join gen_relational_mapping grm on grm.main_id = gtst.id
    </select>

    <!--AND table_name NOT LIKE 'gen_%'-->
    <select id="selectDbTableList" resultType="java.util.Map">
        select table_name, table_comment, create_time, update_time from information_schema.tables
        where table_schema = (select database())
        AND table_name NOT LIKE 'qrtz_%'
        AND table_name NOT LIKE 'gen_%'
        AND table_name NOT IN (select table_name from gen_top_search_table)
        <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="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
            AND date_format(create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
    </select>

    <select id="selectDbTableListByNames" resultMap="TopSearchTableResult">
        select table_name, table_comment, create_time, update_time from information_schema.tables
        where table_name NOT LIKE 'qrtz_%'
            and table_name NOT LIKE 'gen_%'
            and table_schema = (select database())
            and table_name in
        <foreach collection="array" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
    </select>
    <select id="selectDbTableColumnsByName" parameterType="String" resultMap="TopSearchColumnResult">
        select column_name, (case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment, column_type
        from information_schema.columns where table_schema = (select database()) and table_name = (#{tableName})
        order by ordinal_position
    </select>
        
    <insert id="insertTopSearchTable" parameterType="TopSearchTable" useGeneratedKeys="true" keyProperty="id">
        insert into gen_top_search_table
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tableName != null">table_name,</if>
            <if test="tableComment != null">table_comment,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tableName != null">#{tableName},</if>
            <if test="tableComment != null">#{tableComment},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
         </trim>
    </insert>

    <update id="updateTopSearchTable" parameterType="TopSearchTable">
        update gen_top_search_table
        <trim prefix="SET" suffixOverrides=",">
            <if test="tableName != null">table_name = #{tableName},</if>
            <if test="tableComment != null">table_comment = #{tableComment},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTopSearchTableById" parameterType="Long">
        delete from gen_top_search_table where id = #{id}
    </delete>

    <delete id="deleteTopSearchTableByIds" parameterType="String">
        delete from gen_top_search_table where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteTopSearchColumnBySearchTableIds" parameterType="String">
        delete from gen_top_search_column where search_table_id in 
        <foreach item="searchTableId" collection="array" open="(" separator="," close=")">
            #{searchTableId}
        </foreach>
    </delete>

    <delete id="deleteTopSearchColumnBySearchTableId" parameterType="Long">
        delete from gen_top_search_column where search_table_id = #{searchTableId}
    </delete>

    <insert id="batchTopSearchColumn">
        insert into gen_top_search_column(search_table_id,column_name,column_comment,column_type,create_by,create_time,update_by,update_time) values
		<foreach item="item" index="index" collection="list" separator=",">
            (#{item.searchTableId},#{item.columnName},#{item.columnComment},#{item.columnType},#{item.createBy},#{item.createTime},#{item.updateBy},#{item.updateTime})
        </foreach>
    </insert>
</mapper>