forked from liaozb/APIJSON.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonToSql.cs
More file actions
231 lines (222 loc) · 9.12 KB
/
JsonToSql.cs
File metadata and controls
231 lines (222 loc) · 9.12 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
namespace APIJSON.NET
{
using APIJSON.NET.Models;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public class JsonToSql: DbContext
{
protected List<Role> roles;
public JsonToSql(IOptions<DbOptions> options, IOptions<List<Role>> _roles) : base(options)
{
roles = _roles.Value;
}
/// <summary>
/// 对应数据表
/// </summary>
static Dictionary<string, string> dict = new Dictionary<string, string>
{
{"user", "apijson_user"},
};
public Role GetRole(string rolename)
{
var role = new Role();
if (string.IsNullOrEmpty(rolename))
{
role = roles.FirstOrDefault();
}
else
{
role = roles.FirstOrDefault(it => it.Name.Equals(rolename, StringComparison.CurrentCultureIgnoreCase));
}
return role;
}
public (bool, string) GetSelectRole(string rolename, string table)
{
var role = GetRole(rolename);
if (role == null || role.Select == null || role.Select.Table == null)
{
return (false, $"select.json权限配置不正确!");
}
string tablerole = role.Select.Table.FirstOrDefault(it => it.Equals(table, StringComparison.CurrentCultureIgnoreCase));
if (string.IsNullOrEmpty(tablerole))
{
return (false, $"表名{table}没权限查询!");
}
int index = Array.IndexOf(role.Select.Table, tablerole);
string selectrole = role.Select.Column[index];
return (true, selectrole);
}
public dynamic GetTableData(string subtable, int page, int count, string json, JObject dd,string rolename)
{
if (!subtable.IsTable())
{
throw new Exception($"表名{subtable}不正确!");
}
var role = GetSelectRole(rolename, subtable);
if (!role.Item1)
{
throw new Exception(role.Item2);
}
string selectrole = role.Item2;
if (dict.ContainsKey(subtable.ToLower()))
{
subtable = dict.GetValueOrDefault(subtable.ToLower());
}
JObject values = JObject.Parse(json);
var tb = Db.Queryable(subtable, "tb");
if (values["@column"].IsValue())
{
var str = new System.Text.StringBuilder(100);
foreach (var item in values["@column"].ToString().Split(","))
{
string[] ziduan = item.Split(":");
if (ziduan.Length > 1)
{
if (ziduan[0].IsField() && ziduan[1].IsTable()&&(selectrole =="*"|| selectrole.Split(',').Contains(ziduan[0],StringComparer.CurrentCultureIgnoreCase)))
{
str.Append(ziduan[0] + " as " + ziduan[1] + ",");
}
}
else
{
if (item.IsField() && (selectrole == "*" || selectrole.Split(',').Contains(item, StringComparer.CurrentCultureIgnoreCase)))
{
str.Append(item + ",");
}
}
}
tb.Select(str.ToString().TrimEnd(','));
}
else
{
tb.Select(selectrole);
}
page = values["page"] == null ? page : int.Parse(values["page"].ToString());
count = values["count"] == null ? count : int.Parse(values["count"].ToString());
values.Remove("page");
values.Remove("count");
List<IConditionalModel> conModels = new List<IConditionalModel>();
foreach (var va in values)
{
string vakey = va.Key.Trim();
if (vakey.EndsWith("$"))//模糊查询
{
if (vakey.TrimEnd('$').IsTable())
{
conModels.Add(new ConditionalModel() { FieldName = va.Key.TrimEnd('$'), ConditionalType = ConditionalType.Like, FieldValue = va.Value.ToString() });
}
}
else if (vakey.EndsWith("{}"))//逻辑运算
{
string field = va.Key.TrimEnd("{}".ToCharArray());
if (va.Value.HasValues)
{
conModels.Add(new ConditionalModel() { FieldName = field, ConditionalType = field.EndsWith("!") ? ConditionalType.NotIn : ConditionalType.In, FieldValue = va.Value.ToString() });
}
else
{
var ddt = new List<KeyValuePair<WhereType, ConditionalModel>>();
foreach (var and in va.Value.ToString().Split(','))
{
var model = new ConditionalModel();
model.FieldName = field;
if (and.StartsWith(">="))
{
model.ConditionalType = ConditionalType.GreaterThanOrEqual;
model.FieldValue = and.TrimStart(">=".ToCharArray());
}
else if (and.StartsWith("<="))
{
model.ConditionalType = ConditionalType.LessThanOrEqual;
model.FieldValue = and.TrimStart("<=".ToCharArray());
}
else if (and.StartsWith(">"))
{
model.ConditionalType = ConditionalType.GreaterThan;
model.FieldValue = and.TrimStart('>');
}
else if (and.StartsWith("<"))
{
model.ConditionalType = ConditionalType.LessThan;
model.FieldValue = and.TrimStart('<');
}
ddt.Add(new KeyValuePair<WhereType, ConditionalModel>((field.EndsWith("&") ? WhereType.And : WhereType.Or), model));
}
conModels.Add(new ConditionalCollections() { ConditionalList = ddt });
}
}
else if (vakey.EndsWith("@") && dd != null) // 关联上一个table
{
string[] str = va.Value.ToString().Split("/");
string value = string.Empty;
if (str.Length == 3)
{
value = dd[str[1]][str[2]].ToString();
}
else if (str.Length == 2)
{
value = dd[str[0]][str[1]].ToString();
}
conModels.Add(new ConditionalModel() { FieldName = vakey.TrimEnd('@'), ConditionalType = ConditionalType.Equal, FieldValue = value });
}
else if (vakey.IsTable()) //其他where条件
{
conModels.Add(new ConditionalModel() { FieldName = vakey, ConditionalType = ConditionalType.Equal, FieldValue = va.Value.ToString() });
}
}
tb.Where(conModels);
//排序
if (values["@order"].IsValue())
{
foreach (var item in values["@order"].ToString().Split(","))
{
if (item.Replace("-", "").IsTable())
{
if (item.EndsWith("-"))
{
tb.OrderBy($"{item.Replace("-", " desc")}");
}
else
{
tb.OrderBy($"{item.ToString()}");
}
}
}
}
else
{
tb.OrderBy("id");
}
if (values["@group"].IsValue())
{
var str = new System.Text.StringBuilder(100);
foreach (var and in values["@group"].ToString().Split(','))
{
if (and.IsField())
{
str.Append(and + ",");
}
}
tb.GroupBy(str.ToString().TrimEnd(','));
}
if (values["@having"].IsValue())
{
tb.Having($"{values["@having"].ToString()}");
}
if (count > 0)
{
return tb.ToPageList(page, count);
}
else
{
return tb.ToList();
}
}
}
}