<?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.AimaTaskInstanceMapper">
    <resultMap type="AimaTaskInstance" id="AimaTaskInstanceResult">
        <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="taskId" column="task_id"/><result property="phoneId" column="phone_id"/>
        <result property="vehicleId" column="vehicle_id"/>
        <result property="executionNumber" column="execution_number"/><result property="status" column="status"/><result property="taskInsId" column="task_ins_id"/>
        <result property="totalCases" column="total_cases"/>
        <result property="completedCases" column="completed_cases"/><result property="passedCases" column="passed_cases"/><result property="failedCases" column="failed_cases"/>
        <result property="progress" column="progress"/>
        <result property="startTime" column="start_time"/><result property="endTime" column="end_time"/>
    </resultMap>

    <resultMap type="com.cmvr.aima.domain.vo.AimaTaskInstanceVo" id="AimaTaskInstanceVoResult">
        <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="taskId" column="task_id"/><result property="taskCode" column="task_code"/><result property="taskName" column="task_name"/>
        <result property="phoneId" column="phone_id"/><result property="phoneName" column="phone_name"/>
        <result property="vehicleId" column="vehicle_id"/><result property="vehicleName" column="vehicle_name"/><result property="vin" column="vin"/>
        <result property="executionNumber" column="execution_number"/><result property="status" column="status"/><result property="taskInsId" column="task_ins_id"/>
        <result property="totalCases" column="total_cases"/>
        <result property="completedCases" column="completed_cases"/><result property="passedCases" column="passed_cases"/><result property="failedCases" column="failed_cases"/>
        <result property="progress" column="progress"/>
        <result property="startTime" column="start_time"/><result property="endTime" column="end_time"/>
        <result property="createByName" column="create_by_name"/><result property="updateByName" column="update_by_name"/>
    </resultMap>

    <sql id="selectAimaTaskInstanceVo">
        select id, create_by, create_time, update_by, update_time, remark, task_id, phone_id, vehicle_id, execution_number, status, task_ins_id, total_cases, completed_cases, passed_cases, failed_cases, progress, start_time, end_time from aima_task_instance
    </sql>

    <select id="selectAimaTaskInstanceList" parameterType="AimaTaskInstance" resultMap="AimaTaskInstanceResult">
        <include refid="selectAimaTaskInstanceVo"/>
        <where>
            <if test="taskId != null and taskId != ''"> and task_id = #{taskId}</if>
            <if test="phoneId != null and phoneId != ''"> and phone_id = #{phoneId}</if>
            <if test="vehicleId != null and vehicleId != ''"> and vehicle_id = #{vehicleId}</if>
            <if test="status != null"> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAimaTaskInstanceById" parameterType="String" resultMap="AimaTaskInstanceResult">
        <include refid="selectAimaTaskInstanceVo"/> where id = #{id}
    </select>

    <select id="selectAimaTaskInstanceVoList" parameterType="com.cmvr.aima.domain.dto.AimaTaskInstanceQuery" resultMap="AimaTaskInstanceVoResult">
        select ti.id, ti.create_by, ti.create_time, ti.update_by, ti.update_time, ti.remark,
               ti.task_id, t.task_code, t.task_name,
               ti.phone_id, p.phone_name, ti.vehicle_id, v.vehicle_name, v.vin,
               ti.execution_number, ti.status, ti.task_ins_id,
               ti.total_cases, ti.completed_cases, ti.passed_cases, ti.failed_cases, ti.progress,
               ti.start_time, ti.end_time,
               u1.nick_name as create_by_name, u2.nick_name as update_by_name
        from aima_task_instance ti
        left join aima_task t on ti.task_id = t.id
        left join aima_phone p on ti.phone_id = p.id
        left join aima_vehicle v on ti.vehicle_id = v.id
        left join sys_user u1 on ti.create_by = u1.user_name
        left join sys_user u2 on ti.update_by = u2.user_name
        <where>
            <if test="taskId != null and taskId != ''"> and ti.task_id = #{taskId}</if>
            <if test="phoneId != null and phoneId != ''"> and ti.phone_id = #{phoneId}</if>
            <if test="vehicleId != null and vehicleId != ''"> and ti.vehicle_id = #{vehicleId}</if>
            <if test="status != null"> and ti.status = #{status}</if>
            <if test="statusList != null and statusList.size() > 0">
                and ti.status in
                <foreach item="statusItem" collection="statusList" open="(" separator="," close=")">
                    #{statusItem}
                </foreach>
            </if>
        </where>
        order by ti.create_time desc
    </select>

    <insert id="insertAimaTaskInstance" parameterType="AimaTaskInstance">
        insert into aima_task_instance <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="taskId != null">task_id,</if><if test="phoneId != null">phone_id,</if>
            <if test="vehicleId != null">vehicle_id,</if>
            <if test="executionNumber != null">execution_number,</if><if test="status != null">status,</if><if test="totalCases != null">total_cases,</if>
            <if test="completedCases != null">completed_cases,</if><if test="passedCases != null">passed_cases,</if><if test="failedCases != null">failed_cases,</if>
            <if test="progress != null">progress,</if>
            <if test="startTime != null">start_time,</if><if test="endTime != null">end_time,</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="taskId != null">#{taskId},</if><if test="phoneId != null">#{phoneId},</if>
            <if test="vehicleId != null">#{vehicleId},</if>
            <if test="executionNumber != null">#{executionNumber},</if><if test="status != null">#{status},</if><if test="totalCases != null">#{totalCases},</if>
            <if test="completedCases != null">#{completedCases},</if><if test="passedCases != null">#{passedCases},</if><if test="failedCases != null">#{failedCases},</if>
            <if test="progress != null">#{progress},</if>
            <if test="startTime != null">#{startTime},</if><if test="endTime != null">#{endTime},</if>
        </trim>
    </insert>

    <update id="updateAimaTaskInstance" parameterType="AimaTaskInstance">
        update aima_task_instance <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="taskId != null">task_id = #{taskId},</if>
            <if test="phoneId != null">phone_id = #{phoneId},</if>
            <if test="vehicleId != null">vehicle_id = #{vehicleId},</if>
            <if test="executionNumber != null">execution_number = #{executionNumber},</if>
            <if test="status != null">status = #{status},</if><if test="totalCases != null">total_cases = #{totalCases},</if>
            <if test="completedCases != null">completed_cases = #{completedCases},</if><if test="passedCases != null">passed_cases = #{passedCases},</if>
            <if test="failedCases != null">failed_cases = #{failedCases},</if><if test="progress != null">progress = #{progress},</if>
            <if test="startTime != null">start_time = #{startTime},</if><if test="endTime != null">end_time = #{endTime},</if>
            <if test="taskInsId != null">task_ins_id = #{taskInsId},</if>

    </trim> where id = #{id}
    </update>

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