Files
hms/crates/erp-server/migration/src/m20260505_000112_create_shift_management.rs
iven 7e57565ecd
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled
feat(health): BLE 网关后端接入 — 网关管理 + API Key 认证 + 多患者批量上报
- 新增 ble_gateways + gateway_patient_bindings 表迁移 (000113)
- 网关 CRUD:注册/编辑/删除/重生成 API Key,含患者绑定管理
- API Key 认证中间件(SHA-256 hash + prefix 快速查找)
- 网关数据上报端点:多患者批量读数,复用 device_reading_service 管道
- 网关心跳端点:固件版本/IP 更新 + last_heartbeat_at
- 10 个管理端路由(JWT)+ 2 个网关端路由(API Key)
- health.ble-gateways.list/manage 权限声明
- 修复 000112 迁移 ForeignKey 借用错误
2026-05-04 20:28:26 +08:00

251 lines
8.6 KiB
Rust

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(Shift::Table)
.col(
ColumnDef::new(Shift::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(Shift::TenantId).uuid().not_null())
.col(ColumnDef::new(Shift::ShiftDate).date().not_null())
.col(ColumnDef::new(Shift::Period).string_len(20).not_null())
.col(ColumnDef::new(Shift::NurseId).uuid())
.col(ColumnDef::new(Shift::Status).string_len(20).not_null().default("scheduled"))
.col(ColumnDef::new(Shift::Notes).text())
.col(ColumnDef::new(Shift::CreatedAt).timestamp_with_time_zone().not_null())
.col(ColumnDef::new(Shift::UpdatedAt).timestamp_with_time_zone().not_null())
.col(ColumnDef::new(Shift::CreatedBy).uuid())
.col(ColumnDef::new(Shift::UpdatedBy).uuid())
.col(ColumnDef::new(Shift::DeletedAt).timestamp_with_time_zone())
.col(ColumnDef::new(Shift::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_shifts_tenant_date")
.table(Shift::Table)
.col(Shift::TenantId)
.col(Shift::ShiftDate)
.col(Shift::DeletedAt)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_shifts_tenant_nurse")
.table(Shift::Table)
.col(Shift::TenantId)
.col(Shift::NurseId)
.col(Shift::DeletedAt)
.to_owned(),
)
.await?;
// 患者分配表
manager
.create_table(
Table::create()
.table(PatientAssignment::Table)
.col(
ColumnDef::new(PatientAssignment::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(PatientAssignment::TenantId).uuid().not_null())
.col(ColumnDef::new(PatientAssignment::ShiftId).uuid().not_null())
.col(ColumnDef::new(PatientAssignment::PatientId).uuid().not_null())
.col(ColumnDef::new(PatientAssignment::CareLevel).string_len(20).not_null().default("routine"))
.col(ColumnDef::new(PatientAssignment::Notes).text())
.col(ColumnDef::new(PatientAssignment::CreatedAt).timestamp_with_time_zone().not_null())
.col(ColumnDef::new(PatientAssignment::UpdatedAt).timestamp_with_time_zone().not_null())
.col(ColumnDef::new(PatientAssignment::CreatedBy).uuid())
.col(ColumnDef::new(PatientAssignment::UpdatedBy).uuid())
.col(ColumnDef::new(PatientAssignment::DeletedAt).timestamp_with_time_zone())
.col(ColumnDef::new(PatientAssignment::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_patient_assignments_shift")
.from(PatientAssignment::Table, PatientAssignment::ShiftId)
.to(Shift::Table, Shift::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_patient_assignments_shift")
.table(PatientAssignment::Table)
.col(PatientAssignment::TenantId)
.col(PatientAssignment::ShiftId)
.col(PatientAssignment::DeletedAt)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_patient_assignments_patient")
.table(PatientAssignment::Table)
.col(PatientAssignment::TenantId)
.col(PatientAssignment::PatientId)
.col(PatientAssignment::DeletedAt)
.to_owned(),
)
.await?;
// 交接日志表
manager
.create_table(
Table::create()
.table(HandoffLog::Table)
.col(
ColumnDef::new(HandoffLog::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(HandoffLog::TenantId).uuid().not_null())
.col(ColumnDef::new(HandoffLog::FromShiftId).uuid().not_null())
.col(ColumnDef::new(HandoffLog::ToShiftId).uuid().not_null())
.col(ColumnDef::new(HandoffLog::PatientId).uuid().not_null())
.col(ColumnDef::new(HandoffLog::Notes).text())
.col(ColumnDef::new(HandoffLog::PendingItems).json_binary())
.col(ColumnDef::new(HandoffLog::CreatedAt).timestamp_with_time_zone().not_null())
.col(ColumnDef::new(HandoffLog::UpdatedAt).timestamp_with_time_zone().not_null())
.col(ColumnDef::new(HandoffLog::CreatedBy).uuid())
.col(ColumnDef::new(HandoffLog::UpdatedBy).uuid())
.col(ColumnDef::new(HandoffLog::DeletedAt).timestamp_with_time_zone())
.col(ColumnDef::new(HandoffLog::Version).integer().not_null().default(1))
.to_owned(),
)
.await?;
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_handoff_log_from_shift")
.from(HandoffLog::Table, HandoffLog::FromShiftId)
.to(Shift::Table, Shift::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_handoff_log_to_shift")
.from(HandoffLog::Table, HandoffLog::ToShiftId)
.to(Shift::Table, Shift::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_handoff_log_to_shift")
.table(HandoffLog::Table)
.col(HandoffLog::TenantId)
.col(HandoffLog::ToShiftId)
.col(HandoffLog::DeletedAt)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(HandoffLog::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(PatientAssignment::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Shift::Table).to_owned())
.await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum Shift {
Table,
Id,
TenantId,
ShiftDate,
Period,
NurseId,
Status,
Notes,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}
#[derive(DeriveIden)]
enum PatientAssignment {
Table,
Id,
TenantId,
ShiftId,
PatientId,
CareLevel,
Notes,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}
#[derive(DeriveIden)]
enum HandoffLog {
Table,
Id,
TenantId,
FromShiftId,
ToShiftId,
PatientId,
Notes,
PendingItems,
CreatedAt,
UpdatedAt,
CreatedBy,
UpdatedBy,
DeletedAt,
Version,
}