use axum::Extension; use axum::extract::{FromRef, Json, Path, State}; use erp_core::error::AppError; use erp_core::rbac::require_permission; use erp_core::types::{ApiResponse, TenantContext}; use serde::Deserialize; use crate::service::critical_value_threshold_service; use crate::state::HealthState; #[derive(Debug, Deserialize)] pub struct CreateThresholdReq { pub indicator: String, pub direction: String, pub threshold_value: f64, pub level: Option, pub department: Option, pub age_min: Option, pub age_max: Option, } #[derive(Debug, Deserialize)] pub struct UpdateThresholdReq { pub threshold_value: f64, pub level: Option, pub department: Option, pub age_min: Option, pub age_max: Option, pub version: i32, } pub async fn list_thresholds( State(state): State, Extension(ctx): Extension, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.critical-value-thresholds.list")?; let list = critical_value_threshold_service::find_thresholds(&state.db, ctx.tenant_id).await?; Ok(Json(ApiResponse::ok(list))) } pub async fn create_threshold( State(state): State, Extension(ctx): Extension, Json(req): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.critical-value-thresholds.manage")?; let result = critical_value_threshold_service::create_threshold( &state.db, ctx.tenant_id, Some(ctx.user_id), req.indicator, req.direction, req.threshold_value, req.level.unwrap_or_else(|| "critical".to_string()), req.department, req.age_min, req.age_max, ) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn update_threshold( State(state): State, Extension(ctx): Extension, Path(id): Path, Json(req): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.critical-value-thresholds.manage")?; let result = critical_value_threshold_service::update_threshold( &state.db, ctx.tenant_id, id, Some(ctx.user_id), req.threshold_value, req.level.unwrap_or_else(|| "critical".to_string()), req.department, req.age_min, req.age_max, req.version, ) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn delete_threshold( State(state): State, Extension(ctx): Extension, Path(id): Path, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.critical-value-thresholds.manage")?; critical_value_threshold_service::delete_threshold( &state.db, ctx.tenant_id, id, Some(ctx.user_id), ) .await?; Ok(Json(ApiResponse::ok(()))) } /// 患者端只读接口 — 返回当前租户所有活跃阈值,仅需认证。 pub async fn list_public_thresholds( State(state): State, Extension(ctx): Extension, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { let list = critical_value_threshold_service::find_thresholds(&state.db, ctx.tenant_id).await?; Ok(Json(ApiResponse::ok(list))) }