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

    <insert id="batchInsert">
        insert into query_table_column (table_id, column_name, column_comment, column_type, pk, `use`, sort, create_by, create_time) VALUES
            <foreach collection="list" separator="," item="item">
                (
                #{item.tableId},
                #{item.columnName},
                #{item.columnComment},
                #{item.columnType},
                #{item.pk},
                #{item.use},
                #{item.sort},
                #{item.createBy},
                #{item.createTime}
                )
            </foreach>
    </insert>

    <update id="batchUpdate">
        update query_table_column
        <set>
            <trim prefix="column_comment = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.columnComment != null">
                        when column_id = #{item.columnId} then #{item.columnComment}
                    </if>
                </foreach>
            </trim>
            <trim prefix="`use` = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.use != null">
                        when column_id = #{item.columnId} then #{item.use}
                    </if>
                </foreach>
            </trim>
        </set>
        where column_id in
        <foreach collection="list" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.columnId}
        </foreach>
    </update>


    <update id="syncUpdate">
        update query_table_column
        <set>
            <trim prefix="column_comment = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.columnComment != null">
                        when column_id = #{item.columnId} then #{item.columnComment}
                    </if>
                </foreach>
            </trim>
            <trim prefix="column_type = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.columnType != null">
                        when column_id = #{item.columnId} then #{item.columnType}
                    </if>
                </foreach>
            </trim>
            <trim prefix="pk = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.pk != null">
                        when column_id = #{item.columnId} then #{item.pk}
                    </if>
                </foreach>
            </trim>
        </set>
        where column_id in
        <foreach collection="list" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.columnId}
        </foreach>
    </update>


    <delete id="batchRemove">
        delete from query_table_column where column_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>




    <delete id="deleteByTableId">
        delete from query_table_column where table_id = #{tableId}
    </delete>
    <select id="selectByTableId" resultType="cn.fateverse.query.entity.TableColumn">
        select column_id, table_id, column_name, column_comment, column_type, pk, `use`, sort, create_by, create_time, update_by, update_time
        from query_table_column where table_id = #{tableId}
    </select>

    <select id="selectByTableIds" resultType="cn.fateverse.query.entity.TableColumn">
        select column_id, table_id, column_name, column_comment, column_type, pk, `use`, sort, create_by, create_time, update_by, update_time
        from query_table_column where `use` = 1
        and table_id in
        <foreach collection="list" item="tableId" open="(" separator="," close=")">
            #{tableId}
        </foreach>
    </select>

</mapper>