Skip to content

Commit 0fa1123

Browse files
committed
[feat] 优化分页条件查询
1 parent c5fe809 commit 0fa1123

23 files changed

+183
-204
lines changed

Ape.Volo.Api/wwwroot/resources/db/sys_user.tsv

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"UpdateBy": null,
1818
"UpdateTime": null,
1919
"IsDeleted": false,
20-
"TenantId" : null
20+
"TenantId" : 0
2121
},
2222
{
2323
"Id": "1306054134645919751",
@@ -37,6 +37,6 @@
3737
"UpdateBy": null,
3838
"UpdateTime": null,
3939
"IsDeleted": false,
40-
"TenantId" : null
40+
"TenantId" : 1001
4141
}
4242
]

Ape.Volo.Business/Message/Email/EmailAccountService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,13 @@ public async Task<List<EmailAccountDto>> QueryAsync(EmailAccountQueryCriteria em
9292
Pagination pagination)
9393
{
9494
var whereExpression = GetWhereExpression(emailAccountQueryCriteria);
95+
var queryOptions = new QueryOptions<EmailAccount>
96+
{
97+
Pagination = pagination,
98+
WhereLambda = whereExpression,
99+
};
95100
return ApeContext.Mapper.Map<List<EmailAccountDto>>(
96-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
101+
await SugarRepository.QueryPageListAsync(queryOptions));
97102
}
98103

99104
public async Task<List<ExportBase>> DownloadAsync(EmailAccountQueryCriteria emailAccountQueryCriteria)

Ape.Volo.Business/Message/Email/EmailMessageTemplateService.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,13 @@ public async Task<List<EmailMessageTemplateDto>> QueryAsync(
8989
EmailMessageTemplateQueryCriteria messageTemplateQueryCriteria, Pagination pagination)
9090
{
9191
var whereExpression = GetWhereExpression(messageTemplateQueryCriteria);
92-
92+
var queryOptions = new QueryOptions<EmailMessageTemplate>
93+
{
94+
Pagination = pagination,
95+
WhereLambda = whereExpression,
96+
};
9397
return ApeContext.Mapper.Map<List<EmailMessageTemplateDto>>(
94-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
98+
await SugarRepository.QueryPageListAsync(queryOptions));
9599
}
96100

97101
#endregion

Ape.Volo.Business/Monitor/AuditInfoService.cs

+17-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ public async Task<List<AuditLogDto>> QueryAsync(LogQueryCriteria logQueryCriteri
4949
l.CreateTime >= logQueryCriteria.CreateTime[0] && l.CreateTime <= logQueryCriteria.CreateTime[1]);
5050
}
5151

52-
var auditInfos = await SugarRepository.QueryPageListAsync(whereLambda, pagination, null, true);
52+
var queryOptions = new QueryOptions<AuditLog>
53+
{
54+
Pagination = pagination,
55+
WhereLambda = whereLambda,
56+
IsSplitTable = true
57+
};
58+
59+
var auditInfos = await SugarRepository.QueryPageListAsync(queryOptions);
5360
return ApeContext.Mapper.Map<List<AuditLogDto>>(auditInfos);
5461
}
5562

@@ -62,14 +69,20 @@ public async Task<List<AuditLogDto>> QueryByCurrentAsync(string userName, Pagina
6269
}
6370

6471

65-
Expression<Func<AuditLog, AuditLog>> expression = x => new AuditLog
72+
Expression<Func<AuditLog, AuditLog>> selectExpression = x => new AuditLog
6673
{
6774
Id = x.Id, Description = x.Description, RequestIp = x.RequestIp, IpAddress = x.IpAddress,
6875
OperatingSystem = x.OperatingSystem, DeviceType = x.DeviceType, BrowserName = x.BrowserName,
6976
Version = x.Version, ExecutionDuration = x.ExecutionDuration, CreateTime = x.CreateTime
7077
};
71-
72-
var auditInfos = await SugarRepository.QueryPageListAsync(whereLambda, pagination, expression, true);
78+
var queryOptions = new QueryOptions<AuditLog>
79+
{
80+
Pagination = pagination,
81+
WhereLambda = whereLambda,
82+
SelectExpression = selectExpression,
83+
IsSplitTable = true
84+
};
85+
var auditInfos = await SugarRepository.QueryPageListAsync(queryOptions);
7386
return ApeContext.Mapper.Map<List<AuditLogDto>>(auditInfos);
7487
}
7588

Ape.Volo.Business/Monitor/ExceptionLogService.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ public async Task<List<ExceptionLogDto>> QueryAsync(LogQueryCriteria logQueryCri
4848
l.CreateTime >= logQueryCriteria.CreateTime[0] && l.CreateTime <= logQueryCriteria.CreateTime[1]);
4949
}
5050

