126 lines
3.5 KiB
Rust
126 lines
3.5 KiB
Rust
use axum::Extension;
|
|
use axum::extract::{FromRef, Path, Query, State};
|
|
use axum::response::IntoResponse;
|
|
use serde::Deserialize;
|
|
use utoipa::IntoParams;
|
|
use uuid::Uuid;
|
|
|
|
use erp_core::error::AppError;
|
|
use erp_core::rbac::require_permission;
|
|
use erp_core::types::{ApiResponse, TenantContext};
|
|
|
|
use crate::service::device_reading_service;
|
|
use crate::state::HealthState;
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct PatientPath {
|
|
pub patient_id: Uuid,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, IntoParams)]
|
|
pub struct ReadingListQuery {
|
|
pub device_type: Option<String>,
|
|
pub hours: Option<i64>,
|
|
pub page: Option<u64>,
|
|
pub page_size: Option<u64>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, IntoParams)]
|
|
pub struct HourlyQuery {
|
|
pub device_type: String,
|
|
pub days: Option<i64>,
|
|
pub page: Option<u64>,
|
|
pub page_size: Option<u64>,
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post,
|
|
path = "/health/patients/{patient_id}/device-readings/batch",
|
|
responses((status = 200, description = "批量创建成功")),
|
|
tag = "设备数据",
|
|
security(("bearer_auth" = [])),
|
|
)]
|
|
pub async fn batch_create<S>(
|
|
State(state): State<HealthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(path): Path<PatientPath>,
|
|
axum::Json(body): axum::Json<device_reading_service::BatchReadingRequest>,
|
|
) -> Result<impl IntoResponse, AppError>
|
|
where
|
|
HealthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "health.device-readings.manage")?;
|
|
let result =
|
|
device_reading_service::batch_create_readings(&state, ctx.tenant_id, path.patient_id, body)
|
|
.await?;
|
|
Ok(axum::Json(ApiResponse::ok(result)))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/health/patients/{patient_id}/device-readings",
|
|
responses((status = 200, description = "设备读数列表")),
|
|
tag = "设备数据",
|
|
security(("bearer_auth" = [])),
|
|
)]
|
|
pub async fn list_readings<S>(
|
|
State(state): State<HealthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(path): Path<PatientPath>,
|
|
Query(query): Query<ReadingListQuery>,
|
|
) -> Result<impl IntoResponse, AppError>
|
|
where
|
|
HealthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "health.device-readings.list")?;
|
|
let page = query.page.unwrap_or(1);
|
|
let page_size = query.page_size.unwrap_or(20).min(100);
|
|
let result = device_reading_service::query_device_readings(
|
|
&state,
|
|
ctx.tenant_id,
|
|
path.patient_id,
|
|
query.device_type.as_deref(),
|
|
query.hours,
|
|
page,
|
|
page_size,
|
|
)
|
|
.await?;
|
|
Ok(axum::Json(ApiResponse::ok(result)))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/health/patients/{patient_id}/device-readings/hourly",
|
|
responses((status = 200, description = "小时聚合数据")),
|
|
tag = "设备数据",
|
|
security(("bearer_auth" = [])),
|
|
)]
|
|
pub async fn list_hourly<S>(
|
|
State(state): State<HealthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(path): Path<PatientPath>,
|
|
Query(query): Query<HourlyQuery>,
|
|
) -> Result<impl IntoResponse, AppError>
|
|
where
|
|
HealthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "health.device-readings.list")?;
|
|
let page = query.page.unwrap_or(1);
|
|
let page_size = query.page_size.unwrap_or(20).min(100);
|
|
let days = query.days.unwrap_or(7);
|
|
let result = device_reading_service::query_hourly_readings(
|
|
&state,
|
|
ctx.tenant_id,
|
|
path.patient_id,
|
|
&query.device_type,
|
|
days,
|
|
page,
|
|
page_size,
|
|
)
|
|
.await?;
|
|
Ok(axum::Json(ApiResponse::ok(result)))
|
|
}
|