修复错误的组别判断

This commit is contained in:
马梓彭 2026-11-02 15:16:42 +08:00
parent 74311db71a
commit 9f93b742ef
3 changed files with 39 additions and 115 deletions

View File

@ -3,6 +3,7 @@ using YD_Event.Application.Project.Dtos;
using YD_Event.Core.Entities;
using YD_Event.Core.Enum;
using Easy.Core;
using EasyCaching.Core;
using SqlSugar;
using Mapster;
@ -31,6 +32,7 @@ public class EventService : BaseService<EventInfo>, ITransient
private readonly YD_Event.Application.Menu.SysMenuService _sysMenuService;
private readonly YD_Event.Application.Auth.AuthManager _authManager;
private readonly EventRuleSnapshotService _ruleSnapshotService;
private readonly IEasyCachingProvider _easyCachingProvider;
public EventService(
ISqlSugarRepository<EventInfo> repository,
@ -50,7 +52,8 @@ public class EventService : BaseService<EventInfo>, ITransient
IIdGenerator idGenerator,
YD_Event.Application.Menu.SysMenuService sysMenuService,
YD_Event.Application.Auth.AuthManager authManager,
EventRuleSnapshotService ruleSnapshotService) : base(repository)
EventRuleSnapshotService ruleSnapshotService,
IEasyCachingProvider easyCachingProvider) : base(repository)
{
_repository = repository;
_unitRepository = unitRepository;
@ -70,6 +73,20 @@ public class EventService : BaseService<EventInfo>, ITransient
_sysMenuService = sysMenuService;
_authManager = authManager;
_ruleSnapshotService = ruleSnapshotService;
_easyCachingProvider = easyCachingProvider;
}
/// <summary>
/// 清除赛事限报与项目相关缓存,确保后台修改后前台立即生效
/// </summary>
private async Task ClearEventRegistrationCacheAsync(long eventId)
{
await _easyCachingProvider.RemoveAsync($"EventLimit:{eventId}");
await _easyCachingProvider.RemoveAsync($"EventProjectLimit:{eventId}");
await _easyCachingProvider.RemoveAsync($"EventProjects:{eventId}");
await _easyCachingProvider.RemoveAsync($"EventProjectGroups:{eventId}");
await _easyCachingProvider.RemoveAsync($"BaseProjects:{eventId}");
await _easyCachingProvider.RemoveAsync($"EventConfig:{eventId}");
}
/// <summary>
@ -835,6 +852,9 @@ public class EventService : BaseService<EventInfo>, ITransient
await _registrationLimitRepository.InsertAsync(limit);
}
// 清除赛事限报与项目相关缓存
await ClearEventRegistrationCacheAsync(eventInfo.Id);
// 5. 更新费用配置
if (input.FeeConfig != null)
{
@ -1478,6 +1498,9 @@ public class EventService : BaseService<EventInfo>, ITransient
eventInfo.ProcessStatus = ProcessStatus.ProjectConfigCompleted;
await _repository.UpdateAsync(eventInfo);
}
// 清除赛事限报与项目相关缓存
await ClearEventRegistrationCacheAsync(input.EventId);
}
/// <summary>

View File

