<?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.server.persistence.mybatis.mapper.RetryTaskLogMapper">
    <resultMap id="BaseResultMap" type="com.aizuda.easy.retry.server.persistence.mybatis.po.RetryTaskLog">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="unique_id" jdbcType="VARCHAR" property="uniqueId"/>
        <result column="group_name" jdbcType="VARCHAR" property="groupName"/>
        <result column="scene_name" jdbcType="VARCHAR" property="sceneName"/>
        <result column="idempotent_id" jdbcType="VARCHAR" property="idempotentId"/>
        <result column="biz_no" jdbcType="VARCHAR" property="bizNo"/>
        <result column="executor_name" jdbcType="VARCHAR" property="executorName"/>
        <result column="args_str" jdbcType="VARCHAR" property="argsStr"/>
        <result column="ext_attrs" jdbcType="VARCHAR" property="extAttrs"/>
        <result column="retry_status" jdbcType="TINYINT" property="retryStatus"/>
        <result column="task_type" jdbcType="TINYINT" property="taskType"/>
        <result column="error_message" jdbcType="VARCHAR" property="errorMessage"/>
        <result column="create_dt" jdbcType="TIMESTAMP" property="createDt"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, unique_id, group_name, scene_name, idempotent_id, biz_no, executor_name, args_str, ext_attrs, retry_status, error_message,
    create_dt, task_type
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from retry_task_log
        where id = #{id,jdbcType=BIGINT}
    </select>
    <select id="countTaskTotal" resultType="java.lang.Long">
        select count(*)
        from (
                 select group_name, scene_name, idempotent_id from retry_task_log group by group_name, scene_name, idempotent_id) a
    </select>
    <select id="countTaskByRetryStatus" resultType="java.lang.Long">
        select count(*)
        from (
                 select group_name, scene_name, idempotent_id
                 from retry_task_log
                 where retry_status = #{retryStatus}
                 group by group_name, scene_name, idempotent_id) a

    </select>
    <select id="rankSceneQuantity"
            resultType="com.aizuda.easy.retry.server.web.model.response.SceneQuantityRankResponseVO">
        select group_name, scene_name, count(*) total
        from (
        select group_name, scene_name, idempotent_id, count(*)
        from retry_task_log
        <where>
            <if test="groupName != '' and groupName != null">
                group_name = #{groupName}
            </if>
            and create_dt >= #{startTime} and create_dt &lt;= #{endTime}
        </where>
        group by group_name, scene_name, idempotent_id) a
        group by group_name, scene_name
        order by total desc;
    </select>
    <select id="lineDispatchQuantity"
            resultType="com.aizuda.easy.retry.server.web.model.response.DispatchQuantityResponseVO">
        select
        distinct(create_dt), count(*) total
        from (
        select group_name, scene_name, idempotent_id,
        <choose>
            <when test="type == 'day'">
                DATE_FORMAT(create_dt,'%H')
            </when>
            <when test="type == 'week'">
                DATE_FORMAT(create_dt,'%Y-%m-%d')
            </when>
            <when test="type =='month'">
                DATE_FORMAT(create_dt,'%Y-%m-%d')
            </when>
            <when test="type == 'year'">
                DATE_FORMAT(create_dt,'%Y-%m')
            </when>
            <otherwise>
                DATE_FORMAT(create_dt,'%Y-%m-%d')
            </otherwise>
        </choose>
        as create_dt, count(*)
        from retry_task_log
        <where>
            <if test="groupName != '' and groupName != null">
                group_name = #{groupName}
            </if>
            <if test="retryStatus!=null ">
               and retry_status = #{retryStatus}
            </if>

            and create_dt >= #{startTime} and create_dt &lt;= #{endTime}
        </where>
        group by group_name, scene_name, idempotent_id, create_dt) a
        group by create_dt
        order by total desc;
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from retry_task_log
        where id = #{id,jdbcType=BIGINT}
    </delete>
</mapper>
