2026-02-07 18:02:21 +08:00

170 lines
5.4 KiB
C#

using YD_Event.Application.Banner.Dtos;
using YD_Event.Core.Entities;
namespace YD_Event.Application.Banner;
/// <summary>
/// 轮播图服务
/// </summary>
public class BannerService : BaseService<YD_Event.Core.Entities.Banner>, ITransient
{
private readonly ISqlSugarRepository<YD_Event.Core.Entities.Banner> _repository;
private readonly ISqlSugarRepository<BannerConfig> _configRepository;
private readonly ISqlSugarRepository<EventInfo> _eventRepository;
private readonly IIdGenerator _idGenerator;
public BannerService(
ISqlSugarRepository<YD_Event.Core.Entities.Banner> repository,
ISqlSugarRepository<BannerConfig> configRepository,
ISqlSugarRepository<EventInfo> eventRepository,
IIdGenerator idGenerator) : base(repository)
{
_repository = repository;
_configRepository = configRepository;
_eventRepository = eventRepository;
_idGenerator = idGenerator;
}
/// <summary>
/// 获取轮播图配置模式
/// </summary>
/// <returns></returns>
public async Task<BannerConfigOutput> GetConfig()
{
var config = await _configRepository.GetFirstAsync(x => true);
return new BannerConfigOutput { Mode = config?.Mode ?? 0 };
}
/// <summary>
/// 更新轮播图配置模式
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task UpdateConfig(BannerConfigInput input)
{
var config = await _configRepository.GetFirstAsync(x => true);
if (config == null)
{
config = new BannerConfig { Id = _idGenerator.NextId(), Mode = input.Mode };
await _configRepository.InsertAsync(config);
}
else
{
config.Mode = input.Mode;
await _configRepository.UpdateAsync(config);
}
}
/// <summary>
/// 获取轮播图列表(前台展示用)
/// </summary>
/// <returns></returns>
public async Task<List<BannerOutput>> GetList()
{
var config = await GetConfig();
if (config.Mode == 1) // 自动配置
{
// 获取最新的4场赛事
var events = await _eventRepository.AsQueryable()
.Where(x => !x.DeleteMark) // 假设EventInfo有DeleteMark
.OrderByDescending(x => x.CreatedTime)
.Take(4)
.Select(x => new BannerOutput
{
Id = x.Id,
Title = x.EventName,
ImageUrl = x.CoverImage,
LinkType = 1, // 赛事
LinkTarget = x.Id.ToString(),
Sort = 0,
Status = Core.Enum.AvailabilityStatus.Enable,
CreatedTime = x.CreatedTime
})
.ToListAsync();
return events;
}
else // 手动配置
{
return await _repository.AsQueryable()
.Where(x => x.Status == Core.Enum.AvailabilityStatus.Enable)
.OrderBy(x => x.Sort)
.Select(x => new BannerOutput
{
Id = x.Id,
Title = x.Title,
ImageUrl = x.ImageUrl,
LinkType = x.LinkType,
LinkTarget = x.LinkTarget,
Sort = x.Sort,
Status = x.Status,
Remark = x.Remark,
CreatedTime = x.CreatedTime
})
.ToListAsync();
}
}
/// <summary>
/// 分页查询(后台管理用)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<PageResult<BannerOutput>> Page(BannerQueryInput input)
{
var query = _repository.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.Title), x => x.Title.Contains(input.Title))
.OrderBy(x => x.Sort)
.Select(x => new BannerOutput
{
Id = x.Id,
Title = x.Title,
ImageUrl = x.ImageUrl,
LinkType = x.LinkType,
LinkTarget = x.LinkTarget,
Sort = x.Sort,
Status = x.Status,
Remark = x.Remark,
CreatedTime = x.CreatedTime
});
return await query.ToPagedListAsync(input);
}
/// <summary>
/// 添加轮播图
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task Add(AddBannerInput input)
{
var entity = input.Adapt<YD_Event.Core.Entities.Banner>();
entity.Id = _idGenerator.NextId();
await _repository.InsertAsync(entity);
}
/// <summary>
/// 更新轮播图
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task Update(UpdateBannerInput input)
{
var entity = await _repository.GetByIdAsync(input.Id);
if (entity == null) throw new InvalidOperationException("轮播图不存在");
input.Adapt(entity);
await _repository.UpdateAsync(entity);
}
/// <summary>
/// 删除轮播图
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public async Task Delete(List<long> ids)
{
if (ids == null || !ids.Any()) return;
await _repository.DeleteAsync(x => ids.Contains(x.Id));
}
}