查询语句优化

This commit is contained in:
马梓彭 2026-05-08 09:25:07 +08:00
parent 465cd239da
commit d573fb73c4
4 changed files with 51 additions and 35 deletions

View File

@ -370,10 +370,10 @@ public class MyController : IDynamicApiController
// 先查询原始数据,然后在内存中转换证书名称
var query = _certificateRepository.AsQueryable()
.LeftJoin<EventInfo>((c, e) => c.EventId == e.Id)
.LeftJoin<ProjectEntity>((c, e, p) => c.ProjectId == p.Id)
.Where((c, e, p) => c.UserId == userId && c.CertificateStatus == 2 && !c.DeleteMark)
.Select((c, e, p) => new
{
.LeftJoin<EventProject>((c, e, ep) => c.ProjectId == ep.Id)
.LeftJoin<ProjectEntity>((c, e, ep, p) => ep.ProjectId == p.Id)
.Where((c, e, ep, p) => c.UserId == userId && c.CertificateStatus == 2 && !c.DeleteMark)
.Select((c, e, ep, p) => new {
Id = c.Id,
EventName = e.EventName,
ProjectName = p.Name,
@ -451,24 +451,25 @@ public class MyController : IDynamicApiController
var userId = GetCurrentUserId();
var query = _pointsRecordRepository.AsQueryable()
.LeftJoin<EventInfo>((p, e) => p.EventId == e.Id)
.LeftJoin<ProjectEntity>((p, e, pr) => p.ProjectId == pr.Id)
.LeftJoin<ProjectGroup>((p, e, pr, pg) => p.ProjectGroupId == pg.Id)
.LeftJoin<EventProject>((p, e, ep) => p.ProjectId == ep.Id)
.LeftJoin<ProjectEntity>((p, e, ep, pr) => ep.ProjectId == pr.Id)
.LeftJoin<EventProjectGroup>((p, e, ep, pr, pg) => p.ProjectGroupId == pg.Id)
// 小程序端只查看个人积分:UserId = 当前用户且 MerchantCertificationId = null
.Where((p, e, pr, pg) => p.UserId == userId && p.MerchantCertificationId == null && !p.DeleteMark);
.Where((p, e, ep, pr, pg) => p.UserId == userId && p.MerchantCertificationId == null && !p.DeleteMark);
if (input.Type.HasValue)
{
var typeFilter = (PointsOperationType)input.Type.Value;
query = query.Where((p, e, pr, pg) => p.Type == typeFilter);
query = query.Where((p, e, ep, pr, pg) => p.Type == typeFilter);
}
// 如果前端指定了 MerchantCertificationId,优先使用前端的(保持向后兼容)
if (input.MerchantCertificationId.HasValue)
{
query = query.Where((p, e, pr, pg) => p.MerchantCertificationId == input.MerchantCertificationId.Value);
query = query.Where((p, e, ep, pr, pg) => p.MerchantCertificationId == input.MerchantCertificationId.Value);
}
var resultQuery = query.Select((p, e, pr, pg) => new MyPointsRecordDto
var resultQuery = query.Select((p, e, ep, pr, pg) => new MyPointsRecordDto
{
Id = p.Id,
Title = p.Remark ?? (p.Type == PointsOperationType.Acquire ? "获取积分" : "消耗积分"),

View File

@ -1511,12 +1511,13 @@ public class EventScoreService : BaseService<EventResult>, ITransient
var list = await _certificateRepository.AsQueryable()
.LeftJoin<EventResult>((c, r) => c.ResultId == r.Id)
.LeftJoin<EventRegistration>((c, r, reg) => c.RegistrationId == reg.Id)
.LeftJoin<YD_Event.Core.Entities.Project>((c, r, reg, p) => c.ProjectId == p.Id)
.Where((c, r, reg, p) => c.EventId == input.EventId)
.WhereIF(input.ProjectId.HasValue, (c, r, reg, p) => c.ProjectId == input.ProjectId)
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupName), (c, r, reg, p) => c.GroupName == input.GroupName)
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), (c, r, reg, p) => c.ParticipantName.Contains(input.Name))
.Select((c, r, reg, p) => new CertificateExportDto
.LeftJoin<EventProject>((c, r, reg, ep) => c.ProjectId == ep.Id)
.LeftJoin<YD_Event.Core.Entities.Project>((c, r, reg, ep, p) => ep.ProjectId == p.Id)
.Where((c, r, reg, ep, p) => c.EventId == input.EventId)
.WhereIF(input.ProjectId.HasValue, (c, r, reg, ep, p) => c.ProjectId == input.ProjectId)
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupName), (c, r, reg, ep, p) => c.GroupName == input.GroupName)
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), (c, r, reg, ep, p) => c.ParticipantName.Contains(input.Name))
.Select((c, r, reg, ep, p) => new CertificateExportDto
{
CertificateNo = c.CertificateNo,
ParticipantName = c.ParticipantName,

View File

@ -749,7 +749,7 @@ public class UnifiedRegistrationService : ITransient
var regItems = new List<EventRegistrationItem>();
var persons = new List<EventRegistrationPerson>();
var uniqueUserIds = new HashSet<long>(); // 用于去重的用户ID集合
var uniqueUserIds = new HashSet<long>(); // 用于去重的用户ID集合(仅用于单价赛事模式)
foreach (var item in input.Items)
{
@ -817,10 +817,22 @@ public class UnifiedRegistrationService : ITransient
await _registrationItemRepository.InsertRangeAsync(regItems);
await _registrationPersonRepository.InsertRangeAsync(persons);
// 使用去重后的用户数量计算金额
var totalAmount = CalculateTotalAmount(feeConfig, eventProjects, input.Items, uniqueUserIds.Count);
// 根据收费模式决定使用哪种人数计算金额
int countForCalculation;
if (feeConfig != null && feeConfig.ChargeMode == ChargeMode.UnitPriceEvent)
{
// 单价(赛事)模式:使用去重后的人数
countForCalculation = uniqueUserIds.Count;
}
else
{
// 其他模式(包括单价项目):使用实际人数
countForCalculation = persons.Count;
}
return (totalAmount, uniqueUserIds.Count);
var totalAmount = CalculateTotalAmount(feeConfig, eventProjects, input.Items, countForCalculation);
return (totalAmount, countForCalculation);
}
/// <summary>

View File

@ -238,15 +238,16 @@ public class PointsRecordService : BaseService<PointsRecord>, ITransient
{
var query = _repository.AsQueryable()
.LeftJoin<EventInfo>((p, e) => p.EventId == e.Id)
.LeftJoin<YD_Event.Core.Entities.Project>((p, e, pr) => p.ProjectId == pr.Id)
.LeftJoin<ProjectGroup>((p, e, pr, pg) => p.ProjectGroupId == pg.Id)
.Where((p, e, pr, pg) => !p.DeleteMark)
.Where((p, e, pr, pg) => p.UserId == input.UserId)
.WhereIF(input.Type.HasValue, (p, e, pr, pg) => p.Type == input.Type)
.OrderByDescending((p, e, pr, pg) => p.CreatedTime);
.LeftJoin<EventProject>((p, e, ep) => p.ProjectId == ep.Id)
.LeftJoin<YD_Event.Core.Entities.Project>((p, e, ep, pr) => ep.ProjectId == pr.Id)
.LeftJoin<ProjectGroup>((p, e, ep, pr, pg) => p.ProjectGroupId == pg.Id)
.Where((p, e, ep, pr, pg) => !p.DeleteMark)
.Where((p, e, ep, pr, pg) => p.UserId == input.UserId)
.WhereIF(input.Type.HasValue, (p, e, ep, pr, pg) => p.Type == input.Type)
.OrderByDescending((p, e, ep, pr, pg) => p.CreatedTime);
return await query
.Select((p, e, pr, pg) => new PointsDetailOutput
.Select((p, e, ep, pr, pg) => new PointsDetailOutput
{
CreatedTime = p.CreatedTime,
Type = p.Type,
@ -264,16 +265,17 @@ public class PointsRecordService : BaseService<PointsRecord>, ITransient
// input.UserId 在这里是 MerchantCertificationId
var query = _repository.AsQueryable()
.LeftJoin<EventInfo>((p, e) => p.EventId == e.Id)
.LeftJoin<YD_Event.Core.Entities.Project>((p, e, pr) => p.ProjectId == pr.Id)
.LeftJoin<ProjectGroup>((p, e, pr, pg) => p.ProjectGroupId == pg.Id)
.LeftJoin<YD_Event.Core.Entities.AppUser>((p, e, pr, pg, u) => p.UserId == u.Id && !u.DeleteMark)
.Where((p, e, pr, pg, u) => !p.DeleteMark)
.Where((p, e, pr, pg, u) => p.MerchantCertificationId == input.UserId)
.WhereIF(input.Type.HasValue, (p, e, pr, pg, u) => p.Type == input.Type)
.OrderByDescending((p, e, pr, pg, u) => p.CreatedTime);
.LeftJoin<EventProject>((p, e, ep) => p.ProjectId == ep.Id)
.LeftJoin<YD_Event.Core.Entities.Project>((p, e, ep, pr) => ep.ProjectId == pr.Id)
.LeftJoin<ProjectGroup>((p, e, ep, pr, pg) => p.ProjectGroupId == pg.Id)
.LeftJoin<YD_Event.Core.Entities.AppUser>((p, e, ep, pr, pg, u) => p.UserId == u.Id && !u.DeleteMark)
.Where((p, e, ep, pr, pg, u) => !p.DeleteMark)
.Where((p, e, ep, pr, pg, u) => p.MerchantCertificationId == input.UserId)
.WhereIF(input.Type.HasValue, (p, e, ep, pr, pg, u) => p.Type == input.Type)
.OrderByDescending((p, e, ep, pr, pg, u) => p.CreatedTime);
return await query
.Select((p, e, pr, pg, u) => new PointsDetailOutput
.Select((p, e, ep, pr, pg, u) => new PointsDetailOutput
{
CreatedTime = p.CreatedTime,
Type = p.Type,