2026-03-01 22:32:44 +08:00

173 lines
5.9 KiB
C#

using YD_Event.Application.Project.Dtos;
using YD_Event.Core.Entities;
namespace YD_Event.Application.Project;
/// <summary>
/// 项目管理
/// </summary>
public class ProjectService : BaseService<YD_Event.Core.Entities.Project>, ITransient
{
private readonly ISqlSugarRepository<YD_Event.Core.Entities.Project> _repository;
private readonly ISqlSugarRepository<ProjectGroup> _groupRepository;
private readonly ISqlSugarRepository<ProjectType> _typeRepository;
private readonly IIdGenerator _idGenerator;
public ProjectService(
ISqlSugarRepository<YD_Event.Core.Entities.Project> repository,
ISqlSugarRepository<ProjectGroup> groupRepository,
ISqlSugarRepository<ProjectType> typeRepository,
IIdGenerator idGenerator) : base(repository)
{
_repository = repository;
_groupRepository = groupRepository;
_typeRepository = typeRepository;
_idGenerator = idGenerator;
}
/// <summary>
/// 项目分页查询
/// </summary>
[DisplayName("项目分页查询")]
[HttpGet("page")]
public async Task<PageResult<ProjectDto>> Page([FromQuery] ProjectQueryInput input)
{
return await _repository.AsQueryable()
.LeftJoin<ProjectType>((p, pt) => p.ProjectTypeId == pt.Id)
.WhereIF(input.EventId.HasValue, (p, pt) => p.EventId == input.EventId)
.WhereIF(!string.IsNullOrWhiteSpace(input.ProjectName), (p, pt) => p.Name.Contains(input.ProjectName))
.WhereIF(input.ProjectTypeId.HasValue, (p, pt) => p.ProjectTypeId == input.ProjectTypeId || pt.ParentId == input.ProjectTypeId)
.OrderByDescending((p, pt) => p.Id)
.Select((p, pt) => new ProjectDto
{
Id = p.Id,
EventId = p.EventId,
Name = p.Name,
ProjectTypeId = p.ProjectTypeId,
ProjectTypeName = pt.Name,
CompletionCount = p.CompletionCount,
Fee = p.Fee,
DurationMinutes = p.DurationMinutes,
DurationSeconds = p.DurationSeconds,
VenueCount = p.VenueCount,
SortIndex = p.SortIndex,
Status = p.Status,
DeleteMark = p.DeleteMark,
CreatedUserId = p.CreatedUserId,
CreatedTime = p.CreatedTime,
UpdatedUserId = p.UpdatedUserId,
UpdatedTime = p.UpdatedTime
})
.ToPagedListAsync(input);
}
/// <summary>
/// 添加项目
/// </summary>
[DisplayName("添加项目")]
[HttpPost("add")]
[UnitOfWork]
public async Task Add(AddProjectInput input)
{
// 检查项目名称在同一赛事中是否重复
var isExist = await _repository.IsAnyAsync(x => x.EventId == input.EventId && x.Name == input.Name);
if (isExist) throw Oops.Bah("该项目名称在当前赛事中已存在");
var entity = input.Adapt<YD_Event.Core.Entities.Project>();
entity.Id = _idGenerator.NextId();
await _repository.InsertAsync(entity);
if (input.Groups != null && input.Groups.Any())
{
var groups = input.Groups.Adapt<List<ProjectGroup>>();
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(UpdateProjectInput input)
{
var entity = await _repository.GetByIdAsync(input.Id);
if (entity == null) throw Oops.Bah("项目不存在");
// 如果修改了名称,检查是否重复
if (entity.Name != input.Name)
{
var isExist = await _repository.IsAnyAsync(x => x.EventId == input.EventId && x.Name == input.Name && x.Id != input.Id);
if (isExist) 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<ProjectGroup>>();
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<ProjectDto> Detail([FromQuery] long id)
{
var p = await _repository.GetByIdAsync(id);
if (p == null) throw Oops.Bah("项目不存在");
var dto = p.Adapt<ProjectDto>();
var type = await _typeRepository.GetByIdAsync(p.ProjectTypeId);
if (type != null)
{
dto.ProjectTypeName = type.Name;
}
dto.Groups = await _groupRepository.AsQueryable()
.Where(x => x.ProjectId == id)
.Select(x => new ProjectGroupDto
{
Id = x.Id,
GroupName = x.GroupName,
BirthDateStart = x.BirthDateStart,
BirthDateEnd = x.BirthDateEnd,
Gender = x.Gender
})
.ToListAsync();
return dto;
}
}