feat(base): 更新设备参数值数据访问层实现

- 优化最新参数聚合查询逻辑,支持按设备代码和参数代码分组获取最新记录
- 新增时间窗口过滤条件,提升查询性能和数据准确性
- 扩展设备状态统计功能,新增报警状态统计支持
- 更新三色灯状态查询逻辑,包含报警状态并优化时间范围筛选
- 添加数据库并行查询提示,提升大数据量查询性能
- 完善设备状态映射,增加报警状态及其对应的状态码排序
master
zangch@mesnac.com 1 week ago
parent 79bff94d1a
commit 4915378334

@ -24,13 +24,13 @@
<select id="selectBaseDeviceParamValList" parameterType="BaseDeviceParamVal" resultMap="BaseDeviceParamValResult">
<include refid="selectBaseDeviceParamValVo"/>
<where>
<if test="paramCode != null and paramCode != ''"> and param_code = #{paramCode}</if>
<if test="deviceCode != null and deviceCode != ''"> and device_code = #{deviceCode}</if>
<if test="deviceId != null "> and device_id = #{deviceId}</if>
<if test="paramName != null and paramName != ''"> and param_name like concat(concat('%', #{paramName}), '%')</if>
<if test="paramValue != null and paramValue != ''"> and param_value = #{paramValue}</if>
<if test="collectTime != null "> and collect_time = #{collectTime}</if>
<if test="recordTime != null "> and record_time = #{recordTime}</if>
<if test="paramCode != null and paramCode != ''"> and param_code = #{paramCode}</if>
<if test="deviceCode != null and deviceCode != ''"> and device_code = #{deviceCode}</if>
<if test="deviceId != null "> and device_id = #{deviceId}</if>
<if test="paramName != null and paramName != ''"> and param_name like concat(concat('%', #{paramName}), '%')</if>
<if test="paramValue != null and paramValue != ''"> and param_value = #{paramValue}</if>
<if test="collectTime != null "> and collect_time = #{collectTime}</if>
<if test="recordTime != null "> and record_time = #{recordTime}</if>
</where>
</select>
@ -92,7 +92,7 @@
</delete>
<!-- 最新参数聚合查询每设备每参数取最新一条记录限制返回200条 -->
<select id="selectLatestBaseDeviceParamValList" parameterType="BaseDeviceParamVal" resultMap="BaseDeviceParamValResult">
<!-- <select id="selectLatestBaseDeviceParamValList" parameterType="BaseDeviceParamVal" resultMap="BaseDeviceParamValResult">
SELECT * FROM (
SELECT
record_id, param_code, device_code, device_id, param_name, param_value, collect_time, record_time,
@ -110,18 +110,43 @@
</where>
) WHERE rn = 1
) WHERE row_num &lt;= 200
</select> -->
<select id="selectLatestBaseDeviceParamValList" parameterType="BaseDeviceParamVal" resultMap="BaseDeviceParamValResult">
SELECT *
FROM (
SELECT
record_id, param_code, device_code, device_id, param_name, param_value,
collect_time, record_time,
ROW_NUMBER() OVER (ORDER BY device_code, param_code) AS row_num
FROM (
SELECT
record_id, param_code, device_code, device_id, param_name, param_value,
collect_time, record_time,
ROW_NUMBER() OVER (PARTITION BY device_code, param_code ORDER BY NVL(record_time, collect_time) DESC) AS rn
FROM base_device_param_val B
WHERE 1=1
<if test="deviceCode != null and deviceCode != ''">AND device_code = #{deviceCode}</if>
<if test="paramCode != null and paramCode != ''">AND param_code = #{paramCode}</if>
<if test="deviceId != null">AND device_id = #{deviceId}</if>
<if test="paramName != null and paramName != ''">AND param_name LIKE '%' || #{paramName} || '%'</if>
<!-- 强烈建议增加时间窗口 -->
<if test="beginTime != null and endTime != null">
AND NVL(record_time, collect_time) BETWEEN #{beginTime} AND #{endTime}
</if>
) WHERE rn = 1
) WHERE row_num &lt;= 200
</select>
<!-- 设备状态统计:基于三色灯参数获取设备运行状态统计 -->
<select id="selectDeviceStatusStatistics" resultType="java.util.Map">
WITH today_latest AS (
SELECT
SELECT /*+ PARALLEL(4) */
device_code,
param_name,
ROW_NUMBER() OVER (PARTITION BY device_code ORDER BY NVL(record_time, collect_time) DESC) AS rn
FROM base_device_param_val
WHERE TRUNC(collect_time) = TRUNC(SYSDATE)
AND param_name IN ('机台状态-三色灯机器运行', '机台状态-三色灯机器暂停', '机台状态-三色灯机器待机')
WHERE param_name IN ('机台状态-三色灯机器运行', '机台状态-三色灯机器暂停', '机台状态-三色灯机器待机', '机台状态-三色灯机器报警')
AND collect_time &gt;= TRUNC(SYSDATE) AND collect_time &lt; TRUNC(SYSDATE) + 1
),
device_status AS (
SELECT device_code, param_name FROM today_latest WHERE rn = 1
@ -130,7 +155,8 @@
SELECT
NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器运行' THEN 1 ELSE 0 END), 0) AS runningCount,
NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器暂停' THEN 1 ELSE 0 END), 0) AS stoppedCount,
NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器待机' THEN 1 ELSE 0 END), 0) AS standbyCount
NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器待机' THEN 1 ELSE 0 END), 0) AS standbyCount,
NVL(SUM(CASE WHEN param_name = '机台状态-三色灯机器报警' THEN 1 ELSE 0 END), 0) AS alarmCount
FROM device_status
),
total_cnt AS (
@ -141,20 +167,21 @@
s.runningCount,
s.stoppedCount,
s.standbyCount,
t.totalCount - s.runningCount - s.stoppedCount - s.standbyCount AS notStartedCount
s.alarmCount,
t.totalCount - s.runningCount - s.stoppedCount - s.standbyCount - s.alarmCount AS notStartedCount
FROM status_sum s, total_cnt t
</select>
<!-- 获取每个设备的编号、名称、三色灯状态 -->
<select id="selectDeviceStatusList" resultType="java.util.Map">
WITH today_latest AS (
SELECT
SELECT /*+ PARALLEL(4) */
device_code,
param_name,
ROW_NUMBER() OVER (PARTITION BY device_code ORDER BY NVL(record_time, collect_time) DESC) AS rn
FROM base_device_param_val
WHERE TRUNC(collect_time) = TRUNC(SYSDATE)
AND param_name IN ('机台状态-三色灯机器运行', '机台状态-三色灯机器暂停', '机台状态-三色灯机器待机')
WHERE param_name IN ('机台状态-三色灯机器运行', '机台状态-三色灯机器暂停', '机台状态-三色灯机器待机','机台状态-三色灯机器报警')
AND collect_time &gt;= TRUNC(SYSDATE) AND collect_time &lt; TRUNC(SYSDATE) + 1
),
device_status AS (
SELECT device_code, param_name FROM today_latest WHERE rn = 1
@ -167,12 +194,14 @@
WHEN s.param_name = '机台状态-三色灯机器运行' THEN '运行'
WHEN s.param_name = '机台状态-三色灯机器暂停' THEN '停机'
WHEN s.param_name = '机台状态-三色灯机器待机' THEN '待机'
WHEN s.param_name = '机台状态-三色灯机器报警' THEN '报警'
ELSE '未开机'
END AS deviceStatus,
CASE
WHEN s.param_name = '机台状态-三色灯机器运行' THEN 1
WHEN s.param_name = '机台状态-三色灯机器暂停' THEN 2
WHEN s.param_name = '机台状态-三色灯机器待机' THEN 3
WHEN s.param_name = '机台状态-三色灯机器报警' THEN 4
ELSE 0
END AS statusCode
FROM base_deviceledger d
@ -184,13 +213,13 @@
<!-- 获取每个设备的开机时间当天最新值 -->
<select id="selectDeviceStartTimeList" resultType="java.util.Map">
WITH today_latest AS (
SELECT
SELECT /*+ PARALLEL(4) */
device_code,
param_value,
ROW_NUMBER() OVER (PARTITION BY device_code ORDER BY NVL(record_time, collect_time) DESC) AS rn
FROM base_device_param_val
WHERE TRUNC(collect_time) = TRUNC(SYSDATE)
AND param_name = '机台状态-开机时间'
WHERE param_name = '机台状态-开机时间'
AND collect_time &gt;= TRUNC(SYSDATE) AND collect_time &lt; TRUNC(SYSDATE) + 1
)
SELECT
d.device_code AS deviceCode,

Loading…
Cancel
Save