96 lines
3.1 KiB
C#
96 lines
3.1 KiB
C#
using HealthManager.Domain.Entities;
|
|
using HealthManager.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Notification = HealthManager.Domain.Entities.Notification;
|
|
|
|
namespace HealthManager.Application.Services;
|
|
|
|
public class ReportService(AppDbContext db)
|
|
{
|
|
public async Task<List<Report>> GetPatientReportsAsync(Guid patientId)
|
|
=> await db.Reports
|
|
.Include(r => r.Doctor)
|
|
.Include(r => r.Items)
|
|
.Where(r => r.PatientId == patientId)
|
|
.OrderByDescending(r => r.UploadedAt)
|
|
.ToListAsync();
|
|
|
|
public async Task<List<Report>> GetAllReportsAsync()
|
|
=> await db.Reports
|
|
.Include(r => r.Patient)
|
|
.Include(r => r.Doctor)
|
|
.Include(r => r.Items)
|
|
.OrderByDescending(r => r.UploadedAt)
|
|
.ToListAsync();
|
|
|
|
public async Task<Report?> GetByIdAsync(Guid id)
|
|
=> await db.Reports
|
|
.Include(r => r.Patient)
|
|
.Include(r => r.Doctor)
|
|
.Include(r => r.Items)
|
|
.FirstOrDefaultAsync(r => r.Id == id);
|
|
|
|
public async Task<Report> UploadAsync(Guid patientId, string title, string category, List<string> imageUrls)
|
|
{
|
|
var report = new Report
|
|
{
|
|
PatientId = patientId,
|
|
Title = title,
|
|
Category = category,
|
|
ImageUrls = imageUrls,
|
|
UploadedBy = patientId,
|
|
};
|
|
db.Reports.Add(report);
|
|
await db.SaveChangesAsync();
|
|
return report;
|
|
}
|
|
|
|
public async Task<List<Report>> GetPendingAsync()
|
|
=> await db.Reports
|
|
.Include(r => r.Patient)
|
|
.Where(r => r.Status == "pending")
|
|
.OrderByDescending(r => r.UploadedAt)
|
|
.ToListAsync();
|
|
|
|
public async Task<Report> InterpretAsync(Guid reportId, Guid doctorId, string summary,
|
|
List<(string name, string value, string? unit, string? range, bool abnormal)> items,
|
|
string riskLevel, string? suggestions)
|
|
{
|
|
var report = await db.Reports.FindAsync(reportId) ?? throw new InvalidOperationException("Report not found");
|
|
report.DoctorId = doctorId;
|
|
report.Status = "completed";
|
|
report.Summary = summary;
|
|
report.RiskLevel = riskLevel;
|
|
report.Suggestions = suggestions;
|
|
report.CompletedAt = DateTime.UtcNow;
|
|
|
|
var order = 0;
|
|
foreach (var (name, value, unit, range, abnormal) in items)
|
|
{
|
|
db.ReportItems.Add(new ReportItem
|
|
{
|
|
ReportId = reportId,
|
|
ItemName = name,
|
|
ResultValue = value,
|
|
Unit = unit,
|
|
ReferenceRange = range,
|
|
IsAbnormal = abnormal,
|
|
SortOrder = order++,
|
|
});
|
|
}
|
|
|
|
// Notify patient
|
|
db.Notifications.Add(new Notification
|
|
{
|
|
UserId = report.PatientId,
|
|
Type = "report",
|
|
Title = "报告已解读",
|
|
Content = $"您的报告「{report.Title}」已有解读结果",
|
|
RelatedId = reportId,
|
|
});
|
|
|
|
await db.SaveChangesAsync();
|
|
return report;
|
|
}
|
|
}
|