<?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.JobSummaryMapper">
    <resultMap id="BaseResultMap" type="com.aizuda.easy.retry.template.datasource.persistence.po.JobSummary">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="namespace_id" jdbcType="TINYINT" property="namespaceId"/>
        <result column="group_name" jdbcType="TINYINT" property="groupName"/>
        <result column="business_id" jdbcType="TINYINT" property="businessId"/>
        <result column="trigger_at" jdbcType="TIMESTAMP" property="triggerAt"/>
        <result column="system_task_type" jdbcType="TINYINT" property="systemTaskType"/>
        <result column="success_num" jdbcType="TINYINT" property="successNum"/>
        <result column="fail_num" jdbcType="TINYINT" property="failNum"/>
        <result column="fail_reason" jdbcType="VARCHAR" property="failReason"/>
        <result column="stop_num" jdbcType="TINYINT" property="stopNum"/>
        <result column="stop_reason" jdbcType="VARCHAR" property="stopReason"/>
        <result column="cancel_num" jdbcType="TINYINT" property="cancelNum"/>
        <result column="cancel_reason" jdbcType="VARCHAR" property="cancelReason"/>
        <result column="create_dt" jdbcType="TIMESTAMP" property="createDt"/>
        <result column="update_dt" jdbcType="TIMESTAMP" property="updateDt"/>
    </resultMap>

    <insert id="insertOrUpdate" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
        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>
        ON DUPLICATE KEY UPDATE
        success_num = values(`success_num`),
        fail_num = values(`fail_num`),
        fail_reason = values(`fail_reason`),
        stop_num = values(`stop_num`),
        stop_reason = values(`stop_reason`),
        cancel_num = values(`cancel_num`),
        cancel_reason = values(`cancel_reason`)
    </insert>

    <select id="jobLineList"
            resultType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.DashboardLineResponseDO">
        SELECT
        DATE_FORMAT(trigger_at, #{dateFormat})                         AS createDt,
        ifnull(SUM(success_num), 0)                                    AS success,
        ifnull(SUM(stop_num), 0)                                       AS stop,
        ifnull(SUM(cancel_num), 0)                                     AS cancel,
        ifnull(SUM(fail_num), 0)                                       AS fail,
        ifnull(SUM(success_num + fail_num + stop_num + cancel_num), 0) AS total
        FROM job_summary
        <where>
            <if test="groupNames != null and groupNames.size > 0">
                AND group_name IN
                <foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
                    #{groupName}
                </foreach>
            </if>
            <if test="groupName != null and groupName != '' ">
                AND group_name = #{groupName}
            </if>
            AND system_task_type = #{systemTaskType} AND namespace_id = #{namespaceId} AND trigger_at BETWEEN #{from} AND #{to}
        </where>
        GROUP BY DATE_FORMAT(trigger_at, #{dateFormat})
    </select>

    <select id="toJobTask"
            resultType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.DashboardCardResponseDO$JobTask">
        SELECT
        ifnull(sum(success_num), 0) AS successNum,
        ifnull(sum(stop_num), 0) AS stopNum,
        ifnull(sum(cancel_num), 0) AS cancelNum,
        ifnull(sum(fail_num), 0) AS failNum,
        ifnull(sum(success_num + fail_num + stop_num + cancel_num), 0) AS totalNum
        FROM job_summary
        WHERE system_task_type = #{systemTaskType} AND namespace_id = #{namespaceId}
        <if test="groupNames != null and groupNames.size > 0">
            AND group_name IN
            <foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
                #{groupName}
            </foreach>
        </if>
    </select>

    <select id="dashboardRank"
            resultType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.DashboardRetryLineResponseDO$Rank">
        SELECT
        <if test="systemTaskType == 3">
            CONCAT(group_name, '/', (SELECT job_name FROM job WHERE id = business_id)) name,
        </if>
        <if test="systemTaskType == 4">
        CONCAT(group_name, '/', (SELECT workflow_name FROM workflow WHERE id = business_id)) name,
        </if>
        SUM(fail_num) AS total FROM job_summary
        <where>
            <if test="groupNames != null and groupNames.size > 0">
                AND group_name IN
                <foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
                    #{groupName}
                </foreach>
            </if>
            <if test="groupName != '' and groupName != null">
                AND group_name = #{groupName}
            </if>
            AND trigger_at >= #{startTime} AND trigger_at &lt;= #{endTime}
        </where>
        AND system_task_type = #{systemTaskType}
        AND namespace_id = #{namespaceId}
        GROUP BY namespace_id, group_name, business_id
        HAVING total > 0
        ORDER BY total DESC LIMIT 10
    </select>

    <select id="jobTaskList"
            resultType="com.aizuda.easy.retry.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 job
        WHERE namespace_id = #{namespaceId}
        <if test="groupNames != null and groupNames.size > 0">
            AND group_name IN
            <foreach collection="groupNames" item="groupName"  open="("  separator="," close=")">
                #{groupName}
            </foreach>
        </if>
        GROUP BY namespace_id, group_name
    </select>
</mapper>