1
0

ApiService.cs 7.3 KB

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