DspReportLogMapper.xml 6 KB
<?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.zteits.parking.module.park.dsp.dal.mysql.DspReportLogMapper">

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO dsp_report_log (
            platform_code, object_type, biz_key, request_id, request_path, http_status,
            total, success, failed, error_code, error_msg, payload, cost_ms, client_ip, create_time
        ) VALUES (
            #{platformCode}, #{objectType}, #{bizKey}, #{requestId}, #{requestPath}, #{httpStatus},
            #{total}, #{success}, #{failed}, #{errorCode}, #{errorMsg}, #{payload}, #{costMs}, #{clientIp}, #{createTime}
        )
    </insert>

    <!-- ==================== 接收日志页 ==================== -->

    <!--
      展示口径:1 全部成功 / 2 部分失败 / 3 失败。
      传输层拒绝(验签、限流)没有 total/success/failed,靠 error_code 判为失败。
    -->
    <sql id="resultStateExpr">
        CASE
            WHEN failed &gt; 0 AND success &gt; 0 THEN 2
            WHEN failed &gt; 0 THEN 3
            WHEN error_code IS NOT NULL AND error_code &lt;&gt; '0' THEN 3
            ELSE 1
        END
    </sql>

    <sql id="logColumns">
        id, platform_code, object_type, biz_key, request_id, request_path, http_status,
        total, success, failed, error_code, error_msg, cost_ms, client_ip, create_time
    </sql>

    <sql id="logWhere">
        <where>
            <if test="q.platformCode != null and q.platformCode != ''">
                AND platform_code = #{q.platformCode}
            </if>
            <if test="q.objectType != null and q.objectType != ''">
                AND object_type = #{q.objectType}
            </if>
            <if test="q.bizKey != null and q.bizKey != ''">
                AND biz_key LIKE CONCAT('%', #{q.bizKey}, '%')
            </if>
            <if test="q.requestId != null and q.requestId != ''">
                AND request_id = #{q.requestId}
            </if>
            <if test="q.startTime != null">
                AND create_time &gt;= #{q.startTime}
            </if>
            <if test="q.endTime != null">
                AND create_time &lt; #{q.endTime}
            </if>
            <if test="q.resultState != null">
                <choose>
                    <when test="q.resultState == 1">
                        AND failed = 0 AND (error_code IS NULL OR error_code = '0')
                    </when>
                    <when test="q.resultState == 2">
                        AND failed &gt; 0 AND success &gt; 0
                    </when>
                    <otherwise>
                        AND (failed &gt; 0 OR (error_code IS NOT NULL AND error_code &lt;&gt; '0'))
                        AND NOT (failed &gt; 0 AND success &gt; 0)
                    </otherwise>
                </choose>
            </if>
        </where>
    </sql>

    <select id="selectCount" resultType="java.lang.Long">
        SELECT COUNT(*) FROM dsp_report_log
        <include refid="logWhere"/>
    </select>

    <select id="selectPage" resultType="com.zteits.parking.module.park.dsp.dal.dataobject.DspReportLogDO">
        SELECT <include refid="logColumns"/>,
               <include refid="resultStateExpr"/> AS result_state
          FROM dsp_report_log
        <include refid="logWhere"/>
         ORDER BY id DESC
         LIMIT #{q.offset}, #{q.limit}
    </select>

    <select id="selectById" resultType="com.zteits.parking.module.park.dsp.dal.dataobject.DspReportLogDO">
        SELECT id, platform_code, object_type, biz_key, request_id, request_path, http_status,
               total, success, failed, error_code, error_msg, payload, cost_ms, client_ip, create_time,
               <include refid="resultStateExpr"/> AS result_state
          FROM dsp_report_log
         WHERE id = #{id}
    </select>

    <select id="selectForExport" resultType="com.zteits.parking.module.park.dsp.dal.dataobject.DspReportLogDO">
        SELECT <include refid="logColumns"/>,
               <include refid="resultStateExpr"/> AS result_state
          FROM dsp_report_log
        <include refid="logWhere"/>
         ORDER BY id DESC
         LIMIT 5000
    </select>

    <!-- ==================== 总览页统计 ==================== -->

    <select id="countByObjectType" resultType="java.util.Map">
        SELECT object_type AS objectType,
               COUNT(*) AS calls,
               SUM(CASE WHEN failed &gt; 0 THEN 1 ELSE 0 END) AS failed,
               SUM(CASE WHEN failed = 0 AND (error_code IS NULL OR error_code = '0') THEN 1 ELSE 0 END) AS success
          FROM dsp_report_log
         WHERE create_time &gt;= #{start} AND create_time &lt; #{end}
         GROUP BY object_type
         ORDER BY calls DESC
    </select>

    <select id="countByPlatform" resultType="java.util.Map">
        SELECT platform_code AS platformCode,
               COUNT(*) AS calls,
               SUM(CASE WHEN failed &gt; 0 THEN 1 ELSE 0 END) AS failed
          FROM dsp_report_log
         WHERE create_time &gt;= #{start} AND create_time &lt; #{end}
         GROUP BY platform_code
         ORDER BY calls DESC
    </select>

    <select id="topErrors" resultType="java.util.Map">
        SELECT error_code AS errorCode,
               MAX(error_msg) AS errorMsg,
               COUNT(*) AS cnt
          FROM dsp_report_log
         WHERE create_time &gt;= #{start} AND create_time &lt; #{end}
           AND error_code IS NOT NULL AND error_code &lt;&gt; '0'
         GROUP BY error_code
         ORDER BY cnt DESC
         LIMIT #{limit}
    </select>

    <select id="selectRecentFailures" resultType="com.zteits.parking.module.park.dsp.dal.dataobject.DspReportLogDO">
        SELECT <include refid="logColumns"/>,
               <include refid="resultStateExpr"/> AS result_state
          FROM dsp_report_log
         WHERE failed &gt; 0 OR (error_code IS NOT NULL AND error_code &lt;&gt; '0')
         ORDER BY id DESC
         LIMIT #{limit}
    </select>

</mapper>