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

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

@ -88,6 +88,25 @@
</if> </if>
</select> </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 id="selectCustomRfidDeviceVoPage" resultMap="RfidDeviceResult">
select t.id, select t.id,

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

Loading…
Cancel
Save