YD_Event.Admin/YD_Event.Application/Event/EventProjectService.cs

119 lines
3.8 KiB
C#
Raw Normal View History

using YD_Event.Application.Event.Dtos;
using YD_Event.Core.Entities;
namespace YD_Event.Application.Event;
/// <summary>
/// 赛事项目管理
/// </summary>
public class EventProjectService : BaseService<EventProject>, ITransient
{
private readonly ISqlSugarRepository<EventProject> _repository;
private readonly ISqlSugarRepository<EventProjectGroup> _groupRepository;
private readonly IIdGenerator _idGenerator;
public EventProjectService(ISqlSugarRepository<EventProject> repository,
ISqlSugarRepository<EventProjectGroup> groupRepository,
IIdGenerator idGenerator) : base(repository)
{
_repository = repository;
_groupRepository = groupRepository;
_idGenerator = idGenerator;
}
/// <summary>
/// 赛事项目分页查询
/// </summary>
[DisplayName("赛事项目分页查询")]
[HttpGet]
public async Task<PageResult<EventProject>> 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);
}
/// <summary>
/// 添加赛事项目
/// </summary>
[DisplayName("添加赛事项目")]
[HttpPost("add")]
[UnitOfWork]
public async Task Add(AddEventProjectInput input)
{
var entity = input.Adapt<EventProject>();
entity.Id = _idGenerator.NextId();
await _repository.InsertAsync(entity);
if (input.Groups != null && input.Groups.Any())
{
var groups = input.Groups.Adapt<List<EventProjectGroup>>();
groups.ForEach(x =>
{
x.ProjectId = entity.Id;
x.Id = _idGenerator.NextId();
});
await _groupRepository.InsertRangeAsync(groups);
}
}
/// <summary>
/// 更新赛事项目
/// </summary>
[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<List<EventProjectGroup>>();
groups.ForEach(x =>
{
x.ProjectId = entity.Id;
x.Id = _idGenerator.NextId();
});
await _groupRepository.InsertRangeAsync(groups);
}
}
/// <summary>
/// 删除赛事项目
/// </summary>
[DisplayName("删除赛事项目")]
[HttpPost("delete")]
[UnitOfWork]
public async Task Delete([FromBody] List<long> ids)
{
await _repository.DeleteAsync(x => ids.Contains(x.Id));
await _groupRepository.DeleteAsync(x => ids.Contains(x.ProjectId));
}
/// <summary>
/// 获取赛事项目详情
/// </summary>
[DisplayName("获取赛事项目详情")]
[HttpGet("detail")]
public async Task<EventProject> 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;
}
}