refactor: 优化RFID相关Mapper查询逻辑与关联关系

1. 移除RfidLocationMapper.xml冗余的末尾空格
2. 新增设备统计和看板统计的聚合查询方法
3. 补全读取记录的设备编码字段与位置关联查询
4. 优化多表联合查询的字段映射逻辑
main
zch 1 month ago
parent 89d01c51a7
commit 79cb8d1b9b

@ -88,6 +88,25 @@
</if>
</select>
<!-- 看板高频刷新场景合并统计,避免同一张设备表被连续扫描四次 -->
<select id="selectMarkedDeviceStatistics" resultType="org.dromara.rfid.domain.vo.DashboardVO$StatisticsOverview">
select count(1) as deviceTotal,
coalesce(sum(case when t.online_status = '1' then 1 else 0 end), 0) as onlineCount,
coalesce(sum(case when t.online_status = '0' then 1 else 0 end), 0) as offlineCount,
coalesce(sum(case when t.alarm_status = '1' then 1 else 0 end), 0) as alarmCount
from rfid_device t
where t.is_marked = '1'
</select>
<!-- 旧首页统计接口保留原有“全设备”口径,只将多次 count 合并为一次聚合 -->
<select id="selectDashboardStats" resultType="org.dromara.rfid.domain.vo.RfidDashboardStatsVo">
select count(1) as totalDevices,
coalesce(sum(case when t.online_status = '1' then 1 else 0 end), 0) as onlineDevices,
coalesce(sum(case when t.online_status = '0' then 1 else 0 end), 0) as offlineDevices,
coalesce(sum(case when t.alarm_status = '1' then 1 else 0 end), 0) as alarmDevices
from rfid_device t
</select>
<!-- 分页查询(带自定义条件) -->
<select id="selectCustomRfidDeviceVoPage" resultMap="RfidDeviceResult">
select t.id,

@ -8,7 +8,7 @@
</resultMap>
<select id="selectCustomRfidLocationVoList" resultMap="RfidLocationResult">
select t.id, t.location_code, t.location_alias, t.location_type, t.parent_id, t.is_marked, t.remark, t.created_by, t.created_at, t.updated_by, t.updated_at
select t.id, t.location_code, t.location_alias, t.location_type, t.parent_id, t.is_marked, t.remark, t.created_by, t.created_at, t.updated_by, t.updated_at
from rfid_location t
<if test="ew != null">
${ew.customSqlSegment}

