exists (
select 1 from t_test_channel_binding binding
join t_detection_channel channel_fact on channel_fact.channel_id = binding.channel_id
join t_test test_fact on test_fact.test_id = binding.test_id
where channel_fact.cabinet_id = t_detection_cabinet.cabinet_id
and channel_fact.del_flag = 0 and test_fact.del_flag = 0
and test_fact.test_status in ('RUNNING', 'PAUSED', 'RUNNING_ABNORMAL')
)
select cabinet_id, adapter_id, adapter_code, cabinet_code, cabinet_name, ip_address, mac_address,
manufacturer, model, capacity, channel_count,
case when upper(online_status) = 'ONLINE' or online_status = '0' then '0' else '1' end as online_status,
case when then '1' else '0' end as test_status,
status, create_by, create_time, update_by, update_time, remark, del_flag, registration_status
from t_detection_cabinet
insert into t_detection_cabinet
(adapter_id, adapter_code, cabinet_code, cabinet_name, manufacturer, model, capacity, channel_count,
vendor_device_type, vendor_device_id, ip_address, mac_address, online_status, last_seen_at_utc,
registration_status, status, create_by, create_time, remark, del_flag)
values
(#{adapterId}, #{adapterCode}, #{cabinetCode}, #{cabinetName}, #{manufacturer}, #{model}, #{capacity},
#{channelCount}, #{vendorDeviceType}, #{vendorDeviceId}, #{ipAddress}, #{macAddress}, #{onlineStatus},
#{lastSeenAtUtc}, 'REGISTERED', #{status}, #{createBy}, sysdate(), #{remark}, 0)
insert into t_detection_channel
(adapter_id, adapter_code, cabinet_id, devtype, devid, subdevid, chlid, display_no,
online_status, work_status, last_seen_at_utc, status, create_time, del_flag)
values
(#{adapterId}, #{adapterCode}, #{cabinetId}, #{devtype}, #{devid}, #{subdevid}, #{chlid}, #{displayNo},
#{onlineStatus}, #{workStatus}, #{lastSeenAtUtc}, '0', sysdate(), 0)
update t_detection_cabinet
set vendor_device_type = coalesce(vendor_device_type, #{devtype}),
vendor_device_id = coalesce(vendor_device_id, #{devid}),
online_status = #{onlineStatus}, update_time = sysdate()
, last_seen_at_utc = #{lastSeenAtUtc}
where adapter_code = #{adapterCode}
and ((vendor_device_type = #{devtype} and vendor_device_id = #{devid})
or cabinet_code = concat(#{adapterCode}, '-', #{devtype}, '-', #{devid}))
and del_flag = 0 and registration_status = 'REGISTERED'
update t_detection_channel
set online_status = case when #{onlineStatus} in ('ONLINE', '1') then 'ONLINE' else 'OFFLINE' end,
work_status = case #{workStatus}
when 'PAUSED' then 'PAUSED'
when 'PROTECTED' then 'ABNORMAL'
when 'FINISHED' then 'ENDED'
when 'STOPPED' then 'ENDED'
when 'WORKING' then 'RUNNING'
when 'SYNCHRONIZING' then 'RUNNING'
when 'LIT' then 'RUNNING'
when 'WAITING_TIMEOUT' then 'RUNNING'
when 'WAITING_START' then 'RUNNING'
when 'LOCKED' then 'RUNNING'
when 'RUNNING' then 'RUNNING'
else 'UNOBSERVABLE' end,
update_time = sysdate()
, last_seen_at_utc = #{lastSeenAtUtc}
where adapter_code = #{adapterCode}
and devtype = #{devtype} and devid = #{devid}
and subdevid = #{subdevid} and chlid = #{chlid}
and del_flag = 0
and exists (select 1 from t_detection_cabinet cabinet
where cabinet.cabinet_id = t_detection_channel.cabinet_id
and cabinet.del_flag = 0 and cabinet.registration_status = 'REGISTERED')
update t_detection_channel
set online_status = 'OFFLINE', work_status = 'UNOBSERVABLE', update_time = sysdate()
where adapter_code = #{adapterCode} and del_flag = 0
update t_detection_cabinet
set online_status = 'OFFLINE', update_time = sysdate()
where adapter_code = #{adapterCode} and del_flag = 0 and registration_status = 'REGISTERED'
update t_detection_channel
set last_seen_at_utc = #{lastSeenAtUtc}, update_time = sysdate()
where adapter_code = #{adapterCode}
and devtype = #{devtype} and devid = #{devid}
and subdevid = #{subdevid} and chlid = #{chlid}
and online_status = 'ONLINE' and del_flag = 0
update t_detection_cabinet
set last_seen_at_utc = #{lastSeenAtUtc}, update_time = sysdate()
where adapter_code = #{adapterCode}
and vendor_device_type = #{devtype} and vendor_device_id = #{devid}
and online_status = 'ONLINE' and del_flag = 0 and registration_status = 'REGISTERED'
update t_detection_cabinet
cabinet_code = #{cabinetCode},
cabinet_name = #{cabinetName},
ip_address = #{ipAddress},
mac_address = #{macAddress},
manufacturer = #{manufacturer},
model = #{model},
capacity = #{capacity},
channel_count = #{channelCount},
remark = #{remark},
update_by = #{updateBy}, update_time = sysdate()
where cabinet_id = #{cabinetId} and del_flag = 0 and registration_status = 'REGISTERED'
update t_detection_cabinet set status = #{status}, update_by = #{updateBy}, update_time = sysdate()
where cabinet_id = #{cabinetId} and del_flag = 0 and registration_status = 'REGISTERED'
update t_detection_channel set del_flag = channel_id, update_time = sysdate()
where cabinet_id = #{cabinetId} and del_flag = 0
update t_detection_cabinet
set del_flag = cabinet_id, update_by = #{updateBy}, update_time = sysdate()
where cabinet_id = #{cabinetId} and del_flag = 0 and registration_status = 'REGISTERED'