ViewService.cs 6.4 KB

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