@ -181,8 +181,11 @@ public class UnifiedRegistrationService : ITransient
/// </summary>
private async Task<EventRegistration> GetOrCreateRegistrationAsync(UnifiedRegistrationInput input)
{
var existingRegistration = await _registrationRepository.GetFirstAsync(
r => r.EventId == input.EventId && r.CreatedUserId == input.CreatedUserId && r.PaymentStatus != 4 && !r.DeleteMark);
var existingRegistration = await _registrationRepository.GetFirstAsync(r =>
r.EventId == input.EventId &&
r.CreatedUserId == input.CreatedUserId &&
r.PaymentStatus != 4 &&
!r.DeleteMark);
if (existingRegistration != null)
{

View File

@ -1479,11 +1479,15 @@ public class UserEventRegistrationService : IDynamicApiController, ITransient
// 获取所有赛事项目,用于关联 ProjectId
var eventProjects = await _eventProjectRepository.GetListAsync(x => x.EventId == eventId);
// 优化:一次查询统计所有组别的已报名队伍数,替代循环查询
// 注意:与原逻辑保持一致,统计的是 registrationItem 的数量(不去重 RegistrationId)
// 统计已报名队伍数
var registrationItems = await _registrationItemRepository.AsQueryable()
.Where(x => x.EventProjectGroupId.HasValue && !x.DeleteMark)
.InnerJoin<EventRegistration>((i, r) => i.RegistrationId == r.Id && !r.DeleteMark && r.EventId == eventId)
.InnerJoin<EventRegistration>((i, r) =>
i.RegistrationId == r.Id &&
!r.DeleteMark &&
r.EventId == eventId &&
((r.PaymentMethod == 2 && r.PaymentStatus == 1) ||
(r.PaymentMethod == 1 && (r.PaymentStatus == 1 || r.PaymentStatus == 0))))
.Select((i, r) => new
{
GroupId = i.EventProjectGroupId
@ -1504,7 +1508,7 @@ public class UserEventRegistrationService : IDynamicApiController, ITransient
// 查找该组别的项目限报配置
var eventProject = eventProjects.FirstOrDefault(ep => ep.Id == eventProjectGroup.EventProjectId);
var projectLimit = projectLimits.FirstOrDefault(pl => pl.ProjectId == eventProject?.Id);
var maxTeams = projectLimit?.LimitCount ?? 2; // 默认2队
var maxTeams = projectLimit?.LimitCount ?? 2;
result.Add(new GroupTeamLimitOutput
{
@ -1801,8 +1805,7 @@ public class UserEventRegistrationService : IDynamicApiController, ITransient
CanSelect = true
};
// 获取组别信息
var group = await _eventProjectGroupRepository.GetByIdAsync(input.GroupId);
var group = await _eventProjectGroupRepository.GetFirstAsync(x => x.Id == input.GroupId && !x.DeleteMark);
if (group == null)
{
result.CanSelect = false;
@ -1810,8 +1813,7 @@ public class UserEventRegistrationService : IDynamicApiController, ITransient
return result;
}
// 获取项目信息
var eventProject = await _eventProjectRepository.GetByIdAsync(group.EventProjectId);
var eventProject = await _eventProjectRepository.GetFirstAsync(x => x.Id == group.EventProjectId && !x.DeleteMark);
if (eventProject == null)
{
result.CanSelect = false;
@ -1819,110 +1821,6 @@ public class UserEventRegistrationService : IDynamicApiController, ITransient
return result;
}
// 获取基础项目信息
var baseProject = await _projectRepository.GetByIdAsync(eventProject.ProjectId);
// 获取赛事信息获取报名限制
var eventInfo = await _eventRepository.GetByIdAsync(input.EventId);
var registrationLimit = eventInfo != null
? await _limitRepository.GetFirstAsync(x => x.EventId == input.EventId && !x.DeleteMark)
: null;
// 1. 检查组别自身限制(这里暂不实现,因为 EventProjectGroup 实体没有 GroupLimitCount 字段)
// 后续可以根据实际需求添加相关的限制配置表
// 2. 检查项目限制
var selectedGroupIds = input.SelectedGroupIds ?? new List<long>();
var otherSelectedGroups = await _eventProjectGroupRepository.GetListAsync(x => selectedGroupIds.Contains(x.Id));
var projectGroups = otherSelectedGroups.Where(g => g.EventProjectId == eventProject.Id).ToList();
// 计算该项目已选的组别数量(排除当前组别)
var selectedCountForProject = projectGroups.Count;
// 获取该项目的限报配置
var projectLimit = await _projectLimitRepository.GetFirstAsync(x =>
x.EventId == input.EventId && x.ProjectId == eventProject.ProjectId && !x.DeleteMark);
if (projectLimit != null)
{
result.ProjectLimit = new ProjectLimitDetail
{
ProjectLimitCount = projectLimit.LimitCount,
CurrentSelectedCount = selectedCountForProject,
IsLimitReached = selectedCountForProject >= projectLimit.LimitCount
};
if (result.ProjectLimit.IsLimitReached && !selectedGroupIds.Contains(input.GroupId))
{
result.CanSelect = false;
result.LimitMessage = $"该项目最多可报名{projectLimit.LimitCount}个组别";
return result;
}
}
// 3. 检查团队/个人项目限制
if (registrationLimit != null && baseProject != null)
{
var isTeamProject = baseProject.IsTeamProject();
var isIndividualProject = baseProject.IsIndividualProject();
// 计算已选的团队项目和个人项目数量
var allSelectedEventProjectIds = otherSelectedGroups
.Where(g => g.Id != input.GroupId)
.Select(g => g.EventProjectId)
.Distinct()
.ToList();
var allSelectedEventProjects = await _eventProjectRepository.GetListAsync(x => allSelectedEventProjectIds.Contains(x.Id));
var allSelectedProjectIds = allSelectedEventProjects.Select(ep => ep.ProjectId).Distinct().ToList();
var allSelectedProjects = await _projectRepository.GetListAsync(x => allSelectedProjectIds.Contains(x.Id));
var selectedTeamCount = 0;
var selectedIndividualCount = 0;
foreach (var selectedProject in allSelectedProjects)
{
if (selectedProject.IsTeamProject()) selectedTeamCount++;
if (selectedProject.IsIndividualProject()) selectedIndividualCount++;
}
// 团队项目限制
if (isTeamProject)
{
result.TeamProjectLimit = new EventLimitDetail
{
LimitCount = registrationLimit.TeamProjectLimit,
CurrentSelectedCount = selectedTeamCount,
IsLimitReached = selectedTeamCount >= registrationLimit.TeamProjectLimit
};
if (result.TeamProjectLimit.IsLimitReached && !selectedGroupIds.Contains(input.GroupId))
{
result.CanSelect = false;
result.LimitMessage = $"团队项目最多可报名{registrationLimit.TeamProjectLimit}个";
return result;
}
}
// 个人项目限制
if (isIndividualProject)
{
result.IndividualProjectLimit = new EventLimitDetail
{
LimitCount = registrationLimit.IndividualProjectLimit,
CurrentSelectedCount = selectedIndividualCount,
IsLimitReached = selectedIndividualCount >= registrationLimit.IndividualProjectLimit
};
if (result.IndividualProjectLimit.IsLimitReached && !selectedGroupIds.Contains(input.GroupId))
{
result.CanSelect = false;
result.LimitMessage = $"个人项目最多可报名{registrationLimit.IndividualProjectLimit}个";
return result;
}
}
}
return result;
}