<?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.aizuda.easy.retry.template.datasource.persistence.mapper.JobTaskBatchMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.aizuda.easy.retry.template.datasource.persistence.po.JobTaskBatch">
        <id column="id" property="id" />
        <result column="group_name" property="groupName" />
        <result column="job_id" property="jobId" />
        <result column="namespace_id" property="namespaceId" />
        <result column="task_batch_status" property="taskBatchStatus" />
        <result column="create_dt" property="createDt" />
        <result column="update_dt" property="updateDt" />
        <result column="deleted" property="deleted" />
    </resultMap>
    <select id="selectJobBatchPageList"
        parameterType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.JobBatchQueryDO"
        resultType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.JobBatchResponseDO">
        SELECT a.*, b.job_name, b.task_type, b.block_strategy, b.trigger_type
        FROM job_task_batch a join job b on a.job_id = b.id
        <where>
            a.namespace_id = #{queryDO.namespaceId}
            <if test="queryDO.jobId != null">
                and a.job_id = #{queryDO.jobId}
            </if>
            <if test="queryDO.groupNames != null and queryDO.groupNames.size > 0">
                and a.group_name IN
                <foreach collection="queryDO.groupNames" item="groupName"  open="("  separator="," close=")">
                    #{groupName}
                </foreach>
            </if>
            <if test="queryDO.taskBatchStatus != null">
                and task_batch_status = #{queryDO.taskBatchStatus}
            </if>
            <if test="queryDO.jobName != null">
                and job_name like #{queryDO.jobName}
            </if>
         and a.deleted = 0
        order by a.id desc
        </where>

    </select>

    <select id="summaryJobBatchList"
            resultType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.JobBatchSummaryResponseDO">
        SELECT namespace_id                                             AS namespaceId,
               job_id                                                   AS jobId,
               group_name                                               AS groupName,
               task_batch_status                                        AS taskBatchStatus,
               operation_reason                                         AS operationReason,
               COUNT(operation_reason)                                  AS operationReasonTotal,
               SUM(CASE WHEN (task_batch_status = 3) THEN 1 ELSE 0 END) AS successNum,
               SUM(CASE WHEN (task_batch_status = 6) THEN 1 ELSE 0 END) AS cancelNum,
               SUM(CASE WHEN (task_batch_status = 5) THEN 1 ELSE 0 END) AS stopNum,
               SUM(CASE WHEN (task_batch_status = 4) THEN 1 ELSE 0 END) AS failNum
        FROM job_task_batch
        WHERE create_dt BETWEEN #{from} AND #{to}
        GROUP BY namespace_id, group_name, job_id, task_batch_status, operation_reason

    </select>

    <select id="selectJobBatchListByIds"
            resultType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.JobBatchResponseDO">
        SELECT a.*, b.job_name, b.task_type, b.block_strategy, b.trigger_type,b.executor_info,b.args_str
        FROM job_task_batch a join job b on a.job_id = b.id
        <where>
            a.id in
            <foreach collection="ids" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
            and a.deleted = 0
        </where>
    </select>
</mapper>