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

    <resultMap id="UniQueryResult" type="cn.fateverse.query.entity.UniQuery">
        <id column="id" property="id"/>
        <result column="data_source_id" property="dataSourceId"/>
        <result column="menu_id" property="menuId"/>
        <result column="uq_name" property="uqName"/>
        <result column="uq_sql" property="uqSql"/>
        <result column="uq_describe" property="uqDescribe"/>
        <result column="publish" property="publish"/>
        <result column="preview" property="preview"/>
        <result column="top_json" property="topJson"/>
        <result column="type" property="type"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="selectUniQueryVo">
        select id, data_source_id, menu_id, uq_name, select_key, table_info, relevance, uq_sql, uq_describe, publish, single, preview, type, create_by, create_time, update_by, update_time
        from uni_query
    </sql>

    <sql id="selectUniQueryDetailsVo">
        select id, data_source_id, menu_id, uq_name, select_key, table_info, relevance, uq_sql, uq_describe, publish, single, preview, top_json, type, create_by, create_time, update_by, update_time
        from uni_query
    </sql>

    <select id="selectList" resultMap="UniQueryResult">
        <include refid="selectUniQueryVo"/>
        <where>
            <if test="uqName != null  and uqName != ''">and uq_name like concat('%', #{uqName}, '%')</if>
            <if test="publish != null ">and publish = #{publish}</if>
            <if test="preview != null ">and preview = #{preview}</if>
            <if test="dataSourceId != null ">and data_source_id = #{dataSourceId}</if>
            <if test="type != null ">and type = #{type}</if>
        </where>
    </select>

    <select id="selectById" resultMap="UniQueryResult">
        <include refid="selectUniQueryDetailsVo"/>
        where id = #{id}
    </select>

    <select id="selectSampleById" resultType="cn.fateverse.query.entity.UniQuery">
        <include refid="selectUniQueryVo"/>
        where id = #{id}
    </select>

    <insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into uni_query
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dataSourceId != null">data_source_id,</if>
            <if test="uqName != null">uq_name,</if>
            <if test="uqDescribe != null">uq_describe,</if>
            <if test="publish != null">publish,</if>
            <if test="single != null">single,</if>
            <if test="preview != null">preview,</if>
            <if test="type != null">type,</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="dataSourceId != null">#{dataSourceId},</if>
            <if test="uqName != null">#{uqName},</if>
            <if test="uqDescribe != null">#{uqDescribe},</if>
            <if test="publish != null">#{publish},</if>
            <if test="single != null">#{single},</if>
            <if test="preview != null">#{preview},</if>
            <if test="type != null">#{type},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="update">
        update uni_query
        <set>
            <if test="menuId != null">menu_id = #{menuId},</if>
            <if test="uqName != null">uq_name = #{uqName},</if>
            <if test="uqSql != null">uq_sql = #{uqSql},</if>
            <if test="uqDescribe != null">uq_describe = #{uqDescribe},</if>
            <if test="selectKey != null">select_key = #{selectKey},</if>
            <if test="tableInfo != null">table_info = #{tableInfo},</if>
            <if test="relevance != null">relevance = #{relevance},</if>
            <if test="publish != null">publish = #{publish},</if>
            <if test="single != null">single = #{single},</if>
            <if test="preview != null">preview = #{preview},</if>
            <if test="topJson != null">top_json = #{topJson},</if>
            <if test="type != null">type = #{type},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteById">
        delete from uni_query
        where id = #{id}
    </delete>

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

</mapper>