SelectTable.cs 33 KB

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