using YD_Event.Application.Banner.Dtos; using YD_Event.Core.Entities; namespace YD_Event.Application.Banner; /// /// 轮播图服务 /// public class BannerService : BaseService, ITransient { private readonly ISqlSugarRepository _repository; private readonly ISqlSugarRepository _configRepository; private readonly ISqlSugarRepository _eventRepository; private readonly IIdGenerator _idGenerator; public BannerService( ISqlSugarRepository repository, ISqlSugarRepository configRepository, ISqlSugarRepository eventRepository, IIdGenerator idGenerator) : base(repository) { _repository = repository; _configRepository = configRepository; _eventRepository = eventRepository; _idGenerator = idGenerator; } /// /// 获取轮播图配置模式 /// /// public async Task GetConfig() { var config = await _configRepository.GetFirstAsync(x => true); return new BannerConfigOutput { Mode = config?.Mode ?? 0 }; } /// /// 更新轮播图配置模式 /// /// /// 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); } } /// /// 获取轮播图列表(前台展示用) /// /// public async Task> GetList() { var config = await GetConfig(); if (config.Mode == 0) // 自动配置 { // 获取最新的4场赛事 var events = await _eventRepository.AsQueryable() .Where(x => !x.DeleteMark && x.Status == AvailabilityStatus.Enable) // 仅查询启用且未删除的赛事 .OrderByDescending(x => x.CreatedTime) .Take(4) .Select(x => new BannerOutput { Id = x.Id, Title = x.EventName, ImageUrl = x.CoverImage, LinkType = LinkType.Event, // 赛事 EventId = x.Id, 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, LinkUrl = x.LinkUrl, EventId = x.EventId, Sort = x.Sort, Status = x.Status, Remark = x.Remark, CreatedTime = x.CreatedTime }) .ToListAsync(); } } /// /// 分页查询(后台管理用) /// /// /// public async Task> 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, LinkUrl = x.LinkUrl, EventId = x.EventId, Sort = x.Sort, Status = x.Status, Remark = x.Remark, CreatedTime = x.CreatedTime }); return await query.ToPagedListAsync(input); } /// /// 添加轮播图 /// /// /// public async Task Add(AddBannerInput input) { if (input.LinkType == LinkType.Event && input.EventId.HasValue) { var exists = await _eventRepository.IsAnyAsync(x => x.Id == input.EventId.Value && !x.DeleteMark); if (!exists) throw Oops.Bah("关联的赛事不存在"); } var entity = input.Adapt(); entity.Id = _idGenerator.NextId(); await _repository.InsertAsync(entity); } /// /// 更新轮播图 /// /// /// public async Task Update(UpdateBannerInput input) { var entity = await _repository.GetByIdAsync(input.Id); if (entity == null) throw new InvalidOperationException("轮播图不存在"); if (input.LinkType == LinkType.Event && input.EventId.HasValue) { var exists = await _eventRepository.IsAnyAsync(x => x.Id == input.EventId.Value && !x.DeleteMark); if (!exists) throw Oops.Bah("关联的赛事不存在"); } input.Adapt(entity); await _repository.UpdateAsync(entity); } /// /// 排序 /// /// /// [HttpPost("sort")] public async Task Sort(List ids) { var banners = await _repository.AsQueryable().Where(x => ids.Contains(x.Id)).ToListAsync(); foreach (var banner in banners) { var index = ids.IndexOf(banner.Id); if (index >= 0) { banner.Sort = index + 1; } } await _repository.UpdateRangeAsync(banners); } /// /// 删除轮播图 /// /// /// public async Task Delete(List ids) { if (ids == null || !ids.Any()) return; await _repository.SoftDeleteByIdsAsync(ids); } }