204 lines
6.6 KiB
C#
204 lines
6.6 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 && 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();
|
|
}
|
|
}
|
|
|
|
/// <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,
|
|
LinkUrl = x.LinkUrl,
|
|
EventId = x.EventId,
|
|
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)
|
|
{
|
|
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<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("轮播图不存在");
|
|
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 排序
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("sort")]
|
|
public async Task Sort(List<long> 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);
|
|
}
|
|
|
|
/// <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));
|
|
}
|
|
}
|