0
0

ViewService.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using Admin.Core.Common.Attributes;
  2. using Admin.Core.Common.Input;
  3. using Admin.Core.Common.Output;
  4. using Admin.Core.Model.Admin;
  5. using Admin.Core.Repository.Admin;
  6. using Admin.Core.Service.Admin.View.Input;
  7. using Admin.Core.Service.Admin.View.Output;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. namespace Admin.Core.Service.Admin.View
  12. {
  13. public class ViewService : BaseService, IViewService
  14. {
  15. private readonly IViewRepository _viewRepository;
  16. public ViewService(IViewRepository moduleRepository)
  17. {
  18. _viewRepository = moduleRepository;
  19. }
  20. public async Task<IResponseOutput> GetAsync(long id)
  21. {
  22. var result = await _viewRepository.GetAsync<ViewGetOutput>(id);
  23. return ResponseOutput.Ok(result);
  24. }
  25. public async Task<IResponseOutput> ListAsync(string key)
  26. {
  27. var data = await _viewRepository
  28. .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
  29. .OrderBy(a => a.ParentId)
  30. .OrderBy(a => a.Sort)
  31. .ToListAsync<ViewListOutput>();
  32. return ResponseOutput.Ok(data);
  33. }
  34. public async Task<IResponseOutput> PageAsync(PageInput<ViewEntity> input)
  35. {
  36. var key = input.Filter?.Label;
  37. long total;
  38. var list = await _viewRepository.Select
  39. .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
  40. .Count(out total)
  41. .OrderByDescending(true, c => c.Id)
  42. .Page(input.CurrentPage, input.PageSize)
  43. .ToListAsync();
  44. var data = new PageOutput<ViewEntity>()
  45. {
  46. List = list,
  47. Total = total
  48. };
  49. return ResponseOutput.Ok(data);
  50. }
  51. public async Task<IResponseOutput> AddAsync(ViewAddInput input)
  52. {
  53. var entity = Mapper.Map<ViewEntity>(input);
  54. var id = (await _viewRepository.InsertAsync(entity)).Id;
  55. return ResponseOutput.Result(id > 0);
  56. }
  57. public async Task<IResponseOutput> UpdateAsync(ViewUpdateInput input)
  58. {
  59. if (!(input?.Id > 0))
  60. {
  61. return ResponseOutput.NotOk();
  62. }
  63. var entity = await _viewRepository.GetAsync(input.Id);
  64. if (!(entity?.Id > 0))
  65. {
  66. return ResponseOutput.NotOk("视图不存在!");
  67. }
  68. Mapper.Map(input, entity);
  69. await _viewRepository.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 _viewRepository.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 _viewRepository.SoftDeleteAsync(id);
  84. return ResponseOutput.Result(result);
  85. }
  86. public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
  87. {
  88. var result = await _viewRepository.SoftDeleteAsync(ids);
  89. return ResponseOutput.Result(result);
  90. }
  91. [Transaction]
  92. public async Task<IResponseOutput> SyncAsync(ViewSyncInput input)
  93. {
  94. //查询所有视图
  95. var views = await _viewRepository.Select.ToListAsync();
  96. var names = views.Select(a => a.Name).ToList();
  97. var paths = views.Select(a => a.Path).ToList();
  98. //path处理
  99. foreach (var view in input.Views)
  100. {
  101. view.Path = view.Path?.Trim().ToLower();
  102. }
  103. //批量插入
  104. {
  105. var inputViews = (from a in input.Views where !paths.Contains(a.Path) || !names.Contains(a.Name) select a).ToList();
  106. if (inputViews.Count > 0)
  107. {
  108. var insertViews = Mapper.Map<List<ViewEntity>>(inputViews);
  109. foreach (var insertView in insertViews)
  110. {
  111. insertView.Label = insertView.Name;
  112. }
  113. insertViews = await _viewRepository.InsertAsync(insertViews);
  114. views.AddRange(insertViews);
  115. }
  116. }
  117. //批量更新
  118. {
  119. var inputPaths = input.Views.Select(a => a.Path).ToList();
  120. var inputNames = input.Views.Select(a => a.Name).ToList();
  121. //修改
  122. var updateViews = (from a in views where inputPaths.Contains(a.Path) || inputNames.Contains(a.Name) select a).ToList();
  123. if (updateViews.Count > 0)
  124. {
  125. foreach (var view in updateViews)
  126. {
  127. var inputView = input.Views.Where(a => a.Name == view.Name || a.Path == view.Path).FirstOrDefault();
  128. if (view.Label.IsNull())
  129. {
  130. view.Label = inputView.Name;
  131. }
  132. view.Name = inputView.Name;
  133. view.Path = inputView.Path;
  134. view.Enabled = true;
  135. }
  136. }
  137. //禁用
  138. var disabledViews = (from a in views where (a.Path.NotNull() || a.Name.NotNull()) && (!inputPaths.Contains(a.Path) || !inputNames.Contains(a.Name)) select a).ToList();
  139. if (disabledViews.Count > 0)
  140. {
  141. foreach (var view in disabledViews)
  142. {
  143. view.Enabled = false;
  144. }
  145. }
  146. updateViews.AddRange(disabledViews);
  147. await _viewRepository.UpdateDiy.SetSource(updateViews)
  148. .UpdateColumns(a => new { a.Label, a.Name, a.Path, a.Enabled })
  149. .ExecuteAffrowsAsync();
  150. }
  151. return ResponseOutput.Ok();
  152. }
  153. }
  154. }