forked from liaozb/APIJSON.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonController.cs
More file actions
269 lines (247 loc) · 8.52 KB
/
Copy pathJsonController.cs
File metadata and controls
269 lines (247 loc) · 8.52 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
using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Net.Http;
using APIJSON.Data;
using SqlSugar;
using Volo.Abp.AspNetCore.Mvc;
namespace APIJSON.NET.Controllers;
[Route("api/[controller]")]
[ApiController]
public class JsonController : AbpControllerBase
{
private SelectTable selectTable;
private DbContext db;
private readonly IIdentityService _identitySvc;
private ITableMapper _tableMapper;
public JsonController(IIdentityService identityService, ITableMapper tableMapper, DbContext _db)
{
db = _db;
_tableMapper = tableMapper;
_identitySvc = identityService;
selectTable = new SelectTable(_identitySvc, _tableMapper, _db.Db);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet("/test")]
public ActionResult Test()
{
string str = "{\"page\":1,\"count\":3,\"query\":2,\"Org\":{\"@column\":\"Id,Name\"}}";
var content = new StringContent(str);
return Ok(content);
}
/// <summary>
/// 查询
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
[HttpPost("/get")]
public ActionResult Query([FromBody] JObject jobject)
{
var st = new SelectTable(_identitySvc, _tableMapper, db.Db);
JObject resultJobj = st.Query(jobject);
return Ok(resultJobj);
}
[HttpPost("/{table}")]
public async Task<ActionResult> QueryByTable([FromRoute]string table)
{
string json = string.Empty;
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
json = await reader.ReadToEndAsync();
}
json = HttpUtility.UrlDecode(json);
JObject ht = new JObject();
JObject jobject = JObject.Parse(json);
ht.Add(table + "[]", jobject);
if (jobject["query"] != null && jobject["query"].ToString() != "0" && jobject["total@"] == null)
{
//自动添加总计数量
ht.Add("total@", "");
}
//每页最大1000条数据
if (jobject["count"] != null && int.Parse(jobject["count"].ToString()) > 1000)
{
throw new Exception("count分页数量最大不能超过1000");
}
bool isDebug = (jobject["@debug"] != null && jobject["@debug"].ToString() != "0");
jobject.Remove("@debug");
bool hasTableKey = false;
List<string> ignoreConditions = new List<string> { "page", "count", "query" };
JObject tableConditions = new JObject();//表的其它查询条件,比如过滤,字段等
foreach (var item in jobject)
{
if (item.Key.Equals(table, StringComparison.CurrentCultureIgnoreCase))
{
hasTableKey = true;
break;
}
if (!ignoreConditions.Contains(item.Key.ToLower()))
{
tableConditions.Add(item.Key, item.Value);
}
}
foreach (var removeKey in tableConditions)
{
jobject.Remove(removeKey.Key);
}
if (!hasTableKey)
{
jobject.Add(table, tableConditions);
}
return Query(ht);
}
/// <summary>
/// 新增
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
[HttpPost("/add")]
public ActionResult Add([FromBody]JObject jobject)
{
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{
foreach (var item in jobject)
{
string key = item.Key.Trim();
var role = _identitySvc.GetRole();
if (!role.Insert.Table.Contains(key, StringComparer.CurrentCultureIgnoreCase))
{
ht["code"] = "500";
ht["msg"] = $"没权限添加{key}";
break;
}
var dt = new Dictionary<string, object>();
foreach (var f in JObject.Parse(item.Value.ToString()))
{
if (f.Key.ToLower() != "id" && selectTable.IsCol(key, f.Key) && (role.Insert.Column.Contains("*") || role.Insert.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
dt.Add(f.Key, f.Value);
}
int id = db.Db.Insertable(dt).AS(key).ExecuteReturnIdentity();
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id }));
}
}
catch (Exception ex)
{
ht["code"] = "500";
ht["msg"] = ex.Message;
}
return Ok(ht);
}
/// <summary>
/// 修改
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
[HttpPost("/edit")]
public ActionResult Edit([FromBody]JObject jobject)
{
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{
foreach (var item in jobject)
{
string key = item.Key.Trim();
var role = _identitySvc.GetRole();
if (!role.Update.Table.Contains(key, StringComparer.CurrentCultureIgnoreCase))
{
ht["code"] = "500";
ht["msg"] = $"没权限修改{key}";
break;
}
var value = JObject.Parse(item.Value.ToString());
if (!value.ContainsKey("id"))
{
ht["code"] = "500";
ht["msg"] = "未传主键id";
break;
}
var dt = new Dictionary<string, object>();
foreach (var f in value)
{
if (f.Key.ToLower() != "id" && selectTable.IsCol(key, f.Key) && (role.Update.Column.Contains("*") || role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
{
dt.Add(f.Key, f.Value.ToString());
}
}
db.Db.Updateable(dt).AS(key).Where("id=@id", new { id = value["id"].ToString() }).ExecuteCommand();
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));
}
}
catch (Exception ex)
{
ht["code"] = "500";
ht["msg"] = ex.Message;
}
return Ok(ht);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
[HttpPost("/remove")]
public ActionResult Remove([FromBody]JObject jobject)
{
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{
var role = _identitySvc.GetRole();
foreach (var item in jobject)
{
string key = item.Key.Trim();
var value = JObject.Parse(item.Value.ToString());
var sb = new System.Text.StringBuilder(100);
sb.Append($"delete FROM {key} where ");
if (role.Delete == null || role.Delete.Table == null)
{
ht["code"] = "500";
ht["msg"] = "delete权限未配置";
break;
}
if (!role.Delete.Table.Contains(key, StringComparer.CurrentCultureIgnoreCase))
{
ht["code"] = "500";
ht["msg"] = $"没权限删除{key}";
break;
}
if (!value.ContainsKey("id"))
{
ht["code"] = "500";
ht["msg"] = "未传主键id";
break;
}
var p = new List<SugarParameter>();
foreach (var f in value)
{
sb.Append($"{f.Key}=@{f.Key},");
p.Add(new SugarParameter($"@{f.Key}", f.Value.ToString()));
}
string sql = sb.ToString().TrimEnd(',');
db.Db.Ado.ExecuteCommand(sql, p);
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));
}
}
catch (Exception ex)
{
ht["code"] = "500";
ht["msg"] = ex.Message;
}
return Ok(ht);
}
}