ViewService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using ZhonTai.Admin.Core.Attributes;
  5. using ZhonTai.Admin.Core.Dto;
  6. using ZhonTai.Admin.Domain.View;
  7. using ZhonTai.Admin.Services.View.Dto;
  8. using ZhonTai.DynamicApi;
  9. using ZhonTai.DynamicApi.Attributes;
  10. using ZhonTai.Admin.Core.Consts;
  11. namespace ZhonTai.Admin.Services.View;
  12. /// <summary>
  13. /// 视图服务
  14. /// </summary>
  15. [DynamicApi(Area = AdminConsts.AreaName)]
  16. public class ViewService : BaseService, IViewService, IDynamicApi
  17. {
  18. private IViewRepository _viewRepository => LazyGetRequiredService<IViewRepository>();
  19. public ViewService()
  20. {
  21. }
  22. /// <summary>
  23. /// 查询视图
  24. /// </summary>
  25. /// <param name="id"></param>
  26. /// <returns></returns>
  27. public async Task<ViewGetOutput> GetAsync(long id)
  28. {
  29. var result = await _viewRepository.GetAsync<ViewGetOutput>(id);
  30. return result;
  31. }
  32. /// <summary>
  33. /// 查询列表
  34. /// </summary>
  35. /// <param name="key"></param>
  36. /// <returns></returns>
  37. public async Task<List<ViewListOutput>> GetListAsync(string key)
  38. {
  39. var data = await _viewRepository
  40. .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
  41. .OrderBy(a => a.ParentId)
  42. .OrderBy(a => a.Sort)
  43. .ToListAsync<ViewListOutput>();
  44. return data;
  45. }
  46. /// <summary>
  47. /// 新增
  48. /// </summary>
  49. /// <param name="input"></param>
  50. /// <returns></returns>
  51. public async Task<long> AddAsync(ViewAddInput input)
  52. {
  53. var entity = Mapper.Map<ViewEntity>(input);
  54. await _viewRepository.InsertAsync(entity);
  55. return entity.Id;
  56. }
  57. /// <summary>
  58. /// 修改
  59. /// </summary>
  60. /// <param name="input"></param>
  61. /// <returns></returns>
  62. public async Task UpdateAsync(ViewUpdateInput input)
  63. {
  64. var entity = await _viewRepository.GetAsync(input.Id);
  65. if (!(entity?.Id > 0))
  66. {
  67. throw ResultOutput.Exception("视图不存在!");
  68. }
  69. Mapper.Map(input, entity);
  70. await _viewRepository.UpdateAsync(entity);
  71. }
  72. /// <summary>
  73. /// 彻底删除
  74. /// </summary>
  75. /// <param name="id"></param>
  76. /// <returns></returns>
  77. public async Task DeleteAsync(long id)
  78. {
  79. await _viewRepository.DeleteAsync(m => m.Id == id);
  80. }
  81. /// <summary>
  82. /// 删除
  83. /// </summary>
  84. /// <param name="id"></param>
  85. /// <returns></returns>
  86. public async Task SoftDeleteAsync(long id)
  87. {
  88. await _viewRepository.SoftDeleteAsync(id);
  89. }
  90. /// <summary>
  91. /// 批量删除
  92. /// </summary>
  93. /// <param name="ids"></param>
  94. /// <returns></returns>
  95. public async Task BatchSoftDeleteAsync(long[] ids)
  96. {
  97. await _viewRepository.SoftDeleteAsync(ids);
  98. }
  99. /// <summary>
  100. /// 同步
  101. /// </summary>
  102. /// <param name="input"></param>
  103. /// <returns></returns>
  104. [AdminTransaction]
  105. public virtual async Task SyncAsync(ViewSyncInput input)
  106. {
  107. //查询所有视图
  108. var views = await _viewRepository.Select.ToListAsync();
  109. var names = views.Select(a => a.Name).ToList();
  110. var paths = views.Select(a => a.Path).ToList();
  111. //path处理
  112. foreach (var view in input.Views)
  113. {
  114. view.Path = view.Path?.Trim();
  115. }
  116. //批量插入
  117. {
  118. var inputViews = (from a in input.Views where !(paths.Contains(a.Path) || names.Contains(a.Name)) select a).ToList();
  119. if (inputViews.Count > 0)
  120. {
  121. var insertViews = Mapper.Map<List<ViewEntity>>(inputViews);
  122. foreach (var insertView in insertViews)
  123. {
  124. if (insertView.Label.IsNull())
  125. {
  126. insertView.Label = insertView.Name;
  127. }
  128. }
  129. insertViews = await _viewRepository.InsertAsync(insertViews);
  130. views.AddRange(insertViews);
  131. }
  132. }
  133. //批量更新
  134. {
  135. var inputPaths = input.Views.Select(a => a.Path).ToList();
  136. var inputNames = input.Views.Select(a => a.Name).ToList();
  137. //修改
  138. var updateViews = (from a in views where inputPaths.Contains(a.Path) || inputNames.Contains(a.Name) select a).ToList();
  139. if (updateViews.Count > 0)
  140. {
  141. foreach (var view in updateViews)
  142. {
  143. var inputView = input.Views.Where(a => a.Name == view.Name || a.Path == view.Path).FirstOrDefault();
  144. if (view.Label.IsNull())
  145. {
  146. view.Label = inputView.Label ?? inputView.Name;
  147. }
  148. if (view.Description.IsNull())
  149. {
  150. view.Description = inputView.Description;
  151. }
  152. view.Name = inputView.Name;
  153. view.Path = inputView.Path;
  154. view.Enabled = true;
  155. }
  156. }
  157. //禁用
  158. var disabledViews = (from a in views where (a.Path.NotNull() || a.Name.NotNull()) && (!inputPaths.Contains(a.Path) || !inputNames.Contains(a.Name)) select a).ToList();
  159. if (disabledViews.Count > 0)
  160. {
  161. foreach (var view in disabledViews)
  162. {
  163. view.Enabled = false;
  164. }
  165. }
  166. updateViews.AddRange(disabledViews);
  167. await _viewRepository.UpdateDiy.SetSource(updateViews)
  168. .UpdateColumns(a => new { a.Label, a.Name, a.Path, a.Enabled, a.Description })
  169. .ExecuteAffrowsAsync();
  170. }
  171. }
  172. }