ApiService.cs 7.3 KB

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