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 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_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}, 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'