APIJSONService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. return _selectTable.Query(jobject);
  37. }
  38. /// <summary>
  39. /// 查询 🔖
  40. /// </summary>
  41. /// <param name="table"></param>
  42. /// <param name="jobject"></param>
  43. /// <returns></returns>
  44. [HttpPost("get/{table}")]
  45. [DisplayName("APIJSON查询")]
  46. public JObject QueryByTable([FromRoute] string table, [FromBody] JObject jobject)
  47. {
  48. var ht = new JObject
  49. {
  50. { table + "[]", jobject }
  51. };
  52. // 自动添加总计数量
  53. if (jobject["query"] != null && jobject["query"].ToString() != "0" && jobject["total@"] == null)
  54. ht.Add("total@", "");
  55. // 每页最大1000条数据
  56. if (jobject["count"] != null && int.Parse(jobject["count"].ToString()) > 1000)
  57. throw Oops.Bah("count分页数量最大不能超过1000");
  58. jobject.Remove("@debug");
  59. var hasTableKey = false;
  60. var ignoreConditions = new List<string> { "page", "count", "query" };
  61. var tableConditions = new JObject(); // 表的其它查询条件,比如过滤、字段等
  62. foreach (var item in jobject)
  63. {
  64. if (item.Key.Equals(table, StringComparison.CurrentCultureIgnoreCase))
  65. {
  66. hasTableKey = true;
  67. break;
  68. }
  69. if (!ignoreConditions.Contains(item.Key.ToLower()))
  70. tableConditions.Add(item.Key, item.Value);
  71. }
  72. foreach (var removeKey in tableConditions)
  73. {
  74. jobject.Remove(removeKey.Key);
  75. }
  76. if (!hasTableKey)
  77. jobject.Add(table, tableConditions);
  78. return Query(ht);
  79. }
  80. /// <summary>
  81. /// 新增 🔖
  82. /// </summary>
  83. /// <param name="tables">表对象或数组,若没有传Id则后端生成Id</param>
  84. /// <returns></returns>
  85. [HttpPost("add")]
  86. [DisplayName("APIJSON新增")]
  87. [UnitOfWork]
  88. public JObject Add([FromBody] JObject tables)
  89. {
  90. var ht = new JObject();
  91. foreach (var table in tables)
  92. {
  93. var talbeName = table.Key.Trim();
  94. var role = _identityService.GetRole();
  95. if (!role.Insert.Table.Contains(talbeName, StringComparer.CurrentCultureIgnoreCase))
  96. throw Oops.Bah($"没权限添加{talbeName}");
  97. JToken result;
  98. // 批量插入
  99. if (table.Value is JArray)
  100. {
  101. var ids = new List<object>();
  102. foreach (var record in table.Value)
  103. {
  104. var cols = record.ToObject<JObject>();
  105. var id = _selectTable.InsertSingle(talbeName, cols, role);
  106. ids.Add(id);
  107. }
  108. result = JToken.FromObject(new { id = ids, count = ids.Count });
  109. }
  110. // 单条插入
  111. else
  112. {
  113. var cols = table.Value.ToObject<JObject>();
  114. var id = _selectTable.InsertSingle(talbeName, cols, role);
  115. result = JToken.FromObject(new { id });
  116. }
  117. ht.Add(talbeName, result);
  118. }
  119. return ht;
  120. }
  121. /// <summary>
  122. /// 更新(只支持Id作为条件) 🔖
  123. /// </summary>
  124. /// <param name="tables">支持多表、多Id批量更新</param>
  125. /// <returns></returns>
  126. [HttpPost("update")]
  127. [DisplayName("APIJSON更新")]
  128. [UnitOfWork]
  129. public JObject Edit([FromBody] JObject tables)
  130. {
  131. var ht = new JObject();
  132. foreach (var table in tables)
  133. {
  134. var tableName = table.Key.Trim();
  135. var role = _identityService.GetRole();
  136. var count = _selectTable.UpdateSingleTable(tableName, table.Value, role);
  137. ht.Add(tableName, JToken.FromObject(new { count }));
  138. }
  139. return ht;
  140. }
  141. /// <summary>
  142. /// 删除(支持非Id条件、支持批量) 🔖
  143. /// </summary>
  144. /// <param name="tables"></param>
  145. /// <returns></returns>
  146. [HttpPost("delete")]
  147. [DisplayName("APIJSON删除")]
  148. [UnitOfWork]
  149. public JObject Delete([FromBody] JObject tables)
  150. {
  151. var ht = new JObject();
  152. var role = _identityService.GetRole();
  153. foreach (var table in tables)
  154. {
  155. var talbeName = table.Key.Trim();
  156. if (role.Delete == null || role.Delete.Table == null)
  157. throw Oops.Bah("delete权限未配置");
  158. if (!role.Delete.Table.Contains(talbeName, StringComparer.CurrentCultureIgnoreCase))
  159. throw Oops.Bah($"没权限删除{talbeName}");
  160. //if (!value.ContainsKey("id"))
  161. // throw Oops.Bah("未传主键id");
  162. var value = JObject.Parse(table.Value.ToString());
  163. var sb = new StringBuilder(100);
  164. var parameters = new List<SugarParameter>();
  165. foreach (var f in value)
  166. {
  167. if (f.Value is JArray)
  168. {
  169. sb.Append($"{f.Key} in (@{f.Key}) and ");
  170. var paraArray = FuncList.TransJArrayToSugarPara(f.Value);
  171. parameters.Add(new SugarParameter($"@{f.Key}", paraArray));
  172. }
  173. else
  174. {
  175. sb.Append($"{f.Key}=@{f.Key} and ");
  176. parameters.Add(new SugarParameter($"@{f.Key}", FuncList.TransJObjectToSugarPara(f.Value)));
  177. }
  178. }
  179. if (!parameters.Any())
  180. throw Oops.Bah("请输入删除条件");
  181. var whereSql = sb.ToString().TrimEnd(" and ");
  182. var count = _db.Deleteable<object>().AS(talbeName).Where(whereSql, parameters).ExecuteCommand(); // 无实体删除
  183. value.Add("count", count); // 命中数量
  184. ht.Add(talbeName, value);
  185. }
  186. return ht;
  187. }
  188. }