ApiService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using System.Collections.Generic;
  4. using Admin.Core.Common.Input;
  5. using Admin.Core.Common.Output;
  6. using Admin.Core.Model.Admin;
  7. using Admin.Core.Repository.Admin;
  8. using Admin.Core.Service.Admin.Api.Input;
  9. using Admin.Core.Service.Admin.Api.Output;
  10. using Admin.Core.Common.Attributes;
  11. namespace Admin.Core.Service.Admin.Api
  12. {
  13. public class ApiService : BaseService, IApiService
  14. {
  15. private readonly IApiRepository _apiRepository;
  16. public ApiService(IApiRepository moduleRepository)
  17. {
  18. _apiRepository = moduleRepository;
  19. }
  20. public async Task<IResponseOutput> GetAsync(long id)
  21. {
  22. var result = await _apiRepository.GetAsync<ApiGetOutput>(id);
  23. return ResponseOutput.Ok(result);
  24. }
  25. public async Task<IResponseOutput> ListAsync(string key)
  26. {
  27. var data = await _apiRepository
  28. .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
  29. .ToListAsync<ApiListOutput>();
  30. return ResponseOutput.Ok(data);
  31. }
  32. public async Task<IResponseOutput> PageAsync(PageInput<ApiEntity> input)
  33. {
  34. var key = input.Filter?.Label;
  35. var list = await _apiRepository.Select
  36. .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
  37. .Count(out var total)
  38. .OrderByDescending(true, c => c.Id)
  39. .Page(input.CurrentPage, input.PageSize)
  40. .ToListAsync();
  41. var data = new PageOutput<ApiEntity>()
  42. {
  43. List = list,
  44. Total = total
  45. };
  46. return ResponseOutput.Ok(data);
  47. }
  48. public async Task<IResponseOutput> AddAsync(ApiAddInput input)
  49. {
  50. var entity = Mapper.Map<ApiEntity>(input);
  51. var id = (await _apiRepository.InsertAsync(entity)).Id;
  52. return ResponseOutput.Result(id > 0);
  53. }
  54. public async Task<IResponseOutput> UpdateAsync(ApiUpdateInput input)
  55. {
  56. if (!(input?.Id > 0))
  57. {
  58. return ResponseOutput.NotOk();
  59. }
  60. var entity = await _apiRepository.GetAsync(input.Id);
  61. if (!(entity?.Id > 0))
  62. {
  63. return ResponseOutput.NotOk("接口不存在!");
  64. }
  65. Mapper.Map(input, entity);
  66. await _apiRepository.UpdateAsync(entity);
  67. return ResponseOutput.Ok();
  68. }
  69. public async Task<IResponseOutput> DeleteAsync(long id)
  70. {
  71. var result = false;
  72. if (id > 0)
  73. {
  74. result = (await _apiRepository.DeleteAsync(m => m.Id == id)) > 0;
  75. }
  76. return ResponseOutput.Result(result);
  77. }
  78. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  79. {
  80. var result = await _apiRepository.SoftDeleteAsync(id);
  81. return ResponseOutput.Result(result);
  82. }
  83. public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
  84. {
  85. var result = await _apiRepository.SoftDeleteAsync(ids);
  86. return ResponseOutput.Result(result);
  87. }
  88. [Transaction]
  89. public async Task<IResponseOutput> SyncAsync(ApiSyncInput input)
  90. {
  91. //查询所有api
  92. var apis = await _apiRepository.Select.ToListAsync();
  93. var paths = apis.Select(a => a.Path).ToList();
  94. //path处理
  95. foreach (var api in input.Apis)
  96. {
  97. api.Path = api.Path?.Trim().ToLower();
  98. api.ParentPath = api.ParentPath?.Trim().ToLower();
  99. }
  100. #region 执行插入
  101. //执行父级api插入
  102. var parentApis = input.Apis.FindAll(a => a.ParentPath.IsNull());
  103. var pApis = (from a in parentApis where !paths.Contains(a.Path) select a).ToList();
  104. if (pApis.Count > 0)
  105. {
  106. var insertPApis = Mapper.Map<List<ApiEntity>>(pApis);
  107. insertPApis = await _apiRepository.InsertAsync(insertPApis);
  108. apis.AddRange(insertPApis);
  109. }
  110. //执行子级api插入
  111. var childApis = input.Apis.FindAll(a => a.ParentPath.NotNull());
  112. var cApis = (from a in childApis where !paths.Contains(a.Path) select a).ToList();
  113. if (cApis.Count > 0)
  114. {
  115. var insertCApis = Mapper.Map<List<ApiEntity>>(cApis);
  116. insertCApis = await _apiRepository.InsertAsync(insertCApis);
  117. apis.AddRange(insertCApis);
  118. }
  119. #endregion
  120. #region 修改和禁用
  121. {
  122. //api修改
  123. ApiEntity a;
  124. List<string> labels;
  125. string label;
  126. string desc;
  127. foreach (var api in parentApis)
  128. {
  129. a = apis.Find(a => a.Path == api.Path);
  130. if (a?.Id > 0)
  131. {
  132. labels = api.Label?.Split("\r\n")?.ToList();
  133. label = labels != null && labels.Count > 0 ? labels[0] : string.Empty;
  134. desc = labels != null && labels.Count > 1 ? string.Join("\r\n", labels.GetRange(1, labels.Count() - 1)) : string.Empty;
  135. a.ParentId = 0;
  136. a.Label = label;
  137. a.Description = desc;
  138. a.Enabled = true;
  139. }
  140. }
  141. }
  142. {
  143. //api修改
  144. ApiEntity a;
  145. ApiEntity pa;
  146. List<string> labels;
  147. string label;
  148. string desc;
  149. foreach (var api in childApis)
  150. {
  151. a = apis.Find(a => a.Path == api.Path);
  152. pa = apis.Find(a => a.Path == api.ParentPath);
  153. if (a?.Id > 0)
  154. {
  155. labels = api.Label?.Split("\r\n")?.ToList();
  156. label = labels != null && labels.Count > 0 ? labels[0] : string.Empty;
  157. desc = labels != null && labels.Count > 1 ? string.Join("\r\n", labels.GetRange(1, labels.Count() - 1)) : string.Empty;
  158. a.ParentId = pa.Id;
  159. a.Label = label;
  160. a.Description = desc;
  161. a.HttpMethods = api.HttpMethods;
  162. a.Enabled = true;
  163. }
  164. }
  165. }
  166. {
  167. //api禁用
  168. var inputPaths = input.Apis.Select(a => a.Path).ToList();
  169. var disabledApis = (from a in apis where !inputPaths.Contains(a.Path) select a).ToList();
  170. if (disabledApis.Count > 0)
  171. {
  172. foreach (var api in disabledApis)
  173. {
  174. api.Enabled = false;
  175. }
  176. }
  177. }
  178. #endregion
  179. //批量更新
  180. await _apiRepository.UpdateDiy.SetSource(apis)
  181. .UpdateColumns(a => new { a.ParentId, a.Label, a.HttpMethods,a.Description,a.Enabled })
  182. .ExecuteAffrowsAsync();
  183. return ResponseOutput.Ok();
  184. }
  185. }
  186. }