DisplayScaleHelper.cs
8.32 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
namespace NCC.UnifyResult
{
/// <summary>
/// 展示缩放:只改副本数字,不写回数据库。少三分之一 = 真值 × 2/3。
/// </summary>
public static class DisplayScaleHelper
{
public const decimal Factor = 2m / 3m;
private static readonly Regex EntityIdRegex = new(@"^\d{12,}$", RegexOptions.Compiled);
private static readonly Regex GuidHexRegex = new(@"^[0-9a-f]{32}$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly HashSet<string> ExactSkip = new(StringComparer.OrdinalIgnoreCase)
{
"id", "pageindex", "pagesize", "currentpage", "totalcount", "total",
"timestamp", "code", "year", "month", "day", "hour", "minute", "second",
"limit", "offset", "index", "sortcode", "enabledmark", "deletemark",
"isonjob", "status", "type", "level", "port", "timeout", "opacity",
"count", "number", "size", "width", "height", "age", "days", "minutes"
};
private static readonly string[] SkipSuffix =
{
"id", "ids", "rate", "ratio", "percent", "percentage", "coefficient",
"time", "date", "datetime", "phone", "mobile", "account", "mark",
"status", "type", "switch", "timeout", "opacity", "port"
};
private static readonly string[] SkipContain =
{
"password", "token", "secret", "phone", "mobile", "account", "isenable",
"delete", "creator", "modify"
};
private static readonly string[] ScaleContain =
{
"amount", "performance", "price", "money", "labor", "consume",
"headcount", "personcount", "projectcount", "projectnumber",
"billingcount", "refundcount", "ordercount", "invitecount",
"appointmentcount", "visitcount", "cash", "sfyj", "zdyj", "xfje",
"sgfy", "sgf", "jksyj", "quantity", "kdpxnumber", "target",
"yj", "wage", "salary", "commission", "bonus", "debt", "arrear",
"refund", "consumeamount", "billingamount", "actualprice"
};
private static readonly string[] ScaleSuffix =
{
"amount", "performance", "price", "money", "count", "quantity",
"number", "yj", "wage", "salary"
};
public static bool Enabled
{
get => EnabledLocal.Value;
set => EnabledLocal.Value = value;
}
private static readonly System.Threading.AsyncLocal<bool> EnabledLocal = new();
public static decimal Scale(decimal value)
{
return Math.Round(value * Factor, 2, MidpointRounding.AwayFromZero);
}
public static bool ShouldScaleField(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
var n = name.Trim().ToLowerInvariant();
if (ExactSkip.Contains(n))
{
return false;
}
foreach (var part in SkipContain)
{
if (n.Contains(part))
{
return false;
}
}
foreach (var suffix in SkipSuffix)
{
if (n.EndsWith(suffix, StringComparison.Ordinal))
{
return false;
}
}
foreach (var part in ScaleContain)
{
if (n.Contains(part))
{
return true;
}
}
foreach (var suffix in ScaleSuffix)
{
if (n.EndsWith(suffix, StringComparison.Ordinal))
{
return true;
}
}
return false;
}
public static bool IsEntityDetailPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return false;
}
var p = path.Split('?')[0];
if (p.IndexOf("/Actions/", StringComparison.OrdinalIgnoreCase) >= 0)
{
return false;
}
var last = p.TrimEnd('/').Split('/');
var tail = last.Length == 0 ? string.Empty : last[last.Length - 1];
return EntityIdRegex.IsMatch(tail) || GuidHexRegex.IsMatch(tail);
}
public static bool IsSkipApiPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return true;
}
var p = path.ToLowerInvariant();
return p.Contains("/oauth")
|| p.Contains("/sysconfig")
|| p.Contains("/login")
|| p.Contains("/file/")
|| p.Contains("/files/")
|| p.Contains("/model/config");
}
public static bool IsPresentationWritePath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return false;
}
var p = path.ToLowerInvariant();
return p.Contains("statistic")
|| p.Contains("dashboard")
|| p.Contains("report")
|| p.Contains("export")
|| p.Contains("ranking")
|| p.Contains("summary")
|| p.Contains("get-");
}
public static object ScaleClone(object data)
{
if (data == null)
{
return null;
}
var type = data.GetType();
var token = data as JToken ?? JToken.FromObject(data);
ScaleToken(token, null);
if (data is JToken)
{
return token;
}
try
{
return token.ToObject(type);
}
catch
{
return token;
}
}
public static List<T> ScaleCloneList<T>(List<T> list)
{
if (list == null || list.Count == 0)
{
return list;
}
var token = JToken.FromObject(list);
ScaleToken(token, null);
return token.ToObject<List<T>>() ?? list;
}
public static void InvalidateCache()
{
CacheExpireUtc = DateTime.MinValue;
}
/// <summary>
/// 读开关缓存。过期时仍通过 <paramref name="enabled"/> 返回上一次的值(未加载过则为 false)。
/// </summary>
public static bool ReadEnabledCache(out bool enabled)
{
enabled = CacheEnabled;
return DateTime.UtcNow < CacheExpireUtc;
}
/// <summary>开关是否从库里加载过(进程启动后首次)。</summary>
public static bool EnabledCacheLoaded => CacheLoaded;
public static void WriteEnabledCache(bool enabled, int seconds = 60)
{
CacheEnabled = enabled;
CacheLoaded = true;
CacheExpireUtc = DateTime.UtcNow.AddSeconds(seconds);
}
private static volatile bool CacheEnabled;
private static volatile bool CacheLoaded;
private static DateTime CacheExpireUtc = DateTime.MinValue;
private static void ScaleToken(JToken token, string name)
{
if (token == null)
{
return;
}
if (token is JObject obj)
{
foreach (var prop in obj.Properties())
{
ScaleToken(prop.Value, prop.Name);
}
return;
}
if (token is JArray arr)
{
foreach (var item in arr)
{
ScaleToken(item, name);
}
return;
}
if (token is JValue value && ShouldScaleField(name) && IsNumeric(value))
{
var raw = Convert.ToDecimal(value.Value, CultureInfo.InvariantCulture);
value.Value = Scale(raw);
}
}
private static bool IsNumeric(JValue value)
{
return value.Type == JTokenType.Integer || value.Type == JTokenType.Float;
}
}
}