<?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:09  -->
<mapper namespace="net.northking.cctp.executePlan.db.dao.AtuPlanInfoDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.executePlan.dto.planInfo.AtuPlanInfoDetailDto">
      <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                                                                                <!-- 租户主键 -->
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 系统主键 -->
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 计划名称 -->
    <result column="plan_name" jdbcType="VARCHAR" property="planName"/>
        <!-- 计划描述 -->
    <result column="plan_desc" jdbcType="VARCHAR" property="planDesc"/>
        <!-- 是否启用;0-否，1-是 -->
    <result column="is_enabled" jdbcType="BIT" property="enabled"/>
        <!-- 状态;0-新建，1-等待中，2-执行中，3-已完成，4-已取消 -->
    <result column="status" jdbcType="CHAR" property="status"/>
        <!-- 应用设置;1-清除数据，2-卸载安装 -->
    <result column="app_set" jdbcType="CHAR" property="appSet"/>
        <!-- 录屏设置;0-不保存，1-全部保存，2-出错保存 -->
    <result column="screen_record_set" jdbcType="CHAR" property="screenRecordSet"/>
        <!-- 截图设置;0-不保存，1-全部保存，2-出错保存 -->
    <result column="screenshot_set" jdbcType="CHAR" property="screenshotSet"/>
        <!-- 优先级;0-低，1-高 -->
    <result column="priority" jdbcType="CHAR" property="priority"/>
        <!-- Cron表达式 -->
    <result column="cron_expr" jdbcType="VARCHAR" property="cronExpr"/>
        <!-- 前置计划ID -->
    <result column="up_front_plan_id" jdbcType="VARCHAR" property="upFrontPlanId"/>
        <!-- 触发链接 -->
    <result column="webhook_url" jdbcType="VARCHAR" property="webhookUrl"/>
        <!-- 密保令牌 -->
    <result column="webhook_token" jdbcType="VARCHAR" property="webhookToken"/>
        <!-- 是否发送邮件;0-否，1-是 -->
    <result column="is_send_mail" jdbcType="BIT" property="sendMail"/>
        <!-- 邮件地址;当需要发送邮件时不能为空 -->
    <result column="mail_address" jdbcType="VARCHAR" property="mailAddress"/>
        <!-- 是否失败重试;0-否，1-是 -->
    <result column="is_fail_retry" jdbcType="BIT" property="failRetry"/>
        <!-- 失败重试次数 -->
    <result column="fail_retry_count" jdbcType="INTEGER" property="failRetryCount"/>
    <!-- 失败重试策略 -->
    <result column="retry_strategy" jdbcType="VARCHAR" property="retryStrategy"/>
        <!-- 是否失败生成计划;0-否，1-是 -->
    <result column="is_fail_create_plan" jdbcType="BIT" property="failCreatePlan"/>
        <!-- 最近执行时间 -->
    <result column="last_exec_time" jdbcType="TIMESTAMP" property="lastExecTime"/>
        <!-- 下次执行时间 -->
    <result column="next_exec_time" jdbcType="TIMESTAMP" property="nextExecTime"/>
        <!-- 最后批次号 -->
    <result column="last_batch_id" jdbcType="VARCHAR" property="lastBatchId"/>
        <!-- 最后批次等待中任务数 -->
    <result column="waiting_num" jdbcType="INTEGER" property="waitingNum"/>
        <!-- 最后批次执行中任务数 -->
    <result column="running_num" jdbcType="INTEGER" property="runningNum"/>
        <!-- 最后批次成功任务数 -->
    <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="created_by" jdbcType="VARCHAR" property="createdBy"/>
        <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <!-- 更新人 -->
    <result column="updated_by" jdbcType="VARCHAR" property="updatedBy"/>
        <!-- 更新时间 -->
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <!-- 是否删除;0-否，1-是 -->
    <result column="is_deleted" jdbcType="BIT" property="deleted"/>

    <result column="has_corn" jdbcType="BIT" property="hasCorn"/>

    <result column="has_up_front" jdbcType="BIT" property="hasUpFront"/>

    <result column="has_webhook" jdbcType="BIT" property="hasWebhook"/>

    <result column="case_change_flag" jdbcType="BIT" property="caseChangeFlag"/>

    <result column="principal_id" jdbcType="VARCHAR" property="principalId"/>
    </resultMap>

  <resultMap id="PlanBatchMap" type="net.northking.cctp.executePlan.dto.planBatch.AtuPlanBatchDto">
    <!-- 租户主键 -->
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
    <!-- 系统主键 -->
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
    <!-- 计划主键 -->
    <result column="plan_id" jdbcType="VARCHAR" property="planId"/>
    <!-- 批次主键 -->
    <result column="batch_id" jdbcType="VARCHAR" property="batchId"/>
    <!-- 计划名称 -->
    <result column="plan_name" jdbcType="VARCHAR" property="planName"/>
    <!-- 任务批次 -->
    <result column="batch" jdbcType="INTEGER" property="batch"/>
  </resultMap>


  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.executePlan.db.entity.AtuPlanInfo">
    select
    id
    ,tenant_id
    ,project_id
    ,plan_name
    ,plan_desc
    ,is_enabled
    ,status
    ,app_set
    ,screen_record_set
    ,screenshot_set
    ,priority
    ,cron_expr
    ,up_front_plan_id
    ,webhook_url
    ,webhook_token
    ,is_send_mail
    ,mail_address
    ,is_fail_retry
    ,fail_retry_count
    ,retry_strategy
    ,is_fail_create_plan
    ,last_exec_time
    ,next_exec_time
    ,last_batch_id
    ,waiting_num
    ,running_num
    ,success_num
    ,exec_fail_num
    ,assert_fail_num
    ,timeout_num
    ,cancel_num
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    ,is_deleted
    ,has_corn
    ,has_up_front
    ,has_webhook
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      is_deleted = false
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="planName != null">
        AND LOCATE(#{planName,jdbcType=VARCHAR},plan_name) > 0
      </if>
      <if test="planDesc != null">
        AND plan_desc = #{planDesc,jdbcType=VARCHAR}
      </if>
      <if test="enabled != null">
        AND is_enabled = #{enabled,jdbcType=BIT}
      </if>
      <if test="status != null">
        AND status = #{status,jdbcType=CHAR}
      </if>
      <if test="appSet != null">
        AND app_set = #{appSet,jdbcType=CHAR}
      </if>
      <if test="screenRecordSet != null">
        AND screen_record_set = #{screenRecordSet,jdbcType=CHAR}
      </if>
      <if test="screenshotSet != null">
        AND screenshot_set = #{screenshotSet,jdbcType=CHAR}
      </if>
      <if test="priority != null">
        AND priority = #{priority,jdbcType=CHAR}
      </if>
      <if test="cronExpr != null">
        AND cron_expr = #{cronExpr,jdbcType=VARCHAR}
      </if>
      <if test="upFrontPlanId != null">
        AND up_front_plan_id = #{upFrontPlanId,jdbcType=VARCHAR}
      </if>
      <if test="webhookUrl != null">
        AND webhook_url = #{webhookUrl,jdbcType=VARCHAR}
      </if>
      <if test="webhookToken != null">
        AND webhook_token = #{webhookToken,jdbcType=VARCHAR}
      </if>
      <if test="sendMail != null">
        AND is_send_mail = #{sendMail,jdbcType=BIT}
      </if>
      <if test="mailAddress != null">
        AND mail_address = #{mailAddress,jdbcType=VARCHAR}
      </if>
      <if test="failRetry != null">
        AND is_fail_retry = #{failRetry,jdbcType=BIT}
      </if>
      <if test="failRetryCount != null">
        AND fail_retry_count = #{failRetryCount,jdbcType=INTEGER}
      </if>
      <if test="retryStrategy != null">
        AND retry_strategy = #{retryStrategy,jdbcType=VARCHAR}
      </if>
      <if test="failCreatePlan != null">
        AND is_fail_create_plan = #{failCreatePlan,jdbcType=BIT}
      </if>
      <if test="lastExecTime != null">
        AND last_exec_time = #{lastExecTime,jdbcType=TIMESTAMP}
      </if>
      <if test="nextExecTime != null">
        AND next_exec_time = #{nextExecTime,jdbcType=TIMESTAMP}
      </if>
      <if test="lastBatchId != null">
        AND last_batch_id = #{lastBatchId,jdbcType=VARCHAR}
      </if>
      <if test="waitingNum != null">
        AND waiting_num = #{waitingNum,jdbcType=INTEGER}
      </if>
      <if test="runningNum != null">
        AND running_num = #{runningNum,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="createdBy != null">
        AND created_by = #{createdBy,jdbcType=VARCHAR}
      </if>
      <if test="createdTime != null">
        AND created_time = #{createdTime,jdbcType=TIMESTAMP}
      </if>
      <if test="updatedBy != null">
        AND updated_by = #{updatedBy,jdbcType=VARCHAR}
      </if>
      <if test="updatedTime != null">
        AND updated_time = #{updatedTime,jdbcType=TIMESTAMP}
      </if>
    </trim>
  </select>

  <select id="queryByLastBatchId" resultMap="BaseResultMap">
    select id, last_batch_id from <include refid="Table_Name"/> where last_batch_id = #{batchId}
  </select>

  <select id="queryFollowUpPlanIdList" resultType="java.lang.String">
    select id from <include refid="Table_Name"/>
    where up_front_plan_id = #{planId} and is_enabled = 1 and is_deleted = 0 and has_up_front = 1
  </select>

  <select id="queryPageByPlan" parameterType="net.northking.cctp.executePlan.dto.planInfo.AtuPlanInfoQueryDto" resultMap="DetailMap">
    select
    id
    ,tenant_id
    ,project_id
    ,plan_name
    ,plan_desc
    ,is_enabled
    ,status
    ,app_set
    ,screen_record_set
    ,screenshot_set
    ,priority
    ,cron_expr
    ,up_front_plan_id
    ,webhook_url
    ,webhook_token
    ,is_send_mail
    ,mail_address
    ,is_fail_retry
    ,fail_retry_count
    ,retry_strategy
    ,is_fail_create_plan
    ,last_exec_time
    ,next_exec_time
    ,last_batch_id
    ,waiting_num
    ,running_num
    ,success_num
    ,exec_fail_num
    ,assert_fail_num
    ,timeout_num
    ,cancel_num
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    ,is_deleted
    ,has_corn
    ,has_up_front
    ,has_webhook
    ,case_change_flag
    ,principal_id
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      is_deleted = false
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="projectList != null and projectList.size() > 0">
        AND project_id in
        <foreach collection="projectList" item="projectId" open="(" separator="," close=")">
          #{projectId,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="planName != null">
        AND LOCATE(#{planName,jdbcType=VARCHAR},plan_name) > 0
      </if>
      <if test="status != null">
        AND status = #{status,jdbcType=CHAR}
      </if>
      <if test="tableFlag == true">
        AND status !=0
      </if>
      <if test="startCreatedTime!=null">
        and created_time <![CDATA[>=]]> #{startCreatedTime}
      </if>
      <if test="endCreatedTime!=null">
        and created_time <![CDATA[<=]]> #{endCreatedTime}
      </if>
      <if test="startUpdatedTime!=null">
        and updated_time <![CDATA[>=]]> #{startUpdatedTime}
      </if>
      <if test="endUpdatedTime!=null">
        and updated_time <![CDATA[<=]]> #{endUpdatedTime}
      </if>
      <if test="principalIds != null and principalIds.size > 0">
        AND principal_id IN
        <foreach collection="principalIds" open="(" index="index" item="item" close=")" separator=",">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>

      <if test="principalId != null and principalId!=''">
        AND principal_id =#{principalId}
      </if>

      <if test="createBys != null and createBys.size > 0">
        AND created_by IN
        <foreach collection="createBys" open="(" index="index" item="item" close=")" separator=",">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>

      <if test="creatorId != null and creatorId!=''">
        AND created_by =#{creatorId}
      </if>

      <if test="updateBy != null and updateBy!=''">
        AND updated_by =#{updateBy}
      </if>

    </trim>
    order by created_time desc
  </select>

  <select id="clonePlanName" resultType="string" parameterType="net.northking.cctp.executePlan.db.entity.AtuPlanInfo">
    select
    plan_name
    from
    <include refid="Table_Name"/>
    where
    plan_name REGEXP #{planName,jdbcType=VARCHAR}
    and tenant_id = #{tenantId,jdbcType=VARCHAR}
    and project_id = #{projectId,jdbcType=VARCHAR}
    order by LENGTH(plan_name) DESC, plan_name desc
    limit 0,1
  </select>

  <select id="checkPlanName" resultType="string" parameterType="net.northking.cctp.executePlan.db.entity.AtuPlanInfo">
    select id
    from
    <include refid="Table_Name"/>
    where
    plan_name = #{planName,jdbcType=VARCHAR}
    and is_deleted = false
    and id != #{id,jdbcType=VARCHAR}
    and project_id = #{projectId,jdbcType=VARCHAR}
  </select>

  <select id="findByBatchId" resultMap="BaseResultMap">
    select
    id
    ,tenant_id
    ,project_id
    ,plan_name
    ,plan_desc
    ,is_enabled
    ,status
    ,app_set
    ,screen_record_set
    ,screenshot_set
    ,priority
    ,cron_expr
    ,up_front_plan_id
    ,webhook_url
    ,webhook_token
    ,is_send_mail
    ,mail_address
    ,is_fail_retry
    ,fail_retry_count
    ,retry_strategy
    ,is_fail_create_plan
    ,last_exec_time
    ,next_exec_time
    ,last_batch_id
    ,waiting_num
    ,running_num
    ,success_num
    ,exec_fail_num
    ,assert_fail_num
    ,timeout_num
    ,cancel_num
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    ,is_deleted
    ,has_corn
    ,has_up_front
    ,has_webhook
    ,principal_id
    from
    <include refid="Table_Name"/>
    where id = (select plan_id from atu_plan_batch where id = #{batchId})
  </select>

  <select id="queryByTaskId" resultMap="PlanBatchMap">
    select p.tenant_id,p.project_id,p.id plan_id,p.plan_name,b.id batch_id,b.batch
    from <include refid="Table_Name"/> p left join atu_plan_batch b on b.plan_id = p.id
    left join atu_plan_task t on t.batch_id = b.id
    where t.id = #{taskId}
  </select>

  <update id="updatePlanNextExecTime">
    update <include refid="Table_Name"/> set next_exec_time = #{nextExecTime} where id = #{id}
  </update>

  <update id="updatePlanByLastBatchId">
    update <include refid="Table_Name"/> set status = #{status} where last_batch_id = #{batchId}
  </update>

  <update id="updatePlanByIds">
    update
    <include refid="Table_Name"/>
    set case_change_flag = 1
    where id in
    <foreach collection="ids" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </update>


  <select id="queryPlanInfoByIds" resultType="net.northking.cctp.executePlan.db.entity.AtuPlanInfo">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where is_deleted = '0' and id IN
    <foreach collection="list" open="(" index="index" item="item" close=")" separator=",">
      #{item,jdbcType=VARCHAR}
    </foreach>
  </select>

  <select id="checkNameUnique" resultType="java.lang.Integer" parameterType="net.northking.cctp.executePlan.db.entity.AtuPlanInfo">
    select
    1
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id != #{id,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="planName != null">
        AND plan_name = #{planName,jdbcType=VARCHAR}
      </if>
      <if test="deleted != null">
        AND is_deleted = #{deleted,jdbcType=BIT}
      </if>
    </trim>
    limit 1
  </select>

  <update id="updatePrincipal" parameterType="net.northking.cctp.executePlan.dto.planInfo.AtuPlanPrincipalDto">
    update
    <include refid="Table_Name"/>
    <set>
      principal_id = #{principalId, jdbcType=VARCHAR}
      <if test="updatedBy != null">
        , updated_by = #{updatedBy}
      </if>
      <if test="updatedTime != null">
        , updated_time = #{updatedTime,jdbcType=TIMESTAMP}
      </if>
    </set>
    where id in
    <foreach collection="planList" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </update>
  <update id="updatePlanRetryCount">
    update <include refid="Table_Name"/> set fail_retry_count = #{failRetryCount} where id = #{id}
  </update>

  <select id="queryUpFrontPlan" resultMap="DetailMap" parameterType="net.northking.cctp.executePlan.dto.planInfo.AtuPlanInfoPageDto">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND">
      is_deleted = false
      <if test="planId != null">
        AND id != #{planId,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="planName != null and planName != ''">
        AND plan_name like "%"#{planName,jdbcType=VARCHAR}"%"
      </if>
      <if test="status != null">
        AND status = #{status,jdbcType=CHAR}
      </if>
    </trim>
    order by created_time desc
  </select>
    <select id="queryByPage" resultMap="BaseResultMap">
      select
      <include refid="Base_Column_List" />
      from
      <include refid="Table_Name"/>
      order by created_time
      limit ${fromIndex}, ${toIndex}
    </select>
  <select id="countDeleted" parameterType="net.northking.cctp.executePlan.db.entity.AtuPlanInfo" resultType="java.lang.Integer">
    select
    count(1)
    from
    <include refid="Table_Name"/>
    where 1 = 1
    <if test="id != null">
      AND id = #{id,jdbcType=VARCHAR}
    </if>
    <if test="isDeleted != null">
      AND is_deleted = #{isDeleted,jdbcType=VARCHAR}
    </if>
  </select>
    <select id="selectManagerByProject" resultType="java.lang.String">
      SELECT principal_id FROM atu_plan_info
      WHERE is_deleted = 0 and project_id = #{projectId}
      GROUP BY principal_id
    </select>
  <select id="selectCreatorByProject" resultType="java.lang.String">
    SELECT created_by FROM atu_execute_plan.atu_plan_info
    WHERE is_deleted = 0 and project_id = #{projectId}
    GROUP BY created_by
  </select>
  <select id="selectUpdaterByProject" resultType="java.lang.String">
    SELECT updated_by FROM atu_execute_plan.atu_plan_info
    WHERE is_deleted = 0 and project_id = #{projectId}
    GROUP BY updated_by
  </select>
</mapper>