<?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.cmvr.aima.mapper.AimaTestCaseMapper">
    <resultMap type="AimaTestCase" id="AimaTestCaseResult">
        <result property="id" column="id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="caseCode" column="case_code"/>
        <result property="caseName" column="case_name"/>
        <result property="caseCategory" column="case_category"/>
        <result property="testDescription" column="test_description"/>
        <result property="precondition" column="precondition"/>
        <result property="testSteps" column="test_steps"/>
        <result property="expectedResult" column="expected_result"/>
        <result property="priority" column="priority"/>
        <result property="enabled" column="enabled"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="estimatedDuration" column="estimated_duration"/>
        <result property="detectItemId" column="detect_item_id"/>
    </resultMap>

    <resultMap type="com.cmvr.aima.domain.vo.AimaTestCaseVo" id="AimaTestCaseVoResult">
        <result property="id" column="id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="caseCode" column="case_code"/>
        <result property="caseName" column="case_name"/>
        <result property="caseCategory" column="case_category"/>
        <result property="testDescription" column="test_description"/>
        <result property="precondition" column="precondition"/>
        <result property="testSteps" column="test_steps"/>
        <result property="expectedResult" column="expected_result"/>
        <result property="priority" column="priority"/>
        <result property="enabled" column="enabled"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="estimatedDuration" column="estimated_duration"/>
        <result property="detectItemId" column="detect_item_id"/>
        <result property="configStatus" column="config_status"/>
        <result property="createByName" column="create_by_name"/>
        <result property="updateByName" column="update_by_name"/>
    </resultMap>

    <sql id="selectAimaTestCaseVo">
        select id, create_by, create_time, update_by, update_time, remark, case_code, case_name, case_category, test_description, precondition, test_steps, expected_result, priority, enabled, sort_order, estimated_duration, detect_item_id from aima_test_case
    </sql>

    <select id="selectAimaTestCaseList" parameterType="AimaTestCase" resultMap="AimaTestCaseResult">
        <include refid="selectAimaTestCaseVo"/>
        <where>
            <if test="caseCode != null and caseCode != ''"> and case_code = #{caseCode}</if>
            <if test="caseName != null and caseName != ''"> and case_name like concat('%', #{caseName}, '%')</if>
            <if test="caseCategory != null and caseCategory != ''"> and case_category = #{caseCategory}</if>
            <if test="enabled != null and enabled != ''"> and enabled = #{enabled}</if>
        </where>
        order by sort_order asc, create_time desc
    </select>

    <select id="selectAimaTestCaseById" parameterType="String" resultMap="AimaTestCaseResult">
        <include refid="selectAimaTestCaseVo"/>
        where id = #{id}
    </select>

    <select id="selectAimaTestCaseVoList" parameterType="AimaTestCase" resultMap="AimaTestCaseVoResult">
        select tc.id, tc.create_by, tc.create_time, tc.update_by, tc.update_time, tc.remark,
               tc.case_code, tc.case_name, tc.case_category, tc.test_description, tc.precondition,
               tc.test_steps, tc.expected_result, tc.priority, tc.enabled, tc.sort_order,
               tc.estimated_duration, tc.detect_item_id,
               d.is_deploy as config_status,
               u1.nick_name as create_by_name, u2.nick_name as update_by_name
        from aima_test_case tc
        left join te_detection_item d on tc.detect_item_id = d.id
        left join sys_user u1 on tc.create_by = u1.user_name
        left join sys_user u2 on tc.update_by = u2.user_name
        <where>
            <if test="caseCode != null and caseCode != ''"> and tc.case_code = #{caseCode}</if>
            <if test="caseName != null and caseName != ''"> and tc.case_name like concat('%', #{caseName}, '%')</if>
            <if test="caseCategory != null and caseCategory != ''"> and tc.case_category = #{caseCategory}</if>
            <if test="enabled != null and enabled != ''"> and tc.enabled = #{enabled}</if>
        </where>
        order by tc.sort_order, tc.create_time desc
    </select>

    <insert id="insertAimaTestCase" parameterType="AimaTestCase">
        insert into aima_test_case
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</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>
            <if test="remark != null">remark,</if>
            <if test="caseCode != null">case_code,</if>
            <if test="caseName != null">case_name,</if>
            <if test="caseCategory != null">case_category,</if>
            <if test="testDescription != null">test_description,</if>
            <if test="precondition != null">precondition,</if>
            <if test="testSteps != null">test_steps,</if>
            <if test="expectedResult != null">expected_result,</if>
            <if test="priority != null">priority,</if>
            <if test="enabled != null">enabled,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="estimatedDuration != null">estimated_duration,</if>
            <if test="detectItemId != null">detect_item_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</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>
            <if test="remark != null">#{remark},</if>
            <if test="caseCode != null">#{caseCode},</if>
            <if test="caseName != null">#{caseName},</if>
            <if test="caseCategory != null">#{caseCategory},</if>
            <if test="testDescription != null">#{testDescription},</if>
            <if test="precondition != null">#{precondition},</if>
            <if test="testSteps != null">#{testSteps},</if>
            <if test="expectedResult != null">#{expectedResult},</if>
            <if test="priority != null">#{priority},</if>
            <if test="enabled != null">#{enabled},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="estimatedDuration != null">#{estimatedDuration},</if>
            <if test="detectItemId != null">#{detectItemId},</if>
        </trim>
    </insert>

    <update id="updateAimaTestCase" parameterType="AimaTestCase">
        update aima_test_case
        <trim prefix="SET" suffixOverrides=",">
            <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>
            <if test="remark != null">remark = #{remark},</if>
            <if test="caseCode != null">case_code = #{caseCode},</if>
            <if test="caseName != null">case_name = #{caseName},</if>
            <if test="caseCategory != null">case_category = #{caseCategory},</if>
            <if test="testDescription != null">test_description = #{testDescription},</if>
            <if test="precondition != null">precondition = #{precondition},</if>
            <if test="testSteps != null">test_steps = #{testSteps},</if>
            <if test="expectedResult != null">expected_result = #{expectedResult},</if>
            <if test="priority != null">priority = #{priority},</if>
            <if test="enabled != null">enabled = #{enabled},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="estimatedDuration != null">estimated_duration = #{estimatedDuration},</if>
            <if test="detectItemId != null">detect_item_id = #{detectItemId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAimaTestCaseById" parameterType="String">
        delete from aima_test_case where id = #{id}
    </delete>

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