51-
var logs = await SugarRepository.QueryPageListAsync(whereLambda, pagination, null, true);
51+
var queryOptions = new QueryOptions<ExceptionLog>
52+
{
53+
Pagination = pagination,
54+
WhereLambda = whereLambda,
55+
IsSplitTable = true
56+
};
57+
var logs = await SugarRepository.QueryPageListAsync(queryOptions);
5258
return ApeContext.Mapper.Map<List<ExceptionLogDto>>(logs);
5359
}
5460

Ape.Volo.Business/Permission/ApisService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ public async Task<List<Apis>> QueryAsync(ApisQueryCriteria apisQueryCriteria, Pa
7979
whereExpression = whereExpression.AndAlso(x => x.Method == apisQueryCriteria.Method);
8080
}
8181

82-
return await SugarRepository.QueryPageListAsync(whereExpression, pagination);
82+
var queryOptions = new QueryOptions<Apis>
83+
{
84+
Pagination = pagination,
85+
WhereLambda = whereExpression,
86+
};
87+
return await SugarRepository.QueryPageListAsync(queryOptions);
8388
}
8489

8590
public async Task<List<Apis>> QueryAllAsync()

Ape.Volo.Business/Permission/DepartmentService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ public async Task<List<DepartmentDto>> QueryAsync(DeptQueryCriteria deptQueryCri
165165
List<Department> deptList;
166166
if (deptQueryCriteria.ParentId.IsNull())
167167
{
168-
deptList = await SugarRepository.QueryPageListAsync(whereExpression, pagination);
168+
var queryOptions = new QueryOptions<Department>
169+
{
170+
Pagination = pagination,
171+
WhereLambda = whereExpression
172+
};
173+
deptList = await SugarRepository.QueryPageListAsync(queryOptions);
169174
}
170175
else
171176
{

Ape.Volo.Business/Permission/JobService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,13 @@ public async Task<bool> DeleteAsync(HashSet<long> ids)
8080
public async Task<List<JobDto>> QueryAsync(JobQueryCriteria jobQueryCriteria, Pagination pagination)
8181
{
8282
var whereExpression = GetWhereExpression(jobQueryCriteria);
83+
var queryOptions = new QueryOptions<Job>
84+
{
85+
Pagination = pagination,
86+
WhereLambda = whereExpression,
87+
};
8388
return ApeContext.Mapper.Map<List<JobDto>>(
84-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
89+
await SugarRepository.QueryPageListAsync(queryOptions));
8590
}
8691

8792
public async Task<List<ExportBase>> DownloadAsync(JobQueryCriteria jobQueryCriteria)

Ape.Volo.Business/Permission/RoleService.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,15 @@ public async Task<bool> DeleteAsync(HashSet<long> ids)
135135
public async Task<List<RoleDto>> QueryAsync(RoleQueryCriteria roleQueryCriteria, Pagination pagination)
136136
{
137137
var whereExpression = GetWhereExpression(roleQueryCriteria);
138+
var queryOptions = new QueryOptions<Role>
139+
{
140+
Pagination = pagination,
141+
WhereLambda = whereExpression,
142+
IsIncludes = true,
143+
IgnorePropertyNameList = new[] { "Users" }
144+
};
138145
var roleList =
139-
await SugarRepository.QueryPageListAsync<Role, Menu, Department, Apis>(whereExpression, pagination,
140-
null, null, x => x.MenuList,
141-
x => x.DepartmentList, x => x.Apis);
146+
await SugarRepository.QueryPageListAsync(queryOptions);
142147

143148
return ApeContext.Mapper.Map<List<RoleDto>>(roleList);
144149
}

Ape.Volo.Business/Permission/UserService.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using Ape.Volo.IBusiness.Interface.Permission;
2020
using Ape.Volo.IBusiness.QueryModel;
2121
using Microsoft.AspNetCore.Http;
22+
using NodaTime.TimeZones;
23+
using SqlSugar;
2224

2325
namespace Ape.Volo.Business.Permission;
2426

@@ -190,10 +192,14 @@ public async Task<bool> DeleteAsync(HashSet<long> ids)
190192
public async Task<List<UserDto>> QueryAsync(UserQueryCriteria userQueryCriteria, Pagination pagination)
191193
{
192194
var whereExpression = await GetWhereExpression(userQueryCriteria);
193-
var users = await SugarRepository.QueryPageListAsync(whereExpression,
194-
pagination, null,
195-
x => x.Dept,
196-
x => x.Jobs, x => x.Roles);
195+
196+
var queryOptions = new QueryOptions<User>
197+
{
198+
Pagination = pagination,
199+
WhereLambda = whereExpression,
200+
IsIncludes = true
201+
};
202+
var users = await SugarRepository.QueryPageListAsync(queryOptions);
197203

198204
return ApeContext.Mapper.Map<List<UserDto>>(users);
199205
}

Ape.Volo.Business/Queued/QueuedEmailService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ public async Task<List<QueuedEmailDto>> QueryAsync(QueuedEmailQueryCriteria queu
111111
Pagination pagination)
112112
{
113113
var whereExpression = GetWhereExpression(queuedEmailQueryCriteria);
114+
var queryOptions = new QueryOptions<QueuedEmail>
115+
{
116+
Pagination = pagination,
117+
WhereLambda = whereExpression,
118+
};
114119
return ApeContext.Mapper.Map<List<QueuedEmailDto>>(
115-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
120+
await SugarRepository.QueryPageListAsync(queryOptions));
116121
}
117122

118123
#endregion

Ape.Volo.Business/System/AppSecretService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,13 @@ public async Task<List<AppSecretDto>> QueryAsync(AppsecretQueryCriteria appsecre
7878
Pagination pagination)
7979
{
8080
var whereExpression = GetWhereExpression(appsecretQueryCriteria);
81+
var queryOptions = new QueryOptions<AppSecret>
82+
{
83+
Pagination = pagination,
84+
WhereLambda = whereExpression,
85+
};
8186
return ApeContext.Mapper.Map<List<AppSecretDto>>(
82-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
87+
await SugarRepository.QueryPageListAsync(queryOptions));
8388
}
8489

8590
public async Task<List<ExportBase>> DownloadAsync(AppsecretQueryCriteria appsecretQueryCriteria)

Ape.Volo.Business/System/FileRecordService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,13 @@ public async Task<List<FileRecordDto>> QueryAsync(FileRecordQueryCriteria fileRe
112112
Pagination pagination)
113113
{
114114
var whereExpression = GetWhereExpression(fileRecordQueryCriteria);
115+
var queryOptions = new QueryOptions<FileRecord>
116+
{
117+
Pagination = pagination,
118+
WhereLambda = whereExpression,
119+
};
115120
return ApeContext.Mapper.Map<List<FileRecordDto>>(
116-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
121+
await SugarRepository.QueryPageListAsync(queryOptions));
117122
}
118123

119124
public async Task<List<ExportBase>> DownloadAsync(FileRecordQueryCriteria fileRecordQueryCriteria)

Ape.Volo.Business/System/QuartzNetLogService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ public async Task<List<QuartzNetLogDto>> QueryAsync(QuartzNetLogQueryCriteria qu
4040
Pagination pagination)
4141
{
4242
var whereExpression = GetWhereExpression(quartzNetLogQueryCriteria);
43+
var queryOptions = new QueryOptions<QuartzNetLog>
44+
{
45+
Pagination = pagination,
46+
WhereLambda = whereExpression,
47+
};
4348
return ApeContext.Mapper.Map<List<QuartzNetLogDto>>(
44-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
49+
await SugarRepository.QueryPageListAsync(queryOptions));
4550
}
4651

4752
public async Task<List<ExportBase>> DownloadAsync(QuartzNetLogQueryCriteria quartzNetLogQueryCriteria)

Ape.Volo.Business/System/QuartzNetService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ public async Task<List<QuartzNetDto>> QueryAsync(QuartzNetQueryCriteria quartzNe
101101
Pagination pagination)
102102
{
103103
var whereExpression = GetWhereExpression(quartzNetQueryCriteria);
104+
var queryOptions = new QueryOptions<QuartzNet>
105+
{
106+
Pagination = pagination,
107+
WhereLambda = whereExpression,
108+
};
104109
return ApeContext.Mapper.Map<List<QuartzNetDto>>(
105-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
110+
await SugarRepository.QueryPageListAsync(queryOptions));
106111
}
107112

108113
public async Task<List<ExportBase>> DownloadAsync(QuartzNetQueryCriteria quartzNetQueryCriteria)

Ape.Volo.Business/System/SettingService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ await ApeContext.Cache.RemoveAsync(GlobalConstants.CachePrefix.LoadSettingByName
7777
public async Task<List<SettingDto>> QueryAsync(SettingQueryCriteria settingQueryCriteria, Pagination pagination)
7878
{
7979
var whereExpression = GetWhereExpression(settingQueryCriteria);
80+
var queryOptions = new QueryOptions<Setting>
81+
{
82+
Pagination = pagination,
83+
WhereLambda = whereExpression,
84+
};
8085
return ApeContext.Mapper.Map<List<SettingDto>>(
81-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
86+
await SugarRepository.QueryPageListAsync(queryOptions));
8287
}
8388

8489
public async Task<List<ExportBase>> DownloadAsync(SettingQueryCriteria settingQueryCriteria)

Ape.Volo.Business/System/TenantService.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ public async Task<bool> DeleteAsync(HashSet<long> ids)
121121
public async Task<List<TenantDto>> QueryAsync(TenantQueryCriteria tenantQueryCriteria, Pagination pagination)
122122
{
123123
var whereExpression = GetWhereExpression(tenantQueryCriteria);
124+
var queryOptions = new QueryOptions<Tenant>
125+
{
126+
Pagination = pagination,
127+
WhereLambda = whereExpression,
128+
};
124129
return ApeContext.Mapper.Map<List<TenantDto>>(
125-
await SugarRepository.QueryPageListAsync(whereExpression, pagination));
130+
await SugarRepository.QueryPageListAsync(queryOptions));
126131
}
127132

128133
public async Task<List<TenantDto>> QueryAllAsync()

Ape.Volo.Common/Extensions/Ext.Object.cs

-18
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,6 @@ public static string ToJson(this object obj)
6666
return JsonConvert.SerializeObject(obj, Formatting.None, serializerSettings);
6767
}
6868

69-
/// <summary>
70-
/// 将对象序列化成Json字符串
71-
/// </summary>
72-
/// <param name="obj">需要序列化的对象</param>
73-
/// <returns></returns>
74-
public static string ToRedisJson(this object obj)
75-
{
76-
var serializerSettings = new JsonSerializerSettings
77-
{
78-
// 设置为驼峰命名
79-
//ContractResolver = new CamelCasePropertyNamesContractResolver(),
80-
ContractResolver = new CustomContractResolver(),
81-
DateFormatString = "yyyy-MM-dd HH:mm:ss",
82-
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
83-
};
84-
return JsonConvert.SerializeObject(obj, Formatting.None, serializerSettings);
85-
}
86-
8769
/// <summary>
8870
/// 将对象序列化成Json字符串,同时忽略null字段
8971
/// </summary>

Ape.Volo.Common/Model/QueryOptions.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
4+
namespace Ape.Volo.Common.Model;
5+
6+
/// <summary>
7+
/// 查询操作对象
8+
/// </summary>
9+
/// <typeparam name="T"></typeparam>
10+
public class QueryOptions<T>
11+
{
12+
/// <summary>
13+
/// 分页
14+
/// </summary>
15+
public Pagination Pagination { get; set; }
16+
17+
/// <summary>
18+
/// 条件表达式
19+
/// </summary>
20+
public Expression<Func<T, bool>> WhereLambda { get; set; } = null;
21+
22+
/// <summary>
23+
/// 查询表达式
24+
/// </summary>
25+
public Expression<Func<T, T>> SelectExpression { get; set; } = null;
26+
27+
/// <summary>
28+
/// 是否分表
29+
/// </summary>
30+
public bool IsSplitTable { get; set; } = false;
31+
32+
/// <summary>
33+
/// 是否查询全部一级导航属性
34+
/// </summary>
35+
public bool IsIncludes { get; set; } = false;
36+
37+
/// <summary>
38+
/// 忽略的查询导航属性 IncludesAll为true才有用
39+
/// </summary>
40+
public string[] IgnorePropertyNameList { get; set; }
41+
}

Ape.Volo.Entity/Permission/User.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public class User : BaseEntity, ISoftDeletedEntity
117117
/// 租户
118118
/// </summary>
119119
[SugarColumn(IsIgnore = true)]
120-
[Navigate(NavigateType.OneToOne, nameof(TenantId))]
120+
[Navigate(NavigateType.OneToOne, nameof(Tenant.TenantId), nameof(TenantId))]
121121
public Tenant Tenant { get; set; }
122122

123123
#endregion

Ape.Volo.IBusiness/Dto/Permission/UserDto.cs

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Ape.Volo.Common.AttributeExt;
44
using Ape.Volo.Entity.Permission;
55
using Ape.Volo.IBusiness.Base;
6+
using Ape.Volo.IBusiness.Dto.System;
67

78
namespace Ape.Volo.IBusiness.Dto.Permission;
89

@@ -96,4 +97,9 @@ public UserDto()
9697
/// 租户ID
9798
/// </summary>
9899
public int TenantId { get; set; }
100+
101+
/// <summary>
102+
/// 租户
103+
/// </summary>
104+
public TenantDto Tenant { get; set; }
99105
}

0 commit comments

Comments
 (0)