using YD_Event.Application.Event.Dtos;
using YD_Event.Core.Entities;
namespace YD_Event.Application.Event;
///
/// 赛事项目管理
///
public class EventProjectService : BaseService, ITransient
{
private readonly ISqlSugarRepository _repository;
private readonly ISqlSugarRepository _groupRepository;
private readonly IIdGenerator _idGenerator;
public EventProjectService(ISqlSugarRepository repository,
ISqlSugarRepository groupRepository,
IIdGenerator idGenerator) : base(repository)
{
_repository = repository;
_groupRepository = groupRepository;
_idGenerator = idGenerator;
}
///
/// 赛事项目分页查询
///
[DisplayName("赛事项目分页查询")]
[HttpGet]
public async Task> Page([FromQuery] EventProjectQueryInput input)
{
return await _repository.AsQueryable()
.WhereIF(input.EventId.HasValue, x => x.EventId == input.EventId)
.WhereIF(!string.IsNullOrWhiteSpace(input.ProjectName), x => x.ProjectName.Contains(input.ProjectName))
.WhereIF(input.ProjectMainType.HasValue, x => x.ProjectMainType == input.ProjectMainType)
.OrderByDescending(x => x.Id)
.ToPagedListAsync(input);
}
///
/// 添加赛事项目
///
[DisplayName("添加赛事项目")]
[HttpPost("add")]
[UnitOfWork]
public async Task Add(AddEventProjectInput input)
{
var entity = input.Adapt();
entity.Id = _idGenerator.NextId();
await _repository.InsertAsync(entity);
if (input.Groups != null && input.Groups.Any())
{
var groups = input.Groups.Adapt>();
groups.ForEach(x =>
{
x.ProjectId = entity.Id;
x.Id = _idGenerator.NextId();
});
await _groupRepository.InsertRangeAsync(groups);
}
}
///
/// 更新赛事项目
///
[DisplayName("更新赛事项目")]
[HttpPut("edit")]
[UnitOfWork]
public async Task Update(UpdateEventProjectInput input)
{
var entity = await _repository.GetByIdAsync(input.Id);
if (entity == null) throw Oops.Bah("项目不存在");
input.Adapt(entity);
await _repository.UpdateAsync(entity);
// Update groups: Delete old, Insert new
await _groupRepository.DeleteAsync(x => x.ProjectId == entity.Id);
if (input.Groups != null && input.Groups.Any())
{
var groups = input.Groups.Adapt>();
groups.ForEach(x =>
{
x.ProjectId = entity.Id;
x.Id = _idGenerator.NextId();
});
await _groupRepository.InsertRangeAsync(groups);
}
}
///
/// 删除赛事项目
///
[DisplayName("删除赛事项目")]
[HttpPost("delete")]
[UnitOfWork]
public async Task Delete([FromBody] List ids)
{
await _repository.DeleteAsync(x => ids.Contains(x.Id));
await _groupRepository.DeleteAsync(x => ids.Contains(x.ProjectId));
}
///
/// 获取赛事项目详情
///
[DisplayName("获取赛事项目详情")]
[HttpGet("detail")]
public async Task Detail([FromQuery] long id)
{
var entity = await _repository.GetByIdAsync(id);
if (entity == null) throw Oops.Bah("项目不存在");
entity.Groups = await _groupRepository.AsQueryable().Where(x => x.ProjectId == id).ToListAsync();
return entity;
}
}