@ -11,8 +11,9 @@
<select id="selectCustomRfidReadRecordVoList" resultMap="RfidReadRecordResult">
select t.id,
t.device_id,
d.device_code as deviceCode,
t.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
t.read_status,
TRIM(t.barcode) as barcode,
t.record_time,
@ -22,6 +23,7 @@
t.alarm_action
from ${tableName} t
left join rfid_device d on t.device_id = d.id
left join rfid_location l on d.location_id = l.id
<if test="ew != null">
${ew.customSqlSegment}
</if>
@ -33,8 +35,9 @@
<select id="selectCustomRfidReadRecordVoListMultiTable" resultMap="RfidReadRecordResult">
select combined.id,
combined.device_id,
d.device_code as deviceCode,
combined.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
combined.read_status,
TRIM(combined.barcode) as barcode,
combined.record_time,
@ -46,6 +49,7 @@
<foreach collection="tableNames" item="tbl" separator=" UNION ALL ">
select t.id,
t.device_id,
t.device_code,
t.read_status,
TRIM(t.barcode) as barcode,
t.record_time,
@ -60,6 +64,7 @@
</foreach>
) combined
left join rfid_device d on combined.device_id = d.id
left join rfid_location l on d.location_id = l.id
order by combined.record_time desc
</select>
@ -67,8 +72,9 @@
<select id="selectCustomRfidReadRecordVoById" resultMap="RfidReadRecordResult">
select t.id,
t.device_id,
d.device_code as deviceCode,
t.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
t.read_status,
TRIM(t.barcode) as barcode,
t.record_time,
@ -78,6 +84,7 @@
t.alarm_action
from ${tableName} t
left join rfid_device d on t.device_id = d.id
left join rfid_location l on d.location_id = l.id
where t.id = #{id}
</select>
@ -85,8 +92,9 @@
<select id="selectCustomRfidReadRecordVoByIds" resultMap="RfidReadRecordResult">
select t.id,
t.device_id,
d.device_code as deviceCode,
t.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
t.read_status,
TRIM(t.barcode) as barcode,
t.record_time,
@ -96,6 +104,7 @@
t.alarm_action
from ${tableName} t
left join rfid_device d on t.device_id = d.id
left join rfid_location l on d.location_id = l.id
where t.id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
@ -128,8 +137,9 @@
<select id="selectCustomRfidReadRecordVoPage" resultMap="RfidReadRecordResult">
select t.id,
t.device_id,
d.device_code as deviceCode,
t.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
t.read_status,
TRIM(t.barcode) as barcode,
t.record_time,
@ -139,6 +149,7 @@
t.alarm_action
from ${tableName} t
left join rfid_device d on t.device_id = d.id
left join rfid_location l on d.location_id = l.id
<if test="ew != null">
${ew.customSqlSegment}
</if>
@ -150,8 +161,9 @@
<select id="selectCustomRfidReadRecordVoPageMultiTable" resultMap="RfidReadRecordResult">
select combined.id,
combined.device_id,
d.device_code as deviceCode,
combined.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
combined.read_status,
TRIM(combined.barcode) as barcode,
combined.record_time,
@ -163,6 +175,7 @@
<foreach collection="tableNames" item="tbl" separator=" UNION ALL ">
select t.id,
t.device_id,
t.device_code,
t.read_status,
TRIM(t.barcode) as barcode,
t.record_time,
@ -177,6 +190,7 @@
</foreach>
) combined
left join rfid_device d on combined.device_id = d.id
left join rfid_location l on d.location_id = l.id
order by combined.record_time desc
</select>
@ -185,6 +199,7 @@
insert into ${tableName}(
id,
device_id,
device_code,
read_status,
barcode,
record_time,
@ -196,6 +211,7 @@
values (
#{entity.id},
#{entity.deviceId},
#{entity.deviceCode},
#{entity.readStatus},
#{entity.barcode},
#{entity.recordTime},
@ -211,6 +227,7 @@
insert into ${tableName}(
id,
device_id,
device_code,
read_status,
barcode,
record_time,
@ -224,6 +241,7 @@
(
#{item.id},
#{item.deviceId},
#{item.deviceCode},
#{item.readStatus},
#{item.barcode},
#{item.recordTime},
@ -242,6 +260,9 @@
<if test="entity.deviceId != null">
device_id = #{entity.deviceId},
</if>
<if test="entity.deviceCode != null">
device_code = #{entity.deviceCode},
</if>
<if test="entity.readStatus != null and entity.readStatus != ''">
read_status = #{entity.readStatus},
</if>
@ -275,6 +296,9 @@
<if test="item.deviceId != null">
device_id = #{item.deviceId},
</if>
<if test="item.deviceCode != null">
device_code = #{item.deviceCode},
</if>
<if test="item.readStatus != null and item.readStatus != ''">
read_status = #{item.readStatus},
</if>
@ -352,8 +376,9 @@
<select id="selectLatestRecordByDevice" resultMap="RfidReadRecordResult">
select t.id,
t.device_id,
d.device_code as deviceCode,
t.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
t.read_status,
TRIM(t.barcode) as barcode,
t.record_time,
@ -376,6 +401,7 @@
group by device_id
) latest on t.device_id = latest.device_id and t.record_time = latest.max_time
left join rfid_device d on t.device_id = d.id
left join rfid_location l on d.location_id = l.id
order by t.device_id
</select>
@ -383,8 +409,9 @@
<select id="selectLatestRecordByDeviceMultiTable" resultMap="RfidReadRecordResult">
select combined.id,
combined.device_id,
d.device_code as deviceCode,
combined.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
combined.read_status,
TRIM(combined.barcode) as barcode,
combined.record_time,
@ -394,11 +421,11 @@
combined.alarm_action
from (
<!-- 合并所有分表数据,按设备分组取最新记录 -->
select t1.id, t1.device_id, t1.read_status, t1.barcode, t1.record_time,
select t1.id, t1.device_id, t1.device_code, t1.read_status, t1.barcode, t1.record_time,
t1.alarm_flag, t1.alarm_level, t1.alarm_type, t1.alarm_action
from (
<foreach collection="tableNames" item="tbl" separator=" UNION ALL ">
select id, device_id, read_status, barcode, record_time,
select id, device_id, device_code, read_status, barcode, record_time,
alarm_flag, alarm_level, alarm_type, alarm_action
from ${tbl}
<where>
@ -430,6 +457,7 @@
) latest on t1.device_id = latest.device_id and t1.record_time = latest.max_time
) combined
left join rfid_device d on combined.device_id = d.id
left join rfid_location l on d.location_id = l.id
order by combined.device_id
</select>
@ -450,7 +478,7 @@
<select id="selectAlarmRecordList" resultMap="RfidReadRecordResult">
select t.id,
t.device_id,
d.device_code as deviceCode,
t.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
t.read_status,
@ -476,8 +504,9 @@
<select id="selectWithSampling" resultMap="RfidReadRecordResult">
select t1.id,
t1.device_id,
d.device_code as deviceCode,
t1.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
t1.read_status,
TRIM(t1.barcode) as barcode,
t1.record_time,
@ -487,6 +516,7 @@
t1.alarm_action
from ${tableName} t1
left join rfid_device d on t1.device_id = d.id
left join rfid_location l on d.location_id = l.id
inner join (
<!-- 子查询:按 device_id + time_slot 分组,取每个时间槽的最新记录 -->
select
@ -507,8 +537,9 @@
<select id="selectWithSamplingMultiTable" resultMap="RfidReadRecordResult">
select sampled.id,
sampled.device_id,
d.device_code as deviceCode,
sampled.device_code as deviceCode,
d.device_name as deviceName,
l.location_alias as locationAlias,
sampled.read_status,
TRIM(sampled.barcode) as barcode,
sampled.record_time,
@ -520,6 +551,7 @@
<foreach collection="tableNames" item="tbl" separator=" UNION ALL ">
select t1.id,
t1.device_id,
t1.device_code,
t1.read_status,
TRIM(t1.barcode) as barcode,
t1.record_time,
@ -543,6 +575,7 @@
</foreach>
) sampled
left join rfid_device d on sampled.device_id = d.id
left join rfid_location l on d.location_id = l.id
order by sampled.device_id asc, sampled.record_time desc
</select>

Loading…
Cancel
Save