select t.test_id, t.test_name, t.creator_id, creator.nick_name creator_name,
t.principal_id, principal.nick_name principal_name,
t.test_type_id, ty.test_type_name, t.test_purpose,
case when t.test_status = 'RUNNING'
and exists (select 1 from t_test_collector_exception_interval exception_fact
where exception_fact.test_id = t.test_id
and exception_fact.interval_status = 'OPEN')
then 'RUNNING_ABNORMAL'
else t.test_status end test_status,
t.reception_status, t.monitor_timeout_minutes,
t.started_at_utc, t.ended_at_utc, t.del_flag,
t.create_by, t.create_time, t.update_by, t.update_time, t.remark,
(select count(1) from t_test_battery b where b.test_id = t.test_id and b.del_flag = 0) battery_count,
t.runtime_attach runtime_attach, t.backfill_status, t.backfill_started_at_utc,
t.backfill_completed_at_utc
from t_test t
left join sys_user creator on creator.user_id = t.creator_id and creator.del_flag = '0'
left join sys_user principal on principal.user_id = t.principal_id and principal.del_flag = '0'
left join t_test_type ty on ty.test_type_id = t.test_type_id and ty.del_flag = 0
insert into t_test
(test_name, creator_id, principal_id, test_type_id, test_purpose, test_status, reception_status,
monitor_timeout_minutes, del_flag, create_by, create_time, remark)
values
(#{testName}, #{creatorId}, #{principalId}, #{testTypeId}, #{testPurpose}, #{testStatus}, #{receptionStatus},
#{monitorTimeoutMinutes}, 0, #{createBy}, #{createTime}, #{remark})
update t_test
set test_name = #{testName}, principal_id = #{principalId}, test_type_id = #{testTypeId},
test_purpose = #{testPurpose}, monitor_timeout_minutes = #{monitorTimeoutMinutes},
remark = #{remark}, update_by = #{updateBy}, update_time = #{updateTime}
where test_id = #{testId} and reception_status = 'NOT_STARTED' and del_flag = 0
update t_test set del_flag = test_id, update_by = #{updateBy}, update_time = sysdate()
where test_id = #{testId} and reception_status = 'NOT_STARTED' and del_flag = 0
delete from t_test_channel_binding where test_id = #{testId}
delete from t_test_collector_channel where test_id = #{testId}
delete from t_test_plan_step where test_id = #{testId}
update t_test_battery_field f join t_test_battery b on b.relation_id = f.relation_id
set f.del_flag = f.field_id where b.test_id = #{testId} and f.del_flag = 0
update t_test_battery set del_flag = relation_id where test_id = #{testId} and del_flag = 0
update t_test_warning_threshold set del_flag = threshold_id where test_id = #{testId} and del_flag = 0
insert into t_test_plan_step(test_id, cycle_no, step_no, step_type, step_name, parameters_json, sort_order)
values(#{testId}, #{cycleNo}, #{stepNo}, #{stepType}, #{stepName}, cast(#{parametersJson} as json), #{sortOrder})
insert into t_test_battery(test_id, group_id, battery_no, collector_id, del_flag)
values(#{testId}, #{groupId}, #{batteryNo}, #{collectorId}, 0)
insert into t_test_battery_field(relation_id, field_name, field_value, del_flag)
values(#{relationId}, #{fieldName}, #{fieldValue}, 0)
insert into t_test_channel_binding(test_id, battery_relation_id, channel_id, expected_barcode, claim_status)
values(#{testId}, #{batteryRelationId}, #{channelId}, #{expectedBarcode}, #{claimStatus})
insert into t_test_collector_channel
(test_id, battery_relation_id, collector_id, measurement_code, measurement_name, channel_code)
values(#{testId}, #{batteryRelationId}, #{collectorId}, #{measurementCode}, #{measurementName}, #{channelCode})
insert into t_test_warning_threshold(test_id, metric_code, min_value, max_value, unit, severity, del_flag)
values(#{testId}, #{metricCode}, #{minValue}, #{maxValue}, #{unit}, #{severity}, 0)
insert into t_test_audit
(test_id, event_type, actor_id, actor_name, occurred_at_utc, previous_value, new_value, correlation_id)
values
(#{testId}, #{eventType}, #{actorId}, #{actorName}, utc_timestamp(3),
cast(#{previousValue} as json)null,
cast(#{newValue} as json)null,
uuid())
insert into t_test_backfill_cursor
(test_id, binding_id, source_type, source_id, source_channel_code, protocol_test_id,
next_position, source_status, protocol_metadata, last_error, updated_at_utc)
values (#{testId}, #{bindingId}, #{sourceType}, #{sourceId}, #{sourceChannelCode}, #{protocolTestId},
#{nextPosition}, #{sourceStatus}, cast(#{protocolMetadata} as json), #{lastError}, #{updatedAtUtc})
update t_test_backfill_cursor set protocol_test_id = #{protocolTestId}, next_position = #{nextPosition},
source_status = #{sourceStatus}, protocol_metadata = cast(#{protocolMetadata} as json),
last_error = #{lastError}, updated_at_utc = #{updatedAtUtc}
where cursor_id = #{cursorId}
update t_test set test_status = 'COMPLETION_PENDING', backfill_status = 'RUNNING',
backfill_started_at_utc = coalesce(backfill_started_at_utc, #{updatedAtUtc}),
update_time = #{updatedAtUtc} where test_id = #{testId} and reception_status = 'ENDED'
update t_test set backfill_status = 'WAITING', update_time = #{updatedAtUtc}, remark = #{error}
where test_id = #{testId} and reception_status = 'ENDED'
update t_test set backfill_status = 'COMPLETED', backfill_completed_at_utc = #{updatedAtUtc},
update_time = #{updatedAtUtc} where test_id = #{testId} and reception_status = 'ENDED'
update t_test set test_status = 'COMPLETION_PENDING', reception_status = 'ENDED',
ended_at_utc = #{endedAtUtc}, backfill_status = 'NOT_STARTED', update_time = #{endedAtUtc}
where test_id = #{testId} and reception_status in ('RECEIVING', 'PAUSED')
insert ignore into t_test_sample
(test_id, binding_id, channel_id, reception_interval_id, bts_test_id, seq_id, device_time_utc,
device_time_raw, received_at_utc, cycle_no, step_no, step_type, test_time_ms, voltage_v,
current_a, capacity_ah, energy_wh, content_sha256)
values (#{testId}, #{bindingId}, #{channelId}, #{intervalId}, #{btsTestId}, #{sequenceId}, #{deviceTimeUtc},
#{deviceTimeRaw}, #{receivedAtUtc}, #{cycleNo}, #{stepNo}, #{stepType}, #{testTimeMs}, #{voltageV},
#{currentA}, #{capacityAh}, #{energyWh}, #{contentSha256})
insert ignore into t_test_sample_measurement
(sample_id, metric_code, metric_value, unit, source_type)
values (#{sampleId}, #{metricCode}, #{metricValue}, #{unit}, #{sourceType})
insert ignore into t_test_collector_sample
(test_id, binding_id, collector_id, collector_channel_code, sequence_id, device_time_utc,
device_time_raw, server_received_at_utc, metric_code, metric_value, unit, content_sha256)
values (#{testId}, #{bindingId}, #{collectorId}, #{channelCode}, #{sequenceId}, #{deviceTimeUtc},
#{deviceTimeRaw}, #{receivedAtUtc}, #{metricCode}, #{metricValue}, #{unit}, #{contentSha256})
insert into t_test_exception
(test_id, channel_id, exception_type, exception_status, severity,
first_occurred_at_utc, last_occurred_at_utc, occurrence_count, detail)
values (#{testId}, #{channelId}, 'HISTORY_CONTENT_CONFLICT', 'OPEN', 'WARNING', utc_timestamp(3),
utc_timestamp(3), 1, #{detail})
on duplicate key update last_occurred_at_utc = utc_timestamp(3), occurrence_count = occurrence_count + 1,
detail = values(detail)
insert into t_channel_lease(channel_id, test_id, binding_id, acquired_by, acquired_at_utc)
values(#{channelId}, #{testId}, #{bindingId}, #{acquiredBy}, #{acquiredAtUtc})
insert into t_collector_channel_lease(collector_id, channel_code, test_id, binding_id, acquired_by, acquired_at_utc)
values(#{collectorId}, #{channelCode}, #{testId}, #{bindingId}, #{acquiredBy}, #{acquiredAtUtc})
insert into t_test_reception_interval
(test_id, binding_id, source_type, source_id, source_channel_code,
action_at_utc, start_boundary_at_utc, boundary_note, interval_status)
values(#{testId}, #{bindingId}, #{sourceType}, #{sourceId}, #{sourceChannelCode},
#{actionAtUtc}, #{actionAtUtc}, #{boundaryNote}, 'OPEN')
update t_test_reception_interval
set end_boundary_at_utc = #{endBoundaryAtUtc},
boundary_note = #{boundaryNote}, interval_status = 'CLOSED'
where test_id = #{testId} and interval_status = 'OPEN'
delete from t_channel_lease where test_id = #{testId}
delete from t_collector_channel_lease where test_id = #{testId}
update t_test
set test_status = 'RUNNING', reception_status = 'RECEIVING',
runtime_attach = #{runtimeAttach},
started_at_utc = coalesce(started_at_utc, #{startedAtUtc}),
update_by = #{updateBy}, update_time = #{startedAtUtc}
where test_id = #{testId} and reception_status = 'NOT_STARTED' and del_flag = 0
update t_test
set test_status = 'PAUSED', reception_status = 'PAUSED', update_by = #{updateBy}, update_time = #{updatedAtUtc}
where test_id = #{testId} and reception_status = 'RECEIVING' and del_flag = 0
update t_test
set test_status = 'RUNNING', reception_status = 'RECEIVING', update_by = #{updateBy}, update_time = #{updatedAtUtc}
where test_id = #{testId} and reception_status = 'PAUSED' and del_flag = 0
update t_test
set test_status = 'COMPLETED', reception_status = 'ENDED', ended_at_utc = #{endedAtUtc},
backfill_status = 'NOT_STARTED',
update_by = #{updateBy}, update_time = #{endedAtUtc}
where test_id = #{testId} and reception_status in ('RECEIVING', 'PAUSED') and del_flag = 0
insert into t_test_collector_exception_interval
(test_id, battery_relation_id, collector_id, collector_channel_code, exception_type,
opened_at_utc, missing_from_utc, detail)
values (#{testId}, #{batteryRelationId}, #{collectorId}, #{collectorChannelCode},
#{exceptionType}, #{openedAtUtc}, #{missingFromUtc}, #{detail})
update t_test_collector_exception_interval
set recovered_at_utc = #{recoveredAtUtc}, missing_to_utc = #{missingToUtc}, interval_status = 'CLOSED'
where interval_id = #{intervalId} and interval_status = 'OPEN'