SelectTable.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. namespace Admin.NET.Core.Service;
  5. using AspectCore.Extensions.Reflection;
  6. using NewLife.Data;
  7. using Newtonsoft.Json.Linq;
  8. using Newtonsoft.Json.Schema;
  9. using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
  10. using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
  11. using Org.BouncyCastle.Bcpg.OpenPgp;
  12. using SqlSugar;
  13. using StackExchange.Redis;
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Dynamic;
  18. using System.Linq;
  19. using System.Text.RegularExpressions;
  20. using static OfficeOpenXml.ExcelErrorValue;
  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>(null, tb.MergeTable().Count());
  93. else
  94. {
  95. if (page > 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. IList l = tb.ToList();
  107. if (query == 0)
  108. return new Tuple<dynamic, int>(l, 0);
  109. else
  110. return new Tuple<dynamic, int>(l, l.Count);
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// 解析并查询
  116. /// </summary>
  117. /// <param name="query"></param>
  118. /// <returns></returns>
  119. public virtual JObject Query(string queryJson)
  120. {
  121. JObject resultObj = new JObject();
  122. JObject queryJobj = JObject.Parse(queryJson);
  123. resultObj = Query(queryJobj);
  124. return resultObj;
  125. }
  126. /// <summary>
  127. /// 单表查询
  128. /// </summary>
  129. /// <param name="queryObj"></param>
  130. /// <param name="nodeName">返回数据的节点名称 默认为 infos</param>
  131. /// <returns></returns>
  132. public virtual JObject QuerySingle(JObject queryObj, string nodeName = "infos")
  133. {
  134. JObject resultObj = new JObject();
  135. int total = 0;
  136. foreach (var item in queryObj)
  137. {
  138. string key = item.Key.Trim();
  139. if (key.EndsWith("[]"))
  140. {
  141. total = QuerySingleList(resultObj, item, nodeName);
  142. }
  143. else if (key.Equals("func"))
  144. {
  145. ExecFunc(resultObj, item);
  146. }
  147. else if (key.Equals("total@") || key.Equals("total"))
  148. {
  149. resultObj.Add("total", total);
  150. }
  151. }
  152. return resultObj;
  153. }
  154. /// <summary>
  155. /// 获取查询语句
  156. /// </summary>
  157. /// <param name="queryObj"></param>
  158. /// <returns></returns>
  159. public virtual string ToSql(JObject queryObj)
  160. {
  161. foreach (var item in queryObj)
  162. {
  163. string key = item.Key.Trim();
  164. if (key.EndsWith("[]"))
  165. {
  166. return ToSql(item);
  167. }
  168. }
  169. return string.Empty;
  170. }
  171. /// <summary>
  172. /// 解析并查询
  173. /// </summary>
  174. /// <param name="query"></param>
  175. /// <returns></returns>
  176. public virtual JObject Query(JObject queryObj)
  177. {
  178. JObject resultObj = new JObject();
  179. int total;
  180. foreach (var item in queryObj)
  181. {
  182. string key = item.Key.Trim();
  183. if (key.Equals("[]"))
  184. {
  185. total = QueryMoreList(resultObj, item);
  186. resultObj.Add("total", total);//只要是列表查询都自动返回总数
  187. }
  188. else if (key.EndsWith("[]"))
  189. {
  190. total = QuerySingleList(resultObj, item);
  191. }
  192. else if (key.Equals("func"))
  193. {
  194. ExecFunc(resultObj, item);
  195. }
  196. else if (key.Equals("total@") || key.Equals("total"))
  197. {
  198. //resultObj.Add("total", total);
  199. continue;
  200. }
  201. else
  202. {
  203. var template = GetFirstData(key, item.Value.ToString(), resultObj);
  204. if (template != null)
  205. {
  206. resultObj.Add(key, JToken.FromObject(template));
  207. }
  208. }
  209. }
  210. return resultObj;
  211. }
  212. //动态调用方法
  213. private object ExecFunc(string funcname, object[] param, Type[] types)
  214. {
  215. var method = typeof(FuncList).GetMethod(funcname);
  216. var reflector = method.GetReflector();
  217. var result = reflector.Invoke(new FuncList(), param);
  218. return result;
  219. }
  220. //生成sql
  221. private string ToSql(string subtable, int page, int count, int query, string json)
  222. {
  223. JObject values = JObject.Parse(json);
  224. page = values["page"] == null ? page : int.Parse(values["page"].ToString());
  225. count = values["count"] == null ? count : int.Parse(values["count"].ToString());
  226. query = values["query"] == null ? query : int.Parse(values["query"].ToString());
  227. values.Remove("page");
  228. values.Remove("count");
  229. subtable = _tableMapper.GetTableName(subtable);
  230. var tb = SugarQueryable(subtable, "*", values, null);
  231. var sqlObj = tb.Skip((page - 1) * count).Take(10).ToSql();
  232. return sqlObj.Key;
  233. }
  234. //
  235. private dynamic GetFirstData(string subtable, string json, JObject job)
  236. {
  237. var role = _identitySvc.GetSelectRole(subtable);
  238. if (!role.Item1)//没有权限返回异常
  239. {
  240. throw new Exception(role.Item2);
  241. }
  242. string selectrole = role.Item2;
  243. subtable = _tableMapper.GetTableName(subtable);
  244. JObject values = JObject.Parse(json);
  245. values.Remove("page");
  246. values.Remove("count");
  247. //todo *
  248. var tb = SugarQueryable(subtable, selectrole, values, job).First();
  249. var dic = (IDictionary<string, object>)tb;
  250. foreach (var item in values.Properties().Where(it => it.Name.EndsWith("()")))
  251. {
  252. if (item.Value.HasValue())
  253. {
  254. string func = item.Value.ToString().Substring(0, item.Value.ToString().IndexOf("("));
  255. string param = item.Value.ToString().Substring(item.Value.ToString().IndexOf("(") + 1).TrimEnd(')');
  256. var types = new List<Type>();
  257. var paramss = new List<object>();
  258. foreach (var va in param.Split(','))
  259. {
  260. types.Add(typeof(object));
  261. paramss.Add(tb.Where(it => it.Key.Equals(va)).Select(i => i.Value));
  262. }
  263. dic[item.Name] = ExecFunc(func, paramss.ToArray(), types.ToArray());
  264. }
  265. }
  266. return tb;
  267. }
  268. //单表查询,返回的数据在指定的NodeName节点
  269. private int QuerySingleList(JObject resultObj, KeyValuePair<string, JToken> item, string nodeName)
  270. {
  271. string key = item.Key.Trim();
  272. var jb = JObject.Parse(item.Value.ToString());
  273. int page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
  274. int count = jb["count"] == null ? 10 : int.Parse(jb["count"].ToString());
  275. int query = jb["query"] == null ? 2 : int.Parse(jb["query"].ToString());//默认输出数据和数量
  276. int total = 0;
  277. jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
  278. var htt = new JArray();
  279. foreach (var t in jb)
  280. {
  281. var datas = GetTableData(t.Key, page, count, query, t.Value.ToString(), null);
  282. if (query > 0)
  283. {
  284. total = datas.Item2;
  285. }
  286. foreach (var data in datas.Item1)
  287. {
  288. htt.Add(JToken.FromObject(data));
  289. }
  290. }
  291. if (!string.IsNullOrEmpty(nodeName))
  292. {
  293. resultObj.Add(nodeName, htt);
  294. }
  295. else
  296. resultObj.Add(key, htt);
  297. return total;
  298. }
  299. //生成sql
  300. private string ToSql(KeyValuePair<string, JToken> item)
  301. {
  302. string key = item.Key.Trim();
  303. var jb = JObject.Parse(item.Value.ToString());
  304. int page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
  305. int count = jb["count"] == null ? 10 : int.Parse(jb["count"].ToString());
  306. int query = jb["query"] == null ? 2 : int.Parse(jb["query"].ToString());//默认输出数据和数量
  307. jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
  308. var htt = new JArray();
  309. foreach (var t in jb)
  310. {
  311. return ToSql(t.Key, page, count, query, t.Value.ToString());
  312. }
  313. return string.Empty;
  314. }
  315. //单表查询
  316. private int QuerySingleList(JObject resultObj, KeyValuePair<string, JToken> item)
  317. {
  318. string key = item.Key.Trim();
  319. return QuerySingleList(resultObj, item, key);
  320. }
  321. //多列表查询
  322. private int QueryMoreList(JObject resultObj, KeyValuePair<string, JToken> item)
  323. {
  324. int total = 0;
  325. var jb = JObject.Parse(item.Value.ToString());
  326. var page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
  327. var count = jb["count"] == null ? 10 : int.Parse(jb["count"].ToString());
  328. var query = jb["query"] == null ? 2 : int.Parse(jb["query"].ToString());//默认输出数据和数量
  329. jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
  330. var htt = new JArray();
  331. List<string> tables = new List<string>(), where = new List<string>();
  332. foreach (var t in jb)
  333. {
  334. tables.Add(t.Key); where.Add(t.Value.ToString());
  335. }
  336. if (tables.Count > 0)
  337. {
  338. string table = tables[0];
  339. var temp = GetTableData(table, page, count, query, where[0], null);
  340. if (query > 0)
  341. {
  342. total = temp.Item2;
  343. }
  344. foreach (var dd in temp.Item1)
  345. {
  346. var zht = new JObject();
  347. zht.Add(table, JToken.FromObject(dd));
  348. for (int i = 1; i < tables.Count; i++)
  349. {
  350. string subtable = tables[i];
  351. if (subtable.EndsWith("[]"))
  352. {
  353. subtable = subtable.TrimEnd("[]".ToCharArray());
  354. var jbb = JObject.Parse(where[i]);
  355. page = jbb["page"] == null ? 0 : int.Parse(jbb["page"].ToString());
  356. count = jbb["count"] == null ? 0 : int.Parse(jbb["count"].ToString());
  357. var lt = new JArray();
  358. foreach (var d in GetTableData(subtable, page, count, query, jbb[subtable].ToString(), zht).Item1)
  359. {
  360. lt.Add(JToken.FromObject(d));
  361. }
  362. zht.Add(tables[i], lt);
  363. }
  364. else
  365. {
  366. var ddf = GetFirstData(subtable, where[i].ToString(), zht);
  367. if (ddf != null)
  368. {
  369. zht.Add(subtable, JToken.FromObject(ddf));
  370. }
  371. }
  372. }
  373. htt.Add(zht);
  374. }
  375. }
  376. if (query != 1)
  377. {
  378. resultObj.Add("[]", htt);
  379. }
  380. //分页自动添加当前页数和数量
  381. if (page > 0 && count > 0)
  382. {
  383. resultObj.Add("page", page);
  384. resultObj.Add("count", count);
  385. resultObj.Add("max", total / count + 1);
  386. }
  387. return total;
  388. }
  389. //执行方法
  390. private void ExecFunc(JObject resultObj, KeyValuePair<string, JToken> item)
  391. {
  392. JObject jb = JObject.Parse(item.Value.ToString());
  393. Type type = typeof(FuncList);
  394. var dataJObj = new JObject();
  395. foreach (var f in jb)
  396. {
  397. var types = new List<Type>();
  398. var param = new List<object>();
  399. foreach (var va in JArray.Parse(f.Value.ToString()))
  400. {
  401. types.Add(typeof(object));
  402. param.Add(va);
  403. }
  404. dataJObj.Add(f.Key, JToken.FromObject(ExecFunc(f.Key, param.ToArray(), types.ToArray())));
  405. }
  406. resultObj.Add("func", dataJObj);
  407. }
  408. /// <summary>
  409. /// 构造查询过程
  410. /// </summary>
  411. /// <param name="subtable"></param>
  412. /// <param name="selectrole"></param>
  413. /// <param name="values"></param>
  414. /// <param name="dd"></param>
  415. /// <returns></returns>
  416. private ISugarQueryable<ExpandoObject> SugarQueryable(string subtable, string selectrole, JObject values, JObject dd)
  417. {
  418. IsTable(subtable);
  419. var tb = _db.Queryable(subtable, "tb");
  420. //select
  421. if (values["@column"].HasValue())
  422. {
  423. ProcessColumn(subtable, selectrole, values, tb);
  424. }
  425. else
  426. {
  427. tb.Select(selectrole);
  428. }
  429. //前几行
  430. ProcessLimit(values, tb);
  431. //where
  432. ProcessWhere(subtable, values, tb, dd);
  433. //排序
  434. ProcessOrder(subtable, values, tb);
  435. //分组
  436. PrccessGroup(subtable, values, tb);
  437. //Having
  438. ProcessHaving(values, tb);
  439. return tb;
  440. }
  441. //处理字段重命名 "@column":"toId:parentId",对应SQL是toId AS parentId,将查询的字段toId变为parentId返回
  442. private void ProcessColumn(string subtable, string selectrole, JObject values, ISugarQueryable<ExpandoObject> tb)
  443. {
  444. var str = new System.Text.StringBuilder(100);
  445. foreach (var item in values["@column"].ToString().Split(','))
  446. {
  447. string[] ziduan = item.Split(':');
  448. string colName = ziduan[0];
  449. var ma = new Regex(@"\((\w+)\)").Match(colName);
  450. //处理max,min这样的函数
  451. if (ma.Success && ma.Groups.Count > 1)
  452. {
  453. colName = ma.Groups[1].Value;
  454. }
  455. //判断列表是否有权限 sum(1),sum(*),Count(1)这样的值直接有效
  456. if (colName == "*" || int.TryParse(colName, out int colNumber) || (IsCol(subtable, colName) && _identitySvc.ColIsRole(colName, selectrole.Split(','))))
  457. {
  458. if (ziduan.Length > 1)
  459. {
  460. if (ziduan[1].Length > 20)
  461. {
  462. throw new Exception("别名不能超过20个字符");
  463. }
  464. str.Append(ziduan[0] + " as `" + ReplaceSQLChar(ziduan[1]) + "`,");
  465. }
  466. //不对函数加``,解决sum(*),Count(1)等不能使用的问题
  467. else if (ziduan[0].Contains('('))
  468. {
  469. str.Append(ziduan[0] + ",");
  470. }
  471. else
  472. str.Append("`" + ziduan[0] + "`" + ",");
  473. }
  474. }
  475. if (string.IsNullOrEmpty(str.ToString()))
  476. {
  477. throw new Exception($"表名{subtable}没有可查询的字段!");
  478. }
  479. tb.Select(str.ToString().TrimEnd(','));
  480. }
  481. /// <summary>
  482. /// 构造查询条件 where
  483. /// </summary>
  484. /// <param name="subtable"></param>
  485. /// <param name="values"></param>
  486. /// <param name="tb"></param>
  487. /// <param name="dd"></param>
  488. private void ProcessWhere(string subtable, JObject values, ISugarQueryable<ExpandoObject> tb, JObject dd)
  489. {
  490. List<IConditionalModel> conModels = new List<IConditionalModel>();
  491. if (values["identity"].HasValue())
  492. {
  493. conModels.Add(new ConditionalModel() { FieldName = values["identity"].ToString(), ConditionalType = ConditionalType.Equal, FieldValue = _identitySvc.GetUserIdentity() });
  494. }
  495. foreach (var va in values)
  496. {
  497. string key = va.Key.Trim();
  498. string fieldValue = va.Value.ToString();
  499. if (key.StartsWith("@"))
  500. {
  501. continue;
  502. }
  503. if (key.EndsWith("$"))//模糊查询
  504. {
  505. FuzzyQuery(subtable, conModels, va);
  506. }
  507. else if (key.EndsWith("{}"))//逻辑运算
  508. {
  509. ConditionQuery(subtable, conModels, va);
  510. }
  511. else if (key.EndsWith("%"))//bwtween查询
  512. {
  513. ConditionBetween(subtable, conModels, va,tb);
  514. }
  515. else if (key.EndsWith("@") && dd != null) // 关联上一个table
  516. {
  517. string[] str = fieldValue.Split('/', StringSplitOptions.RemoveEmptyEntries);
  518. string value = string.Empty;
  519. if (str.Length == 3)
  520. {
  521. value = dd[str[1]][str[2]].ToString();
  522. }
  523. else if (str.Length == 2)
  524. {
  525. value = dd[str[0]][str[1]].ToString();
  526. }
  527. conModels.Add(new ConditionalModel() { FieldName = key.TrimEnd('@'), ConditionalType = ConditionalType.Equal, FieldValue = value });
  528. }
  529. else if (key.EndsWith("~"))//不等于
  530. {
  531. conModels.Add(new ConditionalModel() { FieldName = key.TrimEnd('~'), ConditionalType = ConditionalType.NoEqual, FieldValue = fieldValue });
  532. }
  533. else if (IsCol(subtable, key)) //其他where条件
  534. {
  535. if (string.IsNullOrEmpty(fieldValue))
  536. {
  537. conModels.Add(new ConditionalModel() { FieldName = key, ConditionalType = ConditionalType.IsNullOrEmpty });//增加null、""匹配
  538. }
  539. else
  540. {
  541. conModels.Add(new ConditionalModel() { FieldName = key, ConditionalType = ConditionalType.Equal, FieldValue = fieldValue });
  542. }
  543. }
  544. }
  545. if (conModels.Any())
  546. {
  547. tb.Where(conModels);
  548. }
  549. }
  550. // "@having":"function0(...)?value0;function1(...)?value1;function2(...)?value2...",
  551. // SQL函数条件,一般和 @group一起用,函数一般在 @column里声明
  552. private void ProcessHaving(JObject values, ISugarQueryable<ExpandoObject> tb)
  553. {
  554. if (values["@having"].HasValue())
  555. {
  556. List<IConditionalModel> hw = new List<IConditionalModel>();
  557. List<string> havingItems = new List<string>();
  558. if (values["@having"].HasValues)
  559. {
  560. havingItems = values["@having"].Select(p => p.ToString()).ToList();
  561. }
  562. else
  563. {
  564. havingItems.Add(values["@having"].ToString());
  565. }
  566. foreach (var item in havingItems)
  567. {
  568. string and = item.ToString();
  569. var model = new ConditionalModel();
  570. if (and.Contains(">="))
  571. {
  572. model.FieldName = and.Split(new string[] { ">=" }, StringSplitOptions.RemoveEmptyEntries)[0];
  573. model.ConditionalType = ConditionalType.GreaterThanOrEqual;
  574. model.FieldValue = and.Split(new string[] { ">=" }, StringSplitOptions.RemoveEmptyEntries)[1];
  575. }
  576. else if (and.Contains("<="))
  577. {
  578. model.FieldName = and.Split(new string[] { "<=" }, StringSplitOptions.RemoveEmptyEntries)[0];
  579. model.ConditionalType = ConditionalType.LessThanOrEqual;
  580. model.FieldValue = and.Split(new string[] { "<=" }, StringSplitOptions.RemoveEmptyEntries)[1];
  581. }
  582. else if (and.Contains(">"))
  583. {
  584. model.FieldName = and.Split(new string[] { ">" }, StringSplitOptions.RemoveEmptyEntries)[0];
  585. model.ConditionalType = ConditionalType.GreaterThan;
  586. model.FieldValue = and.Split(new string[] { ">" }, StringSplitOptions.RemoveEmptyEntries)[1];
  587. }
  588. else if (and.Contains("<"))
  589. {
  590. model.FieldName = and.Split(new string[] { "<" }, StringSplitOptions.RemoveEmptyEntries)[0];
  591. model.ConditionalType = ConditionalType.LessThan;
  592. model.FieldValue = and.Split(new string[] { "<" }, StringSplitOptions.RemoveEmptyEntries)[1];
  593. }
  594. else if (and.Contains("!="))
  595. {
  596. model.FieldName = and.Split(new string[] { "!=" }, StringSplitOptions.RemoveEmptyEntries)[0];
  597. model.ConditionalType = ConditionalType.NoEqual;
  598. model.FieldValue = and.Split(new string[] { "!=" }, StringSplitOptions.RemoveEmptyEntries)[1];
  599. }
  600. else if (and.Contains("="))
  601. {
  602. model.FieldName = and.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[0];
  603. model.ConditionalType = ConditionalType.Equal;
  604. model.FieldValue = and.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries)[1];
  605. }
  606. hw.Add(model);
  607. }
  608. //var d = db.Context.Utilities.ConditionalModelToSql(hw);
  609. //tb.Having(d.Key, d.Value);
  610. tb.Having(string.Join(",", havingItems));
  611. }
  612. }
  613. //"@group":"column0,column1...",分组方式。如果 @column里声明了Table的id,则id也必须在 @group中声明;其它情况下必须满足至少一个条件:
  614. //1.分组的key在 @column里声明
  615. //2.Table主键在 @group中声明
  616. private void PrccessGroup(string subtable, JObject values, ISugarQueryable<ExpandoObject> tb)
  617. {
  618. if (values["@group"].HasValue())
  619. {
  620. List<GroupByModel> groupList = new();//多库兼容写法
  621. foreach (var col in values["@group"].ToString().Split(','))
  622. {
  623. if (IsCol(subtable, col))
  624. {
  625. //str.Append(and + ",");
  626. groupList.Add(new GroupByModel() { FieldName = col });
  627. }
  628. }
  629. if (groupList.Any())
  630. {
  631. tb.GroupBy(groupList);
  632. }
  633. }
  634. }
  635. //处理排序 "@order":"name-,id"查询按 name降序、id默认顺序 排序的User数组
  636. private void ProcessOrder(string subtable, JObject values, ISugarQueryable<ExpandoObject> tb)
  637. {
  638. if (values["@order"].HasValue())
  639. {
  640. List<OrderByModel> orderList = new(); //多库兼容写法
  641. foreach (var item in values["@order"].ToString().Split(','))
  642. {
  643. string col = item.Replace("-", "").Replace("+", "").Replace(" desc", "").Replace(" asc", "");//增加对原生排序的支持
  644. if (IsCol(subtable, col))
  645. {
  646. orderList.Add(new OrderByModel()
  647. {
  648. FieldName = col,
  649. OrderByType = item.EndsWith("-") || item.EndsWith(" desc") ? OrderByType.Desc : OrderByType.Asc
  650. });
  651. }
  652. }
  653. if (orderList.Any())
  654. {
  655. tb.OrderBy(orderList);
  656. }
  657. }
  658. }
  659. /// <summary>
  660. /// 表内参数"@count"(int):查询前几行,不能同时使用count和@count函数
  661. /// </summary>
  662. /// <param name="values"></param>
  663. /// <param name="tb"></param>
  664. private void ProcessLimit(JObject values, ISugarQueryable<ExpandoObject> tb)
  665. {
  666. if (values["@count"].HasValue())
  667. {
  668. int c = values["@count"].ToObject<int>();
  669. tb.Take(c);
  670. }
  671. }
  672. //条件查询 "key{}":"条件0,条件1...",条件为任意SQL比较表达式字符串,非Number类型必须用''包含条件的值,如'a'
  673. //&, |, ! 逻辑运算符,对应数据库 SQL 中的 AND, OR, NOT。
  674. // 横或纵与:同一字段的值内条件默认 | 或连接,不同字段的条件默认 & 与连接。
  675. // ① & 可用于"key&{}":"条件"等
  676. // ② | 可用于"key|{}":"条件", "key|{}":[] 等,一般可省略
  677. // ③ ! 可单独使用,如"key!":Object,也可像&,|一样配合其他功能符使用
  678. private void ConditionQuery(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va)
  679. {
  680. string vakey = va.Key.Trim();
  681. string field = vakey.TrimEnd("{}".ToCharArray());
  682. if (va.Value.HasValues)
  683. {
  684. List<string> inValues = new List<string>();
  685. foreach (var cm in va.Value)
  686. {
  687. inValues.Add(cm.ToString());
  688. }
  689. conModels.Add(new ConditionalModel()
  690. {
  691. FieldName = field.TrimEnd("!".ToCharArray()),
  692. ConditionalType = field.EndsWith("!") ? ConditionalType.NotIn : ConditionalType.In,
  693. FieldValue = string.Join(",", inValues)
  694. });
  695. }
  696. else
  697. {
  698. var ddt = new List<KeyValuePair<WhereType, ConditionalModel>>();
  699. foreach (var and in va.Value.ToString().Split(','))
  700. {
  701. var model = new ConditionalModel();
  702. model.FieldName = field.TrimEnd("&".ToCharArray());//处理&()的查询方式
  703. if (and.StartsWith(">="))
  704. {
  705. model.ConditionalType = ConditionalType.GreaterThanOrEqual;
  706. model.FieldValue = and.TrimStart(">=".ToCharArray());
  707. }
  708. else if (and.StartsWith("<="))
  709. {
  710. model.ConditionalType = ConditionalType.LessThanOrEqual;
  711. model.FieldValue = and.TrimStart("<=".ToCharArray());
  712. }
  713. else if (and.StartsWith(">"))
  714. {
  715. model.ConditionalType = ConditionalType.GreaterThan;
  716. model.FieldValue = and.TrimStart('>');
  717. }
  718. else if (and.StartsWith("<"))
  719. {
  720. model.ConditionalType = ConditionalType.LessThan;
  721. model.FieldValue = and.TrimStart('<');
  722. }
  723. ddt.Add(new KeyValuePair<WhereType, ConditionalModel>((field.EndsWith("&") ? WhereType.And : WhereType.Or), model));
  724. }
  725. conModels.Add(new ConditionalCollections() { ConditionalList = ddt });
  726. }
  727. }
  728. /// <summary>
  729. /// "key%":"start,end" => "key%":["start,end"],其中 start 和 end 都只能为 Boolean, Number, String 中的一种,如 "2017-01-01,2019-01-01" ,["1,90000", "82001,100000"] ,可用于连续范围内的筛选
  730. /// 目前不支持数组形式
  731. /// </summary>
  732. /// <param name="subtable"></param>
  733. /// <param name="conModels"></param>
  734. /// <param name="va"></param>
  735. /// <param name="tb"></param>
  736. private void ConditionBetween(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va, ISugarQueryable<ExpandoObject> tb)
  737. {
  738. string vakey = va.Key.Trim();
  739. string field = vakey.TrimEnd("%".ToCharArray());
  740. List<string> inValues = new List<string>();
  741. if (va.Value.HasValues)
  742. {
  743. foreach (var cm in va.Value)
  744. {
  745. inValues.Add(cm.ToString());
  746. }
  747. }
  748. else
  749. {
  750. inValues.Add(va.Value.ToString());
  751. }
  752. for (var i = 0; i < inValues.Count; i++)
  753. {
  754. var fileds = inValues[i].Split(',');
  755. if (fileds.Length == 2)
  756. {
  757. ObjectFuncModel f;
  758. if (DateTime.TryParse(fileds[0], out _))//要么日期型,要么数值型
  759. {
  760. f = ObjectFuncModel.Create("between", field, "{Datetime}:" + fileds[0], "{Datetime}:" + fileds[1]);
  761. }
  762. else
  763. {
  764. f = ObjectFuncModel.Create("between", field, "{Decimal}:" + fileds[0], "{Decimal}:" + fileds[1],"|","{int}:40","{int}:50");
  765. }
  766. tb.Where(f);
  767. }
  768. }
  769. }
  770. //模糊搜索 "key$":"SQL搜索表达式" => "key$":["SQL搜索表达式"],任意SQL搜索表达式字符串,如 %key%(包含key), key%(以key开始), %k%e%y%(包含字母k,e,y) 等,%表示任意字符
  771. private void FuzzyQuery(string subtable, List<IConditionalModel> conModels, KeyValuePair<string, JToken> va)
  772. {
  773. string vakey = va.Key.Trim();
  774. string fieldValue = va.Value.ToString();
  775. var conditionalType = ConditionalType.Like;
  776. if (IsCol(subtable, vakey.TrimEnd('$')))
  777. {
  778. //支持三种like查询
  779. if (fieldValue.StartsWith("%") && fieldValue.EndsWith("%"))
  780. {
  781. conditionalType = ConditionalType.Like;
  782. }
  783. else if (fieldValue.StartsWith("%"))
  784. {
  785. conditionalType = ConditionalType.LikeRight;
  786. }
  787. else if (fieldValue.EndsWith("%"))
  788. {
  789. conditionalType = ConditionalType.LikeLeft;
  790. }
  791. conModels.Add(new ConditionalModel() { FieldName = vakey.TrimEnd('$'), ConditionalType = conditionalType, FieldValue = fieldValue.TrimEnd("%".ToArray()).TrimStart("%".ToArray()) });
  792. }
  793. }
  794. //处理sql注入
  795. private string ReplaceSQLChar(string str)
  796. {
  797. if (str == String.Empty)
  798. return String.Empty;
  799. str = str.Replace("'", "");
  800. str = str.Replace(";", "");
  801. str = str.Replace(",", "");
  802. str = str.Replace("?", "");
  803. str = str.Replace("<", "");
  804. str = str.Replace(">", "");
  805. str = str.Replace("(", "");
  806. str = str.Replace(")", "");
  807. str = str.Replace("@", "");
  808. str = str.Replace("=", "");
  809. str = str.Replace("+", "");
  810. str = str.Replace("*", "");
  811. str = str.Replace("&", "");
  812. str = str.Replace("#", "");
  813. str = str.Replace("%", "");
  814. str = str.Replace("$", "");
  815. str = str.Replace("\"", "");
  816. //删除与数据库相关的词
  817. str = Regex.Replace(str, "delete from", "", RegexOptions.IgnoreCase);
  818. str = Regex.Replace(str, "drop table", "", RegexOptions.IgnoreCase);
  819. str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
  820. str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase);
  821. str = Regex.Replace(str, "exec master", "", RegexOptions.IgnoreCase);
  822. str = Regex.Replace(str, "net localgroup administrators", "", RegexOptions.IgnoreCase);
  823. str = Regex.Replace(str, "net user", "", RegexOptions.IgnoreCase);
  824. str = Regex.Replace(str, "-", "", RegexOptions.IgnoreCase);
  825. str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
  826. return str;
  827. }
  828. /// <summary>
  829. /// 单条插入
  830. /// </summary>
  831. /// <param name="tableName"></param>
  832. /// <param name="cols"></param>
  833. /// <param name="role"></param>
  834. /// <returns>(各种类型的)id</returns>
  835. public object InsertSingle(string tableName, JObject cols, APIJSON_Role role = null)
  836. {
  837. role ??= _identitySvc.GetRole();
  838. var dt = new Dictionary<string, object>();
  839. foreach (var f in cols)//遍历字段
  840. {
  841. if (//f.Key.ToLower() != "id" && //是否一定要传id
  842. IsCol(tableName, f.Key) &&
  843. (role.Insert.Column.Contains("*") || role.Insert.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
  844. dt.Add(f.Key, FuncList.TransJObjectToSugarPara(f.Value));
  845. }
  846. //如果外部没传id,就后端生成或使用数据库默认值,如果都没有会出错
  847. object id;
  848. if (!dt.ContainsKey("id"))
  849. {
  850. id = YitIdHelper.NextId();//自己生成id的方法,可以由外部传入
  851. dt.Add("id", id);
  852. }
  853. else
  854. {
  855. id = dt["id"];
  856. }
  857. _db.Insertable(dt).AS(tableName).ExecuteCommand();//根据主键类型设置返回雪花或自增,目前返回条数
  858. return id;
  859. }
  860. /// <summary>
  861. /// 为每天记录创建udpate sql
  862. /// </summary>
  863. /// <param name="tableName"></param>
  864. /// <param name="record"></param>
  865. /// <param name="role"></param>
  866. /// <returns></returns>
  867. public int UpdateSingleRecord(string tableName, JObject record, APIJSON_Role role = null)
  868. {
  869. role ??= _identitySvc.GetRole();
  870. if (!record.ContainsKey("id"))
  871. {
  872. throw Oops.Bah("未传主键id");
  873. }
  874. var dt = new Dictionary<string, object>();
  875. var sb = new StringBuilder(100);
  876. object id = null;
  877. foreach (var f in record)//遍历每个字段
  878. {
  879. if (f.Key.Equals("id", StringComparison.OrdinalIgnoreCase))
  880. {
  881. if (f.Value is JArray)//id数组
  882. {
  883. sb.Append($"{f.Key} in (@{f.Key})");
  884. id = FuncList.TransJArrayToSugarPara(f.Value);
  885. }
  886. else//单个id
  887. {
  888. sb.Append($"{f.Key}=@{f.Key}");
  889. id = FuncList.TransJObjectToSugarPara(f.Value);
  890. }
  891. }
  892. else if (IsCol(tableName, f.Key) && (role.Update.Column.Contains("*") || role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
  893. {
  894. dt.Add(f.Key, FuncList.TransJObjectToSugarPara(f.Value));
  895. }
  896. }
  897. string whereSql = sb.ToString();
  898. int count = _db.Updateable(dt).AS(tableName).Where(whereSql, new { id }).ExecuteCommand();
  899. return count;
  900. }
  901. /// <summary>
  902. /// 更新单表,支持同表多条记录
  903. /// </summary>
  904. /// <param name="tableName"></param>
  905. /// <param name="records"></param>
  906. /// <param name="role"></param>
  907. /// <returns></returns>
  908. public int UpdateSingleTable(string tableName, JToken records, APIJSON_Role role = null)
  909. {
  910. role ??= _identitySvc.GetRole();
  911. int count = 0;
  912. if (records is JArray)//遍历每行记录
  913. {
  914. foreach (var record in records.ToObject<JObject[]>())
  915. {
  916. count += UpdateSingleRecord(tableName, record, role);
  917. }
  918. }
  919. else//单条记录
  920. {
  921. count = UpdateSingleRecord(tableName, records.ToObject<JObject>(), role);
  922. }
  923. return count;
  924. }
  925. }