This repository was archived by the owner on Sep 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSysConfigs.cs
More file actions
158 lines (144 loc) · 5.19 KB
/
Copy pathSysConfigs.cs
File metadata and controls
158 lines (144 loc) · 5.19 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
using StarBot.Entity;
using StarBot.Extension;
using StarBot.IService;
using StarBot.Model;
using StarBot.Repository;
using Newtonsoft.Json.Linq;
using TBC.CommonLib;
using System.Text.RegularExpressions;
namespace StarBot.Service
{
/// <summary>
/// 系统配置
/// </summary>
public class SysConfigs(ICacheService _cache) : Repository<SysConfig>, ISysConfig
{
ICacheService cache = _cache;
/// <summary>
/// 获取值
/// </summary>
/// <returns></returns>
public async Task<string> GetValue(string key, int pid = 0)
{
var model = new SysConfig();
if (pid > 0)
model = await GetModelAsync(t => t.Pid == pid && t.Key == key);
else
model = await GetModelAsync(t => t.Key == key);
return model.Value ?? "";
}
public async Task<bool> SaveConfig(Config config)
{
var whriteList = new List<string>() { "MsgTypeAll" };
var updates = new List<SysConfig>();
var jobj = JObject.FromObject(config)!;
foreach (var item in jobj)
{
var pModel = await GetModelAsync(t => t.Key == item.Key && t.DataType.Contains("class"));
if (pModel == null) continue;
var children = await GetListAsync(t => t.Pid == pModel.Id);
var subItem = JObject.Parse(item.Value!.ToString());
foreach (var child in children)
{
if (whriteList.Contains(child.Key)) continue;
var newVal = subItem[child.Key]!.ToString();
if (newVal == "False" || newVal == "True") newVal = newVal.ToLower();
if (newVal == "[]") newVal = "";
if (newVal == child.Value) continue;
child.Value = Regex.Replace(newVal, @"[ \t\r\n]", ""); ;
ClearConfig(pModel.Key);
updates.Add(child);
}
}
if (updates.Count == 0) return true;
var b = await UpdateRangeAsync(updates);
if (b)
{
ClearConfig();
await GetConfig();
}
return b;
}
public async Task<Config> GetConfig()
{
var config = new Config();
//EnableModule
var enableModule = cache.GetCache<EnableModule>("EnableModule");
enableModule ??= await SetCache<EnableModule>(6, "EnableModule");
config.EnableModule = enableModule;
//Bot
var bot = cache.GetCache<Bot>("Bot");
bot ??= await SetCache<Bot>(1, "Bot");
config.Bot = bot;
//qq
var qq = cache.GetCache<QQ>("QQ");
qq ??= await SetCache<QQ>(8, "QQ");
config.QQ = qq;
//WB
var wb = cache.GetCache<WB>("WB");
wb ??= await SetCache<WB>(9, "WB");
config.WB = wb;
//BZ
var bz = cache.GetCache<BZ>("BZ");
bz ??= await SetCache<BZ>(10, "BZ");
config.BZ = bz;
//KD
var kd = cache.GetCache<KD>("KD");
kd ??= await SetCache<KD>(11, "KD");
config.KD = kd;
//XHS
var xhs = cache.GetCache<XHS>("XHS");
xhs ??= await SetCache<XHS>(12, "XHS");
config.XHS = xhs;
//DY
var dy = cache.GetCache<DY>("DY");
dy ??= await SetCache<DY>(13, "DY");
config.DY = dy;
//BD
var bd = cache.GetCache<BD>("BD");
bd ??= await SetCache<BD>(14, "BD");
config.BD = bd;
return config;
}
private async Task<T> SetCache<T>(int pid, string key)
{
var list = await GetListAsync(t => t.Pid == pid);
JObject obj = [];
foreach (var item in list)
{
if (item.DataType.Contains("bool"))
obj.Add(item.Key, item.Value.ToBool(false));
else if (item.DataType.Contains("list"))
{
if (string.IsNullOrWhiteSpace(item.Value))
obj.Add(item.Key, new JArray());
else
obj.Add(item.Key, JArray.Parse(item.Value));
}
else if (item.DataType.Contains("int"))
obj.Add(item.Key, item.Value.ToInt(0));
else obj.Add(item.Key, item.Value);
}
var val = obj.ToObject<T>()!;
cache.SetCache(key, val);
return val;
}
public void ClearConfig(string key = "")
{
if (!string.IsNullOrWhiteSpace(key))
{
cache.RemoveCache(key);
return;
}
cache.RemoveCache("EnableModule");
cache.RemoveCache("Bot");
cache.RemoveCache("QQ");
cache.RemoveCache("WB");
cache.RemoveCache("XHS");
cache.RemoveCache("BD");
cache.RemoveCache("BZ");
cache.RemoveCache("DY");
cache.RemoveCache("KD");
}
}
}