<?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.ruoyi.system.mapper.DragTableMapper">
    
    <resultMap type="com.ruoyi.system.entity.DragTable" id="DragTableResult">
        <result property="tId"    column="t_id"    />
        <result property="dtName"    column="dt_name"    />
        <result property="dtNickname"    column="dt_nickname"    />
        <result property="tableKey"    column="table_key"    />
        <result property="sqlKey"    column="sql_key"    />
        <result property="dtTableName"    column="dt_table_name"    />
        <result property="dtNotes"    column="dt_notes"    />
        <result property="dtColumnName"    column="dt_column_name"    />
        <result property="timeFormat" column="time_format"/>
        <result property="spare"    column="spare"    />
        <result property="spare1"    column="spare1"    />
        <result property="delFlag"    column="del_flag"    />
        <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="isSelection"   column="is_selection"/>
        <result property="echoData"      column="echo_data"/>
        <result property="menuId" column="menu_id"/>
        <result property="primaryKey" column="primary_key"/>
    </resultMap>

    <resultMap type="com.ruoyi.system.entity.vo.DragTableVo" id="DragTableVoResult">
        <result property="tId"    column="t_id"/>
        <result property="dtName"    column="dt_name"    />
        <result property="dtNickname"    column="dt_nickname"    />
        <result property="tableKey"    column="table_key"    />
        <result property="sqlKey"    column="sql_key"    />
        <result property="dtTableName"    column="dt_table_name"    />
        <result property="dtColumnNameEcho"    column="dt_column_name"    />
        <result property="timeFormat" column="time_format"/>
        <result property="isSelection"   column="is_selection"/>
        <result property="tableSql" column="table_sql"/>
        <result property="tableExportFieldEcho" column="table_export_field"/>
        <result property="orderByColumn" column="order_by_column"/>
        <result property="echoData"      column="echo_data"/>
        <result property="menuId" column="menu_id"/>
        <result property="primaryKey" column="primary_key"/>
    </resultMap>

    <sql id="selectDragTableVo">
        select t_id, dt_name, dt_nickname, table_key, sql_key, dt_table_name,time_format, dt_notes, dt_column_name, spare, spare1, del_flag, create_by, create_time, update_by, update_time, is_selection,echo_data,menu_id,primary_key from drag_table
    </sql>

    <select id="selectDragTableList" parameterType="com.ruoyi.system.entity.DragTable" resultMap="DragTableResult">
        <include refid="selectDragTableVo"/>
        <where>
            <if test="dtName != null  and dtName != ''">and dt_name like concat('%', #{dtName}, '%')</if>
            <if test="dtNickname != null  and dtNickname != ''">and dt_nickname like concat('%', #{dtNickname}, '%')
            </if>
            <if test="tableKey != null  and tableKey != ''">and table_key = #{tableKey}</if>
            <if test="sqlKey != null  and sqlKey != ''">and sql_key = #{sqlKey}</if>
            <if test="dtTableName != null  and dtTableName != ''">and dt_table_name like concat('%', #{dtTableName},
                '%')
            </if>
            <if test="dtNotes != null  and dtNotes != ''">and dt_notes = #{dtNotes}</if>
            <if test="dtColumnName != null  and dtColumnName != ''">and dt_column_name like concat('%', #{dtColumnName},
                '%')
            </if>
            <if test="spare != null  and spare != ''">and spare = #{spare}</if>
            <if test="spare1 != null  and spare1 != ''">and spare1 = #{spare1}</if>
            <if test="dtType !=null and  dtType!=''">and dt_type=#{dtType}</if>
        </where>
        ORDER BY t_id DESC
    </select>
    
    <select id="selectDragTableByTId" parameterType="Long" resultMap="DragTableResult">
        <include refid="selectDragTableVo"/>
        where t_id = #{tId}
    </select>
        
    <insert id="insertDragTable" parameterType="com.ruoyi.system.entity.DragTable" useGeneratedKeys="true" keyProperty="tId">
        insert into drag_table
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dtName != null">dt_name,</if>
            <if test="dtNickname != null">dt_nickname,</if>
            <if test="tableKey != null">table_key,</if>
            <if test="sqlKey != null">sql_key,</if>
            <if test="dtType != null">dt_type,</if>
            <if test="dtTableName != null">dt_table_name,</if>
            <if test="dtNotes != null">dt_notes,</if>
            <if test="dtColumnName != null">dt_column_name,</if>
            <if test="timeFormat != null">time_format,</if>
            <if test="spare != null">spare,</if>
            <if test="spare1 != null">spare1,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="isSelection != null">is_selection,</if>
            <if test="echoData != null">echo_data,</if>
            <if test="menuId != null">menu_id,</if>
            <if test="primaryKey != null">primary_key,</if>
            <if test="createById != null">create_by_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dtName != null">#{dtName},</if>
            <if test="dtNickname != null">#{dtNickname},</if>
            <if test="tableKey != null">#{tableKey},</if>
            <if test="sqlKey != null">#{sqlKey},</if>
            <if test="dtType != null">#{dtType},</if>
            <if test="dtTableName != null">#{dtTableName},</if>
            <if test="dtNotes != null">#{dtNotes},</if>
            <if test="dtColumnName != null">#{dtColumnName},</if>
            <if test="timeFormat != null">#{timeFormat},</if>
            <if test="spare != null">#{spare},</if>
            <if test="spare1 != null">#{spare1},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="isSelection != null">#{isSelection},</if>
            <if test="echoData != null">#{echoData},</if>
            <if test="menuId != null">#{menuId},</if>
            <if test="primaryKey != null">#{primaryKey},</if>
            <if test="createById != null">#{createById},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateDragTable" parameterType="com.ruoyi.system.entity.DragTable">
        update drag_table
        <trim prefix="SET" suffixOverrides=",">
            <if test="dtName != null">dt_name = #{dtName},</if>
            <if test="dtNickname != null">dt_nickname = #{dtNickname},</if>
            <if test="tableKey != null">table_key = #{tableKey},</if>
            <if test="sqlKey != null">sql_key = #{sqlKey},</if>
            <if test="dtType != null">dt_type = #{dtType},</if>
            <if test="dtTableName != null">dt_table_name = #{dtTableName},</if>
            <if test="dtNotes != null">dt_notes = #{dtNotes},</if>
            <if test="dtColumnName != null">dt_column_name = #{dtColumnName},</if>
            <if test="timeFormat != null">time_format = #{timeFormat},</if>
            <if test="spare != null">spare = #{spare},</if>
            <if test="spare1 != null">spare1 = #{spare1},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createById != null">create_by_id = #{createById},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateById != null">update_by_id = #{updateById},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isSelection != null">is_selection = #{isSelection},</if>
            <if test="echoData != null">echo_data = #{echoData},</if>
            <if test="menuId != null">menu_id = #{menuId},</if>
            <if test="primaryKey != null">primary_key = #{primaryKey},</if>
        </trim>
        where t_id = #{tId}
    </update>

    <delete id="deleteDragTableByTId" parameterType="Long">
        delete from drag_table where t_id = #{tId}
    </delete>

    <delete id="deleteDragTableByTIds" parameterType="String">
        delete from drag_table where t_id in 
        <foreach item="tId" collection="list" open="(" separator="," close=")">
            #{tId}
        </foreach>
    </delete>

    <delete id="deleteDragTableByTableKeys" parameterType="list" >
        delete from drag_table where table_key in
        <foreach item="tableKey" collection="list" open="(" separator="," close=")">
            #{tableKey}
        </foreach>
    </delete>

    <select id="selectDragTableBySqlKey" parameterType="String" resultMap="DragTableResult">
        <include refid="selectDragTableVo"/>
        where sql_key = #{sqlKey}
    </select>

    <select id="selectDragTableVoByTId" resultMap="DragTableVoResult">
        SELECT
            dt.*,
            ts.table_sql,
            ts.table_export_field FROM drag_table dt
                LEFT JOIN table_sql ts ON dt.sql_key = ts.sql_key
        WHERE
            dt.t_id = #{tId}
    </select>

    <select id="selectDragTableByTableKey" parameterType="String" resultMap="DragTableResult">
        <include refid="selectDragTableVo"/>
        where table_key = #{tableKey}
    </select>

    <select id="selectDragTableTableKeys" parameterType="list" resultMap="DragTableVoResult">
        SELECT
        dt.*,
        ts.table_sql,
        ts.table_export_field,
        ts.order_by_column
        FROM drag_table dt
        LEFT JOIN table_sql ts ON dt.sql_key = ts.sql_key
        where  table_key in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="selectDragTableTableKeysOrderby" parameterType="list" resultMap="DragTableVoResult">
        SELECT
        dt.*,
        ts.table_sql,
        ts.table_export_field,
        ts.order_by_column
        FROM drag_table dt
        LEFT JOIN table_sql ts ON dt.sql_key = ts.sql_key
        where  table_key in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        order by field
        (table_key,
        <foreach collection="list" index="index" item="item" separator=","  close=")">
            #{item}
        </foreach>
    </select>
</mapper>