Files
erp_sb/ruoyi-system/target/classes/mapper/system/ClientMonitorMapper.xml.bak
2025-09-22 11:51:16 +08:00

538 lines
25 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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.ruoyi.system.mapper.ClientMonitorMapper">
<resultMap type="ClientInfo" id="ClientInfoResult">
<result property="id" column="id" />
<result property="clientId" column="client_id" />
<result property="username" column="username" />
<result property="accessToken" column="access_token" />
<result property="osName" column="os_name" />
<result property="osVersion" column="os_version" />
<result property="javaVersion" column="java_version" />
<result property="appVersion" column="app_version" />
<result property="hostname" column="hostname" />
<result property="ipAddress" column="ip_address" />
<result property="status" column="status" />
<result property="authTime" column="auth_time" />
<result property="lastActiveTime" column="last_active_time" />
<result property="online" column="online" />
<result property="cpuUsage" column="cpu_usage" />
<result property="memoryUsage" column="memory_usage" />
<result property="diskUsage" column="disk_usage" />
<result property="networkStatus" column="network_status" />
<result property="heartbeatTime" column="heartbeat_time" />
</resultMap>
<resultMap type="ClientErrorReport" id="ClientErrorReportResult">
<result property="id" column="id" />
<result property="clientId" column="client_id" />
<result property="errorType" column="error_type" />
<result property="errorMessage" column="error_message" />
<result property="errorTime" column="error_time" />
</resultMap>
<resultMap type="ClientDataReport" id="ClientDataReportResult">
<result property="id" column="id" />
<result property="clientId" column="client_id" />
<result property="dataType" column="data_type" />
<result property="dataCount" column="data_count" />
<result property="collectTime" column="collect_time" />
<result property="status" column="status" />
</resultMap>
<resultMap type="ClientEventLog" id="ClientEventLogResult">
<result property="id" column="id" />
<result property="clientId" column="client_id" />
<result property="eventType" column="event_type" />
<result property="eventContent" column="event_content" />
<result property="eventTime" column="event_time" />
<result property="eventLevel" column="event_level" />
<result property="status" column="status" />
</resultMap>
<!-- 插入客户端信息 -->
<insert id="insertClientInfo" parameterType="ClientInfo">
insert into client_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="clientId != null and clientId != ''">client_id,</if>
<if test="username != null and username != ''">username,</if>
<if test="accessToken != null and accessToken != ''">access_token,</if>
<if test="osName != null and osName != ''">os_name,</if>
<if test="osVersion != null and osVersion != ''">os_version,</if>
<if test="javaVersion != null and javaVersion != ''">java_version,</if>
<if test="appVersion != null and appVersion != ''">app_version,</if>
<if test="hostname != null and hostname != ''">hostname,</if>
<if test="ipAddress != null and ipAddress != ''">ip_address,</if>
<if test="status != null and status != ''">status,</if>
<if test="authTime != null">auth_time,</if>
<if test="lastActiveTime != null">last_active_time,</if>
<if test="online != null and online != ''">online,</if>
<if test="cpuUsage != null">cpu_usage,</if>
<if test="memoryUsage != null">memory_usage,</if>
<if test="diskUsage != null">disk_usage,</if>
<if test="networkStatus != null and networkStatus != ''">network_status,</if>
<if test="heartbeatTime != null">heartbeat_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="clientId != null and clientId != ''">#{clientId},</if>
<if test="username != null and username != ''">#{username},</if>
<if test="accessToken != null and accessToken != ''">#{accessToken},</if>
<if test="osName != null and osName != ''">#{osName},</if>
<if test="osVersion != null and osVersion != ''">#{osVersion},</if>
<if test="javaVersion != null and javaVersion != ''">#{javaVersion},</if>
<if test="appVersion != null and appVersion != ''">#{appVersion},</if>
<if test="hostname != null and hostname != ''">#{hostname},</if>
<if test="ipAddress != null and ipAddress != ''">#{ipAddress},</if>
<if test="status != null and status != ''">#{status},</if>
<if test="authTime != null">#{authTime},</if>
<if test="lastActiveTime != null">#{lastActiveTime},</if>
<if test="online != null and online != ''">#{online},</if>
<if test="cpuUsage != null">#{cpuUsage},</if>
<if test="memoryUsage != null">#{memoryUsage},</if>
<if test="diskUsage != null">#{diskUsage},</if>
<if test="networkStatus != null and networkStatus != ''">#{networkStatus},</if>
<if test="heartbeatTime != null">#{heartbeatTime},</if>
</trim>
</insert>
<!-- 更新客户端信息 -->
<update id="updateClientInfo" parameterType="ClientInfo">
update client_info
<trim prefix="SET" suffixOverrides=",">
<if test="username != null and username != ''">username = #{username},</if>
<if test="accessToken != null and accessToken != ''">access_token = #{accessToken},</if>
<if test="osName != null and osName != ''">os_name = #{osName},</if>
<if test="osVersion != null and osVersion != ''">os_version = #{osVersion},</if>
<if test="javaVersion != null and javaVersion != ''">java_version = #{javaVersion},</if>
<if test="appVersion != null and appVersion != ''">app_version = #{appVersion},</if>
<if test="hostname != null and hostname != ''">hostname = #{hostname},</if>
<if test="ipAddress != null and ipAddress != ''">ip_address = #{ipAddress},</if>
<if test="status != null and status != ''">status = #{status},</if>
<if test="lastActiveTime != null">last_active_time = #{lastActiveTime},</if>
<if test="online != null and online != ''">online = #{online},</if>
<if test="cpuUsage != null">cpu_usage = #{cpuUsage},</if>
<if test="memoryUsage != null">memory_usage = #{memoryUsage},</if>
<if test="diskUsage != null">disk_usage = #{diskUsage},</if>
<if test="networkStatus != null and networkStatus != ''">network_status = #{networkStatus},</if>
<if test="heartbeatTime != null">heartbeat_time = #{heartbeatTime},</if>
update_time = NOW()
</trim>
where client_id = #{clientId}
</update>
<!-- 插入客户端事件日志 -->
<insert id="insertClientEventLog" parameterType="ClientEventLog">
insert into client_event_log(
client_id,
event_type,
event_content,
event_time,
event_level,
status
)values(
#{clientId},
#{eventType},
#{eventContent},
#{eventTime},
#{eventLevel},
#{status}
)
</insert>
<!-- 插入数据采集报告 -->
<insert id="insertDataReport" parameterType="ClientDataReport">
insert into client_data_report
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="clientId != null and clientId != ''">client_id,</if>
<if test="dataType != null and dataType != ''">data_type,</if>
<if test="dataCount != null">data_count,</if>
<if test="collectTime != null">collect_time,</if>
<if test="status != null and status != ''">status,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="clientId != null and clientId != ''">#{clientId},</if>
<if test="dataType != null and dataType != ''">#{dataType},</if>
<if test="dataCount != null">#{dataCount},</if>
<if test="collectTime != null">#{collectTime},</if>
<if test="status != null and status != ''">#{status},</if>
</trim>
</insert>
<!-- 根据客户端ID查询客户端信息 -->
<select id="selectClientInfoByClientId" parameterType="String" resultMap="ClientInfoResult">
select * from client_info where client_id = #{clientId}
</select>
<!-- 查询客户端信息列表 -->
<select id="selectClientInfoList" parameterType="ClientInfo" resultMap="ClientInfoResult">
select * from client_info
<where>
<if test="clientId != null and clientId != ''">AND client_id like concat('%', #{clientId}, '%')</if>
<if test="username != null and username != ''">AND username like concat('%', #{username}, '%')</if>
<if test="osName != null and osName != ''">AND os_name like concat('%', #{osName}, '%')</if>
<if test="status != null and status != ''">AND status = #{status}</if>
<if test="online != null and online != ''">AND online = #{online}</if>
</where>
order by last_active_time desc
</select>
<!-- 查询客户端错误报告列表 -->
<select id="selectClientErrorList" parameterType="ClientErrorReport" resultMap="ClientErrorReportResult">
select * from client_error_report
<where>
<if test="clientId != null and clientId != ''">AND client_id like concat('%', #{clientId}, '%')</if>
<if test="errorType != null and errorType != ''">AND error_type = #{errorType}</if>
</where>
order by error_time desc
</select>
<!-- 查询客户端事件日志列表 -->
<select id="selectClientEventLogList" parameterType="ClientEventLog" resultMap="ClientEventLogResult">
select * from client_event_log
<where>
<if test="clientId != null and clientId != ''">AND client_id like concat('%', #{clientId}, '%')</if>
<if test="eventType != null and eventType != ''">AND event_type = #{eventType}</if>
<if test="eventLevel != null and eventLevel != ''">AND event_level = #{eventLevel}</if>
<if test="status != null and status != ''">AND status = #{status}</if>
</where>
order by event_time desc
</select>
<!-- 查询客户端数据采集报告列表 -->
<select id="selectClientDataReportList" parameterType="ClientDataReport" resultMap="ClientDataReportResult">
select * from client_data_report
<where>
<if test="clientId != null and clientId != ''">AND client_id like concat('%', #{clientId}, '%')</if>
<if test="dataType != null and dataType != ''">AND data_type = #{dataType}</if>
<if test="status != null and status != ''">AND status = #{status}</if>
</where>
order by collect_time desc
</select>
<!-- 查询在线客户端数量 -->
<select id="selectOnlineClientCount" resultType="int">
select count(*) from client_info where online = '1'
</select>
<!-- 查询客户端总数 -->
<select id="selectTotalClientCount" resultType="int">
SELECT COUNT(*) FROM client_info
</select>
<!-- 查询今日错误数量 -->
<select id="selectTodayErrorCount" resultType="int">
SELECT COUNT(*) FROM client_error_report
WHERE DATE(error_time) = CURDATE()
</select>
<!-- 查询今日数据采集数量 -->
<select id="selectTodayDataCount" resultType="int">
SELECT COALESCE(SUM(data_count), 0) FROM client_data_report
WHERE DATE(collect_time) = CURDATE()
AND data_type IN ('BANMA', 'RAKUTEN', 'AMAZON', 'ORDER', 'AMAZON_BATCH', 'RAKUTEN_BATCH')
</select>
<!-- 查询数据采集类型分布统计 -->
<select id="selectDataTypeDistribution" resultType="map">
SELECT
CASE
WHEN data_type IN ('ORDER', 'BANMA') THEN 'BANMA'
WHEN data_type LIKE 'AMAZON%' THEN 'AMAZON'
WHEN data_type LIKE 'RAKUTEN%' THEN 'RAKUTEN'
ELSE data_type
END as dataType,
COALESCE(SUM(data_count), 0) as count
FROM client_data_report
WHERE data_type IN ('ORDER', 'BANMA', 'RAKUTEN', 'AMAZON', 'AMAZON_BATCH', 'RAKUTEN_BATCH')
AND DATE(collect_time) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY
CASE
WHEN data_type IN ('ORDER', 'BANMA') THEN 'BANMA'
WHEN data_type LIKE 'AMAZON%' THEN 'AMAZON'
WHEN data_type LIKE 'RAKUTEN%' THEN 'RAKUTEN'
ELSE data_type
END
ORDER BY count DESC
</select>
<!-- 插入客户端错误报告 -->
<insert id="insertClientError" parameterType="ClientErrorReport">
INSERT INTO client_error_report
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="clientId != null and clientId != ''">client_id,</if>
<if test="errorType != null and errorType != ''">error_type,</if>
<if test="errorMessage != null and errorMessage != ''">error_message,</if>
<if test="errorTime != null">error_time</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="clientId != null and clientId != ''">#{clientId},</if>
<if test="errorType != null and errorType != ''">#{errorType},</if>
<if test="errorMessage != null and errorMessage != ''">#{errorMessage},</if>
<if test="errorTime != null">#{errorTime}</if>
</trim>
</insert>
<!-- 更新客户端在线状态 -->
<update id="updateClientOnlineStatus">
update client_info set online = #{param2} where client_id = #{param1}
</update>
<!-- 更新客户端心跳信息 -->
<update id="updateClientHeartbeat" parameterType="ClientInfo">
update client_info
<set>
<if test="lastActiveTime != null">last_active_time = #{lastActiveTime},</if>
<if test="heartbeatTime != null">heartbeat_time = #{heartbeatTime},</if>
<if test="online != null and online != ''">online = #{online},</if>
<if test="cpuUsage != null">cpu_usage = #{cpuUsage},</if>
<if test="memoryUsage != null">memory_usage = #{memoryUsage},</if>
<if test="diskUsage != null">disk_usage = #{diskUsage},</if>
<if test="networkStatus != null and networkStatus != ''">network_status = #{networkStatus}</if>
</set>
where client_id = #{clientId}
</update>
<!-- 更新客户端认证信息 -->
<update id="updateClientAuth" parameterType="ClientAuthInfo">
update client_auth_info
<set>
<if test="username != null and username != ''">username = #{username},</if>
<if test="accessToken != null and accessToken != ''">access_token = #{accessToken},</if>
<if test="osName != null and osName != ''">os_name = #{osName},</if>
<if test="osVersion != null and osVersion != ''">os_version = #{osVersion},</if>
<if test="javaVersion != null and javaVersion != ''">java_version = #{javaVersion},</if>
<if test="appVersion != null and appVersion != ''">app_version = #{appVersion},</if>
<if test="hostname != null and hostname != ''">hostname = #{hostname},</if>
<if test="ipAddress != null and ipAddress != ''">ip_address = #{ipAddress},</if>
<if test="status != null and status != ''">status = #{status},</if>
<if test="lastActiveTime != null">last_active_time = #{lastActiveTime},</if>
<if test="online != null and online != ''">online = #{online}</if>
</set>
where client_id = #{clientId}
</update>
<!-- 查询客户端认证信息通用SQL -->
<sql id="selectClientAuthVo">
select id, client_id, username, access_token, os_name, os_version, java_version,
app_version, hostname, ip_address, status, auth_time, last_active_time, online
from client_auth_info
</sql>
<!-- 查询客户端错误报告通用SQL -->
<sql id="selectClientErrorVo">
select id, client_id, username, error_type, error_message, stack_trace,
error_time, os_name, os_version, app_version
from client_error_report
</sql>
<!-- 查询客户端心跳记录通用SQL -->
<sql id="selectClientHeartbeatVo">
select id, client_id, username, heartbeat_time, cpu_usage, memory_usage,
disk_usage, network_status
from client_heartbeat
</sql>
<!-- 查询客户端数据采集报告通用SQL -->
<sql id="selectClientDataReportVo">
select id, client_id, username, data_type, data_count, collect_time, status
from client_data_report
</sql>
<!-- 查询最近7天的客户端活跃趋势 -->
<select id="selectClientActiveTrend" resultType="map">
SELECT
days.date_str as date,
IFNULL(counts.client_count, 0) as count
FROM
(
SELECT DATE_FORMAT(CURDATE() - INTERVAL 6 DAY, '%Y-%m-%d') as date_str
UNION SELECT DATE_FORMAT(CURDATE() - INTERVAL 5 DAY, '%Y-%m-%d')
UNION SELECT DATE_FORMAT(CURDATE() - INTERVAL 4 DAY, '%Y-%m-%d')
UNION SELECT DATE_FORMAT(CURDATE() - INTERVAL 3 DAY, '%Y-%m-%d')
UNION SELECT DATE_FORMAT(CURDATE() - INTERVAL 2 DAY, '%Y-%m-%d')
UNION SELECT DATE_FORMAT(CURDATE() - INTERVAL 1 DAY, '%Y-%m-%d')
UNION SELECT DATE_FORMAT(CURDATE(), '%Y-%m-%d')
) days
LEFT JOIN
(
SELECT
DATE_FORMAT(auth_time, '%Y-%m-%d') as heartbeat_date,
COUNT(DISTINCT client_id) as client_count
FROM
client_info
WHERE
auth_time >= DATE_SUB(CURDATE(), INTERVAL 6 DAY)
GROUP BY
DATE_FORMAT(auth_time, '%Y-%m-%d')
) counts ON days.date_str = counts.heartbeat_date
ORDER BY
days.date_str ASC
</select>
<!-- 查询客户端详细信息 -->
<select id="selectClientDetail" parameterType="String" resultType="map">
SELECT
a.client_id as clientId,
a.username,
a.os_name as osName,
a.os_version as osVersion,
a.java_version as javaVersion,
a.app_version as appVersion,
a.hostname,
a.ip_address as ipAddress,
CASE a.status WHEN '0' THEN '正常' ELSE '异常' END as status,
DATE_FORMAT(a.auth_time, '%Y-%m-%d %H:%i:%s') as authTime,
DATE_FORMAT(a.last_active_time, '%Y-%m-%d %H:%i:%s') as lastActiveTime,
a.online
FROM client_info a
WHERE a.client_id = #{clientId}
</select>
<!-- 查询客户端版本分布 -->
<select id="selectVersionDistribution" resultType="map">
SELECT
app_version as version,
COUNT(*) as count
FROM client_info
GROUP BY app_version
ORDER BY count DESC
</select>
<!-- 查询API请求统计信息 -->
<select id="selectApiRequestStats" resultType="java.util.Map">
SELECT
COUNT(*) as requestCount,
0 as avgResponseTime
FROM client_data_report
WHERE data_type = 'API_CALL'
AND collect_time >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
</select>
<!-- 插入1688爬取风控监控数据 -->
<insert id="insertAlibaba1688MonitorData" parameterType="java.util.Map">
INSERT INTO client_alibaba1688_monitor
(
client_id,
ip_address,
event_type,
event_time,
duration,
create_time
)
VALUES
(
#{clientId},
#{ipAddress},
#{eventType},
#{eventTime},
#{duration},
#{createTime},
)
</insert>
<!-- 查询1688爬取风控监控数据列表 -->
<select id="selectAlibaba1688MonitorList" parameterType="java.util.Map" resultType="java.util.Map">
SELECT
m.id,
m.client_id,
m.ip_address,
m.event_type,
m.event_time,
m.duration,
m.create_time,
a.username,
a.hostname
FROM
client_alibaba1688_monitor m
LEFT JOIN
client_info a ON m.client_id = a.client_id
<where>
AND m.event_type != 'MOBILE_FIRST_ACCESS'
<if test="clientId != null and clientId != ''">
AND m.client_id = #{clientId}
</if>
<if test="ipAddress != null and ipAddress != ''">
AND m.ip_address LIKE concat('%', #{ipAddress}, '%')
</if>
<if test="eventType != null and eventType != ''">
AND m.event_type = #{eventType}
</if>
<if test="beginTime != null and beginTime != ''">
AND DATE_FORMAT(m.create_time, '%Y-%m-%d') &gt;= #{beginTime}
</if>
<if test="endTime != null and endTime != ''">
AND DATE_FORMAT(m.create_time, '%Y-%m-%d') &lt;= #{endTime}
</if>
</where>
ORDER BY m.create_time DESC
</select>
<!-- 查询1688爬取风控监控统计信息 -->
<select id="selectAlibaba1688Statistics" resultType="java.util.Map">
SELECT
COUNT(*) AS totalEvents,
SUM(CASE WHEN event_type = 'MOBILE_BLOCKED' THEN 1 ELSE 0 END) AS mobileBlockedCount,
SUM(CASE WHEN event_type = 'DESKTOP_BLOCKED' THEN 1 ELSE 0 END) AS desktopBlockedCount,
COUNT(DISTINCT ip_address) AS uniqueIpCount,
AVG(CASE WHEN event_type = 'MOBILE_BLOCKED' AND duration > 0 THEN duration ELSE NULL END) / 1000 AS avgMobileDuration,
AVG(CASE WHEN event_type = 'DESKTOP_BLOCKED' AND duration > 0 THEN duration ELSE NULL END) / 1000 AS avgDesktopDuration
FROM
client_alibaba1688_monitor
WHERE
create_time >= DATE_SUB(NOW(), INTERVAL 30 DAY)
AND event_type != 'MOBILE_FIRST_ACCESS'
</select>
<!-- 将过期客户端设置为离线状态7天未心跳 -->
<update id="updateExpiredClientsOffline">
UPDATE client_info
SET online = '0'
WHERE online = '1'
AND (heartbeat_time IS NULL OR heartbeat_time &lt; DATE_SUB(NOW(), INTERVAL 7 DAY))
</update>
<!-- 删除30天前的错误报告 -->
<delete id="deleteExpiredErrorReports">
DELETE FROM client_error_report
WHERE error_time &lt; DATE_SUB(NOW(), INTERVAL 30 DAY)
</delete>
<!-- 删除90天前的数据采集报告保留API调用记录 -->
<delete id="deleteExpiredDataReports">
DELETE FROM client_data_report
WHERE collect_time &lt; DATE_SUB(NOW(), INTERVAL 90 DAY)
AND data_type NOT IN ('API_CALL')
</delete>
<!-- 删除7天前的事件日志 -->
<delete id="deleteExpiredEventLogs">
DELETE FROM client_event_log
WHERE event_time &lt; DATE_SUB(NOW(), INTERVAL 7 DAY)
</delete>
<!-- 查找最近1天内相同条件的数据报告 -->
<select id="findRecentDataReport" parameterType="map" resultMap="ClientDataReportResult">
SELECT * FROM client_data_report
WHERE client_id = #{clientId}
AND data_type = #{dataType}
AND status = #{status}
AND DATE(collect_time) = DATE(NOW())
ORDER BY collect_time DESC
LIMIT 1
</select>
<!-- 更新数据报告的数量 -->
<update id="updateDataReportCount">
UPDATE client_data_report
SET data_count = #{newCount},
collect_time = NOW()
WHERE id = #{id}
</update>
</mapper>