- 新建 home_navigations 表,独立管理首页卡片导航 - 回退 assessment_types 表的 LinkUrl 字段 - 后台管理:ContentController 新增导航 CRUD 接口 - 小程序 API:HomeController 新增 getNavigationList 接口 - 前端:首页改用 navigationList 数据源,支持配置化跳转 - 数据库已插入3条导航记录(多元测评/学业规划/学科测评)
122 lines
3.6 KiB
C#
122 lines
3.6 KiB
C#
using MiAssessment.Core.Interfaces;
|
|
using MiAssessment.Model.Data;
|
|
using MiAssessment.Model.Models.Home;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace MiAssessment.Core.Services;
|
|
|
|
/// <summary>
|
|
/// 首页服务实现
|
|
/// </summary>
|
|
public class HomeService : IHomeService
|
|
{
|
|
private readonly MiAssessmentDbContext _dbContext;
|
|
private readonly ILogger<HomeService> _logger;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="dbContext">数据库上下文</param>
|
|
/// <param name="logger">日志记录器</param>
|
|
public HomeService(
|
|
MiAssessmentDbContext dbContext,
|
|
ILogger<HomeService> logger)
|
|
{
|
|
_dbContext = dbContext;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<BannerDto>> GetBannerListAsync()
|
|
{
|
|
_logger.LogDebug("获取Banner列表");
|
|
|
|
var banners = await _dbContext.Banners
|
|
.AsNoTracking()
|
|
.Where(b => b.Status == 1 && !b.IsDeleted)
|
|
.OrderByDescending(b => b.Sort)
|
|
.Select(b => new BannerDto
|
|
{
|
|
Id = b.Id,
|
|
Title = b.Title,
|
|
ImageUrl = b.ImageUrl,
|
|
LinkType = b.LinkType,
|
|
LinkUrl = b.LinkUrl,
|
|
AppId = b.AppId
|
|
})
|
|
.ToListAsync();
|
|
|
|
_logger.LogDebug("获取到 {Count} 条Banner记录", banners.Count);
|
|
return banners;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<AssessmentTypeDto>> GetAssessmentListAsync()
|
|
{
|
|
_logger.LogDebug("获取测评类型列表");
|
|
|
|
var assessmentTypes = await _dbContext.AssessmentTypes
|
|
.AsNoTracking()
|
|
.Where(a => !a.IsDeleted)
|
|
.OrderByDescending(a => a.Sort)
|
|
.Select(a => new AssessmentTypeDto
|
|
{
|
|
Id = a.Id,
|
|
Name = a.Name,
|
|
Code = a.Code,
|
|
ImageUrl = a.ImageUrl ?? string.Empty,
|
|
Price = a.Price,
|
|
Status = a.Status
|
|
})
|
|
.ToListAsync();
|
|
|
|
_logger.LogDebug("获取到 {Count} 条测评类型记录", assessmentTypes.Count);
|
|
return assessmentTypes;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<HomeNavigationDto>> GetNavigationListAsync()
|
|
{
|
|
_logger.LogDebug("获取首页导航入口列表");
|
|
|
|
var navigations = await _dbContext.HomeNavigations
|
|
.AsNoTracking()
|
|
.Where(n => n.Status == 1 && !n.IsDeleted)
|
|
.OrderByDescending(n => n.Sort)
|
|
.Select(n => new HomeNavigationDto
|
|
{
|
|
Id = n.Id,
|
|
Name = n.Name,
|
|
ImageUrl = n.ImageUrl ?? string.Empty,
|
|
LinkUrl = n.LinkUrl,
|
|
Status = n.Status
|
|
})
|
|
.ToListAsync();
|
|
|
|
_logger.LogDebug("获取到 {Count} 条导航入口记录", navigations.Count);
|
|
return navigations;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<PromotionDto>> GetPromotionListAsync()
|
|
{
|
|
_logger.LogDebug("获取首页宣传图列表");
|
|
|
|
var promotions = await _dbContext.Promotions
|
|
.AsNoTracking()
|
|
.Where(p => p.Position == 1 && p.Status == 1 && !p.IsDeleted)
|
|
.OrderByDescending(p => p.Sort)
|
|
.Select(p => new PromotionDto
|
|
{
|
|
Id = p.Id,
|
|
Title = p.Title,
|
|
ImageUrl = p.ImageUrl
|
|
})
|
|
.ToListAsync();
|
|
|
|
_logger.LogDebug("获取到 {Count} 条宣传图记录", promotions.Count);
|
|
return promotions;
|
|
}
|
|
}
|