using System.Security.Claims; using HealthManager.Application.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace HealthManager.WebApi.Controllers; [ApiController] [Route("api/follow-ups")] [Authorize] public class FollowUpController(FollowUpService followUpService) : ControllerBase { private Guid UserId => Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); private string Role => User.FindFirstValue(ClaimTypes.Role)!; [HttpGet] public async Task GetFollowUps() { var followUps = Role == "doctor" ? await followUpService.GetDoctorFollowUpsAsync(UserId) : await followUpService.GetPatientFollowUpsAsync(UserId); return Ok(followUps.Select(f => new { f.Id, f.PatientId, f.DoctorId, f.Title, f.Description, f.ScheduledAt, f.Status, f.Notes, f.ReminderEnabled, f.CreatedAt, PatientName = f.Patient?.Name, DoctorName = f.Doctor?.Name, })); } [HttpGet("{id:guid}")] public async Task GetFollowUp(Guid id) { var followUp = await followUpService.GetByIdAsync(id); if (followUp == null) return NotFound(new { message = "复查不存在" }); return Ok(new { followUp.Id, followUp.PatientId, followUp.DoctorId, followUp.Title, followUp.Description, followUp.ScheduledAt, followUp.Status, followUp.Notes, followUp.ReminderEnabled, followUp.CreatedAt, }); } [HttpPost] public async Task AddFollowUp([FromBody] FollowUpCreateRequest request) { var patientId = UserId; Guid? doctorId = null; if (Role == "doctor" && request.PatientId.HasValue) { patientId = request.PatientId.Value; doctorId = UserId; } var followUp = await followUpService.AddAsync(patientId, request.Title, request.Description, request.ScheduledAt, request.ReminderEnabled, doctorId); return Ok(new { followUp.Id, followUp.Title, followUp.Status }); } [HttpPut("{id:guid}")] [Authorize(Roles = "doctor")] public async Task UpdateFollowUp(Guid id, [FromBody] FollowUpUpdateRequest request) { var followUp = await followUpService.UpdateAsync(id, UserId, request.Title, request.Description, request.ScheduledAt, request.Status, request.Notes); if (followUp == null) return NotFound(new { message = "复查不存在" }); return Ok(new { followUp.Id, followUp.Title, followUp.Status }); } } public class FollowUpCreateRequest { public string Title { get; set; } = string.Empty; public string? Description { get; set; } public DateTime ScheduledAt { get; set; } public bool ReminderEnabled { get; set; } = true; public Guid? PatientId { get; set; } } public class FollowUpUpdateRequest { public string? Title { get; set; } public string? Description { get; set; } public DateTime? ScheduledAt { get; set; } public string? Status { get; set; } public string? Notes { get; set; } }