<?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.snailjob.template.datasource.persistence.mapper.JobSummaryMapper">

    <insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="false">
        INSERT INTO sj_job_summary (namespace_id, group_name, business_id, trigger_at, system_task_type,
                                    success_num,fail_num,fail_reason,stop_num,stop_reason, cancel_num,cancel_reason)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.namespaceId},
                #{item.groupName},
                #{item.businessId},
                #{item.triggerAt},
                #{item.systemTaskType},
                #{item.successNum},
                #{item.failNum},
                #{item.failReason},
                #{item.stopNum},
                #{item.stopReason},
                #{item.cancelNum},
                #{item.cancelReason}
            )
        </foreach>
    </insert>

    <select id="selectJobTaskList"
            resultType="com.aizuda.snailjob.template.datasource.persistence.dataobject.DashboardRetryLineResponseDO$Task">
        SELECT group_name                                               AS groupName,
               SUM(CASE WHEN (job_status = 1) THEN 1 ELSE 0 END)        AS run,
               COUNT(*)                                                 AS total
          FROM sj_job
        ${ew.customSqlSegment}
         GROUP BY namespace_id, group_name
         ORDER BY group_name
    </select>

    <select id="selectJobTaskListCount" resultType="long">
        SELECT COUNT(DISTINCT group_name)
          FROM sj_job
        ${ew.customSqlSegment}
    </select>

    <select id="selectWorkflowTaskList"
            resultType="com.aizuda.snailjob.template.datasource.persistence.dataobject.DashboardRetryLineResponseDO$Task">
        SELECT group_name                                               AS groupName,
               SUM(CASE WHEN (workflow_status = 1) THEN 1 ELSE 0 END)   AS run,
               COUNT(*)                                                 AS total
          FROM sj_workflow
        ${ew.customSqlSegment}
         GROUP BY namespace_id, group_name
         ORDER BY group_name
    </select>

    <select id="selectWorkflowTaskListCount" resultType="long">
        SELECT COUNT(DISTINCT group_name)
          FROM sj_workflow
        ${ew.customSqlSegment}
    </select>

</mapper>