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 == 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(); } } /// /// 分页查询(后台管理用) /// /// /// 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, LinkTarget = x.LinkTarget, Sort = x.Sort, Status = x.Status, Remark = x.Remark, CreatedTime = x.CreatedTime }); return await query.ToPagedListAsync(input); } /// /// 添加轮播图 /// /// /// public async Task Add(AddBannerInput input) { 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("轮播图不存在"); input.Adapt(entity); await _repository.UpdateAsync(entity); } /// /// 删除轮播图 /// /// /// public async Task Delete(List ids) { if (ids == null || !ids.Any()) return; await _repository.DeleteAsync(x => ids.Contains(x.Id)); } }