using YD_Event.Application.Project.Dtos; using YD_Event.Core.Entities; using Mapster; using SqlSugar; 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("项目分页查询")] public async Task> Page(ProjectQueryInput input) { var query = _repository.AsQueryable() .LeftJoin((p, pt) => p.ProjectTypeId == pt.Id) .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, 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; } /// /// 添加项目 /// [DisplayName("添加项目")] [UnitOfWork] public async Task Add(AddProjectInput input) { // 检查项目名称是否重复 var isExist = await _repository.IsAnyAsync(x => x.Name == input.Name); if (isExist) throw new InvalidOperationException("该项目名称已存在"); var entity = input.Adapt(); 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(); group.Id = _idGenerator.NewLong(); group.ProjectId = entity.Id; return group; }).ToList(); await _groupRepository.InsertRangeAsync(groups); } } /// /// 更新项目 /// [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.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(); group.Id = _idGenerator.NewLong(); group.ProjectId = entity.Id; return group; }).ToList(); await _groupRepository.InsertRangeAsync(groups); } } /// /// 删除项目 /// [DisplayName("删除项目")] [UnitOfWork] public async Task Delete(List ids) { await _repository.DeleteAsync(x => ids.Contains(x.Id)); await _groupRepository.DeleteAsync(x => ids.Contains(x.ProjectId)); } /// /// 获取项目详情 /// [DisplayName("获取项目详情")] public async Task Detail(long id) { var p = await _repository.GetByIdAsync(id); if (p == null) throw new InvalidOperationException("项目不存在"); var dto = p.Adapt(); 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>(); return dto; } }