APIJSONService.cs 7.1 KB

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