use axum::Extension; use axum::extract::{FromRef, Json, Path, Query, State}; use erp_core::error::AppError; use erp_core::rbac::require_permission; use erp_core::types::{ApiResponse, TenantContext}; use uuid::Uuid; use crate::dto::DeleteWithVersion; use crate::dto::ble_gateway_dto::*; use crate::gateway_auth::GatewayAuthContext; use crate::service::ble_gateway_service; use crate::state::HealthState; // --------------------------------------------------------------------------- // Gateway 管理(需要用户 JWT 认证) // --------------------------------------------------------------------------- pub async fn list_gateways( State(state): State, Extension(ctx): Extension, Query(params): Query, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.list")?; let result = ble_gateway_service::list_gateways(&state, ctx.tenant_id, ¶ms).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn get_gateway( State(state): State, Extension(ctx): Extension, Path(gateway_id): Path, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.list")?; let result = ble_gateway_service::get_gateway(&state, ctx.tenant_id, gateway_id).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn create_gateway( State(state): State, Extension(ctx): Extension, Json(body): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.manage")?; let result = ble_gateway_service::create_gateway(&state, ctx.tenant_id, Some(ctx.user_id), body).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn update_gateway( State(state): State, Extension(ctx): Extension, Path(gateway_id): Path, Json(body): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.manage")?; let result = ble_gateway_service::update_gateway( &state, ctx.tenant_id, gateway_id, Some(ctx.user_id), body, ) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn delete_gateway( State(state): State, Extension(ctx): Extension, Path(gateway_id): Path, Query(params): Query, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.manage")?; ble_gateway_service::delete_gateway( &state, ctx.tenant_id, gateway_id, Some(ctx.user_id), params.version, ) .await?; Ok(Json(ApiResponse::ok(()))) } pub async fn regenerate_api_key( State(state): State, Extension(ctx): Extension, Path(gateway_id): Path, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.manage")?; let result = ble_gateway_service::regenerate_api_key( &state, ctx.tenant_id, gateway_id, Some(ctx.user_id), ) .await?; Ok(Json(ApiResponse::ok(result))) } // --------------------------------------------------------------------------- // Binding 管理(需要用户 JWT 认证) // --------------------------------------------------------------------------- pub async fn list_bindings( State(state): State, Extension(ctx): Extension, Path(gateway_id): Path, Query(params): Query, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.list")?; let page = params.page.unwrap_or(1); let page_size = params.page_size.unwrap_or(20).min(100); let result = ble_gateway_service::list_bindings(&state, ctx.tenant_id, gateway_id, page, page_size) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn bind_patient( State(state): State, Extension(ctx): Extension, Path(gateway_id): Path, Json(body): Json, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.manage")?; let result = ble_gateway_service::bind_patient( &state, ctx.tenant_id, gateway_id, Some(ctx.user_id), body, ) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn batch_bind( State(state): State, Extension(ctx): Extension, Path(gateway_id): Path, Json(body): Json, ) -> Result>>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.manage")?; let result = ble_gateway_service::batch_bind(&state, ctx.tenant_id, gateway_id, Some(ctx.user_id), body) .await?; Ok(Json(ApiResponse::ok(result))) } pub async fn unbind_patient( State(state): State, Extension(ctx): Extension, Path((gateway_id, binding_id)): Path<(Uuid, Uuid)>, Query(params): Query, ) -> Result>, AppError> where HealthState: FromRef, S: Clone + Send + Sync + 'static, { require_permission(&ctx, "health.ble-gateways.manage")?; ble_gateway_service::unbind_patient( &state, ctx.tenant_id, gateway_id, binding_id, Some(ctx.user_id), params.version, ) .await?; Ok(Json(ApiResponse::ok(()))) } // --------------------------------------------------------------------------- // 网关端点(API Key 认证,无需用户 JWT) // --------------------------------------------------------------------------- pub async fn gateway_upload( State(state): State, Extension(ctx): Extension, Json(body): Json, ) -> Result>, AppError> { let result = ble_gateway_service::gateway_upload(&state, &ctx, body).await?; Ok(Json(ApiResponse::ok(result))) } pub async fn gateway_heartbeat( State(state): State, Extension(ctx): Extension, Json(body): Json, ) -> Result>, AppError> { ble_gateway_service::heartbeat(&state, &ctx, body).await?; Ok(Json(ApiResponse::ok(()))) }