ApiService.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Mvc;
  5. using ZhonTai.Admin.Core.Attributes;
  6. using ZhonTai.Admin.Core.Dto;
  7. using ZhonTai.Admin.Domain.Api;
  8. using ZhonTai.Admin.Services.Api.Dto;
  9. using ZhonTai.Admin.Domain.Api.Dto;
  10. using ZhonTai.DynamicApi;
  11. using ZhonTai.DynamicApi.Attributes;
  12. namespace ZhonTai.Admin.Services.Api
  13. {
  14. /// <summary>
  15. /// 接口服务
  16. /// </summary>
  17. [DynamicApi(Area = "admin")]
  18. public class ApiService : BaseService, IApiService, IDynamicApi
  19. {
  20. private readonly IApiRepository _apiRepository;
  21. public ApiService(IApiRepository moduleRepository)
  22. {
  23. _apiRepository = moduleRepository;
  24. }
  25. /// <summary>
  26. /// 查询接口
  27. /// </summary>
  28. /// <param name="id"></param>
  29. /// <returns></returns>
  30. public async Task<IResultOutput> GetAsync(long id)
  31. {
  32. var result = await _apiRepository.GetAsync<ApiGetOutput>(id);
  33. return ResultOutput.Ok(result);
  34. }
  35. /// <summary>
  36. /// 查询列表
  37. /// </summary>
  38. /// <param name="key"></param>
  39. /// <returns></returns>
  40. public async Task<IResultOutput> GetListAsync(string key)
  41. {
  42. var data = await _apiRepository
  43. .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
  44. .ToListAsync<ApiListOutput>();
  45. return ResultOutput.Ok(data);
  46. }
  47. /// <summary>
  48. /// 查询分页
  49. /// </summary>
  50. /// <param name="input"></param>
  51. /// <returns></returns>
  52. [HttpPost]
  53. public async Task<IResultOutput> GetPageAsync(PageInput<ApiGetPageDto> input)
  54. {
  55. var key = input.Filter?.Label;
  56. var list = await _apiRepository.Select
  57. .WhereDynamicFilter(input.DynamicFilter)
  58. .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
  59. .Count(out var total)
  60. .OrderByDescending(true, c => c.Id)
  61. .Page(input.CurrentPage, input.PageSize)
  62. .ToListAsync();
  63. var data = new PageOutput<ApiEntity>()
  64. {
  65. List = list,
  66. Total = total
  67. };
  68. return ResultOutput.Ok(data);
  69. }
  70. /// <summary>
  71. /// 添加
  72. /// </summary>
  73. /// <param name="input"></param>
  74. /// <returns></returns>
  75. public async Task<IResultOutput> AddAsync(ApiAddInput input)
  76. {
  77. var entity = Mapper.Map<ApiEntity>(input);
  78. var id = (await _apiRepository.InsertAsync(entity)).Id;
  79. return ResultOutput.Result(id > 0);
  80. }
  81. /// <summary>
  82. /// 修改
  83. /// </summary>
  84. /// <param name="input"></param>
  85. /// <returns></returns>
  86. public async Task<IResultOutput> UpdateAsync(ApiUpdateInput input)
  87. {
  88. if (!(input?.Id > 0))
  89. {
  90. return ResultOutput.NotOk();
  91. }
  92. var entity = await _apiRepository.GetAsync(input.Id);
  93. if (!(entity?.Id > 0))
  94. {
  95. return ResultOutput.NotOk("接口不存在!");
  96. }
  97. Mapper.Map(input, entity);
  98. await _apiRepository.UpdateAsync(entity);
  99. return ResultOutput.Ok();
  100. }
  101. /// <summary>
  102. /// 彻底删除
  103. /// </summary>
  104. /// <param name="id"></param>
  105. /// <returns></returns>
  106. public async Task<IResultOutput> DeleteAsync(long id)
  107. {
  108. var result = false;
  109. if (id > 0)
  110. {
  111. result = (await _apiRepository.DeleteAsync(m => m.Id == id)) > 0;
  112. }
  113. return ResultOutput.Result(result);
  114. }
  115. /// <summary>
  116. /// 删除
  117. /// </summary>
  118. /// <param name="id"></param>
  119. /// <returns></returns>
  120. [HttpDelete]
  121. public async Task<IResultOutput> SoftDeleteAsync(long id)
  122. {
  123. var result = await _apiRepository.SoftDeleteAsync(id);
  124. return ResultOutput.Result(result);
  125. }
  126. /// <summary>
  127. /// 批量删除
  128. /// </summary>
  129. /// <param name="ids"></param>
  130. /// <returns></returns>
  131. public async Task<IResultOutput> BatchSoftDeleteAsync(long[] ids)
  132. {
  133. var result = await _apiRepository.SoftDeleteAsync(ids);
  134. return ResultOutput.Result(result);
  135. }
  136. /// <summary>
  137. /// 同步
  138. /// </summary>
  139. /// <param name="input"></param>
  140. /// <returns></returns>
  141. [Transaction]
  142. public async Task<IResultOutput> SyncAsync(ApiSyncInput input)
  143. {
  144. //查询所有api
  145. var apis = await _apiRepository.Select.ToListAsync();
  146. var paths = apis.Select(a => a.Path).ToList();
  147. //path处理
  148. foreach (var api in input.Apis)
  149. {
  150. api.Path = api.Path?.Trim().ToLower();
  151. api.ParentPath = api.ParentPath?.Trim().ToLower();
  152. }
  153. #region 执行插入
  154. //执行父级api插入
  155. var parentApis = input.Apis.FindAll(a => a.ParentPath.IsNull());
  156. var pApis = (from a in parentApis where !paths.Contains(a.Path) select a).ToList();
  157. if (pApis.Count > 0)
  158. {
  159. var insertPApis = Mapper.Map<List<ApiEntity>>(pApis);
  160. insertPApis = await _apiRepository.InsertAsync(insertPApis);
  161. apis.AddRange(insertPApis);
  162. }
  163. //执行子级api插入
  164. var childApis = input.Apis.FindAll(a => a.ParentPath.NotNull());
  165. var cApis = (from a in childApis where !paths.Contains(a.Path) select a).ToList();
  166. if (cApis.Count > 0)
  167. {
  168. var insertCApis = Mapper.Map<List<ApiEntity>>(cApis);
  169. insertCApis = await _apiRepository.InsertAsync(insertCApis);
  170. apis.AddRange(insertCApis);
  171. }
  172. #endregion 执行插入
  173. #region 修改和禁用
  174. {
  175. //api修改
  176. ApiEntity a;
  177. List<string> labels;
  178. string label;
  179. string desc;
  180. foreach (var api in parentApis)
  181. {
  182. a = apis.Find(a => a.Path == api.Path);
  183. if (a?.Id > 0)
  184. {
  185. labels = api.Label?.Split("\r\n")?.ToList();
  186. label = labels != null && labels.Count > 0 ? labels[0] : string.Empty;
  187. desc = labels != null && labels.Count > 1 ? string.Join("\r\n", labels.GetRange(1, labels.Count() - 1)) : string.Empty;
  188. a.ParentId = 0;
  189. a.Label = label;
  190. a.Description = desc;
  191. a.Enabled = true;
  192. }
  193. }
  194. }
  195. {
  196. //api修改
  197. ApiEntity a;
  198. ApiEntity pa;
  199. List<string> labels;
  200. string label;
  201. string desc;
  202. foreach (var api in childApis)
  203. {
  204. a = apis.Find(a => a.Path == api.Path);
  205. pa = apis.Find(a => a.Path == api.ParentPath);
  206. if (a?.Id > 0)
  207. {
  208. labels = api.Label?.Split("\r\n")?.ToList();
  209. label = labels != null && labels.Count > 0 ? labels[0] : string.Empty;
  210. desc = labels != null && labels.Count > 1 ? string.Join("\r\n", labels.GetRange(1, labels.Count() - 1)) : string.Empty;
  211. a.ParentId = pa.Id;
  212. a.Label = label;
  213. a.Description = desc;
  214. a.HttpMethods = api.HttpMethods;
  215. a.Enabled = true;
  216. }
  217. }
  218. }
  219. {
  220. //api禁用
  221. var inputPaths = input.Apis.Select(a => a.Path).ToList();
  222. var disabledApis = (from a in apis where !inputPaths.Contains(a.Path) select a).ToList();
  223. if (disabledApis.Count > 0)
  224. {
  225. foreach (var api in disabledApis)
  226. {
  227. api.Enabled = false;
  228. }
  229. }
  230. }
  231. #endregion 修改和禁用
  232. //批量更新
  233. await _apiRepository.UpdateDiy.SetSource(apis)
  234. .UpdateColumns(a => new { a.ParentId, a.Label, a.HttpMethods, a.Description, a.Enabled })
  235. .ExecuteAffrowsAsync();
  236. return ResultOutput.Ok();
  237. }
  238. }
  239. }