SelectTable.cs 30 KB

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