using MiAssessment.Core.Interfaces;
using MiAssessment.Model.Data;
using MiAssessment.Model.Models.Report;
using Microsoft.EntityFrameworkCore;
namespace MiAssessment.Api.Pages.Report;
///
/// 学习的关键能力分析页 PageModel
/// 路由:/report/learning-abilities?recordId=3
/// CategoryType=5 的5种能力:学习专注力、学习思考力、学习转化力、学习记忆力、学习动机力
///
public class LearningAbilitiesModel : ReportPageModelBase
{
///
/// 能力列表(按 CategoryId 排序,雷达图用)
///
public List Items { get; set; } = new();
///
/// 按分数降序排列(柱状图用)
///
public List ItemsByScore { get; set; } = new();
///
/// 最弱能力名称
///
public string WeakestName { get; set; } = "";
///
/// 结论内容
///
public string ConclusionContent { get; set; } = "";
private readonly MiAssessmentDbContext _dbContext;
public LearningAbilitiesModel(IReportDataService reportDataService, MiAssessmentDbContext dbContext)
: base(reportDataService)
{
_dbContext = dbContext;
}
protected override async Task OnDataLoadedAsync()
{
if (ReportData?.ResultsByType == null ||
!ReportData.ResultsByType.TryGetValue(5, out var allItems))
{
ErrorMessage = "缺少学习关键能力数据";
return;
}
Items = allItems.OrderBy(x => x.CategoryId).ToList();
if (Items.Count == 0)
{
ErrorMessage = "未找到学习关键能力数据";
return;
}
ItemsByScore = Items.OrderByDescending(x => x.Score).ToList();
// 加载最弱能力结论
var weakest = ItemsByScore.Last();
WeakestName = weakest.CategoryName;
if (ReportData.ConclusionsByCategory.TryGetValue(weakest.CategoryId, out var wc) && wc.Content != null)
{
ConclusionContent = wc.Content;
}
else
{
var template = await _dbContext.ReportConclusions
.AsNoTracking()
.FirstOrDefaultAsync(t =>
t.CategoryId == weakest.CategoryId &&
t.ConclusionType == 4 &&
!t.IsDeleted);
ConclusionContent = template?.Content ?? "";
}
}
}