<?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.zkqy.system.mapper.TableSqlMapper">

    <resultMap type="com.zkqy.system.entity.TableSql" id="TableSqlResult">
        <result property="tId" column="t_id"/>
        <result property="tableSql" column="table_sql"/>
        <result property="tableCondition" column="table_condition"/>
        <result property="tableAlias" column="table_alias"/>
        <result property="sqlKey" column="sql_key"/>
        <result property="tableExportField" column="table_export_field"/>
        <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="delFlag" column="del_flag"/>
        <result property="orderByColumn" column="order_by_column"/>
        <result property="sortOrder" column="is_asc"/>
    </resultMap>

    <sql id="selectTableSqlVo">
        select t_id,
               table_sql,
               table_condition,
               table_alias,
               sql_key,
               table_export_field,
               create_by,
               create_time,
               update_by,
               update_time,
               del_flag,
               order_by_column,
               is_asc
        from table_sql
    </sql>

    <select id="selectTableSqlList" parameterType="com.zkqy.system.entity.TableSql" resultMap="TableSqlResult">
        <include refid="selectTableSqlVo"/>
        <where>
            <if test="tableSql != null  and tableSql != ''">and table_sql = #{tableSql}</if>
            <if test="tableCondition != null  and tableCondition != ''">and table_condition = #{tableCondition}</if>
            <if test="tableAlias != null  and tableAlias != ''">and table_alias = #{tableAlias}</if>
            <if test="sqlKey != null  and sqlKey != ''">and sql_key = #{sqlKey}</if>
        </where>
    </select>

    <select id="selectTableSqlByTId" parameterType="Long" resultMap="TableSqlResult">
        <include refid="selectTableSqlVo"/>
        where t_id = #{tId}
    </select>

    <select id="selectTableSqlByTSqlKey" parameterType="String" resultMap="TableSqlResult">
        <include refid="selectTableSqlVo"/>
        where sql_key = #{SQLKEY}
    </select>


    <insert id="insertTableSql" parameterType="com.zkqy.system.entity.TableSql" useGeneratedKeys="true"
            keyProperty="tId">
        insert into table_sql
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tableSql != null">table_sql,</if>
            <if test="tableCondition != null">table_condition,</if>
            <if test="tableAlias != null">table_alias,</if>
            <if test="sqlKey != null">sql_key,</if>
            <if test="tableExportField != null">table_export_field,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="orderByColumn != null">order_by_column,</if>
            <if test="sortOrder != null">is_asc,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tableSql != null">#{tableSql},</if>
            <if test="tableCondition != null">#{tableCondition},</if>
            <if test="tableAlias != null">#{tableAlias},</if>
            <if test="sqlKey != null">#{sqlKey},</if>
            <if test="tableExportField != null">#{tableExportField},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="orderByColumn != null">#{orderByColumn},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
        </trim>
    </insert>

    <update id="updateTableSql" parameterType="com.zkqy.system.entity.TableSql">
        update table_sql
        <trim prefix="SET" suffixOverrides=",">
            <if test="tableSql != null">table_sql = #{tableSql},</if>
            <if test="tableCondition != null">table_condition = #{tableCondition},</if>
            <if test="tableAlias != null">table_alias = #{tableAlias},</if>
            <if test="sqlKey != null">sql_key = #{sqlKey},</if>
            <if test="tableExportField != null">table_export_field = #{tableExportField},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="orderByColumn != null">order_by_column = #{orderByColumn},</if>
            <if test="sortOrder != null">is_asc = #{sortOrder},</if>
        </trim>
        where t_id = #{tId}
    </update>

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

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

    <update id="updateTableSqlBySqlKey">
        update table_sql
        <trim prefix="SET" suffixOverrides=",">
            <if test="tableSql != null">table_sql = #{tableSql},</if>
            <if test="tableCondition != null">table_condition = #{tableCondition},</if>
            <if test="tableAlias != null">table_alias = #{tableAlias},</if>
            <if test="tableExportField != null">table_export_field = #{tableExportField},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="orderByColumn != null">order_by_column = #{orderByColumn},</if>
            <if test="sortOrder != null">is_asc = #{sortOrder},</if>
        </trim>
        where sql_key = #{sqlKey}
    </update>

    <delete id="deleteTableSqlBySqlKeys" parameterType="String">
        delete from table_sql where sql_key in
        <foreach item="sqlKey" collection="list" open="(" separator="," close=")">
            #{sqlKey}
        </foreach>
    </delete>

</mapper>