use sea_orm_migration::prelude::*; #[derive(DeriveMigrationName)] pub struct Migration; #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .create_table( Table::create() .table(Alias::new("dialysis_prescription")) .if_not_exists() .col(ColumnDef::new(Alias::new("id")).uuid().not_null().primary_key()) .col(ColumnDef::new(Alias::new("tenant_id")).uuid().not_null()) .col(ColumnDef::new(Alias::new("patient_id")).uuid().not_null()) // 透析器型号 .col(ColumnDef::new(Alias::new("dialyzer_model")).string_len(100).null()) // 膜面积 (m²) .col(ColumnDef::new(Alias::new("membrane_area")).decimal_len(5, 2).null()) // 透析液钾浓度 (mmol/L) .col(ColumnDef::new(Alias::new("dialysate_potassium")).decimal_len(5, 2).null()) // 透析液钙浓度 (mmol/L) .col(ColumnDef::new(Alias::new("dialysate_calcium")).decimal_len(5, 2).null()) // 透析液碳酸氢盐浓度 (mmol/L) .col(ColumnDef::new(Alias::new("dialysate_bicarbonate")).decimal_len(5, 2).null()) // 抗凝方式: heparin/lmwh/heparin_free .col(ColumnDef::new(Alias::new("anticoagulation_type")).string_len(20).null()) // 抗凝剂剂量 .col(ColumnDef::new(Alias::new("anticoagulation_dose")).string_len(50).null()) // 目标超滤量 (ml) .col(ColumnDef::new(Alias::new("target_ultrafiltration_ml")).integer().null()) // 目标干体重 (kg) .col(ColumnDef::new(Alias::new("target_dry_weight")).decimal_len(5, 2).null()) // 血流量 (ml/min) .col(ColumnDef::new(Alias::new("blood_flow_rate")).integer().null()) // 透析液流量 (ml/min) .col(ColumnDef::new(Alias::new("dialysate_flow_rate")).integer().null()) // 每周透析频次 .col(ColumnDef::new(Alias::new("frequency_per_week")).integer().null()) // 每次透析时长 (分钟) .col(ColumnDef::new(Alias::new("duration_minutes")).integer().null()) // 血管通路类型: avf/avg/cvc .col(ColumnDef::new(Alias::new("vascular_access_type")).string_len(20).null()) // 血管通路位置 .col(ColumnDef::new(Alias::new("vascular_access_location")).string_len(100).null()) // 生效日期 .col(ColumnDef::new(Alias::new("effective_from")).date().null()) // 失效日期 .col(ColumnDef::new(Alias::new("effective_to")).date().null()) // 状态: active/discontinued .col( ColumnDef::new(Alias::new("status")) .string_len(20) .not_null() .default("active"), ) // 开方医生 ID .col(ColumnDef::new(Alias::new("prescribed_by")).uuid().null()) // 备注 .col(ColumnDef::new(Alias::new("notes")).text().null()) // 标准审计字段 .col( ColumnDef::new(Alias::new("created_at")) .timestamp_with_time_zone() .not_null() .default(Expr::current_timestamp()), ) .col( ColumnDef::new(Alias::new("updated_at")) .timestamp_with_time_zone() .not_null() .default(Expr::current_timestamp()), ) .col(ColumnDef::new(Alias::new("created_by")).uuid().null()) .col(ColumnDef::new(Alias::new("updated_by")).uuid().null()) .col(ColumnDef::new(Alias::new("deleted_at")).timestamp_with_time_zone().null()) .col( ColumnDef::new(Alias::new("version")) .integer() .not_null() .default(1), ) .to_owned(), ) .await?; // 租户索引 manager .create_index( Index::create() .if_not_exists() .name("idx_dialysis_prescription_tenant_id") .table(Alias::new("dialysis_prescription")) .col(Alias::new("tenant_id")) .to_owned(), ) .await?; // 患者索引 manager .create_index( Index::create() .if_not_exists() .name("idx_dialysis_prescription_patient_id") .table(Alias::new("dialysis_prescription")) .col(Alias::new("patient_id")) .to_owned(), ) .await?; // 患者+状态复合索引(仅未删除记录)— 查询当前生效处方 manager .create_index( Index::create() .if_not_exists() .name("idx_dialysis_prescription_patient_status") .table(Alias::new("dialysis_prescription")) .col(Alias::new("patient_id")) .col(Alias::new("status")) .to_owned(), ) .await?; Ok(()) } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .drop_table(Table::drop().table(Alias::new("dialysis_prescription")).to_owned()) .await } }