fix: 修复测试发现的 7 个问题 + 全 workspace clippy 清零
功能修复: 1. 患者创建空名称验证:后端添加 name.trim().is_empty() 检查 2. 仪表盘统计容错:单个查询失败返回零值而非 500 3. FHIR 路由修复:从 /fhir 移到 /api/v1/fhir 保持一致 4. 冻结模块后端中间件:新增 frozen_module_middleware 拦截冻结路径 5. 积分端点权限码:health.health-data.list → health.points.list 6. 角色权限迁移:护士补充 devices.list,运营补充 points.list/manage 7. 测试结果文档:R01-R05 角色测试 + T00/T10 结果归档 Clippy 全 workspace 清零(14→0 errors): - erp-core: 修复 empty doc line、collapsible if、redundant closure 等 9 处 - erp-health: 修复 too_many_arguments、unused var、unnecessary parens 等 58 处 - erp-ai: 修复 dead_code、unused import 等 11 处 - erp-plugin: 修复 too_many_arguments、wildcard pattern 等 11 处 - erp-server-migration: 修复 enum_variant_names 5 处 - erp-auth/config/workflow/message: 各 1-3 处 工程改进: - lint-staged 配置迁移到 .lintstagedrc.js(函数式避免文件列表传给 clippy) - cargo fmt 统一格式化
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
/// 预留事件处理器注册
|
||||
pub fn register_handlers_with_state(_state: crate::state::DialysisState) {
|
||||
// 透析业务事件由 erp-health 统一消费(见 erp-health/src/event.rs:425 dialysis_notifier)
|
||||
|
||||
@@ -8,8 +8,8 @@ use erp_core::error::AppError;
|
||||
use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, PaginatedResponse, TenantContext};
|
||||
|
||||
use crate::dto::dialysis_dto::*;
|
||||
use crate::dto::DeleteWithVersion;
|
||||
use crate::dto::dialysis_dto::*;
|
||||
use crate::service::dialysis_service;
|
||||
use crate::state::DialysisState;
|
||||
|
||||
@@ -44,10 +44,9 @@ where
|
||||
require_permission(&ctx, "health.dialysis.list")?;
|
||||
let page = params.page.unwrap_or(1);
|
||||
let page_size = params.page_size.unwrap_or(20);
|
||||
let result = dialysis_service::list_dialysis_records(
|
||||
&state, ctx.tenant_id, patient_id, page, page_size,
|
||||
)
|
||||
.await?;
|
||||
let result =
|
||||
dialysis_service::list_dialysis_records(&state, ctx.tenant_id, patient_id, page, page_size)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
}
|
||||
|
||||
@@ -61,10 +60,7 @@ where
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis.list")?;
|
||||
let result = dialysis_service::get_dialysis_record(
|
||||
&state, ctx.tenant_id, record_id,
|
||||
)
|
||||
.await?;
|
||||
let result = dialysis_service::get_dialysis_record(&state, ctx.tenant_id, record_id).await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
}
|
||||
|
||||
@@ -80,10 +76,9 @@ where
|
||||
require_permission(&ctx, "health.dialysis.manage")?;
|
||||
let mut req = req;
|
||||
req.sanitize();
|
||||
let result = dialysis_service::create_dialysis_record(
|
||||
&state, ctx.tenant_id, Some(ctx.user_id), req,
|
||||
)
|
||||
.await?;
|
||||
let result =
|
||||
dialysis_service::create_dialysis_record(&state, ctx.tenant_id, Some(ctx.user_id), req)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
}
|
||||
|
||||
@@ -101,7 +96,12 @@ where
|
||||
let mut data = req.data;
|
||||
data.sanitize();
|
||||
let result = dialysis_service::update_dialysis_record(
|
||||
&state, ctx.tenant_id, record_id, Some(ctx.user_id), data, req.version,
|
||||
&state,
|
||||
ctx.tenant_id,
|
||||
record_id,
|
||||
Some(ctx.user_id),
|
||||
data,
|
||||
req.version,
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
@@ -119,7 +119,11 @@ where
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis.manage")?;
|
||||
let result = dialysis_service::review_dialysis_record(
|
||||
&state, ctx.tenant_id, record_id, ctx.user_id, req.version,
|
||||
&state,
|
||||
ctx.tenant_id,
|
||||
record_id,
|
||||
ctx.user_id,
|
||||
req.version,
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
@@ -137,7 +141,11 @@ where
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis.manage")?;
|
||||
let result = dialysis_service::complete_dialysis_record(
|
||||
&state, ctx.tenant_id, record_id, Some(ctx.user_id), req.version,
|
||||
&state,
|
||||
ctx.tenant_id,
|
||||
record_id,
|
||||
Some(ctx.user_id),
|
||||
req.version,
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
@@ -155,7 +163,11 @@ where
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis.manage")?;
|
||||
dialysis_service::delete_dialysis_record(
|
||||
&state, ctx.tenant_id, record_id, Some(ctx.user_id), req.version,
|
||||
&state,
|
||||
ctx.tenant_id,
|
||||
record_id,
|
||||
Some(ctx.user_id),
|
||||
req.version,
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(())))
|
||||
|
||||
@@ -8,8 +8,8 @@ use erp_core::error::AppError;
|
||||
use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, PaginatedResponse, TenantContext};
|
||||
|
||||
use crate::dto::dialysis_prescription_dto::*;
|
||||
use crate::dto::DeleteWithVersion;
|
||||
use crate::dto::dialysis_prescription_dto::*;
|
||||
use crate::service::dialysis_prescription_service;
|
||||
use crate::state::DialysisState;
|
||||
|
||||
@@ -41,7 +41,12 @@ where
|
||||
let page = params.page.unwrap_or(1);
|
||||
let page_size = params.page_size.unwrap_or(20);
|
||||
let result = dialysis_prescription_service::list_prescriptions(
|
||||
&state, ctx.tenant_id, page, page_size, params.patient_id, params.status,
|
||||
&state,
|
||||
ctx.tenant_id,
|
||||
page,
|
||||
page_size,
|
||||
params.patient_id,
|
||||
params.status,
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
@@ -74,7 +79,10 @@ where
|
||||
let mut req = req;
|
||||
req.sanitize();
|
||||
let result = dialysis_prescription_service::create_prescription(
|
||||
&state, ctx.tenant_id, Some(ctx.user_id), req,
|
||||
&state,
|
||||
ctx.tenant_id,
|
||||
Some(ctx.user_id),
|
||||
req,
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
@@ -94,7 +102,12 @@ where
|
||||
let mut data = req.data;
|
||||
data.sanitize();
|
||||
let result = dialysis_prescription_service::update_prescription(
|
||||
&state, ctx.tenant_id, id, Some(ctx.user_id), data, req.version,
|
||||
&state,
|
||||
ctx.tenant_id,
|
||||
id,
|
||||
Some(ctx.user_id),
|
||||
data,
|
||||
req.version,
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
@@ -112,7 +125,11 @@ where
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis-prescription.manage")?;
|
||||
dialysis_prescription_service::delete_prescription(
|
||||
&state, ctx.tenant_id, id, Some(ctx.user_id), req.version,
|
||||
&state,
|
||||
ctx.tenant_id,
|
||||
id,
|
||||
Some(ctx.user_id),
|
||||
req.version,
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(ApiResponse::ok(())))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use axum::extract::{Extension, FromRef, State};
|
||||
use axum::Json;
|
||||
use axum::extract::{Extension, FromRef, State};
|
||||
use erp_core::error::AppError;
|
||||
use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, TenantContext};
|
||||
@@ -18,6 +18,7 @@ where
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis.stats")?;
|
||||
let dialysis_state = DialysisState::from_ref(&state);
|
||||
let stats = dialysis_stats_service::get_dialysis_statistics(&dialysis_state, ctx.tenant_id).await?;
|
||||
let stats =
|
||||
dialysis_stats_service::get_dialysis_statistics(&dialysis_state, ctx.tenant_id).await?;
|
||||
Ok(Json(ApiResponse::ok(stats)))
|
||||
}
|
||||
|
||||
@@ -49,7 +49,13 @@ pub async fn list_prescriptions(
|
||||
let total_pages = total.div_ceil(limit.max(1));
|
||||
let data = models.into_iter().map(model_to_resp).collect();
|
||||
|
||||
Ok(PaginatedResponse { data, total, page, page_size: limit, total_pages })
|
||||
Ok(PaginatedResponse {
|
||||
data,
|
||||
total,
|
||||
page,
|
||||
page_size: limit,
|
||||
total_pages,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_prescription(
|
||||
@@ -85,14 +91,24 @@ pub async fn create_prescription(
|
||||
tenant_id: Set(tenant_id),
|
||||
patient_id: Set(req.patient_id),
|
||||
dialyzer_model: Set(req.dialyzer_model),
|
||||
membrane_area: Set(req.membrane_area.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
dialysate_potassium: Set(req.dialysate_potassium.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
dialysate_calcium: Set(req.dialysate_calcium.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
dialysate_bicarbonate: Set(req.dialysate_bicarbonate.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
membrane_area: Set(req
|
||||
.membrane_area
|
||||
.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
dialysate_potassium: Set(req
|
||||
.dialysate_potassium
|
||||
.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
dialysate_calcium: Set(req
|
||||
.dialysate_calcium
|
||||
.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
dialysate_bicarbonate: Set(req
|
||||
.dialysate_bicarbonate
|
||||
.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
anticoagulation_type: Set(req.anticoagulation_type),
|
||||
anticoagulation_dose: Set(req.anticoagulation_dose),
|
||||
target_ultrafiltration_ml: Set(req.target_ultrafiltration_ml),
|
||||
target_dry_weight: Set(req.target_dry_weight.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
target_dry_weight: Set(req
|
||||
.target_dry_weight
|
||||
.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
blood_flow_rate: Set(req.blood_flow_rate),
|
||||
dialysate_flow_rate: Set(req.dialysate_flow_rate),
|
||||
frequency_per_week: Set(req.frequency_per_week),
|
||||
@@ -114,10 +130,16 @@ pub async fn create_prescription(
|
||||
let m = active.insert(&state.db).await?;
|
||||
|
||||
audit_service::record(
|
||||
AuditLog::new(tenant_id, operator_id, "dialysis_prescription.created", "dialysis_prescription")
|
||||
.with_resource_id(m.id),
|
||||
AuditLog::new(
|
||||
tenant_id,
|
||||
operator_id,
|
||||
"dialysis_prescription.created",
|
||||
"dialysis_prescription",
|
||||
)
|
||||
.with_resource_id(m.id),
|
||||
&state.db,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(model_to_resp(m))
|
||||
}
|
||||
@@ -141,29 +163,71 @@ pub async fn update_prescription(
|
||||
let next_ver = check_version(expected_version, model.version)
|
||||
.map_err(|_| DialysisError::VersionMismatch)?;
|
||||
|
||||
if let Some(ref t) = req.anticoagulation_type { validate_anticoagulation_type(Some(t))?; }
|
||||
if let Some(ref t) = req.vascular_access_type { validate_vascular_access_type(Some(t))?; }
|
||||
if let Some(ref t) = req.anticoagulation_type {
|
||||
validate_anticoagulation_type(Some(t))?;
|
||||
}
|
||||
if let Some(ref t) = req.vascular_access_type {
|
||||
validate_vascular_access_type(Some(t))?;
|
||||
}
|
||||
|
||||
let mut active: dialysis_prescription::ActiveModel = model.into();
|
||||
if let Some(v) = req.dialyzer_model { active.dialyzer_model = Set(Some(v)); }
|
||||
if let Some(v) = req.membrane_area { active.membrane_area = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default())); }
|
||||
if let Some(v) = req.dialysate_potassium { active.dialysate_potassium = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default())); }
|
||||
if let Some(v) = req.dialysate_calcium { active.dialysate_calcium = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default())); }
|
||||
if let Some(v) = req.dialysate_bicarbonate { active.dialysate_bicarbonate = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default())); }
|
||||
if let Some(v) = req.anticoagulation_type { active.anticoagulation_type = Set(Some(v)); }
|
||||
if let Some(v) = req.anticoagulation_dose { active.anticoagulation_dose = Set(Some(v)); }
|
||||
if let Some(v) = req.target_ultrafiltration_ml { active.target_ultrafiltration_ml = Set(Some(v)); }
|
||||
if let Some(v) = req.target_dry_weight { active.target_dry_weight = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default())); }
|
||||
if let Some(v) = req.blood_flow_rate { active.blood_flow_rate = Set(Some(v)); }
|
||||
if let Some(v) = req.dialysate_flow_rate { active.dialysate_flow_rate = Set(Some(v)); }
|
||||
if let Some(v) = req.frequency_per_week { active.frequency_per_week = Set(Some(v)); }
|
||||
if let Some(v) = req.duration_minutes { active.duration_minutes = Set(Some(v)); }
|
||||
if let Some(v) = req.vascular_access_type { active.vascular_access_type = Set(Some(v)); }
|
||||
if let Some(v) = req.vascular_access_location { active.vascular_access_location = Set(Some(v)); }
|
||||
if let Some(v) = req.effective_from { active.effective_from = Set(Some(v)); }
|
||||
if let Some(v) = req.effective_to { active.effective_to = Set(Some(v)); }
|
||||
if let Some(v) = req.status { active.status = Set(v); }
|
||||
if let Some(v) = req.notes { active.notes = Set(Some(v)); }
|
||||
if let Some(v) = req.dialyzer_model {
|
||||
active.dialyzer_model = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.membrane_area {
|
||||
active.membrane_area = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default()));
|
||||
}
|
||||
if let Some(v) = req.dialysate_potassium {
|
||||
active.dialysate_potassium = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default()));
|
||||
}
|
||||
if let Some(v) = req.dialysate_calcium {
|
||||
active.dialysate_calcium = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default()));
|
||||
}
|
||||
if let Some(v) = req.dialysate_bicarbonate {
|
||||
active.dialysate_bicarbonate = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default()));
|
||||
}
|
||||
if let Some(v) = req.anticoagulation_type {
|
||||
active.anticoagulation_type = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.anticoagulation_dose {
|
||||
active.anticoagulation_dose = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.target_ultrafiltration_ml {
|
||||
active.target_ultrafiltration_ml = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.target_dry_weight {
|
||||
active.target_dry_weight = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default()));
|
||||
}
|
||||
if let Some(v) = req.blood_flow_rate {
|
||||
active.blood_flow_rate = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.dialysate_flow_rate {
|
||||
active.dialysate_flow_rate = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.frequency_per_week {
|
||||
active.frequency_per_week = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.duration_minutes {
|
||||
active.duration_minutes = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.vascular_access_type {
|
||||
active.vascular_access_type = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.vascular_access_location {
|
||||
active.vascular_access_location = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.effective_from {
|
||||
active.effective_from = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.effective_to {
|
||||
active.effective_to = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.status {
|
||||
active.status = Set(v);
|
||||
}
|
||||
if let Some(v) = req.notes {
|
||||
active.notes = Set(Some(v));
|
||||
}
|
||||
active.updated_at = Set(Utc::now());
|
||||
active.updated_by = Set(operator_id);
|
||||
active.version = Set(next_ver);
|
||||
@@ -171,10 +235,16 @@ pub async fn update_prescription(
|
||||
let m = active.update(&state.db).await?;
|
||||
|
||||
audit_service::record(
|
||||
AuditLog::new(tenant_id, operator_id, "dialysis_prescription.updated", "dialysis_prescription")
|
||||
.with_resource_id(m.id),
|
||||
AuditLog::new(
|
||||
tenant_id,
|
||||
operator_id,
|
||||
"dialysis_prescription.updated",
|
||||
"dialysis_prescription",
|
||||
)
|
||||
.with_resource_id(m.id),
|
||||
&state.db,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(model_to_resp(m))
|
||||
}
|
||||
@@ -205,10 +275,16 @@ pub async fn delete_prescription(
|
||||
active.update(&state.db).await?;
|
||||
|
||||
audit_service::record(
|
||||
AuditLog::new(tenant_id, operator_id, "dialysis_prescription.deleted", "dialysis_prescription")
|
||||
.with_resource_id(id),
|
||||
AuditLog::new(
|
||||
tenant_id,
|
||||
operator_id,
|
||||
"dialysis_prescription.deleted",
|
||||
"dialysis_prescription",
|
||||
)
|
||||
.with_resource_id(id),
|
||||
&state.db,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -252,7 +328,8 @@ fn validate_anticoagulation_type(val: Option<&str>) -> DialysisResult<()> {
|
||||
let valid = ["heparin", "lmwh", "heparin_free"];
|
||||
if !valid.contains(&t) {
|
||||
return Err(DialysisError::Validation(format!(
|
||||
"anticoagulation_type 必须为: {}", valid.join(", ")
|
||||
"anticoagulation_type 必须为: {}",
|
||||
valid.join(", ")
|
||||
)));
|
||||
}
|
||||
}
|
||||
@@ -264,7 +341,8 @@ fn validate_vascular_access_type(val: Option<&str>) -> DialysisResult<()> {
|
||||
let valid = ["avf", "avg", "cvc"];
|
||||
if !valid.contains(&t) {
|
||||
return Err(DialysisError::Validation(format!(
|
||||
"vascular_access_type 必须为: {}", valid.join(", ")
|
||||
"vascular_access_type 必须为: {}",
|
||||
valid.join(", ")
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,13 @@ pub async fn list_dialysis_records(
|
||||
let crypto = &state.crypto;
|
||||
let data: Vec<DialysisRecordResp> = models.into_iter().map(|m| to_resp(crypto, m)).collect();
|
||||
|
||||
Ok(PaginatedResponse { data, total, page, page_size: limit, total_pages })
|
||||
Ok(PaginatedResponse {
|
||||
data,
|
||||
total,
|
||||
page,
|
||||
page_size: limit,
|
||||
total_pages,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_dialysis_record(
|
||||
@@ -92,15 +98,19 @@ pub async fn create_dialysis_record(
|
||||
let kek = state.crypto.kek();
|
||||
|
||||
// PII 加密
|
||||
let encrypted_symptoms = req.symptoms.as_ref()
|
||||
let encrypted_symptoms = req
|
||||
.symptoms
|
||||
.as_ref()
|
||||
.map(|v| -> DialysisResult<serde_json::Value> {
|
||||
let json_str = serde_json::to_string(v)
|
||||
.map_err(|e| DialysisError::Validation(e.to_string()))?;
|
||||
let json_str =
|
||||
serde_json::to_string(v).map_err(|e| DialysisError::Validation(e.to_string()))?;
|
||||
Ok(serde_json::Value::String(pii::encrypt(kek, &json_str)?))
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
let encrypted_complication = req.complication_notes.as_ref()
|
||||
let encrypted_complication = req
|
||||
.complication_notes
|
||||
.as_ref()
|
||||
.map(|c| pii::encrypt(kek, c))
|
||||
.transpose()?;
|
||||
|
||||
@@ -112,9 +122,15 @@ pub async fn create_dialysis_record(
|
||||
dialysis_date: Set(req.dialysis_date),
|
||||
start_time: Set(req.start_time),
|
||||
end_time: Set(req.end_time),
|
||||
dry_weight: Set(req.dry_weight.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
pre_weight: Set(req.pre_weight.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
post_weight: Set(req.post_weight.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
dry_weight: Set(req
|
||||
.dry_weight
|
||||
.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
pre_weight: Set(req
|
||||
.pre_weight
|
||||
.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
post_weight: Set(req
|
||||
.post_weight
|
||||
.map(|v| Decimal::from_f64_retain(v).unwrap_or_default())),
|
||||
pre_bp_systolic: Set(req.pre_bp_systolic),
|
||||
pre_bp_diastolic: Set(req.pre_bp_diastolic),
|
||||
post_bp_systolic: Set(req.post_bp_systolic),
|
||||
@@ -142,10 +158,16 @@ pub async fn create_dialysis_record(
|
||||
let m = active.insert(&state.db).await?;
|
||||
|
||||
audit_service::record(
|
||||
AuditLog::new(tenant_id, operator_id, "dialysis_record.created", "dialysis_record")
|
||||
.with_resource_id(m.id),
|
||||
AuditLog::new(
|
||||
tenant_id,
|
||||
operator_id,
|
||||
"dialysis_record.created",
|
||||
"dialysis_record",
|
||||
)
|
||||
.with_resource_id(m.id),
|
||||
&state.db,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
|
||||
// 发布透析记录创建事件
|
||||
let event = DomainEvent::new(
|
||||
@@ -182,27 +204,61 @@ pub async fn update_dialysis_record(
|
||||
.map_err(|_| DialysisError::VersionMismatch)?;
|
||||
|
||||
let mut active: dialysis_record::ActiveModel = model.into();
|
||||
if let Some(v) = req.dialysis_date { active.dialysis_date = Set(v); }
|
||||
if let Some(v) = req.start_time { active.start_time = Set(Some(v)); }
|
||||
if let Some(v) = req.end_time { active.end_time = Set(Some(v)); }
|
||||
if let Some(v) = req.dry_weight { active.dry_weight = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default())); }
|
||||
if let Some(v) = req.pre_weight { active.pre_weight = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default())); }
|
||||
if let Some(v) = req.post_weight { active.post_weight = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default())); }
|
||||
if let Some(v) = req.pre_bp_systolic { active.pre_bp_systolic = Set(Some(v)); }
|
||||
if let Some(v) = req.pre_bp_diastolic { active.pre_bp_diastolic = Set(Some(v)); }
|
||||
if let Some(v) = req.post_bp_systolic { active.post_bp_systolic = Set(Some(v)); }
|
||||
if let Some(v) = req.post_bp_diastolic { active.post_bp_diastolic = Set(Some(v)); }
|
||||
if let Some(v) = req.pre_heart_rate { active.pre_heart_rate = Set(Some(v)); }
|
||||
if let Some(v) = req.post_heart_rate { active.post_heart_rate = Set(Some(v)); }
|
||||
if let Some(v) = req.ultrafiltration_volume { active.ultrafiltration_volume = Set(Some(v)); }
|
||||
if let Some(v) = req.dialysis_duration { active.dialysis_duration = Set(Some(v)); }
|
||||
if let Some(v) = req.blood_flow_rate { active.blood_flow_rate = Set(Some(v)); }
|
||||
if let Some(ref v) = req.dialysis_type { validate_dialysis_type(v)?; active.dialysis_type = Set(v.clone()); }
|
||||
if let Some(v) = req.dialysis_date {
|
||||
active.dialysis_date = Set(v);
|
||||
}
|
||||
if let Some(v) = req.start_time {
|
||||
active.start_time = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.end_time {
|
||||
active.end_time = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.dry_weight {
|
||||
active.dry_weight = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default()));
|
||||
}
|
||||
if let Some(v) = req.pre_weight {
|
||||
active.pre_weight = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default()));
|
||||
}
|
||||
if let Some(v) = req.post_weight {
|
||||
active.post_weight = Set(Some(Decimal::from_f64_retain(v).unwrap_or_default()));
|
||||
}
|
||||
if let Some(v) = req.pre_bp_systolic {
|
||||
active.pre_bp_systolic = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.pre_bp_diastolic {
|
||||
active.pre_bp_diastolic = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.post_bp_systolic {
|
||||
active.post_bp_systolic = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.post_bp_diastolic {
|
||||
active.post_bp_diastolic = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.pre_heart_rate {
|
||||
active.pre_heart_rate = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.post_heart_rate {
|
||||
active.post_heart_rate = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.ultrafiltration_volume {
|
||||
active.ultrafiltration_volume = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.dialysis_duration {
|
||||
active.dialysis_duration = Set(Some(v));
|
||||
}
|
||||
if let Some(v) = req.blood_flow_rate {
|
||||
active.blood_flow_rate = Set(Some(v));
|
||||
}
|
||||
if let Some(ref v) = req.dialysis_type {
|
||||
validate_dialysis_type(v)?;
|
||||
active.dialysis_type = Set(v.clone());
|
||||
}
|
||||
if let Some(v) = req.symptoms {
|
||||
let kek = state.crypto.kek();
|
||||
let encrypted = Some(serde_json::Value::String(
|
||||
pii::encrypt(kek, &serde_json::to_string(&v).unwrap_or_default())?
|
||||
));
|
||||
let encrypted = Some(serde_json::Value::String(pii::encrypt(
|
||||
kek,
|
||||
&serde_json::to_string(&v).unwrap_or_default(),
|
||||
)?));
|
||||
active.symptoms = Set(encrypted);
|
||||
}
|
||||
if let Some(v) = req.complication_notes {
|
||||
@@ -218,10 +274,16 @@ pub async fn update_dialysis_record(
|
||||
let m = active.update(&state.db).await?;
|
||||
|
||||
audit_service::record(
|
||||
AuditLog::new(tenant_id, operator_id, "dialysis_record.updated", "dialysis_record")
|
||||
.with_resource_id(m.id),
|
||||
AuditLog::new(
|
||||
tenant_id,
|
||||
operator_id,
|
||||
"dialysis_record.updated",
|
||||
"dialysis_record",
|
||||
)
|
||||
.with_resource_id(m.id),
|
||||
&state.db,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(to_resp(&state.crypto, m))
|
||||
}
|
||||
@@ -255,10 +317,16 @@ pub async fn complete_dialysis_record(
|
||||
let m = active.update(&state.db).await?;
|
||||
|
||||
audit_service::record(
|
||||
AuditLog::new(tenant_id, operator_id, "dialysis_record.completed", "dialysis_record")
|
||||
.with_resource_id(m.id),
|
||||
AuditLog::new(
|
||||
tenant_id,
|
||||
operator_id,
|
||||
"dialysis_record.completed",
|
||||
"dialysis_record",
|
||||
)
|
||||
.with_resource_id(m.id),
|
||||
&state.db,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(to_resp(&state.crypto, m))
|
||||
}
|
||||
@@ -294,10 +362,16 @@ pub async fn review_dialysis_record(
|
||||
let m = active.update(&state.db).await?;
|
||||
|
||||
audit_service::record(
|
||||
AuditLog::new(tenant_id, Some(reviewer_id), "dialysis_record.reviewed", "dialysis_record")
|
||||
.with_resource_id(m.id),
|
||||
AuditLog::new(
|
||||
tenant_id,
|
||||
Some(reviewer_id),
|
||||
"dialysis_record.reviewed",
|
||||
"dialysis_record",
|
||||
)
|
||||
.with_resource_id(m.id),
|
||||
&state.db,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(to_resp(&state.crypto, m))
|
||||
}
|
||||
@@ -328,10 +402,16 @@ pub async fn delete_dialysis_record(
|
||||
active.update(&state.db).await?;
|
||||
|
||||
audit_service::record(
|
||||
AuditLog::new(tenant_id, operator_id, "dialysis_record.deleted", "dialysis_record")
|
||||
.with_resource_id(record_id),
|
||||
AuditLog::new(
|
||||
tenant_id,
|
||||
operator_id,
|
||||
"dialysis_record.deleted",
|
||||
"dialysis_record",
|
||||
)
|
||||
.with_resource_id(record_id),
|
||||
&state.db,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -345,7 +425,8 @@ fn validate_dialysis_type(dialysis_type: &str) -> DialysisResult<()> {
|
||||
match dialysis_type {
|
||||
"HD" | "HDF" | "HF" => Ok(()),
|
||||
_ => Err(DialysisError::Validation(format!(
|
||||
"无效的透析类型: {},允许值: HD, HDF, HF", dialysis_type
|
||||
"无效的透析类型: {},允许值: HD, HDF, HF",
|
||||
dialysis_type
|
||||
))),
|
||||
}
|
||||
}
|
||||
@@ -365,7 +446,8 @@ fn validate_dialysis_status_transition(current: &str, new: &str) -> DialysisResu
|
||||
Ok(())
|
||||
} else {
|
||||
Err(DialysisError::InvalidStatusTransition(format!(
|
||||
"dialysis_record.status: 不允许从 '{}' 转换到 '{}'", current, new
|
||||
"dialysis_record.status: 不允许从 '{}' 转换到 '{}'",
|
||||
current, new
|
||||
)))
|
||||
}
|
||||
}
|
||||
@@ -374,14 +456,18 @@ fn to_resp(crypto: &erp_core::crypto::PiiCrypto, m: dialysis_record::Model) -> D
|
||||
let kek = crypto.kek();
|
||||
|
||||
// 解密症状 JSON(加密时存储为 Value::String(ciphertext))
|
||||
let symptoms = m.symptoms.as_ref()
|
||||
let symptoms = m
|
||||
.symptoms
|
||||
.as_ref()
|
||||
.and_then(|v| v.as_str())
|
||||
.and_then(|s| pii::decrypt(kek, s).ok())
|
||||
.and_then(|s| serde_json::from_str(&s).ok())
|
||||
.or(m.symptoms);
|
||||
|
||||
// 解密并发症备注
|
||||
let complication_notes = m.complication_notes.as_ref()
|
||||
let complication_notes = m
|
||||
.complication_notes
|
||||
.as_ref()
|
||||
.map(|c| pii::decrypt(kek, c).unwrap_or_else(|_| c.clone()))
|
||||
.or(m.complication_notes);
|
||||
|
||||
@@ -421,25 +507,45 @@ mod tests {
|
||||
|
||||
// --- validate_dialysis_type ---
|
||||
#[test]
|
||||
fn dialysis_type_hd() { assert!(validate_dialysis_type("HD").is_ok()); }
|
||||
fn dialysis_type_hd() {
|
||||
assert!(validate_dialysis_type("HD").is_ok());
|
||||
}
|
||||
#[test]
|
||||
fn dialysis_type_hdf() { assert!(validate_dialysis_type("HDF").is_ok()); }
|
||||
fn dialysis_type_hdf() {
|
||||
assert!(validate_dialysis_type("HDF").is_ok());
|
||||
}
|
||||
#[test]
|
||||
fn dialysis_type_hf() { assert!(validate_dialysis_type("HF").is_ok()); }
|
||||
fn dialysis_type_hf() {
|
||||
assert!(validate_dialysis_type("HF").is_ok());
|
||||
}
|
||||
#[test]
|
||||
fn dialysis_type_invalid() { assert!(validate_dialysis_type("PD").is_err()); }
|
||||
fn dialysis_type_invalid() {
|
||||
assert!(validate_dialysis_type("PD").is_err());
|
||||
}
|
||||
|
||||
// --- validate_dialysis_status_transition ---
|
||||
#[test]
|
||||
fn dial_draft_to_completed() { assert!(validate_dialysis_status_transition("draft", "completed").is_ok()); }
|
||||
fn dial_draft_to_completed() {
|
||||
assert!(validate_dialysis_status_transition("draft", "completed").is_ok());
|
||||
}
|
||||
#[test]
|
||||
fn dial_draft_to_reviewed_fails() { assert!(validate_dialysis_status_transition("draft", "reviewed").is_err()); }
|
||||
fn dial_draft_to_reviewed_fails() {
|
||||
assert!(validate_dialysis_status_transition("draft", "reviewed").is_err());
|
||||
}
|
||||
#[test]
|
||||
fn dial_completed_to_reviewed() { assert!(validate_dialysis_status_transition("completed", "reviewed").is_ok()); }
|
||||
fn dial_completed_to_reviewed() {
|
||||
assert!(validate_dialysis_status_transition("completed", "reviewed").is_ok());
|
||||
}
|
||||
#[test]
|
||||
fn dial_completed_to_draft_fails() { assert!(validate_dialysis_status_transition("completed", "draft").is_err()); }
|
||||
fn dial_completed_to_draft_fails() {
|
||||
assert!(validate_dialysis_status_transition("completed", "draft").is_err());
|
||||
}
|
||||
#[test]
|
||||
fn dial_reviewed_to_any_fails() { assert!(validate_dialysis_status_transition("reviewed", "draft").is_err()); }
|
||||
fn dial_reviewed_to_any_fails() {
|
||||
assert!(validate_dialysis_status_transition("reviewed", "draft").is_err());
|
||||
}
|
||||
#[test]
|
||||
fn dial_same_status_ok() { assert!(validate_dialysis_status_transition("draft", "draft").is_ok()); }
|
||||
fn dial_same_status_ok() {
|
||||
assert!(validate_dialysis_status_transition("draft", "draft").is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use sea_orm::{DatabaseBackend, FromQueryResult, Statement};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::dialysis_stats_dto::{DialysisStatisticsResp, NameValue};
|
||||
use crate::error::{DialysisResult, DialysisError};
|
||||
use crate::error::{DialysisError, DialysisResult};
|
||||
use crate::state::DialysisState;
|
||||
|
||||
pub async fn get_dialysis_statistics(
|
||||
@@ -12,7 +12,9 @@ pub async fn get_dialysis_statistics(
|
||||
let db = &state.db;
|
||||
|
||||
#[derive(FromQueryResult)]
|
||||
struct CountRow { count: i64 }
|
||||
struct CountRow {
|
||||
count: i64,
|
||||
}
|
||||
|
||||
let total_records = CountRow::find_by_statement(Statement::from_sql_and_values(
|
||||
DatabaseBackend::Postgres,
|
||||
@@ -33,12 +35,14 @@ pub async fn get_dialysis_statistics(
|
||||
)).one(db).await?.map(|r| r.count).unwrap_or(0);
|
||||
|
||||
let type_distribution = count_by_field(
|
||||
db, tenant_id,
|
||||
db,
|
||||
tenant_id,
|
||||
"SELECT dialysis_type AS name, COUNT(*) AS value FROM dialysis_record \
|
||||
WHERE tenant_id = $1 AND deleted_at IS NULL \
|
||||
AND created_at >= date_trunc('month', NOW()) \
|
||||
GROUP BY dialysis_type ORDER BY value DESC",
|
||||
).await?;
|
||||
)
|
||||
.await?;
|
||||
|
||||
let complication_rate = compute_complication_rate(db, tenant_id).await?;
|
||||
let avg_ultrafiltration = compute_avg_field(db, tenant_id, "ultrafiltration_volume").await?;
|
||||
@@ -61,7 +65,10 @@ async fn count_by_field(
|
||||
sql: &str,
|
||||
) -> DialysisResult<Vec<NameValue>> {
|
||||
#[derive(FromQueryResult)]
|
||||
struct NameValueRow { name: String, value: i64 }
|
||||
struct NameValueRow {
|
||||
name: String,
|
||||
value: i64,
|
||||
}
|
||||
|
||||
let rows: Vec<NameValueRow> = FromQueryResult::find_by_statement(
|
||||
Statement::from_sql_and_values(DatabaseBackend::Postgres, sql, [tenant_id.into()]),
|
||||
@@ -69,17 +76,29 @@ async fn count_by_field(
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(rows.into_iter().map(|r| NameValue { name: r.name, value: r.value }).collect())
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.map(|r| NameValue {
|
||||
name: r.name,
|
||||
value: r.value,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
#[derive(Debug, FromQueryResult)]
|
||||
struct AvgFieldResult { avg_val: Option<f64> }
|
||||
struct AvgFieldResult {
|
||||
avg_val: Option<f64>,
|
||||
}
|
||||
|
||||
macro_rules! avg_field_sql {
|
||||
($field:literal) => {
|
||||
concat!(
|
||||
"SELECT AVG(", $field, ")::FLOAT8 AS avg_val FROM dialysis_record ",
|
||||
"WHERE tenant_id = $1 AND deleted_at IS NULL AND ", $field, " IS NOT NULL ",
|
||||
"SELECT AVG(",
|
||||
$field,
|
||||
")::FLOAT8 AS avg_val FROM dialysis_record ",
|
||||
"WHERE tenant_id = $1 AND deleted_at IS NULL AND ",
|
||||
$field,
|
||||
" IS NOT NULL ",
|
||||
"AND created_at >= date_trunc('month', NOW())"
|
||||
)
|
||||
};
|
||||
@@ -94,7 +113,11 @@ async fn compute_avg_field(
|
||||
"ultrafiltration_volume" => avg_field_sql!("ultrafiltration_volume"),
|
||||
"dialysis_duration" => avg_field_sql!("dialysis_duration"),
|
||||
"blood_flow_rate" => avg_field_sql!("blood_flow_rate"),
|
||||
_ => return Err(DialysisError::Validation(format!("不允许的字段名: {field}"))),
|
||||
_ => {
|
||||
return Err(DialysisError::Validation(format!(
|
||||
"不允许的字段名: {field}"
|
||||
)));
|
||||
}
|
||||
};
|
||||
let result: Option<AvgFieldResult> = FromQueryResult::find_by_statement(
|
||||
Statement::from_sql_and_values(DatabaseBackend::Postgres, sql, [tenant_id.into()]),
|
||||
@@ -119,7 +142,10 @@ async fn compute_complication_rate(
|
||||
"#;
|
||||
|
||||
#[derive(Debug, FromQueryResult)]
|
||||
struct CompResult { with_comp: i64, total: i64 }
|
||||
struct CompResult {
|
||||
with_comp: i64,
|
||||
total: i64,
|
||||
}
|
||||
|
||||
let result: Option<CompResult> = FromQueryResult::find_by_statement(
|
||||
Statement::from_sql_and_values(DatabaseBackend::Postgres, sql, [tenant_id.into()]),
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
pub mod dialysis_service;
|
||||
pub mod dialysis_prescription_service;
|
||||
pub mod dialysis_service;
|
||||
pub mod dialysis_stats_service;
|
||||
|
||||
Reference in New Issue
Block a user