<?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.inspection.mapper.InspectionTaskInstanceMapper">
    
    <resultMap type="InspectionTaskInstance" id="InspectionTaskInstanceResult">
        <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="robotId"    column="robot_id"    />
        <result property="status"    column="status"    />
        <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"    />
        <result property="taskInsId"    column="task_ins_id"    />
    </resultMap>

    <resultMap type="com.cmvr.inspection.domain.vo.InspectionTaskInstanceVo" id="InspectionTaskInstanceVoResult">
        <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="taskName"    column="task_name"    />
        <result property="taskCode"    column="task_code"    />
        <result property="robotId"    column="robot_id"    />
        <result property="robotName"    column="robot_name"    />
        <result property="mapId"    column="robot_current_map_id"    />
        <result property="mapName"    column="map_name"    />
        <result property="status"    column="status"    />
        <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"    />
        <result property="taskInsId"    column="task_ins_id"    />
        <result property="waypointCount"    column="waypoint_count"    />
    </resultMap>

    <sql id="selectInspectionTaskInstanceVo">
        select id, create_by, create_time, update_by, update_time, remark, task_id, robot_id, status, start_time, end_time, task_ins_id, waypoint_count from inspection_task_instance
    </sql>

    <select id="selectInspectionTaskInstanceList" parameterType="InspectionTaskInstance" resultMap="InspectionTaskInstanceResult">
        <include refid="selectInspectionTaskInstanceVo"/>
        <where>  
            <if test="taskId != null  and taskId != ''"> and task_id = #{taskId}</if>
            <if test="robotId != null  and robotId != ''"> and robot_id = #{robotId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectInspectionTaskInstanceById" parameterType="String" resultMap="InspectionTaskInstanceResult">
        <include refid="selectInspectionTaskInstanceVo"/>
        where id = #{id}
    </select>

    <insert id="insertInspectionTaskInstance" parameterType="InspectionTaskInstance">
        insert into inspection_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="robotId != null">robot_id,</if>
            <if test="status != null">status,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="taskInsId != null">task_ins_id,</if>
            <if test="waypointCount != null">waypoint_count,</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="robotId != null">#{robotId},</if>
            <if test="status != null">#{status},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="taskInsId != null">#{taskInsId},</if>
            <if test="waypointCount != null">#{waypointCount},</if>
         </trim>
    </insert>

    <update id="updateInspectionTaskInstance" parameterType="InspectionTaskInstance">
        update inspection_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="robotId != null">robot_id = #{robotId},</if>
            <if test="status != null">status = #{status},</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>
            <if test="waypointCount != null">waypoint_count = #{waypointCount},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInspectionTaskInstanceById" parameterType="String">
        delete from inspection_task_instance where id = #{id}
    </delete>

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

    <select id="selectInspectionTaskInstanceVoList" parameterType="InspectionTaskInstanceQuery" resultMap="InspectionTaskInstanceVoResult">
        select ti.id, ti.create_by, ti.create_time, ti.update_by, ti.update_time, ti.remark,ti.waypoint_count,
        ti.task_id, t.task_name, t.task_code,
        ti.robot_id, r.robot_name,
        r.current_map_id as robot_current_map_id, m.map_name,
        ti.status, ti.start_time, ti.end_time, ti.task_ins_id,
        u1.nick_name as create_by_name, u2.nick_name as update_by_name
        from inspection_task_instance ti
        left join inspection_task t on ti.task_id = t.id
        left join inspection_robot r on ti.robot_id = r.id
        left join inspection_map m on r.current_map_id = m.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="robotId != null and robotId != ''">
                and ti.robot_id = #{robotId}
            </if>
            <!-- 优先多状态IN查询 -->
            <if test="statusList != null and statusList.size() > 0">
                and ti.status in
                <foreach collection="statusList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <!-- statusList为空再走单个状态精确匹配 -->
            <if test="(statusList == null or statusList.size() == 0) and status != null">
                and ti.status = #{status}
            </if>
            <!-- 任务类型 -->
            <if test="taskType != null and taskType != ''">
                and t.task_type = #{taskType}
            </if>
        </where>
        order by ti.create_time desc
    </select>

    <select id="selectInspectionTaskInstanceVoById" parameterType="String" resultMap="InspectionTaskInstanceVoResult">
        select ti.id, ti.create_by, ti.create_time, ti.update_by, ti.update_time, ti.remark,
               ti.task_id, t.task_name, t.task_code,
               ti.robot_id, r.robot_name,
               r.current_map_id as robot_current_map_id, m.map_name,
               ti.status, ti.start_time, ti.end_time, ti.task_ins_id
        from inspection_task_instance ti
        left join inspection_task t on ti.task_id = t.id
        left join inspection_robot r on ti.robot_id = r.id
        left join inspection_map m on r.current_map_id = m.id
        where ti.id = #{id}
    </select>
</mapper>
