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

    <resultMap type="cn.fateverse.code.entity.Table" id="TableResult">
        <id property="tableId" column="table_id"/>
        <result property="dataSourceId" column="data_source_id"/>
        <result property="parentMenuId" column="parent_menu_id"/>
        <result property="dataSourceType" column="data_source_type"/>
        <result property="tableName" column="table_name"/>
        <result property="tableComment" column="table_comment"/>
        <result property="subTableName" column="sub_table_name"/>
        <result property="subTableFkName" column="sub_table_fk_name"/>
        <result property="className" column="class_name"/>
        <result property="tplCategory" column="tpl_category"/>
        <result property="packageName" column="package_name"/>
        <result property="serviceName" column="service_name"/>
        <result property="moduleName" column="module_name"/>
        <result property="businessName" column="business_name"/>
        <result property="functionName" column="function_name"/>
        <result property="functionAuthor" column="function_author"/>
        <result property="backTemplate" column="back_template"/>
        <result property="frontTemplate" column="front_template"/>
        <result property="options" column="options"/>
        <result property="optionApi" column="option_api"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <resultMap type="cn.fateverse.code.entity.TableColumn" id="TableColumnResult">
        <id property="columnId" column="column_id"/>
        <result property="tableId" column="table_id"/>
        <result property="columnName" column="column_name"/>
        <result property="columnComment" column="column_comment"/>
        <result property="columnType" column="column_type"/>
        <result property="javaType" column="java_type"/>
        <result property="javaField" column="java_field"/>
        <result property="isPk" column="is_pk"/>
        <result property="isIncrement" column="is_increment"/>
        <result property="isRequired" column="is_required"/>
        <result property="isInsert" column="is_insert"/>
        <result property="isEdit" column="is_edit"/>
        <result property="isList" column="is_list"/>
        <result property="isQuery" column="is_query"/>
        <result property="isRegular" column="is_regular"/>
        <result property="regular" column="regular"/>
        <result property="queryType" column="query_type"/>
        <result property="htmlType" column="html_type"/>
        <result property="dictType" column="dict_type"/>
        <result property="sort" column="sort"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap id="TableResultDto" type="cn.fateverse.code.entity.dto.TableDto" extends="TableResult">
        <collection property="columns" javaType="java.util.List" resultMap="TableColumnResult"/>
    </resultMap>

    <sql id="selectGenTable">
        select table_id,
               data_source_id,
               data_source_type,
               table_name,
               table_comment,
               sub_table_name,
               sub_table_fk_name,
               class_name,
               tpl_category,
               package_name,
               service_name,
               module_name,
               business_name,
               function_name,
               function_author,
               back_template,
               front_template,
               options,
               option_api,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from gen_table
    </sql>

    <select id="selectTableList" resultMap="TableResult">
        <include refid="selectGenTable"/>
        <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 like concat('%', #{dataSourceType}, '%')
            </if>
            <if test="startTime != null and startTime != ''"><!-- 开始时间检索 -->
                and create_time,'%y%m%d') &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                and create_time &lt;= #{endTime}
            </if>
        </where>
        order by create_time desc
    </select>

    <!--todo 获取表信息 预览代码使用-->
    <select id="selectTableDtoByTableId" parameterType="Long" resultMap="TableResultDto">
        SELECT gt.table_id,
               gt.data_source_id,
               gt.parent_menu_id,
               gt.data_source_type,
               gt.table_name,
               gt.table_comment,
               gt.sub_table_name,
               gt.sub_table_fk_name,
               gt.class_name,
               gt.tpl_category,
               gt.package_name,
               gt.service_name,
               gt.module_name,
               gt.business_name,
               gt.function_name,
               gt.function_author,
               gt.options,
               gt.option_api,
               gt.remark,
               gt.back_template,
               gt.front_template,
               gtc.column_id,
               gtc.column_name,
               gtc.column_comment,
               gtc.column_type,
               gtc.java_type,
               gtc.java_field,
               gtc.is_pk,
               gtc.is_increment,
               gtc.is_required,
               gtc.is_insert,
               gtc.is_edit,
               gtc.is_list,
               gtc.is_query,
               gtc.query_type,
               gtc.html_type,
               gtc.dict_type,
               gtc.sort,
               gtc.is_regular,
               gr.regular
        FROM gen_table gt
                 LEFT JOIN gen_table_column gtc ON gt.table_id = gtc.table_id
                 LEFT JOIN gen_regular gr ON gtc.is_regular = gr.id
        where gt.table_id = #{tableId}
        order by gtc.sort
    </select>
    <!--todo 获取表信息 生成代码使用-->
    <select id="selectTableByName" parameterType="String" resultMap="TableResultDto">
        SELECT gt.table_id,
               gt.data_source_id,
               gt.parent_menu_id,
               gt.data_source_type,
               gt.table_name,
               gt.table_comment,
               gt.sub_table_name,
               gt.sub_table_fk_name,
               gt.class_name,
               gt.tpl_category,
               gt.package_name,
               gt.service_name,
               gt.module_name,
               gt.business_name,
               gt.function_name,
               gt.function_author,
               gt.back_template,
               gt.front_template,
               gt.options,
               gt.option_api,
               gt.remark,
               gtc.column_id,
               gtc.column_name,
               gtc.column_comment,
               gtc.column_type,
               gtc.java_type,
               gtc.java_field,
               gtc.is_pk,
               gtc.is_increment,
               gtc.is_required,
               gtc.is_insert,
               gtc.is_edit,
               gtc.is_list,
               gtc.is_query,
               gtc.query_type,
               gtc.html_type,
               gtc.dict_type,
               gtc.sort,
               gtc.is_regular,
               gr.regular
        FROM gen_table gt
                 LEFT JOIN gen_table_column gtc ON gt.table_id = gtc.table_id
                 LEFT JOIN gen_regular gr ON gtc.is_regular = gr.id
        where gt.table_name = #{tableName}
        order by gtc.sort
    </select>

    <!-- todo 查询数据库行配置信息-->
    <select id="selectTableAll" parameterType="String" resultMap="TableResultDto">
        SELECT gt.table_id,
               gt.data_source_id,
               gt.parent_menu_id,
               gt.data_source_type,
               gt.table_name,
               gt.table_comment,
               gt.sub_table_name,
               gt.sub_table_fk_name,
               gt.class_name,
               gt.tpl_category,
               gt.package_name,
               gt.service_name,
               gt.module_name,
               gt.business_name,
               gt.function_name,
               gt.function_author,
               gt.options,
               gt.option_api,
               gt.remark,
               gt.back_template,
               gt.front_template,
               gtc.column_id,
               gtc.column_name,
               gtc.column_comment,
               gtc.column_type,
               gtc.java_type,
               gtc.java_field,
               gtc.is_pk,
               gtc.is_increment,
               gtc.is_required,
               gtc.is_insert,
               gtc.is_edit,
               gtc.is_list,
               gtc.is_query,
               gtc.query_type,
               gtc.html_type,
               gtc.dict_type,
               gtc.sort
        FROM gen_table gt
                 LEFT JOIN gen_table_column gtc ON gt.table_id = gtc.table_id
        order by gtc.sort
    </select>
    <select id="selectTableNameByDataSourceId" resultType="java.lang.String">
        select table_name
        from gen_table
        where data_source_id = #{dataSourceId}
    </select>

    <select id="selectCheckTableByDataSourceId" resultType="java.lang.Long">
        select count(*) from gen_table where data_source_id = #{dataSourceId} limit 1
    </select>

    <insert id="insertTable" useGeneratedKeys="true" keyProperty="tableId">
        insert into gen_table
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tableName != null">table_name,</if>
            <if test="dataSourceId != null">data_source_id,</if>
            <if test="parentMenuId != null">parent_menu_id,</if>
            <if test="dataSourceType != null ">data_source_type,</if>
            <if test="tableComment != null and tableComment != ''">table_comment,</if>
            <if test="className != null and className != ''">class_name,</if>
            <if test="tplCategory != null and tplCategory != ''">tpl_category,</if>
            <if test="packageName != null and packageName != ''">package_name,</if>
            <if test="moduleName != null and moduleName != ''">module_name,</if>
            <if test="serviceName != null and serviceName != ''">service_name,</if>
            <if test="businessName != null and businessName != ''">business_name,</if>
            <if test="functionName != null and functionName != ''">function_name,</if>
            <if test="functionAuthor != null and functionAuthor != ''">function_author,</if>
            <if test="backTemplate != null">back_template,</if>
            <if test="frontTemplate != null">front_template,</if>
            <if test="optionApi != null and optionApi != ''">option_api,</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="parentMenuId != null">#{parentMenuId},</if>
            <if test="dataSourceType != null ">#{dataSourceType},</if>
            <if test="tableComment != null and tableComment != ''">#{tableComment},</if>
            <if test="className != null and className != ''">#{className},</if>
            <if test="tplCategory != null and tplCategory != ''">#{tplCategory},</if>
            <if test="packageName != null and packageName != ''">#{packageName},</if>
            <if test="moduleName != null and moduleName != ''">#{moduleName},</if>
            <if test="serviceName != null and serviceName != ''">#{serviceName},</if>
            <if test="businessName != null and businessName != ''">#{businessName},</if>
            <if test="functionName != null and functionName != ''">#{functionName},</if>
            <if test="functionAuthor != null and functionAuthor != ''">#{functionAuthor},</if>
            <if test="backTemplate != null">#{backTemplate},</if>
            <if test="frontTemplate != null">#{frontTemplate},</if>
            <if test="optionApi != null and optionApi != ''">#{optionApi},</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="updateTable">
        update gen_table
        <set>
            <if test="parentMenuId != null">parent_menu_id = #{parentMenuId},</if>
            <if test="tableName != null and tableName != ''">table_name = #{tableName},</if>
            <if test="tableComment != null and tableComment != ''">table_comment = #{tableComment},</if>
            <if test="subTableName != null">sub_table_name = #{subTableName},</if>
            <if test="subTableFkName != null">sub_table_fk_name = #{subTableFkName},</if>
            <if test="className != null and className != ''">class_name = #{className},</if>
            <if test="functionAuthor != null and functionAuthor != ''">function_author = #{functionAuthor},</if>
            <if test="tplCategory != null and tplCategory != ''">tpl_category = #{tplCategory},</if>
            <if test="packageName != null and packageName != ''">package_name = #{packageName},</if>
            <if test="moduleName != null and moduleName != ''">module_name = #{moduleName},</if>
            <if test="serviceName != null and serviceName != ''">service_name = #{serviceName},</if>
            <if test="businessName != null and businessName != ''">business_name = #{businessName},</if>
            <if test="functionName != null and functionName != ''">function_name = #{functionName},</if>
            <if test="optionApi != null and optionApi != ''">option_api = #{optionApi},</if>
            <if test="options != null and options != ''">`options` = #{options},</if>
            <if test="backTemplate != null">back_template = #{backTemplate},</if>
            <if test="frontTemplate != null">front_template = #{frontTemplate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null and updateTime != ''">update_time = #{updateTime},</if>
        </set>
        where table_id = #{tableId}
    </update>

    <delete id="deleteTableByIds" parameterType="Long">
        delete from gen_table where table_id in
        <foreach collection="list" item="tableId" open="(" separator="," close=")">
            #{tableId}
        </foreach>
    </delete>

    <delete id="deleteTableById">
        delete
        from gen_table
        where table_id = #{tableId}
    </delete>


</mapper>
