use axum::Extension; use axum::extract::{FromRef, Json, State}; use erp_core::aggregate::safe_aggregate; use erp_core::error::AppError; use erp_core::rbac::require_permission; use erp_core::types::{ApiResponse, TenantContext}; use crate::dto::stats_dto::*; use crate::service::stats_service; use crate::state::HealthState; pub async fn get_patient_stats( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.patient.list")?; let result = stats_service::get_patient_statistics(&state, ctx.tenant_id).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn get_consultation_stats( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.consultation.list")?; let result = safe_aggregate( stats_service::get_consultation_statistics(&state, ctx.tenant_id), "咨询统计", ) .await; Ok(Json(ApiResponse::ok(result))) } pub async fn get_follow_up_stats( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.follow-up.list")?; let result = stats_service::get_follow_up_statistics(&state, ctx.tenant_id).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn get_dashboard_stats( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.patient.list")?; let patients = safe_aggregate( stats_service::get_patient_statistics(&state, ctx.tenant_id), "患者统计", ) .await; let consultations = safe_aggregate( stats_service::get_consultation_statistics(&state, ctx.tenant_id), "咨询统计", ) .await; let follow_ups = safe_aggregate( stats_service::get_follow_up_statistics(&state, ctx.tenant_id), "随访统计", ) .await; Ok(Json(ApiResponse::ok(DashboardStatsResp { patients, consultations, follow_ups, }))) } // --------------------------------------------------------------------------- // 健康数据统计 // --------------------------------------------------------------------------- pub async fn get_lab_report_stats( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.patient.list")?; let result = safe_aggregate( stats_service::get_lab_report_statistics(&state, ctx.tenant_id), "化验报告统计", ) .await; Ok(Json(ApiResponse::ok(result))) } pub async fn get_appointment_stats( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.patient.list")?; let result = safe_aggregate( stats_service::get_appointment_statistics(&state, ctx.tenant_id), "预约统计", ) .await; Ok(Json(ApiResponse::ok(result))) } pub async fn get_vital_signs_report_rate( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.patient.list")?; let result = safe_aggregate( stats_service::get_vital_signs_report_rate(&state, ctx.tenant_id), "体征上报率统计", ) .await; Ok(Json(ApiResponse::ok(result))) } pub async fn get_health_data_stats( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.patient.list")?; let lab_reports = safe_aggregate( stats_service::get_lab_report_statistics(&state, ctx.tenant_id), "化验报告统计", ) .await; let appointments = safe_aggregate( stats_service::get_appointment_statistics(&state, ctx.tenant_id), "预约统计", ) .await; let vital_signs_report_rate = safe_aggregate( stats_service::get_vital_signs_report_rate(&state, ctx.tenant_id), "体征上报率统计", ) .await; Ok(Json(ApiResponse::ok(HealthDataStatsResp { lab_reports, appointments, vital_signs_report_rate, }))) } // --------------------------------------------------------------------------- // 个人维度统计 // --------------------------------------------------------------------------- pub async fn get_personal_stats( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.patient.list")?; let result = stats_service::get_personal_stats(&state, ctx.user_id, ctx.tenant_id).await?; Ok(Json(ApiResponse::ok(result))) } // --------------------------------------------------------------------------- // 工作台管理统计 // --------------------------------------------------------------------------- pub async fn get_system_health( State(state): State, Extension(_ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { let result = stats_service::get_system_health(&state).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn get_user_activity( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.dashboard.manage")?; let result = stats_service::get_user_activity(&state.db, ctx.tenant_id).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn get_module_status( State(state): State, Extension(ctx): Extension, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.dashboard.manage")?; let result = stats_service::get_module_status(&state).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn get_points_recent_activity( State(state): State, Extension(ctx): Extension, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.dashboard.manage")?; let result = stats_service::get_points_recent_activity(&state.db, ctx.tenant_id, 10).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn get_article_stats( State(state): State, Extension(ctx): Extension, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.dashboard.manage")?; let result = stats_service::get_article_stats(&state.db, ctx.tenant_id).await?; Ok(Json(ApiResponse::ok(result))) }