<?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.test.test.mapper.TestApiMapper">

    <resultMap type="TestApi" id="TestApiResult">
        <result property="id"    column="id"    />
        <result property="groupId"    column="group_id"    />
        <result property="name"    column="name"    />
        <result property="method"    column="method"    />
        <result property="uri"    column="uri"    />
        <result property="header"    column="header"    />
        <result property="param"    column="param"    />
        <result property="contentType"    column="content_type"    />
        <result property="body"    column="body"    />
        <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"    />
    </resultMap>

    <sql id="selectTestApiVo">
        select id, group_id, name, method, uri, header, param, content_type, body, del_flag, create_by, create_time, update_by, update_time from test_api
    </sql>

    <select id="selectTestApiList" parameterType="TestApiListQO" resultMap="TestApiResult">
        <include refid="selectTestApiVo"/>
        <where>
            <if test="groupId != null ">and group_id = #{groupId}</if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="method != null  and method != ''">and method = #{method}</if>
            <if test="uri != null  and uri != ''">and uri like concat('%', #{uri}, '%')</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectTestApiById" parameterType="Long" resultMap="TestApiResult">
        <include refid="selectTestApiVo"/>
        where id = #{id}
    </select>

    <insert id="insertTestApi" parameterType="TestApi" useGeneratedKeys="true" keyProperty="id">
        insert into test_api
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupId != null">group_id,</if>
            <if test="name != null">name,</if>
            <if test="method != null">method,</if>
            <if test="uri != null">uri,</if>
            <if test="header != null">header,</if>
            <if test="param != null">param,</if>
            <if test="contentType != null">content_type,</if>
            <if test="body != null">body,</if>
            <if test="delFlag != null">del_flag,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupId != null">#{groupId},</if>
            <if test="name != null">#{name},</if>
            <if test="method != null">#{method},</if>
            <if test="uri != null">#{uri},</if>
            <if test="header != null">#{header},</if>
            <if test="param != null">#{param},</if>
            <if test="contentType != null">#{contentType},</if>
            <if test="body != null">#{body},</if>
            <if test="delFlag != null">#{delFlag},</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>
        </trim>
    </insert>

    <update id="updateTestApi" parameterType="TestApi">
        update test_api
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupId != null">group_id = #{groupId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="method != null">method = #{method},</if>
            <if test="uri != null">uri = #{uri},</if>
            <if test="header != null">header = #{header},</if>
            <if test="param != null">param = #{param},</if>
            <if test="contentType != null">content_type = #{contentType},</if>
            <if test="body != null">body = #{body},</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="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTestApiById" parameterType="Long">
        delete from test_api where id = #{id}
    </delete>

    <delete id="deleteTestApiByGroups" parameterType="String">
        delete from test_api where group_id in
        <foreach item="groupId" collection="list" open="(" separator="," close=")">
            #{groupId}
        </foreach>
    </delete>
</mapper>