ReportsLocationScopeHelper.cs
4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using FoodLabeling.Application.Services.DbModels;
using FoodLabeling.Domain.Entities;
using SqlSugar;
using Volo.Abp.Users;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace FoodLabeling.Application.Helpers;
/// <summary>
/// Reports 门店数据范围:管理员可查全部(可按 Company/Region/Location 入参收窄);
/// 非管理员仅统计 <c>userlocation</c> 绑定门店的打印任务。
/// </summary>
public static class ReportsLocationScopeHelper
{
/// <summary>
/// 解析报表统计适用的门店 Id 列表。
/// <list type="bullet">
/// <item>返回 <c>null</c>:不限制门店(管理员且未传 Company/Region/Location 筛选);</item>
/// <item>返回空列表:无绑定门店或筛选后无交集;</item>
/// <item>返回非空列表:仅统计这些 <c>fl_label_print_task.LocationId</c>。</item>
/// </list>
/// </summary>
public static async Task<List<string>?> ResolveReportLocationIdsAsync(
ICurrentUser currentUser,
ISqlSugarClient db,
string? partnerId,
string? groupId,
string? locationId)
{
var filterIds = await ResolveFilterLocationIdsFromQueryAsync(db, partnerId, groupId, locationId);
if (ReportsRoleHelper.IsAdminRole(currentUser))
{
return filterIds;
}
if (currentUser.Id is null)
{
return new List<string>();
}
var userId = currentUser.Id.Value.ToString();
var boundIds = LocationScopeBindingHelper.NormalizeIds(
await db.Queryable<UserLocationDbEntity>()
.Where(x => !x.IsDeleted && x.UserId == userId)
.Select(x => x.LocationId)
.Distinct()
.ToListAsync());
if (boundIds.Count == 0)
{
return new List<string>();
}
if (filterIds is null)
{
return boundIds;
}
if (filterIds.Count == 0)
{
return new List<string>();
}
var boundSet = new HashSet<string>(boundIds, StringComparer.OrdinalIgnoreCase);
return filterIds.Where(id => boundSet.Contains(id)).ToList();
}
/// <summary>
/// 按入参 Company / Region / Location 解析门店 Id;均未传时返回 null(不限制)。
/// </summary>
private static async Task<List<string>?> ResolveFilterLocationIdsFromQueryAsync(
ISqlSugarClient db,
string? partnerId,
string? groupId,
string? locationId)
{
var locId = locationId?.Trim();
if (!string.IsNullOrWhiteSpace(locId))
{
return new List<string> { locId };
}
var gid = groupId?.Trim();
var pid = partnerId?.Trim();
if (string.IsNullOrWhiteSpace(pid) && string.IsNullOrWhiteSpace(gid))
{
return null;
}
var q = db.Queryable<LocationAggregateRoot>().Where(x => !x.IsDeleted);
if (!string.IsNullOrWhiteSpace(gid))
{
var g = await db.Queryable<FlGroupDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.Id == gid);
if (g is null)
{
return new List<string>();
}
var gName = g.GroupName?.Trim() ?? string.Empty;
var partner = await db.Queryable<FlPartnerDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.Id == g.PartnerId);
var pName = partner?.PartnerName?.Trim() ?? string.Empty;
q = q.Where(x => x.GroupName == gName && x.Partner == pName);
}
else if (!string.IsNullOrWhiteSpace(pid))
{
var partner = await db.Queryable<FlPartnerDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.Id == pid);
if (partner is null)
{
return new List<string>();
}
var pName = partner.PartnerName?.Trim() ?? string.Empty;
q = q.Where(x => x.Partner == pName);
}
return await q.Select(x => SqlFunc.ToString(x.Id)).ToListAsync();
}
}