170 lines
5.8 KiB
C#

using YD_Event.Application.Project.Dtos;
using YD_Event.Core.Entities;
using Mapster;
using SqlSugar;
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("项目分页查询")]
public async Task<PageResult<ProjectDto>> Page(ProjectQueryInput input)
{
var query = _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)
.OrderByDescending((p, pt) => p.Id);
var result = await query.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);
return result;
}
/// <summary>
/// 添加项目
/// </summary>
[DisplayName("添加项目")]
[UnitOfWork]
public async Task Add(AddProjectInput input)
{
// 检查项目名称在同一赛事中是否重复
var isExist = await _repository.IsAnyAsync(x => x.EventId == input.EventId && x.Name == input.Name);
if (isExist) throw new InvalidOperationException("该项目名称在当前赛事中已存在");
var entity = input.Adapt<YD_Event.Core.Entities.Project>();
entity.Id = _idGenerator.NewLong();
entity.CreatedTime = DateTime.Now;
entity.Status = Core.Enum.AvailabilityStatus.Enable;
await _repository.InsertAsync(entity);
if (input.Groups != null && input.Groups.Any())
{
var groups = input.Groups.Select(g =>
{
var group = g.Adapt<ProjectGroup>();
group.Id = _idGenerator.NewLong();
group.ProjectId = entity.Id;
return group;
}).ToList();
await _groupRepository.InsertRangeAsync(groups);
}
}
/// <summary>
/// 更新项目
/// </summary>
[DisplayName("更新项目")]
[UnitOfWork]
public async Task Update(UpdateProjectInput input)
{
var entity = await _repository.GetByIdAsync(input.Id);
if (entity == null) throw new InvalidOperationException("项目不存在");
// 如果修改了名称,检查是否重复
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 new InvalidOperationException("该项目名称在当前赛事中已存在");
}
input.Adapt(entity);
entity.UpdatedTime = DateTime.Now;
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.Select(g =>
{
var group = g.Adapt<ProjectGroup>();
group.Id = _idGenerator.NewLong();
group.ProjectId = entity.Id;
return group;
}).ToList();
await _groupRepository.InsertRangeAsync(groups);
}
}
/// <summary>
/// 删除项目
/// </summary>
[DisplayName("删除项目")]
[UnitOfWork]
public async Task Delete(List<long> ids)
{
await _repository.DeleteAsync(x => ids.Contains(x.Id));
await _groupRepository.DeleteAsync(x => ids.Contains(x.ProjectId));
}
/// <summary>
/// 获取项目详情
/// </summary>
[DisplayName("获取项目详情")]
public async Task<ProjectDto> Detail(long id)
{
var p = await _repository.GetByIdAsync(id);
if (p == null) throw new InvalidOperationException("项目不存在");
var dto = p.Adapt<ProjectDto>();
var type = await _typeRepository.GetByIdAsync(p.ProjectTypeId);
if (type != null)
{
dto.ProjectTypeName = type.Name;
}
var groups = await _groupRepository.GetListAsync(x => x.ProjectId == id);
dto.Groups = groups.Adapt<List<ProjectGroupDto>>();
return dto;
}
}