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

    <resultMap type="TestTaskCase" id="TestTaskCaseResult">
        <result property="taskId"    column="task_id"    />
        <result property="caseId"    column="case_id"    />
        <result property="sort"    column="sort"    />
    </resultMap>

    <sql id="selectTestTaskCaseVo">
        select task_id, case_id, sort from test_task_case
    </sql>

    <select id="selectTestTaskCaseList" parameterType="TestTaskCase" resultMap="TestTaskCaseResult">
        <include refid="selectTestTaskCaseVo"/>
        <where>
            <if test="sort != null "> and sort = #{sort}</if>
        </where>
    </select>

    <select id="selectTestTaskCaseByTaskId" parameterType="Long" resultMap="TestTaskCaseResult">
        <include refid="selectTestTaskCaseVo"/>
        where task_id = #{taskId}
    </select>

    <insert id="insertTestTaskCase" parameterType="TestTaskCase">
        insert into test_task_case
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="caseId != null">case_id,</if>
            <if test="sort != null">sort,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="caseId != null">#{caseId},</if>
            <if test="sort != null">#{sort},</if>
         </trim>
    </insert>

    <update id="updateTestTaskCase" parameterType="TestTaskCase">
        update test_task_case
        <trim prefix="SET" suffixOverrides=",">
            <if test="sort != null">sort = #{sort},</if>
        </trim>
        where task_id = #{taskId} and case_id = #{caseId}
    </update>

    <delete id="deleteTestTaskCaseByTaskId" parameterType="Long">
        delete from test_task_case where task_id = #{taskId}
    </delete>

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

    <delete id="deleteTestTaskCase" parameterType="TestTaskCase">
        delete from test_task_case where case_id = #{caseId} and task_id = #{taskId}
    </delete>

    <select id="selectTestTaskCaseListByTaskId" parameterType="Long" resultMap="TestTaskCaseResult">
        <include refid="selectTestTaskCaseVo"/>
        where task_id = #{taskId}
        ORDER BY sort
    </select>

    <select id="max" parameterType="Long" resultType="Long">
        select max(sort) from test_task_case where task_id = #{taskId}
    </select>

</mapper>
