using YD_Event.Application.Project.Dtos; using YD_Event.Core.Entities; namespace YD_Event.Application.Project; /// /// 项目管理 /// public class ProjectService : BaseService, ITransient { private readonly ISqlSugarRepository _repository; private readonly ISqlSugarRepository _groupRepository; private readonly ISqlSugarRepository _typeRepository; private readonly IIdGenerator _idGenerator; public ProjectService( ISqlSugarRepository repository, ISqlSugarRepository groupRepository, ISqlSugarRepository typeRepository, IIdGenerator idGenerator) : base(repository) { _repository = repository; _groupRepository = groupRepository; _typeRepository = typeRepository; _idGenerator = idGenerator; } /// /// 项目分页查询 /// [DisplayName("项目分页查询")] [HttpGet("page")] public async Task> Page([FromQuery] ProjectQueryInput input) { return await _repository.AsQueryable() .LeftJoin((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); } /// /// 添加项目 /// [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(); 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(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>(); 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 p = await _repository.GetByIdAsync(id); if (p == null) throw Oops.Bah("项目不存在"); var dto = p.Adapt(); 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; } }