<?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">
<!--  Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：计划场景用例节点任务表 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2022-12-12 16:22:12  -->
<mapper namespace="net.northking.cctp.executePlan.db.dao.AtuPlanSceneCaseTaskDao">


  <!-- 请在本文件内添加自定义SQL语句 -->

  <resultMap id="DetailMap" type="net.northking.cctp.executePlan.dto.planSceneCase.AtuPlanSceneCaseTaskDetailDto">
      <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                                                <!-- 任务主键 -->
    <result column="task_id" jdbcType="VARCHAR" property="taskId"/>
        <!-- 脚本主键 -->
    <result column="script_id" jdbcType="VARCHAR" property="scriptId"/>
        <!-- 脚本名称 -->
    <result column="script_name" jdbcType="VARCHAR" property="scriptName"/>
        <!-- 节点编号 -->
    <result column="node_id" jdbcType="VARCHAR" property="nodeId"/>
        <!-- 节点类型;1-接口，2-PC/BS，3-PC/CS，4-安卓，5-ios -->
    <result column="node_type" jdbcType="CHAR" property="nodeType"/>
        <!-- 上一节点编号 -->
    <result column="prev_node_id" jdbcType="VARCHAR" property="prevNodeId"/>
        <!-- 下一节点编号 -->
    <result column="next_node_id" jdbcType="VARCHAR" property="nextNodeId"/>
        <!-- 下一节点线编号 -->
    <result column="line_id" jdbcType="VARCHAR" property="lineId"/>
        <!-- 引擎主键 -->
    <result column="engine_id" jdbcType="VARCHAR" property="engineId"/>
        <!-- 设备主键 -->
    <result column="device_id" jdbcType="VARCHAR" property="deviceId"/>
        <!-- 应用主键 -->
    <result column="app_id" jdbcType="VARCHAR" property="appId"/>
        <!-- 状态;0-等待中，1-执行中，2-执行成功，3-执行失败，4-断言失败，5-取消，6-超时 -->
    <result column="status" jdbcType="CHAR" property="status"/>
        <!-- 开始时间 -->
    <result column="start_time" jdbcType="BIGINT" property="startTime"/>
        <!-- 结束时间 -->
    <result column="end_time" jdbcType="BIGINT" property="endTime"/>
        <!-- 错误信息 -->
    <result column="error_msg" jdbcType="LONGVARCHAR" property="errorMsg"/>
        <!-- 视频地址 -->
    <result column="video_url" jdbcType="VARCHAR" property="videoUrl"/>
        <!-- 执行结果文件 -->
    <result column="exec_result_file" jdbcType="VARCHAR" property="execResultFile"/>
        <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
    </resultMap>

    <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.executePlan.db.entity.AtuPlanSceneCaseTask">
    select
      <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="taskId != null">
        AND task_id = #{taskId,jdbcType=VARCHAR}
      </if>
      <if test="scriptId != null">
        AND script_id = #{scriptId,jdbcType=VARCHAR}
      </if>
      <if test="scriptName != null">
        AND LOCATE(#{scriptName,jdbcType=VARCHAR},script_name) > 0
      </if>
      <if test="nodeId != null">
        AND node_id = #{nodeId,jdbcType=VARCHAR}
      </if>
      <if test="nodeType != null">
        AND node_type = #{nodeType,jdbcType=CHAR}
      </if>
      <if test="prevNodeId != null">
        AND prev_node_id = #{prevNodeId,jdbcType=VARCHAR}
      </if>
      <if test="nextNodeId != null">
        AND next_node_id = #{nextNodeId,jdbcType=VARCHAR}
      </if>
      <if test="lineId != null">
        AND line_id = #{lineId,jdbcType=VARCHAR}
      </if>
      <if test="engineId != null">
        AND engine_id = #{engineId,jdbcType=VARCHAR}
      </if>
      <if test="deviceId != null">
        AND device_id = #{deviceId,jdbcType=VARCHAR}
      </if>
      <if test="appId != null">
        AND app_id = #{appId,jdbcType=VARCHAR}
      </if>
      <if test="status != null">
        AND status = #{status,jdbcType=CHAR}
      </if>
      <if test="startTime != null">
        AND start_time = #{startTime,jdbcType=BIGINT}
      </if>
      <if test="endTime != null">
        AND end_time = #{endTime,jdbcType=BIGINT}
      </if>
      <if test="errorMsg != null">
        AND error_msg = #{errorMsg,jdbcType=LONGVARCHAR}
      </if>
      <if test="videoUrl != null">
        AND video_url = #{videoUrl,jdbcType=VARCHAR}
      </if>
      <if test="execResultFile != null">
        AND exec_result_file = #{execResultFile,jdbcType=VARCHAR}
      </if>
      <if test="createdTime != null">
        AND created_time = #{createdTime,jdbcType=TIMESTAMP}
      </if>
    </trim>
  </select>

  <select id="queryDeviceIdListByTaskId" resultType="java.lang.String">
    select device_id from <include refid="Table_Name"/> where task_id = #{taskId} and device_id is not null
  </select>

  <select id="queryUnfinishedTaskByTaskId" resultMap="BaseResultMap">
    select
    id
    ,task_id
    ,script_id
    ,script_name
    ,node_id
    ,node_type
    ,prev_node_id
    ,next_node_id
    ,line_id
    ,engine_id
    ,device_id
    ,app_id
    ,status
    ,start_time
    ,end_time
    ,error_msg
    ,video_url
    ,exec_result_file
    ,created_time
    from
    <include refid="Table_Name"/>
    where task_id = #{taskId} and status in ('0', '1') limit 1
  </select>

  <select id="queryTaskVideoUrl" resultMap="BaseResultMap">
    select
    id
    ,task_id
    ,script_id
    ,script_name
    ,node_id
    ,node_type
    ,prev_node_id
    ,next_node_id
    ,line_id
    ,engine_id
    ,device_id
    ,app_id
    ,status
    ,start_time
    ,end_time
    ,error_msg
    ,video_url
    ,exec_result_file
    ,created_time
    from
    <include refid="Table_Name"/>
    where task_id = #{taskId}
    and node_type != '0'
    order by end_time asc
  </select>

  <select id="queryResultFileByTaskIdAndNodeId" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where task_id = #{taskId} and node_id = #{nodeId} order by created_time desc limit 1
  </select>

  <select id="queryNodeInfo" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM atu_plan_scene_case_task
    WHERE (task_id,node_id,created_time) IN (
    SELECT task_id,node_id, MAX(created_time)
    FROM atu_plan_scene_case_task
    WHERE task_id = #{taskId}
    GROUP BY node_id)
    order by end_time asc
  </select>

  <select id="queryFirstNodeTask" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/>
    FROM <include refid="Table_Name"/>
    where task_id = #{taskId}
    order by created_time asc
    limit 1
  </select>
    <select id="queryAppIdListByBatchId" resultType="java.lang.String">
      select app_id from atu_plan_scene_case_task
      where task_id in (select id from atu_plan_task where batch_id = #{batchId})
      and app_id != ''
    </select>
  <select id="countByPlanId" resultType="java.lang.Integer">
    select count(1) from <include refid="Table_Name"/> s
    left join atu_plan_task t on t.id = s.task_id
    left join atu_plan_batch b on b.id = t.batch_id
    where b.plan_id = #{planId}
  </select>
    <select id="querySceneCaseTasksByTaskIds"
            resultType="net.northking.cctp.executePlan.db.entity.AtuPlanSceneCaseTask">
      SELECT * FROM <include refid="Table_Name"/> WHERE task_id IN
      <foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
        #{taskId}
      </foreach>
    </select>
  <select id="queryExecTimeoutTask" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where status = 1 and timestampdiff(second, last_heartbeat_time, NOW()) > #{timeout}
  </select>

  <delete id="deleteByPlanIdLimit">
    delete from atu_plan_scene_case_task
    where task_id in (select id from atu_plan_task
      where batch_id in (select id from atu_plan_batch
        where plan_id = #{planId}))
    limit #{num}
  </delete>
</mapper>