APIJSONService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core.Service;
  7. /// <summary>
  8. /// APIJSON服务 🧩
  9. /// </summary>
  10. [ApiDescriptionSettings(Order = 100)]
  11. public class APIJSONService : IDynamicApiController, ITransient
  12. {
  13. private readonly ISqlSugarClient _db;
  14. private readonly IdentityService _identityService;
  15. private readonly TableMapper _tableMapper;
  16. private readonly SelectTable _selectTable;
  17. public APIJSONService(ISqlSugarClient db,
  18. IdentityService identityService,
  19. TableMapper tableMapper)
  20. {
  21. _db = db;
  22. _tableMapper = tableMapper;
  23. _identityService = identityService;
  24. _selectTable = new SelectTable(_identityService, _tableMapper, _db);
  25. }
  26. /// <summary>
  27. /// 统一查询入口 🔖
  28. /// </summary>
  29. /// <param name="jobject"></param>
  30. /// <remarks>参数:{"[]":{"SYSLOGOP":{}}}</remarks>
  31. /// <returns></returns>
  32. [HttpPost("get")]
  33. [DisplayName("APIJSON统一查询")]
  34. public JObject Query([FromBody] JObject jobject)
  35. {
  36. var database = jobject["@database"]?.ToString();
  37. if (!string.IsNullOrEmpty(database))
  38. {
  39. // 设置数据库
  40. var provider = _db.AsTenant().GetConnectionScope(database);
  41. jobject.Remove("@database");
  42. return new SelectTable(_identityService, _tableMapper, provider).Query(jobject);
  43. }
  44. return _selectTable.Query(jobject);
  45. }
  46. /// <summary>
  47. /// 查询 🔖
  48. /// </summary>
  49. /// <param name="table"></param>
  50. /// <param name="jobject"></param>
  51. /// <returns></returns>
  52. [HttpPost("get/{table}")]
  53. [DisplayName("APIJSON查询")]
  54. public JObject QueryByTable([FromRoute] string table, [FromBody] JObject jobject)
  55. {
  56. var ht = new JObject
  57. {
  58. { table + "[]", jobject }
  59. };
  60. // 自动添加总计数量
  61. if (jobject["query"] != null && jobject["query"].ToString() != "0" && jobject["total@"] == null)
  62. ht.Add("total@", "");
  63. // 每页最大1000条数据
  64. if (jobject["count"] != null && int.Parse(jobject["count"].ToString()) > 1000)
  65. throw Oops.Bah("count分页数量最大不能超过1000");
  66. jobject.Remove("@debug");
  67. var hasTableKey = false;
  68. var ignoreConditions = new List<string> { "page", "count", "query" };
  69. var tableConditions = new JObject(); // 表的其它查询条件,比如过滤、字段等
  70. foreach (var item in jobject)
  71. {
  72. if (item.Key.Equals(table, StringComparison.CurrentCultureIgnoreCase))
  73. {
  74. hasTableKey = true;
  75. break;
  76. }
  77. if (!ignoreConditions.Contains(item.Key.ToLower()))
  78. tableConditions.Add(item.Key, item.Value);
  79. }
  80. foreach (var removeKey in tableConditions)
  81. {
  82. jobject.Remove(removeKey.Key);
  83. }
  84. if (!hasTableKey)
  85. jobject.Add(table, tableConditions);
  86. return Query(ht);
  87. }
  88. /// <summary>
  89. /// 新增 🔖
  90. /// </summary>
  91. /// <param name="tables">表对象或数组,若没有传Id则后端生成Id</param>
  92. /// <returns></returns>
  93. [HttpPost("add")]
  94. [DisplayName("APIJSON新增")]
  95. [UnitOfWork]
  96. public JObject Add([FromBody] JObject tables)
  97. {
  98. var ht = new JObject();
  99. foreach (var table in tables)
  100. {
  101. var talbeName = table.Key.Trim();
  102. var role = _identityService.GetRole();
  103. if (!role.Insert.Table.Contains(talbeName, StringComparer.CurrentCultureIgnoreCase))
  104. throw Oops.Bah($"没权限添加{talbeName}");
  105. JToken result;
  106. // 批量插入
  107. if (table.Value is JArray)
  108. {
  109. var ids = new List<object>();
  110. foreach (var record in table.Value)
  111. {
  112. var cols = record.ToObject<JObject>();
  113. var id = _selectTable.InsertSingle(talbeName, cols, role);
  114. ids.Add(id);
  115. }
  116. result = JToken.FromObject(new { id = ids, count = ids.Count });
  117. }
  118. // 单条插入
  119. else
  120. {
  121. var cols = table.Value.ToObject<JObject>();
  122. var id = _selectTable.InsertSingle(talbeName, cols, role);
  123. result = JToken.FromObject(new { id });
  124. }
  125. ht.Add(talbeName, result);
  126. }
  127. return ht;
  128. }
  129. /// <summary>
  130. /// 更新(只支持Id作为条件) 🔖
  131. /// </summary>
  132. /// <param name="tables">支持多表、多Id批量更新</param>
  133. /// <returns></returns>
  134. [HttpPost("update")]
  135. [DisplayName("APIJSON更新")]
  136. [UnitOfWork]
  137. public JObject Edit([FromBody] JObject tables)
  138. {
  139. var ht = new JObject();
  140. foreach (var table in tables)
  141. {
  142. var tableName = table.Key.Trim();
  143. var role = _identityService.GetRole();
  144. var count = _selectTable.UpdateSingleTable(tableName, table.Value, role);
  145. ht.Add(tableName, JToken.FromObject(new { count }));
  146. }
  147. return ht;
  148. }
  149. /// <summary>
  150. /// 删除(支持非Id条件、支持批量) 🔖
  151. /// </summary>
  152. /// <param name="tables"></param>
  153. /// <returns></returns>
  154. [HttpPost("delete")]
  155. [DisplayName("APIJSON删除")]
  156. [UnitOfWork]
  157. public JObject Delete([FromBody] JObject tables)
  158. {
  159. var ht = new JObject();
  160. var role = _identityService.GetRole();
  161. foreach (var table in tables)
  162. {
  163. var talbeName = table.Key.Trim();
  164. if (role.Delete == null || role.Delete.Table == null)
  165. throw Oops.Bah("delete权限未配置");
  166. if (!role.Delete.Table.Contains(talbeName, StringComparer.CurrentCultureIgnoreCase))
  167. throw Oops.Bah($"没权限删除{talbeName}");
  168. //if (!value.ContainsKey("id"))
  169. // throw Oops.Bah("未传主键id");
  170. var value = JObject.Parse(table.Value.ToString());
  171. var sb = new StringBuilder(100);
  172. var parameters = new List<SugarParameter>();
  173. foreach (var f in value)
  174. {
  175. if (f.Value is JArray)
  176. {
  177. sb.Append($"{f.Key} in (@{f.Key}) and ");
  178. var paraArray = FuncList.TransJArrayToSugarPara(f.Value);
  179. parameters.Add(new SugarParameter($"@{f.Key}", paraArray));
  180. }
  181. else
  182. {
  183. sb.Append($"{f.Key}=@{f.Key} and ");
  184. parameters.Add(new SugarParameter($"@{f.Key}", FuncList.TransJObjectToSugarPara(f.Value)));
  185. }
  186. }
  187. if (!parameters.Any())
  188. throw Oops.Bah("请输入删除条件");
  189. var whereSql = sb.ToString().TrimEnd(" and ");
  190. var count = _db.Deleteable<object>().AS(talbeName).Where(whereSql, parameters).ExecuteCommand(); // 无实体删除
  191. value.Add("count", count); // 命中数量
  192. ht.Add(talbeName, value);
  193. }
  194. return ht;
  195. }
  196. }