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,
t.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,
cast(t.logic_step_mappings_json as char) logic_step_mappings_json,
(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
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)
update t_test set logic_step_mappings_json = cast(#{mappingsJson} as json),
update_by = #{updateBy}, update_time = #{updateTime}
where test_id = #{testId} and del_flag = 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 ignore into t_test_sample
(test_id, binding_id, channel_id, bts_test_id, seq_id, device_time_utc,
device_time_raw, received_at_utc, cycle_no, step_no, step_type, step_name, test_time_ms, voltage_v,
current_a, capacity_ah, energy_wh, content_sha256)
values (#{testId}, #{bindingId}, #{channelId}, #{btsTestId}, #{sequenceId}, #{deviceTimeUtc},
#{deviceTimeRaw}, #{receivedAtUtc}, #{cycleNo}, #{stepNo}, #{stepType}, #{stepName}, #{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, adapter_code, 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}, #{adapterCode}, #{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}, 'SAMPLE_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)
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},
update_by = #{updateBy}, update_time = #{endedAtUtc}
where test_id = #{testId} and reception_status in ('RECEIVING', 'PAUSED') and del_flag = 0
insert into t_test_data_revision_action
(test_id, data_type, operation_type, scope_summary, identity_summary, changes_summary,
reason, operator_id, operator_name, created_at_utc)
values (#{testId}, #{dataType}, #{operationType}, #{scopeSummary}, cast(#{identitySummary} as json),
cast(#{changesSummary} as json), #{reason}, #{operatorId}, #{operatorName}, #{createdAtUtc})
insert into t_test_data_override
(test_id, data_type, stable_identity, field_values, deleted, action_id, created_at_utc)
values (#{testId}, #{dataType}, #{stableIdentity}, cast(#{fieldValues} as json), #{deleted}, #{actionId}, #{createdAtUtc})
on duplicate key update field_values = values(field_values), deleted = values(deleted),
action_id = values(action_id), created_at_utc = values(created_at_utc)
delete from t_test_sample where test_id = #{testId} and sample_id in
#{id}
delete from t_test_sample_measurement where sample_id in
#{id}
delete from t_test_collector_sample where test_id = #{testId} and sample_id in
#{id}
insert into t_test_console_import_record
(test_id, data_type, stable_identity, row_values, action_id, created_at_utc)
values (#{testId}, #{dataType}, #{stableIdentity}, cast(#{rowValues} as json), #{actionId}, #{createdAtUtc})