<?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.RetrySummaryMapper">

    <update id="updateBatch" parameterType="java.util.List">
        UPDATE sj_retry_summary
           SET running_num = src.running_num,
               finish_num = src.finish_num,
               max_count_num = src.max_count_num,
               suspend_num = src.suspend_num,
               update_dt = CURRENT_TIMESTAMP
          FROM sj_retry_summary AS dest
                JOIN (
                <foreach collection="list" item="item" index="index" separator="UNION ALL">
                    SELECT
                        #{item.runningNum}  AS running_num,
                        #{item.finishNum}   AS finish_num,
                        #{item.maxCountNum} AS max_count_num,
                        #{item.suspendNum}  AS suspend_num,
                        #{item.triggerAt}   AS trigger_at,
                        #{item.sceneName}   AS scene_name,
                        #{item.namespaceId} AS namespace_id,
                        #{item.groupName}   AS group_name
                </foreach>
                ) AS src ON (dest.trigger_at = src.trigger_at
                            AND dest.group_name = src.group_name
                            AND dest.scene_name = src.scene_name
                            AND dest.namespace_id = src.namespace_id)
    </update>

    <select id="selectRetryTask"
            resultType="com.aizuda.snailjob.template.datasource.persistence.dataobject.DashboardCardResponseDO$RetryTask">
        SELECT ISNULL(SUM(running_num), 0)                                              AS runningNum,
               ISNULL(SUM(finish_num), 0)                                               AS finishNum,
               ISNULL(SUM(max_count_num), 0)                                            AS maxCountNum,
               ISNULL(SUM(suspend_num), 0)                                              AS suspendNum,
               ISNULL(SUM(running_num + finish_num + max_count_num + suspend_num), 0)   AS totalNum
          FROM sj_retry_summary
        ${ew.customSqlSegment}
    </select>

    <select id="selectRetryTaskBarList"
            resultType="com.aizuda.snailjob.template.datasource.persistence.dataobject.DashboardCardResponseDO$RetryTask">
        SELECT TOP 7
            trigger_at,
            running_num,
            finish_num,
            max_count_num,
            suspend_num
        FROM sj_retry_summary
        ${ew.customSqlSegment}
    </select>

    <select id="selectRetryLineList"
            resultType="com.aizuda.snailjob.template.datasource.persistence.dataobject.DashboardLineResponseDO">
        SELECT createDt                                                                 AS createDt,
               ISNULL(SUM(finish_num), 0)                                               AS successNum,
               ISNULL(SUM(running_num), 0)                                              AS runningNum,
               ISNULL(SUM(max_count_num), 0)                                            AS maxCountNum,
               ISNULL(SUM(suspend_num), 0)                                              AS suspendNum,
               ISNULL(SUM(finish_num + running_num + max_count_num + suspend_num), 0)   AS total
        FROM (
            SELECT FORMAT(create_dt, #{dateFormat}) AS createDt,
                   finish_num,
                   running_num,
                   max_count_num,
                   suspend_num
              FROM sj_retry_summary
            ${ew.customSqlSegment}
        ) AS subquery
        GROUP BY createDt
    </select>

    <select id="selectDashboardRankList"
            resultType="com.aizuda.snailjob.template.datasource.persistence.dataobject.DashboardRetryLineResponseDO$Rank">
        SELECT TOP 10
            CONCAT(group_name, '/', scene_name)                         AS name,
            SUM(running_num + finish_num + max_count_num + suspend_num) AS total
        FROM sj_retry_summary
        ${ew.customSqlSegment}
        HAVING SUM(running_num + finish_num + max_count_num + suspend_num) > 0
        ORDER BY total DESC
    </select>

</mapper>
