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

    <resultMap type="TestCaseStep" id="TestCaseStepResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parent_id"/>
        <result property="caseId" column="case_id"/>
        <result property="name" column="name"/>
        <result property="stepNum" column="step_num"/>
        <result property="type" column="type"/>
        <result property="requestMethod" column="request_method"/>
        <result property="requestUrl" column="request_url"/>
        <result property="requestBody" column="request_body"/>
        <result property="requestHeader" column="request_header"/>
        <result property="requestParams" column="request_params"/>
        <result property="apiHttpId" column="api_http_id"/>
        <result property="apiHost" column="api_host"/>
        <result property="apiPort" column="api_port"/>
        <result property="apiProtocol" column="api_protocol"/>
        <result property="datasourceId" column="datasource_id"/>
        <result property="sqlCommand" column="sql_command"/>
        <result property="count" column="count"/>
        <result property="async" column="async"/>
        <result property="interval" column="interval"/>
        <result property="breakError" column="break_error"/>
        <result property="preScript" column="pre_script"/>
        <result property="postScript" column="post_script"/>
        <result property="assignment" column="assignment"/>
        <result property="assertion" column="assertion"/>
        <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="selectTestCaseStepVo">
        select id,
               parent_id,
               case_id,
               name,
               step_num,
               type,
               request_method,
               request_url,
               request_body,
               request_header,
               request_params,
               api_http_id,
               api_host,
               api_port,
               api_protocol,
               datasource_id,
               sql_command,
               count,
               `async`,
               `interval`,
               break_error,
               pre_script,
               post_script,
               assignment,
               assertion,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from test_case_step
    </sql>

    <select id="selectTestCaseStepList" parameterType="TestCaseStep" resultMap="TestCaseStepResult">
        <include refid="selectTestCaseStepVo"/>
        <where>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="parentId == null ">and parent_id is null</if>
            <if test="caseId != null ">and case_id = #{caseId}</if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="stepNum != null ">and step_num = #{stepNum}</if>
            <if test="type != null ">and type = #{type}</if>
            <if test="requestMethod != null  and requestMethod != ''">and request_method = #{requestMethod}</if>
            <if test="requestUrl != null  and requestUrl != ''">and request_url = #{requestUrl}</if>
            <if test="requestBody != null  and requestBody != ''">and request_body = #{requestBody}</if>
            <if test="requestHeader != null  and requestHeader != ''">and request_header = #{requestHeader}</if>
            <if test="requestParams != null  and requestParams != ''">and request_params = #{requestParams}</if>
            <if test="apiHttpId != null ">and api_http_id = #{apiHttpId}</if>
            <if test="apiHost != null  and apiHost != ''">and api_host = #{apiHost}</if>
            <if test="apiPort != null ">and api_port = #{apiPort}</if>
            <if test="apiProtocol != null  and apiProtocol != ''">and api_protocol = #{apiProtocol}</if>
            <if test="datasourceId != null ">and datasource_id = #{datasourceId}</if>
            <if test="sqlCommand != null  and sqlCommand != ''">and sql_command = #{sqlCommand}</if>
            <if test="count != null ">and count = #{count}</if>
            <if test="async != null ">and async = #{async}</if>
            <if test="interval != null ">and interval = #{interval}</if>
            <if test="breakError != null  and breakError != ''">and break_error = #{breakError}</if>
            <if test="preScript != null  and preScript != ''">and pre_script = #{preScript}</if>
            <if test="postScript != null  and postScript != ''">and post_script = #{postScript}</if>
            <if test="assignment != null  and assignment != ''">and assignment = #{assignment}</if>
            <if test="assertion != null  and assertion != ''">and assertion = #{assertion}</if>
            <if test="delFlag != null">and del_flag = #{delFlag}</if>
        </where>
        ORDER BY step_num
    </select>

    <select id="selectTestCaseStepById" parameterType="Long" resultMap="TestCaseStepResult">
        <include refid="selectTestCaseStepVo"/>
        where id = #{id}
    </select>

    <insert id="insertTestCaseStep" parameterType="TestCaseStep" useGeneratedKeys="true" keyProperty="id">
        insert into test_case_step
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="caseId != null">case_id,</if>
            <if test="name != null">name,</if>
            <if test="stepNum != null">step_num,</if>
            <if test="type != null">type,</if>
            <if test="requestMethod != null">request_method,</if>
            <if test="requestUrl != null">request_url,</if>
            <if test="requestBody != null">request_body,</if>
            <if test="requestHeader != null">request_header,</if>
            <if test="requestParams != null">request_params,</if>
            <if test="apiHttpId != null">api_http_id,</if>
            <if test="apiHost != null">api_host,</if>
            <if test="apiPort != null">api_port,</if>
            <if test="apiProtocol != null">api_protocol,</if>
            <if test="datasourceId != null">datasource_id,</if>
            <if test="sqlCommand != null">sql_command,</if>
            <if test="count != null">count,</if>
            <if test="async != null">async,</if>
            <if test="interval != null">interval,</if>
            <if test="breakError != null">break_error,</if>
            <if test="preScript != null">pre_script,</if>
            <if test="postScript != null">post_script,</if>
            <if test="assignment != null">assignment,</if>
            <if test="assertion != null">assertion,</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="parentId != null">#{parentId},</if>
            <if test="caseId != null">#{caseId},</if>
            <if test="name != null">#{name},</if>
            <if test="stepNum != null">#{stepNum},</if>
            <if test="type != null">#{type},</if>
            <if test="requestMethod != null">#{requestMethod},</if>
            <if test="requestUrl != null">#{requestUrl},</if>
            <if test="requestBody != null">#{requestBody},</if>
            <if test="requestHeader != null">#{requestHeader},</if>
            <if test="requestParams != null">#{requestParams},</if>
            <if test="apiHttpId != null">#{apiHttpId},</if>
            <if test="apiHost != null">#{apiHost},</if>
            <if test="apiPort != null">#{apiPort},</if>
            <if test="apiProtocol != null">#{apiProtocol},</if>
            <if test="datasourceId != null">#{datasourceId},</if>
            <if test="sqlCommand != null">#{sqlCommand},</if>
            <if test="count != null">#{count},</if>
            <if test="async != null">#{async},</if>
            <if test="interval != null">#{interval},</if>
            <if test="breakError != null">#{breakError},</if>
            <if test="preScript != null">#{preScript},</if>
            <if test="postScript != null">#{postScript},</if>
            <if test="assignment != null">#{assignment},</if>
            <if test="assertion != null">#{assertion},</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="updateTestCaseStep" parameterType="TestCaseStep">
        update test_case_step
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="caseId != null">case_id = #{caseId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="stepNum != null">step_num = #{stepNum},</if>
            <if test="type != null">type = #{type},</if>
            <if test="requestMethod != null">request_method = #{requestMethod},</if>
            <if test="requestUrl != null">request_url = #{requestUrl},</if>
            <if test="requestBody != null">request_body = #{requestBody},</if>
            <if test="requestHeader != null">request_header = #{requestHeader},</if>
            <if test="requestParams != null">request_params = #{requestParams},</if>
            <if test="apiHttpId != null">api_http_id = #{apiHttpId},</if>
            <if test="apiHost != null">api_host = #{apiHost},</if>
            <if test="apiPort != null">api_port = #{apiPort},</if>
            <if test="apiProtocol != null">api_protocol = #{apiProtocol},</if>
            <if test="datasourceId != null">datasource_id = #{datasourceId},</if>
            <if test="sqlCommand != null">sql_command = #{sqlCommand},</if>
            <if test="count != null">count = #{count},</if>
            <if test="async != null">async = #{async},</if>
            <if test="interval != null">interval = #{interval},</if>
            <if test="breakError != null">break_error = #{breakError},</if>
            <if test="preScript != null">pre_script = #{preScript},</if>
            <if test="postScript != null">post_script = #{postScript},</if>
            <if test="assignment != null">assignment = #{assignment},</if>
            <if test="assertion != null">assertion = #{assertion},</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="deleteTestCaseStepById" parameterType="Long">
        delete
        from test_case_step
        where id = #{id}
    </delete>

    <delete id="deleteTestCaseStepByCaseId" parameterType="Long">
        delete
        from test_case_step
        where case_id = #{caseId}
    </delete>

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

    <select id="selectTestCaseStepListByCaseId" parameterType="Long" resultMap="TestCaseStepResult">
        <include refid="selectTestCaseStepVo"/>
        where case_id = #{caseId} and del_flag = '0' and parent_id is null
        ORDER BY step_num
    </select>

    <select id="selectAllTestCaseStepIdListByCaseId" parameterType="Long" resultType="Long">
        select id from test_case_step
        where case_id = #{caseId} and del_flag = '0'
    </select>
</mapper>
