Files
soft/backend/src/HealthManager.WebApi/Controllers/ReportController.cs

109 lines
4.2 KiB
C#

using System.Security.Claims;
using HealthManager.Application.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace HealthManager.WebApi.Controllers;
[ApiController]
[Route("api/reports")]
[Authorize]
public class ReportController(ReportService reportService) : ControllerBase
{
private Guid UserId => Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
private string Role => User.FindFirstValue(ClaimTypes.Role)!;
[HttpGet]
public async Task<IActionResult> GetReports()
{
// Patients: see own reports. Doctors: see all reports (or filter by patientId)
if (Role == "doctor")
{
if (Request.Query.ContainsKey("patientId"))
{
var targetUserId = Guid.Parse(Request.Query["patientId"]!);
var reports = await reportService.GetPatientReportsAsync(targetUserId);
return Ok(reports.Select(r => new
{
r.Id, r.PatientId, r.Title, r.Category, r.ImageUrls, r.Status,
r.RiskLevel, r.UploadedAt, r.CompletedAt,
PatientName = r.Patient?.Name,
DoctorName = r.Doctor?.Name,
}));
}
var allReports = await reportService.GetAllReportsAsync();
return Ok(allReports.Select(r => new
{
r.Id, r.PatientId, r.Title, r.Category, r.ImageUrls, r.Status,
r.RiskLevel, r.UploadedAt, r.CompletedAt,
PatientName = r.Patient?.Name,
DoctorName = r.Doctor?.Name,
}));
}
var myReports = await reportService.GetPatientReportsAsync(UserId);
return Ok(myReports.Select(r => new
{
r.Id, r.PatientId, r.Title, r.Category, r.ImageUrls, r.Status,
r.RiskLevel, r.UploadedAt, r.CompletedAt,
DoctorName = r.Doctor?.Name,
}));
}
[HttpGet("pending")]
[Authorize(Roles = "doctor")]
public async Task<IActionResult> GetPending()
{
var reports = await reportService.GetPendingAsync();
return Ok(reports.Select(r => new
{
r.Id, r.PatientId, r.Title, r.Category, r.Status, r.UploadedAt,
PatientName = r.Patient?.Name,
}));
}
[HttpGet("{id:guid}")]
public async Task<IActionResult> GetReport(Guid id)
{
var report = await reportService.GetByIdAsync(id);
if (report == null) return NotFound(new { message = "报告不存在" });
return Ok(new
{
report.Id, report.PatientId, report.Title, report.Category, report.ImageUrls,
report.Status, report.RiskLevel, report.Summary, report.Suggestions,
report.UploadedAt, report.CompletedAt,
PatientName = report.Patient?.Name,
DoctorName = report.Doctor?.Name,
Items = report.Items.Select(i => new
{
i.Id, i.ItemName, i.ResultValue, i.Unit, i.ReferenceRange, i.IsAbnormal,
}),
});
}
[HttpPost]
public async Task<IActionResult> UploadReport([FromBody] ReportUploadRequest request)
{
var report = await reportService.UploadAsync(UserId, request.Title, request.Category, request.ImageUrls);
return Ok(new { report.Id, report.Title, report.Status });
}
[HttpPost("{id:guid}/interpret")]
[Authorize(Roles = "doctor")]
public async Task<IActionResult> InterpretReport(Guid id, [FromBody] ReportInterpretRequest request)
{
var items = request.Items.Select(i => (i.ItemName, i.ResultValue, i.Unit, i.ReferenceRange, i.IsAbnormal)).ToList();
var report = await reportService.InterpretAsync(id, UserId, request.Summary, items, request.RiskLevel, request.Suggestions);
return Ok(new { report.Id, report.Status, report.RiskLevel });
}
}
public record ReportUploadRequest(string Title, string Category, List<string> ImageUrls);
public record ReportInterpretRequest(
string Summary, List<ReportItemRequest> Items, string RiskLevel, string? Suggestions);
public record ReportItemRequest(
string ItemName, string ResultValue, string? Unit, string? ReferenceRange, bool IsAbnormal);