<?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-07 17:41:08  -->
<mapper namespace="net.northking.cctp.executePlan.db.dao.AtuPlanBatchDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.executePlan.dto.planBatch.AtuPlanBatchPageDto">
      <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                                      <!-- 计划主键 -->
    <result column="plan_id" jdbcType="VARCHAR" property="planId"/>
        <!-- 任务批次 -->
    <result column="batch" jdbcType="INTEGER" property="batch"/>
        <!-- 状态;0-等待中，1-执行中，2-已完成，3-已取消 -->
    <result column="status" jdbcType="CHAR" property="status"/>
        <!-- 调度开始时间 -->
    <result column="start_time" jdbcType="TIMESTAMP" property="startTime"/>
        <!-- 调度结束时间 -->
    <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
        <!-- 任务总数 -->
    <result column="task_total" jdbcType="INTEGER" property="taskTotal"/>
        <!-- 成功任务数 -->
    <result column="success_num" jdbcType="INTEGER" property="successNum"/>
        <!-- 执行失败数 -->
    <result column="exec_fail_num" jdbcType="INTEGER" property="execFailNum"/>
        <!-- 断言失败数 -->
    <result column="assert_fail_num" jdbcType="INTEGER" property="assertFailNum"/>
        <!-- 超时任务数 -->
    <result column="timeout_num" jdbcType="INTEGER" property="timeoutNum"/>
        <!-- 取消任务数 -->
    <result column="cancel_num" jdbcType="INTEGER" property="cancelNum"/>
        <!-- 成功率 -->
    <result column="success_rate" jdbcType="VARCHAR" property="successRate"/>
        <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
    </resultMap>

  <resultMap id="DetailResultMap" type="net.northking.cctp.executePlan.dto.planBatch.AtuPlanBatchDetailDto">
    <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
    <!-- 计划主键 -->
    <result column="plan_id" jdbcType="VARCHAR" property="planId"/>
    <!-- 租户主键 -->
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
    <!-- 计划名称 -->
    <result column="plan_name" jdbcType="VARCHAR" property="planName"/>
    <!-- 任务批次 -->
    <result column="batch" jdbcType="INTEGER" property="batch"/>
    <!-- 状态;0-等待中，1-执行中，2-已完成，3-已取消 -->
    <result column="status" jdbcType="CHAR" property="status"/>
    <!-- 调度开始时间 -->
    <result column="start_time" jdbcType="TIMESTAMP" property="startTime"/>
    <!-- 调度结束时间 -->
    <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
    <!-- 任务总数 -->
    <result column="task_total" jdbcType="INTEGER" property="taskTotal"/>
    <!-- 执行成功脚本数 -->
    <result column="success_num" jdbcType="INTEGER" property="successNum"/>
    <!-- 执行失败脚本数 -->
    <result column="exec_fail_num" jdbcType="INTEGER" property="execFailNum"/>
    <!-- 断言失败脚本数 -->
    <result column="assert_fail_num" jdbcType="INTEGER" property="assertFailNum"/>
    <!-- 超时脚本数 -->
    <result column="timeout_num" jdbcType="INTEGER" property="timeoutNum"/>
    <!-- 取消脚本数 -->
    <result column="cancel_num" jdbcType="INTEGER" property="cancelNum"/>
    <!-- 成功率 -->
    <result column="success_rate" jdbcType="VARCHAR" property="successRate"/>
    <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
    <!-- 执行人 -->
    <result column="executed_by" jdbcType="VARCHAR" property="executedBy"/>
    <!-- 触发类型；1-自动，2-手动，3-devops -->
    <result column="trigger_type" jdbcType="CHAR" property="triggerType"/>
    <!-- 脚本总数 -->
    <result column="script_num" jdbcType="INTEGER" property="scriptNum"/>
    <!-- 批次性能数据文件存放地址 -->
    <result column="per_data_path" jdbcType="VARCHAR" property="perDataPath"/>
  </resultMap>
  <update id="updateAttachmentClearStatusByIds">
    UPDATE
    <include refid="Table_Name"></include>
    set attachment_clear_status = 1
    where id in
    <foreach collection="batchIds" item="batchId" close=")" open="(" separator=",">
      #{batchId}
    </foreach>
  </update>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.executePlan.db.entity.AtuPlanBatch">
    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="planId != null">
        AND plan_id = #{planId,jdbcType=VARCHAR}
      </if>
      <if test="batch != null">
        AND batch = #{batch,jdbcType=INTEGER}
      </if>
      <if test="status != null">
        AND status = #{status,jdbcType=CHAR}
      </if>
      <if test="startTime != null">
        AND start_time = #{startTime,jdbcType=TIMESTAMP}
      </if>
      <if test="endTime != null">
        AND end_time = #{endTime,jdbcType=TIMESTAMP}
      </if>
      <if test="taskTotal != null">
        AND task_total = #{taskTotal,jdbcType=INTEGER}
      </if>
      <if test="successNum != null">
        AND success_num = #{successNum,jdbcType=INTEGER}
      </if>
      <if test="execFailNum != null">
        AND exec_fail_num = #{execFailNum,jdbcType=INTEGER}
      </if>
      <if test="assertFailNum != null">
        AND assert_fail_num = #{assertFailNum,jdbcType=INTEGER}
      </if>
      <if test="timeoutNum != null">
        AND timeout_num = #{timeoutNum,jdbcType=INTEGER}
      </if>
      <if test="cancelNum != null">
        AND cancel_num = #{cancelNum,jdbcType=INTEGER}
      </if>
      <if test="successRate != null">
        AND success_rate = #{successRate,jdbcType=VARCHAR}
      </if>
      <if test="createdTime != null">
        AND created_time = #{createdTime,jdbcType=TIMESTAMP}
      </if>
      <if test="source=='PROJECT'">
        AND status in('2','3')
      </if>
    </trim>
    order by created_time desc, batch desc
  </select>

  <select id="queryPlanMaxBatchNumList" resultMap="BaseResultMap">
    SELECT plan_id, MAX(batch) batch FROM atu_plan_batch b
    where created_time &lt;= #{endTime}
    and not EXISTS (select id from atu_plan_info i where is_deleted = 1 and b.plan_id = i.id)
    GROUP BY plan_id order by plan_id limit ${fromIndex}, ${toIndex}
  </select>

  <select id="queryBatchByPlan" resultMap="BaseResultMap" parameterType="string">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where
    plan_id = #{planId,jdbcType=VARCHAR}
    order by created_time desc
  </select>

  <select id="queryBatchDetailById" resultMap="DetailResultMap">
    select
    b.id
    ,b.plan_id
    ,b.batch
    ,b.status
    ,b.start_time
    ,b.end_time
    ,b.task_total
    ,b.success_num
    ,b.exec_fail_num
    ,b.assert_fail_num
    ,b.timeout_num
    ,b.cancel_num
    ,b.success_rate
    ,b.created_time
    ,b.executed_by
    ,b.trigger_type
    ,b.script_num
    ,per_data_path
    ,i.plan_name
    ,i.tenant_id
    from
    <include refid="Table_Name"/> b left join atu_plan_info i on i.id = b.plan_id
    where b.id = #{batchId}
  </select>
  <select id="countPlanMaxBatch" resultType="java.lang.Integer">
    SELECT count(DISTINCT plan_id) FROM atu_plan_batch b where created_time &lt;= #{endTime} and
    not EXISTS (select id from atu_plan_info i where is_deleted = 1 and b.plan_id = i.id)
  </select>
    <select id="countUnFinishById" resultType="java.lang.Long">
      select count(1) from atu_plan_batch where id = #{batchId} and status in ('0','1')
    </select>
    <select id="queryNeedClearBatch" resultType="net.northking.cctp.executePlan.db.entity.AtuPlanBatch">
      SELECT * FROM atu_plan_batch
      WHERE end_time &lt;= DATE_FORMAT(#{localDateTime},'%Y-%m-%d')
      and attachment_clear_status = 0
    </select>
</mapper>