APIJSONService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. namespace Admin.NET.Core.Service;
  5. /// <summary>
  6. /// APIJSON服务
  7. /// </summary>
  8. [ApiDescriptionSettings(Order = 100)]
  9. public class APIJSONService : IDynamicApiController, ITransient
  10. {
  11. private readonly ISqlSugarClient _db;
  12. private readonly IdentityService _identityService;
  13. private readonly TableMapper _tableMapper;
  14. private readonly SelectTable _selectTable;
  15. public APIJSONService(ISqlSugarClient db,
  16. IdentityService identityService,
  17. TableMapper tableMapper)
  18. {
  19. _db = db;
  20. _tableMapper = tableMapper;
  21. _identityService = identityService;
  22. _selectTable = new SelectTable(_identityService, _tableMapper, _db);
  23. }
  24. /// <summary>
  25. /// 统一查询入口
  26. /// </summary>
  27. /// <param name="jobject"></param>
  28. /// <remarks>参数:{"[]":{"SYSLOGOP":{}}}</remarks>
  29. /// <returns></returns>
  30. [HttpPost("get")]
  31. public JObject Query([FromBody] JObject jobject)
  32. {
  33. return _selectTable.Query(jobject);
  34. }
  35. /// <summary>
  36. /// 查询表
  37. /// </summary>
  38. /// <param name="table"></param>
  39. /// <param name="jobject"></param>
  40. /// <returns></returns>
  41. [HttpPost("get/{table}")]
  42. public JObject QueryByTable([FromRoute] string table, [FromBody] JObject jobject)
  43. {
  44. var ht = new JObject
  45. {
  46. { table + "[]", jobject }
  47. };
  48. // 自动添加总计数量
  49. if (jobject["query"] != null && jobject["query"].ToString() != "0" && jobject["total@"] == null)
  50. ht.Add("total@", "");
  51. // 每页最大1000条数据
  52. if (jobject["count"] != null && int.Parse(jobject["count"].ToString()) > 1000)
  53. throw Oops.Bah("count分页数量最大不能超过1000");
  54. jobject.Remove("@debug");
  55. var hasTableKey = false;
  56. var ignoreConditions = new List<string> { "page", "count", "query" };
  57. var tableConditions = new JObject(); // 表的其它查询条件,比如过滤、字段等
  58. foreach (var item in jobject)
  59. {
  60. if (item.Key.Equals(table, StringComparison.CurrentCultureIgnoreCase))
  61. {
  62. hasTableKey = true;
  63. break;
  64. }
  65. if (!ignoreConditions.Contains(item.Key.ToLower()))
  66. tableConditions.Add(item.Key, item.Value);
  67. }
  68. foreach (var removeKey in tableConditions)
  69. {
  70. jobject.Remove(removeKey.Key);
  71. }
  72. if (!hasTableKey)
  73. jobject.Add(table, tableConditions);
  74. return Query(ht);
  75. }
  76. /// <summary>
  77. /// 新增
  78. /// </summary>
  79. /// <param name="tables">表对象或数组,若没有传Id则后端生成Id</param>
  80. /// <returns></returns>
  81. [HttpPost("post")]
  82. [UnitOfWork]
  83. public JObject Add([FromBody] JObject tables)
  84. {
  85. var ht = new JObject();
  86. foreach (var table in tables)
  87. {
  88. var talbeName = table.Key.Trim();
  89. var role = _identityService.GetRole();
  90. if (!role.Insert.Table.Contains(talbeName, StringComparer.CurrentCultureIgnoreCase))
  91. throw Oops.Bah($"没权限添加{talbeName}");
  92. JToken result;
  93. // 批量插入
  94. if (table.Value is JArray)
  95. {
  96. var ids = new List<object>();
  97. foreach (var record in table.Value)
  98. {
  99. var cols = record.ToObject<JObject>();
  100. var id = _selectTable.InsertSingle(talbeName, cols, role);
  101. ids.Add(id);
  102. }
  103. result = JToken.FromObject(new { id = ids, count = ids.Count });
  104. }
  105. // 单条插入
  106. else
  107. {
  108. var cols = table.Value.ToObject<JObject>();
  109. var id = _selectTable.InsertSingle(talbeName, cols, role);
  110. result = JToken.FromObject(new { id });
  111. }
  112. ht.Add(talbeName, result);
  113. }
  114. return ht;
  115. }
  116. /// <summary>
  117. /// 修改(只支持Id作为条件)
  118. /// </summary>
  119. /// <param name="tables">支持多表、多Id批量更新</param>
  120. /// <returns></returns>
  121. [HttpPost("post")]
  122. [UnitOfWork]
  123. public JObject Edit([FromBody] JObject tables)
  124. {
  125. var ht = new JObject();
  126. foreach (var table in tables)
  127. {
  128. var tableName = table.Key.Trim();
  129. var role = _identityService.GetRole();
  130. var count = _selectTable.UpdateSingleTable(tableName, table.Value, role);
  131. ht.Add(tableName, JToken.FromObject(new { count }));
  132. }
  133. return ht;
  134. }
  135. /// <summary>
  136. /// 删除(支持非Id条件、支持批量)
  137. /// </summary>
  138. /// <param name="tables"></param>
  139. /// <returns></returns>
  140. [HttpPost("post")]
  141. [UnitOfWork]
  142. public JObject Delete([FromBody] JObject tables)
  143. {
  144. var ht = new JObject();
  145. var role = _identityService.GetRole();
  146. foreach (var table in tables)
  147. {
  148. var talbeName = table.Key.Trim();
  149. if (role.Delete == null || role.Delete.Table == null)
  150. throw Oops.Bah("delete权限未配置");
  151. if (!role.Delete.Table.Contains(talbeName, StringComparer.CurrentCultureIgnoreCase))
  152. throw Oops.Bah($"没权限删除{talbeName}");
  153. //if (!value.ContainsKey("id"))
  154. // throw Oops.Bah("未传主键id");
  155. var value = JObject.Parse(table.Value.ToString());
  156. var sb = new StringBuilder(100);
  157. var parameters = new List<SugarParameter>();
  158. foreach (var f in value)
  159. {
  160. if (f.Value is JArray)
  161. {
  162. sb.Append($"{f.Key} in (@{f.Key}) and ");
  163. var paraArray = FuncList.TransJArrayToSugarPara(f.Value);
  164. parameters.Add(new SugarParameter($"@{f.Key}", paraArray));
  165. }
  166. else
  167. {
  168. sb.Append($"{f.Key}=@{f.Key} and ");
  169. parameters.Add(new SugarParameter($"@{f.Key}", FuncList.TransJObjectToSugarPara(f.Value)));
  170. }
  171. }
  172. if (!parameters.Any())
  173. throw Oops.Bah("请输入删除条件");
  174. var whereSql = sb.ToString().TrimEnd(" and ");
  175. var count = _db.Deleteable<object>().AS(talbeName).Where(whereSql, parameters).ExecuteCommand(); // 无实体删除
  176. value.Add("count", count); // 命中数量
  177. ht.Add(talbeName, value);
  178. }
  179. return ht;
  180. }
  181. }