gtsoft-snail-job-server/easy-retry-datasource/easy-retry-sqlserver-datasource/src/main/resources/sqlserver/mapper/RetrySummaryMapper.xml

199 lines
8.8 KiB
XML
Raw Normal View History

<?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.RetrySummaryMapper">
<resultMap id="BaseResultMap" type="com.aizuda.easy.retry.template.datasource.persistence.po.RetrySummary">
<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="scene_name" jdbcType="TINYINT" property="sceneName"/>
<result column="trigger_at" jdbcType="TIMESTAMP" property="triggerAt"/>
<result column="running_num" jdbcType="TINYINT" property="runningNum"/>
<result column="finish_num" jdbcType="TINYINT" property="finishNum"/>
<result column="max_count_num" jdbcType="VARCHAR" property="maxCountNum"/>
<result column="suspend_num" jdbcType="TINYINT" property="suspendNum"/>
<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">
MERGE INTO retry_summary AS target
USING (
VALUES
<foreach collection="list" item="item" separator=",">
(
#{item.namespaceId},
#{item.groupName},
#{item.sceneName},
#{item.triggerAt},
#{item.runningNum},
#{item.finishNum},
#{item.maxCountNum},
#{item.suspendNum}
)
</foreach>
) AS source (namespace_id, group_name, scene_name, trigger_at,
running_num, finish_num, max_count_num, suspend_num)
ON target.namespace_id = source.namespace_id
AND target.group_name = source.group_name
AND target.scene_name = source.scene_name
AND target.trigger_at = source.trigger_at
WHEN MATCHED THEN
UPDATE SET
target.running_num = source.running_num,
target.finish_num = source.finish_num,
target.max_count_num = source.max_count_num,
target.suspend_num = source.suspend_num
WHEN NOT MATCHED THEN
INSERT (namespace_id, group_name, scene_name, trigger_at,
running_num, finish_num, max_count_num, suspend_num)
VALUES (source.namespace_id, source.group_name, source.scene_name, source.trigger_at,
source.running_num, source.finish_num, source.max_count_num, source.suspend_num);
</insert>
<select id="retryTask"
resultType="com.aizuda.easy.retry.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 retry_summary
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>
</select>
<select id="retryTaskBarList"
resultType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.DashboardCardResponseDO$RetryTask">
SELECT TOP 7
trigger_at,
running_num,
finish_num,
max_count_num,
suspend_num
FROM retry_summary
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>
ORDER BY id DESC
</select>
<select id="retryLineList"
resultType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.DashboardLineResponseDO">
SELECT
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
<choose>
<when test="type == 'DAY'">
FORMAT(create_dt, 'HH24')
</when>
<when test="type == 'WEEK'">
FORMAT(create_dt, 'yyyy-MM-dd')
</when>
<when test="type =='MONTH'">
FORMAT(create_dt, 'yyyy-MM-dd')
</when>
<when test="type == 'YEAR'">
FORMAT(create_dt, 'yyyy-MM')
</when>
<otherwise>
FORMAT(create_dt,'yyyy-MM-dd')
</otherwise>
</choose> AS createDt,
finish_num,
running_num,
max_count_num,
suspend_num
FROM retry_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 namespace_id = #{namespaceId}
AND trigger_at BETWEEN #{from} AND #{to}
</where>
) AS subquery
GROUP BY createDt
</select>
<select id="dashboardRank"
resultType="com.aizuda.easy.retry.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 retry_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 namespace_id = #{namespaceId}
AND trigger_at BETWEEN #{startTime} AND #{endTime}
</where>
GROUP BY namespace_id, group_name, scene_name
HAVING SUM(running_num + finish_num + max_count_num + suspend_num) > 0
ORDER BY total DESC
</select>
<!-- SQL Server GROUP BY 的分页必须 ORDER BY xxx -->
<select id="retryTaskList"
resultType="com.aizuda.easy.retry.template.datasource.persistence.dataobject.DashboardRetryLineResponseDO$Task">
SELECT
group_name AS groupName,
SUM(CASE WHEN (scene_status = 1) THEN 1 ELSE 0 END) AS run,
COUNT(*) AS total
FROM scene_config
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
ORDER BY group_name
</select>
<!-- 用于 retryTaskList 分页的 自定义 COUNT,
SQL Server SELECT COUNT(*) FROM (... ORDER BY group_name) 会报错 -->
<select id="sqlServer_jobTaskList_Count" resultType="java.lang.Integer">
SELECT COUNT(DISTINCT group_name)
FROM scene_config
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>
</select>
</mapper>