SelectTable.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. namespace Admin.NET.Core.Service.ApiJson
  2. {
  3. using APIJSON.NET.Services;
  4. using AspectCore.Extensions.Reflection;
  5. using Newtonsoft.Json.Linq;
  6. using SqlSugar;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Dynamic;
  10. using System.Linq;
  11. using System.Text.RegularExpressions;
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. public class SelectTable
  16. {
  17. private readonly IIdentityService _identitySvc;
  18. private readonly ITableMapper _tableMapper;
  19. private readonly ISqlSugarClient db;
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. /// <param name="identityService"></param>
  24. /// <param name="tableMapper"></param>
  25. /// <param name="dbClient"></param>
  26. public SelectTable(IIdentityService identityService, ITableMapper tableMapper, ISqlSugarClient dbClient)
  27. {
  28. _identitySvc = identityService;
  29. _tableMapper = tableMapper;
  30. db = dbClient;
  31. }
  32. /// <summary>
  33. /// 判断表名是否正确
  34. /// </summary>
  35. /// <param name="table"></param>
  36. /// <returns></returns>
  37. public bool IsTable(string table)
  38. {
  39. return db.DbMaintenance.GetTableInfoList().Any(it => it.Name.Equals(table, StringComparison.CurrentCultureIgnoreCase));
  40. }
  41. /// <summary>
  42. /// 判断表的列名是否正确
  43. /// </summary>
  44. /// <param name="table"></param>
  45. /// <param name="col"></param>
  46. /// <returns></returns>
  47. public bool IsCol(string table, string col)
  48. {
  49. return db.DbMaintenance.GetColumnInfosByTableName(table).Any(it => it.DbColumnName.Equals(col, StringComparison.CurrentCultureIgnoreCase));
  50. }
  51. /// <summary>
  52. /// 动态调用方法
  53. /// </summary>
  54. /// <param name="funcname"></param>
  55. /// <param name="param"></param>
  56. /// <param name="types"></param>
  57. /// <returns></returns>
  58. public object ExecFunc(string funcname, object[] param, Type[] types)
  59. {
  60. var method = typeof(FuncList).GetMethod(funcname);
  61. var reflector = method.GetReflector();
  62. var result = reflector.Invoke(new FuncList(), param);
  63. return result;
  64. }
  65. private string ToSql(string subtable, int page, int count, int query, string json)
  66. {
  67. JObject values = JObject.Parse(json);
  68. page = values["page"] == null ? page : int.Parse(values["page"].ToString());
  69. count = values["count"] == null ? count : int.Parse(values["count"].ToString());
  70. query = values["query"] == null ? query : int.Parse(values["query"].ToString());
  71. values.Remove("page");
  72. values.Remove("count");
  73. subtable = _tableMapper.GetTableName(subtable);
  74. var tb = sugarQueryable(subtable, "*", values,null);
  75. var xx= tb.Skip((page - 1) * count).Take(10).ToSql();
  76. return xx.Key;
  77. }
  78. /// <summary>
  79. ///
  80. /// </summary>
  81. /// <param name="subtable"></param>
  82. /// <param name="page"></param>
  83. /// <param name="count"></param>
  84. /// <param name="json"></param>
  85. /// <param name="dd"></param>
  86. /// <returns></returns>
  87. public Tuple<dynamic, int> GetTableData(string subtable, int page, int count, int query, string json, JObject dd)
  88. {
  89. var role = _identitySvc.GetSelectRole(subtable);
  90. if (!role.Item1)//没有权限返回异常
  91. {
  92. throw new Exception(role.Item2);
  93. }
  94. string selectrole = role.Item2;
  95. subtable = _tableMapper.GetTableName(subtable);
  96. JObject values = JObject.Parse(json);
  97. page = values["page"] == null ? page : int.Parse(values["page"].ToString());
  98. count = values["count"] == null ? count : int.Parse(values["count"].ToString());
  99. query = values["query"] == null ? query : int.Parse(values["query"].ToString());
  100. values.Remove("page");
  101. values.Remove("count");
  102. var tb = sugarQueryable(subtable, selectrole, values, dd);
  103. if (query == 1)//1-总数
  104. return new Tuple<dynamic, int>(new List<object>(), tb.Count());
  105. else
  106. {
  107. if (count > 0)
  108. {
  109. int total = 0;
  110. if (query == 0)//0-对象
  111. return new Tuple<dynamic, int>(tb.ToPageList(page, count), total);
  112. else
  113. //2-以上全部
  114. return new Tuple<dynamic, int>(tb.ToPageList(page, count, ref total), total);
  115. }
  116. else
  117. {
  118. if (query == 0)
  119. return new Tuple<dynamic, int>(tb.ToList(), 0);
  120. else
  121. return new Tuple<dynamic, int>(tb.ToList(), tb.Count());
  122. }
  123. }
  124. }
  125. /// <summary>
  126. ///
  127. /// </summary>
  128. /// <param name="subtable"></param>
  129. /// <param name="json"></param>
  130. /// <param name="dd"></param>
  131. /// <returns></returns>
  132. public dynamic GetFirstData(string subtable, string json, JObject dd)
  133. {
  134. var role = _identitySvc.GetSelectRole(subtable);
  135. if (!role.Item1)//没有权限返回异常
  136. {
  137. throw new Exception(role.Item2);
  138. }
  139. string selectrole = role.Item2;
  140. subtable = _tableMapper.GetTableName(subtable);
  141. JObject values = JObject.Parse(json);
  142. values.Remove("page");
  143. values.Remove("count");
  144. var tb = sugarQueryable(subtable, selectrole, values, dd).First();
  145. var dic = (IDictionary<string, object>)tb;
  146. foreach (var item in values.Properties().Where(it => it.Name.EndsWith("()")))
  147. {
  148. if (item.Value.IsValue())
  149. {
  150. string func = item.Value.ToString().Substring(0, item.Value.ToString().IndexOf("("));
  151. string param = item.Value.ToString().Substring(item.Value.ToString().IndexOf("(") + 1).TrimEnd(')');
  152. var types = new List<Type>();
  153. var paramss = new List<object>();
  154. foreach (var va in param.Split(','))
  155. {
  156. types.Add(typeof(object));
  157. paramss.Add(tb.Where(it => it.Key.Equals(va)).Select(i => i.Value));
  158. }
  159. dic[item.Name] = ExecFunc(func, paramss.ToArray(), types.ToArray());
  160. }
  161. }
  162. return tb;
  163. }
  164. /// <summary>
  165. /// 解析并查询
  166. /// </summary>
  167. /// <param name="query"></param>
  168. /// <returns></returns>
  169. public JObject Query(string queryJson)
  170. {
  171. JObject resultObj = new JObject();
  172. try
  173. {
  174. JObject queryJobj = JObject.Parse(queryJson);
  175. resultObj = Query(queryJobj);
  176. }
  177. catch (Exception ex)
  178. {
  179. resultObj.Add("code", "500");
  180. resultObj.Add("msg", ex.Message);
  181. }
  182. return resultObj;
  183. }
  184. /// <summary>
  185. /// 单表查询
  186. /// </summary>
  187. /// <param name="queryObj"></param>
  188. /// <param name="nodeName">返回数据的节点名称 默认为 infos</param>
  189. /// <returns></returns>
  190. public JObject QuerySingle(JObject queryObj, string nodeName = "infos")
  191. {
  192. JObject resultObj = new JObject();
  193. resultObj.Add("code", "200");
  194. resultObj.Add("msg", "success");
  195. try
  196. {
  197. int total = 0;
  198. foreach (var item in queryObj)
  199. {
  200. string key = item.Key.Trim();
  201. if (key.EndsWith("[]"))
  202. {
  203. total = QuerySingleList(resultObj, item, nodeName);
  204. }
  205. else if (key.Equals("func"))
  206. {
  207. ExecFunc(resultObj, item);
  208. }
  209. else if (key.Equals("total@"))
  210. {
  211. resultObj.Add("total", total);
  212. }
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. resultObj["code"] = "500";
  218. resultObj["msg"] = ex.Message;
  219. }
  220. return resultObj;
  221. }
  222. /// <summary>
  223. /// 获取查询语句
  224. /// </summary>
  225. /// <param name="queryObj"></param>
  226. /// <returns></returns>
  227. public string ToSql(JObject queryObj)
  228. {
  229. foreach (var item in queryObj)
  230. {
  231. string key = item.Key.Trim();
  232. if (key.EndsWith("[]"))
  233. {
  234. return ToSql(item);
  235. }
  236. }
  237. return string.Empty;
  238. }
  239. /// <summary>
  240. /// 解析并查询
  241. /// </summary>
  242. /// <param name="query"></param>
  243. /// <returns></returns>
  244. public JObject Query(JObject queryObj)
  245. {
  246. JObject resultObj = new JObject();
  247. resultObj.Add("code", "200");
  248. resultObj.Add("msg", "success");
  249. try
  250. {
  251. int total = 0;
  252. foreach (var item in queryObj)
  253. {
  254. string key = item.Key.Trim();
  255. if (key.Equals("[]"))
  256. {
  257. total = QueryMoreList(resultObj, item);
  258. }
  259. else if (key.EndsWith("[]"))
  260. {
  261. total = QuerySingleList(resultObj, item);
  262. }
  263. else if (key.Equals("func"))
  264. {
  265. ExecFunc(resultObj, item);
  266. }
  267. else if (key.Equals("total@"))
  268. {
  269. resultObj.Add("total", total);
  270. }
  271. else
  272. {
  273. var template = GetFirstData(key, item.Value.ToString(), resultObj);
  274. if (template != null)
  275. {
  276. resultObj.Add(key, JToken.FromObject(template));
  277. }
  278. }
  279. }
  280. }
  281. catch (Exception ex)
  282. {
  283. resultObj["code"] = "500";
  284. resultObj["msg"] = ex.Message;
  285. }
  286. return resultObj;
  287. }
  288. //单表查询,返回的数据在指定的NodeName节点
  289. private int QuerySingleList(JObject resultObj, KeyValuePair<string, JToken> item, string nodeName)
  290. {
  291. string key = item.Key.Trim();
  292. var jb = JObject.Parse(item.Value.ToString());
  293. int page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
  294. int count = jb["count"] == null ? 10 : int.Parse(jb["count"].ToString());
  295. int query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
  296. int total = 0;
  297. jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
  298. var htt = new JArray();
  299. foreach (var t in jb)
  300. {
  301. var datas = GetTableData(t.Key, page, count, query, t.Value.ToString(), null);
  302. if (query > 0)
  303. {
  304. total = datas.Item2;
  305. }
  306. foreach (var data in datas.Item1)
  307. {
  308. htt.Add(JToken.FromObject(data));
  309. }
  310. }
  311. if (!string.IsNullOrEmpty(nodeName))
  312. {
  313. resultObj.Add(nodeName, htt);
  314. }
  315. else
  316. resultObj.Add(key, htt);
  317. return total;
  318. }
  319. private string ToSql(KeyValuePair<string, JToken> item)
  320. {
  321. string key = item.Key.Trim();
  322. var jb = JObject.Parse(item.Value.ToString());
  323. int page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
  324. int count = jb["count"] == null ? 10 : int.Parse(jb["count"].ToString());
  325. int query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
  326. jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
  327. var htt = new JArray();
  328. foreach (var t in jb)
  329. {
  330. return ToSql(t.Key, page, count, query, t.Value.ToString());
  331. }
  332. return string.Empty;
  333. }
  334. //单表查询
  335. private int QuerySingleList(JObject resultObj, KeyValuePair<string, JToken> item)
  336. {
  337. string key = item.Key.Trim();
  338. return QuerySingleList(resultObj, item, key);
  339. }
  340. //多列表查询
  341. private int QueryMoreList(JObject resultObj, KeyValuePair<string, JToken> item)
  342. {
  343. int total = 0;
  344. var jb = JObject.Parse(item.Value.ToString());
  345. var page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
  346. var count = jb["count"] == null ? 10 : int.Parse(jb["count"].ToString());
  347. var query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
  348. jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
  349. var htt = new JArray();
  350. List<string> tables = new List<string>(), where = new List<string>();
  351. foreach (var t in jb)
  352. {
  353. tables.Add(t.Key); where.Add(t.Value.ToString());
  354. }
  355. if (tables.Count > 0)
  356. {
  357. string table = tables[0];
  358. var temp = GetTableData(table, page, count, query, where[0], null);
  359. if (query > 0)
  360. {
  361. total = temp.Item2;
  362. }
  363. foreach (var dd in temp.Item1)
  364. {
  365. var zht = new JObject();
  366. zht.Add(table, JToken.FromObject(dd));
  367. for (int i = 1; i < tables.Count; i++)
  368. {
  369. string subtable = tables[i];
  370. if (subtable.EndsWith("[]"))
  371. {
  372. subtable = subtable.TrimEnd("[]".ToCharArray());
  373. var jbb = JObject.Parse(where[i]);
  374. page = jbb["page"] == null ? 0 : int.Parse(jbb["page"].ToString());
  375. count = jbb["count"] == null ? 0 : int.Parse(jbb["count"].ToString());
  376. var lt = new JArray();
  377. foreach (var d in GetTableData(subtable, page, count, query, jbb[subtable].ToString(), zht).Item1)
  378. {
  379. lt.Add(JToken.FromObject(d));
  380. }
  381. zht.Add(tables[i], lt);
  382. }
  383. else
  384. {
  385. var ddf = GetFirstData(subtable, where[i].ToString(), zht);
  386. if (ddf != null)
  387. {
  388. zht.Add(subtable, JToken.FromObject(ddf));
  389. }
  390. }
  391. }
  392. htt.Add(zht);
  393. }
  394. }
  395. if (query != 1)
  396. {
  397. resultObj.Add("[]", htt);
  398. }
  399. return total;
  400. }
  401. private void ExecFunc(JObject resultObj, KeyValuePair<string, JToken> item)
  402. {
  403. JObject jb = JObject.Parse(item.Value.ToString());
  404. Type type = typeof(FuncList);
  405. var dataJObj = new JObject();
  406. foreach (var f in jb)
  407. {
  408. var types = new List<Type>();
  409. var param = new List<object>();
  410. foreach (var va in JArray.Parse(f.Value.ToString()))
  411. {
  412. types.Add(typeof(object));
  413. param.Add(va);
  414. }
  415. dataJObj.Add(f.Key, JToken.FromObject(ExecFunc(f.Key, param.ToArray(), types.ToArray())));
  416. }
  417. resultObj.Add("func", dataJObj);
  418. }
  419. private ISugarQueryable<ExpandoObject> sugarQueryable(string subtable, string selectrole, JObject values, JObject dd)
  420. {
  421. if (!IsTable(subtable))
  422. {
  423. throw new Exception($"表名{subtable}不正确!");
  424. }
  425. var tb = db.Queryable(subtable, "tb");
  426. if (values["@column"].IsValue())
  427. {
  428. ProcessColumn(subtable, selectrole, values, tb);
  429. }
  430. else
  431. {
  432. tb.Select(selectrole);
  433. }
  434. List<IConditionalModel> conModels = new List<IConditionalModel>();
  435. if (values["identity"].IsValue())
  436. {
  437. conModels.Add(new ConditionalModel() { FieldName = values["identity"].ToString(), ConditionalType = ConditionalType.Equal, FieldValue = _identitySvc.GetUserIdentity() });
  438. }
  439. foreach (var va in values)
  440. {
  441. string vakey = va.Key.Trim();
  442. string fieldValue = va.Value.ToString();
  443. if (vakey.EndsWith("$"))//模糊查询
  444. {
  445. FuzzyQuery(subtable, conModels, va);
  446. }
  447. else if (vakey.EndsWith("{}"))//逻辑运算
  448. {
  449. ConditionQuery(subtable, conModels, va);
  450. }
  451. else if (vakey.EndsWith("%"))//bwtween查询
  452. {
  453. ConditionBetween(subtable, conModels, va);
  454. }
  455. else if (vakey.EndsWith("@") && dd != null) // 关联上一个table
  456. {
  457. string[] str = fieldValue.Split('/');
  458. string value = string.Empty;
  459. if (str.Length == 3)
  460. {
  461. value = dd[str[1]][str[2]].ToString();
  462. }
  463. else if (str.Length == 2)
  464. {
  465. value = dd[str[0]][str[1]].ToString();
  466. }
  467. conModels.Add(new ConditionalModel() { FieldName = vakey.TrimEnd('@'), ConditionalType = ConditionalType.Equal, FieldValue = value });
  468. }
  469. else if (IsCol(subtable, vakey)) //其他where条件
  470. {
  471. conModels.Add(new ConditionalModel() { FieldName = vakey, ConditionalType = ConditionalType.Equal, FieldValue = fieldValue });
  472. }
  473. }
  474. tb.Where(conModels);
  475. //排序
  476. ProcessOrder(subtable, values, tb);
  477. //分组
  478. PrccessGroup(subtable, values, tb);
  479. //Having
  480. ProcessHaving(values, tb);
  481. return tb;
  482. }
  483. //处理字段重命名 "@column":"toId:parentId",对应SQL是toId AS parentId,将查询的字段toId变为parentId返回
  484. private void ProcessColumn(string subtable, string selectrole, JObject values, ISugarQueryable<ExpandoObject> tb)
  485. {
  486. var str = new System.Text.StringBuilder(100);
  487. foreach (var item in values["@column"].ToString().Split(','))
  488. {
  489. string[] ziduan = item.Split(':');
  490. string colName = ziduan[0];
  491. var ma = new Regex(@"\((\w+)\)").Match(colName);
  492. //处理max,min这样的函数
  493. if (ma.Success && ma.Groups.Count > 1)
  494. {
  495. colName = ma.Groups[1].Value;
  496. }
  497. //判断列表是否有权限 sum(1),sum(*),Count(1)这样的值直接有效
  498. if (colName == "*" || int.TryParse(colName, out int colNumber) || (IsCol(subtable, colName) && _identitySvc.ColIsRole(colName, selectrole.Split(','))))
  499. {
  500. if (ziduan.Length > 1)
  501. {
  502. if (ziduan[1].Length > 20)
  503. {
  504. throw new Exception("别名不能超过20个字符");
  505. }
  506. str.Append(ziduan[0] + " as " + ReplaceSQLChar(ziduan[1]) + ",");
  507. }
  508. else
  509. str.Append(ziduan[0] + ",");
  510. }
  511. }
  512. if (string.IsNullOrEmpty(str.ToString()))
  513. {
  514. throw new Exception($"表名{subtable}没有可查询的字段!");
  515. }
  516. tb.Select(str.ToString().TrimEnd(','));
  517. }
  518. // "@having":"function0(...)?value0;function1(...)?value1;function2(...)?value2...",
  519. // SQL函数条件,一般和 @group一起用,函数一般在 @column里声明
  520. private void ProcessHaving(JObject values, ISugarQueryable<ExpandoObject> tb)
  521. {
  522. if (values["@having"].IsValue())
  523. {
  524. List<IConditionalModel> hw = new List<IConditionalModel>();
  525. List<string> havingItems = new List<string>();
  526. if (values["@having"].HasValues)
  527. {
  528. havingItems = values["@having"].Select(p => p.ToString()).ToList();
  529. }
  530. else
  531. {
  532. havingItems.Add(values["@having"].ToString());
  533. }
  534. foreach (var item in havingItems)
  535. {
  536. string and = item.ToString();
  537. var model = new ConditionalModel();
  538. if (and.Contains(">="))
  539. {
  540. model.FieldName = and.Split(new string[] { ">=" }, StringSplitOptions.RemoveEmptyEntries)[0];
  541. model.ConditionalType = ConditionalType.GreaterThanOrEqual;
  542. model.FieldValue = and.Split(new string[] { ">=" }, StringSplitOptions.RemoveEmptyEntries)[1];
  543. }
  544. else if (and.Contains("<="))
  545. {
  546. model.FieldName = and.Split(new string[] { "<=" }, StringSplitOptions.RemoveEmptyEntries)[0];
  547. model.ConditionalType = ConditionalType.LessThanOrEqual;
  548. model.FieldValue = and.Split(new string[] { "<=" }, StringSplitOptions.RemoveEmptyEntries)[1];
  549. }
  550. else if (and.Contains(">"))
  551. {
  552. model.FieldName = and.Split(new string[] { ">" }, StringSplitOptions.RemoveEmptyEntries)[0];
  553. model.ConditionalType = ConditionalType.GreaterThan;
  554. model.FieldValue = and.Split(new string[] { ">" }, StringSplitOptions.RemoveEmptyEntries)[1];
  555. }
  556. else if (and.Contains("<"))
  557. {
  558. model.FieldName = and.Split(new string[] { "<" }, StringSplitOptions.RemoveEmptyEntries)[0];
  559. model.ConditionalType = ConditionalType.LessThan;
  560. model.FieldValue = and.Split(new string[] { "<" }, StringSplitOptions.RemoveEmptyEntries)[1];
  561. }
  562. else if (and.Contains("!="))
  563. {
  564. model.FieldName = and.Split(new string[] { "!=" }, StringSplitOptions.RemoveEmptyEntries)[0];
  565. model.ConditionalType = ConditionalType.NoEqual;
  566. model.FieldValue = and.Split(new string[] { "!=" }, StringSplitOptions.RemoveEmptyEntries)[1];
  567. }
  568. else if (and.Contains("="))
  569. {
  570. model.FieldName = and.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[0];
  571. model.ConditionalType = ConditionalType.Equal;
  572. model.FieldValue = and.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[1];
  573. }
  574. hw.Add(model);
  575. }
  576. tb.Having(string.Join(",", havingItems));
  577. }
  578. }
  579. //"@group":"column0,column1...",分组方式。如果 @column里声明了Table的id,则id也必须在 @group中声明;其它情况下必须满足至少一个条件:
  580. //1.分组的key在 @column里声明
  581. //2.Table主键在 @group中声明
  582. private void PrccessGroup(string subtable, JObject values, ISugarQueryable<ExpandoObject> tb)
  583. {
  584. if (values["@group"].IsValue())
  585. {
  586. var str = new System.Text.StringBuilder(100);
  587. foreach (var and in values["@group"].ToString().Split(','))
  588. {
  589. if (IsCol(subtable, and))
  590. {
  591. str.Append(and + ",");
  592. }
  593. }
  594. tb.GroupBy(str.ToString().TrimEnd(','));
  595. }
  596. }
  597. //处理排序 "@order":"name-,id"查询按 name降序、id默认顺序 排序的User数组
  598. private void ProcessOrder(string subtable, JObject values, ISugarQueryable<ExpandoObject> tb)
  599. {
  600. if (values["@order"].IsValue())
  601. {
  602. foreach (var item in values["@order"].ToString().Split(','))
  603. {
  604. string col = item.Replace("-", "").Replace("+", "");
  605. if (IsCol(subtable, col))
  606. {
  607. if (item.EndsWith("-"))
  608. {
  609. tb.OrderBy($"{col} desc");
  610. }
  611. else if (item.EndsWith("+"))
  612. {
  613. tb.OrderBy($"{col} asc");
  614. }
  615. else
  616. {
  617. tb.OrderBy($"{col}");
  618. }
  619. }
  620. }
  621. }
  622. }
  623. //条件查询 "key{}":"条件0,条件1...",条件为任意SQL比较表达式字符串,非Number类型必须用''包含条件的值,如'a'
  624. //&, |, ! 逻辑运算符,对应数据库 SQL 中的 AND, OR, NOT。
  625. // 横或纵与:同一字段的值内条件默认 | 或连接,不同字段的条件默认 & 与连接。
  626. // ① & 可用于"key&{}":"条件"等
  627. // ② | 可用于"key|{}":"条件", "key|{}":[] 等,一般可省略
  628. // ③ ! 可单独使用,如"key!":Object,也可像&,|一样配合其他功能符使用
  629. private void ConditionQuery(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va)
  630. {
  631. string vakey = va.Key.Trim();
  632. string field = vakey.TrimEnd("{}".ToCharArray());
  633. if (va.Value.HasValues)
  634. {
  635. List<string> inValues = new List<string>();
  636. foreach (var cm in va.Value)
  637. {
  638. inValues.Add(cm.ToString());
  639. }
  640. conModels.Add(new ConditionalModel() { FieldName = field, ConditionalType = field.EndsWith("!") ? ConditionalType.NotIn : ConditionalType.In, FieldValue = string.Join(",", inValues) });
  641. }
  642. else
  643. {
  644. var ddt = new List<KeyValuePair<WhereType, ConditionalModel>>();
  645. foreach (var and in va.Value.ToString().Split(','))
  646. {
  647. var model = new ConditionalModel();
  648. model.FieldName = field.TrimEnd("&".ToCharArray());//处理&()的查询方式
  649. if (and.StartsWith(">="))
  650. {
  651. model.ConditionalType = ConditionalType.GreaterThanOrEqual;
  652. model.FieldValue = and.TrimStart(">=".ToCharArray());
  653. }
  654. else if (and.StartsWith("<="))
  655. {
  656. model.ConditionalType = ConditionalType.LessThanOrEqual;
  657. model.FieldValue = and.TrimStart("<=".ToCharArray());
  658. }
  659. else if (and.StartsWith(">"))
  660. {
  661. model.ConditionalType = ConditionalType.GreaterThan;
  662. model.FieldValue = and.TrimStart('>');
  663. }
  664. else if (and.StartsWith("<"))
  665. {
  666. model.ConditionalType = ConditionalType.LessThan;
  667. model.FieldValue = and.TrimStart('<');
  668. }
  669. ddt.Add(new KeyValuePair<WhereType, ConditionalModel>((field.EndsWith("&") ? WhereType.And : WhereType.Or), model));
  670. }
  671. conModels.Add(new ConditionalCollections() { ConditionalList = ddt });
  672. }
  673. }
  674. //"key%":"start,end" => "key%":["start,end"],其中 start 和 end 都只能为 Boolean, Number, String 中的一种,如 "2017-01-01,2019-01-01" ,["1,90000", "82001,100000"] ,可用于连续范围内的筛选
  675. private void ConditionBetween(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va)
  676. {
  677. string vakey = va.Key.Trim();
  678. string field = vakey.TrimEnd("%".ToCharArray());
  679. List<string> inValues = new List<string>();
  680. if (va.Value.HasValues)
  681. {
  682. foreach (var cm in va.Value)
  683. {
  684. inValues.Add(cm.ToString());
  685. }
  686. }
  687. else
  688. {
  689. inValues.Add(va.Value.ToString());
  690. }
  691. for (var i = 0; i < inValues.Count; i++)
  692. {
  693. var fileds = inValues[i].Split(',');
  694. if (fileds.Length == 2)
  695. {
  696. var ddt = new List<KeyValuePair<WhereType, ConditionalModel>>();
  697. var leftCondition = new ConditionalModel()
  698. {
  699. FieldName = field,
  700. ConditionalType = ConditionalType.GreaterThanOrEqual,
  701. FieldValue = fileds[0]
  702. };
  703. ddt.Add(new KeyValuePair<WhereType, ConditionalModel>(i == 0 ? WhereType.And : WhereType.Or, leftCondition));
  704. var rightCondition = new ConditionalModel()
  705. {
  706. FieldName = field,
  707. ConditionalType = ConditionalType.LessThanOrEqual,
  708. FieldValue = fileds[1]
  709. };
  710. ddt.Add(new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, rightCondition));
  711. conModels.Add(new ConditionalCollections() { ConditionalList = ddt });
  712. }
  713. }
  714. }
  715. //模糊搜索 "key$":"SQL搜索表达式" => "key$":["SQL搜索表达式"],任意SQL搜索表达式字符串,如 %key%(包含key), key%(以key开始), %k%e%y%(包含字母k,e,y) 等,%表示任意字符
  716. private void FuzzyQuery(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va)
  717. {
  718. string vakey = va.Key.Trim();
  719. string fieldValue = va.Value.ToString();
  720. var conditionalType = ConditionalType.Like;
  721. if (IsCol(subtable, vakey.TrimEnd('$')))
  722. {
  723. //支持三种like查询
  724. if (fieldValue.StartsWith("%") && fieldValue.EndsWith("%"))
  725. {
  726. conditionalType = ConditionalType.Like;
  727. }
  728. else if (fieldValue.StartsWith("%"))
  729. {
  730. conditionalType = ConditionalType.LikeRight;
  731. }
  732. else if (fieldValue.EndsWith("%"))
  733. {
  734. conditionalType = ConditionalType.LikeLeft;
  735. }
  736. conModels.Add(new ConditionalModel() { FieldName = vakey.TrimEnd('$'), ConditionalType = conditionalType, FieldValue = fieldValue.TrimEnd("%".ToArray()).TrimStart("%".ToArray()) });
  737. }
  738. }
  739. public string ReplaceSQLChar(string str)
  740. {
  741. if (str == String.Empty)
  742. return String.Empty;
  743. str = str.Replace("'", "");
  744. str = str.Replace(";", "");
  745. str = str.Replace(",", "");
  746. str = str.Replace("?", "");
  747. str = str.Replace("<", "");
  748. str = str.Replace(">", "");
  749. str = str.Replace("(", "");
  750. str = str.Replace(")", "");
  751. str = str.Replace("@", "");
  752. str = str.Replace("=", "");
  753. str = str.Replace("+", "");
  754. str = str.Replace("*", "");
  755. str = str.Replace("&", "");
  756. str = str.Replace("#", "");
  757. str = str.Replace("%", "");
  758. str = str.Replace("$", "");
  759. str = str.Replace("\"", "");
  760. //删除与数据库相关的词
  761. str = Regex.Replace(str, "delete from", "", RegexOptions.IgnoreCase);
  762. str = Regex.Replace(str, "drop table", "", RegexOptions.IgnoreCase);
  763. str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
  764. str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase);
  765. str = Regex.Replace(str, "exec master", "", RegexOptions.IgnoreCase);
  766. str = Regex.Replace(str, "net localgroup administrators", "", RegexOptions.IgnoreCase);
  767. str = Regex.Replace(str, "net user", "", RegexOptions.IgnoreCase);
  768. str = Regex.Replace(str, "-", "", RegexOptions.IgnoreCase);
  769. str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
  770. return str;
  771. }
  772. }
  773. }