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.reception_boundary_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, cast(t.console_detail_json as char) console_detail_json, (select count(1) from t_test_battery b where b.test_id = t.test_id and b.del_flag = 0) battery_count, 0 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 test_status = 'WAITING' and reception_status = 'NOT_STARTED' and del_flag = 0 delete from t_test where test_id = #{testId} and test_status = 'WAITING' 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_warning_threshold where test_id = #{testId} delete from t_test_battery 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_battery(test_id, group_id, battery_no, collector_id, del_flag) values(#{testId}, #{groupId}, #{batteryNo}, #{collectorId}, 0) insert into t_test_channel_binding(test_id, test_battery_id, channel_id, expected_barcode, claim_status) values(#{testId}, #{batteryRelationId}, #{channelId}, #{expectedBarcode}, #{claimStatus}) insert into t_test_collector_channel (test_id, test_battery_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_warning_exception (test_id, threshold_id, source_type, source_identity, metric_code, abnormal_value, unit, rule_snapshot, severity, exception_status, first_occurred_at_utc, last_occurred_at_utc, occurrence_count) values (#{testId}, #{thresholdId}, #{sourceType}, #{sourceIdentity}, #{metricCode}, #{value}, #{unit}, cast(#{ruleSnapshot} as json), #{severity}, 'OPEN', #{occurredAt}, #{occurredAt}, 1) on duplicate key update abnormal_value = values(abnormal_value), unit = values(unit), last_occurred_at_utc = values(last_occurred_at_utc), occurrence_count = occurrence_count + 1, source_identity = values(source_identity), severity = values(severity), rule_snapshot = values(rule_snapshot) update t_test_warning_exception set exception_status='CLOSED', handled_by=#{handledBy}, handled_at_utc=#{handledAt}, handling_note=#{handlingNote} where exception_id=#{exceptionId} and exception_status='OPEN' insert ignore into t_test_realtime_snapshot (test_id, binding_id, channel_id, collector_id, capture_slot_utc, cabinet_sequence_id, cabinet_device_time_utc, cabinet_device_time_raw, collector_sequence_id, collector_device_time_utc, collector_device_time_raw, server_received_at_utc, cycle_no, step_no, step_type, step_name, voltage_v, current_a, capacity_ah, energy_wh, temperature, positive_potential, negative_potential, extension_measurements_json, content_sha256) values (#{testId}, #{bindingId}, #{channelId}, #{collectorId}, #{captureSlotUtc}, #{cabinetSequenceId}, #{cabinetDeviceTimeUtc}, #{cabinetDeviceTimeRaw}, #{collectorSequenceId}, #{collectorDeviceTimeUtc}, #{collectorDeviceTimeRaw}, #{serverReceivedAtUtc}, #{cycleNo}, #{stepNo}, #{stepType}, #{stepName}, #{voltageV}, #{currentA}, #{capacityAh}, #{energyWh}, #{temperature}, #{positivePotential}, #{negativePotential}, #{extensionMeasurementsJson}, #{contentSha256}) 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 update t_test set console_detail_json = cast(#{detailJson} as json) 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', started_at_utc = coalesce(started_at_utc, #{startedAtUtc}), reception_boundary_at_utc = #{startedAtUtc}, update_by = #{updateBy}, update_time = #{startedAtUtc} where test_id = #{testId} and test_status = 'WAITING' 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 test_status = 'RUNNING' and reception_status = 'RECEIVING' and del_flag = 0 update t_test set test_status = 'RUNNING', reception_status = 'RECEIVING', reception_boundary_at_utc = #{updatedAtUtc}, update_by = #{updateBy}, update_time = #{updatedAtUtc} where test_id = #{testId} and test_status = 'PAUSED' 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 ((test_status = 'RUNNING' and reception_status = 'RECEIVING') or (test_status = 'PAUSED' and reception_status = '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})