0
0

ViewService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Linq;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Admin.Core.Common.Output;
  5. using Admin.Core.Common.Input;
  6. using Admin.Core.Model.Admin;
  7. using Admin.Core.Repository.Admin;
  8. using Admin.Core.Service.Admin.View.Input;
  9. using Admin.Core.Service.Admin.View.Output;
  10. using Admin.Core.Common.Attributes;
  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. //查询所有view
  95. var views = await _viewRepository.Select.ToListAsync();
  96. var paths = views.Select(a => a.Path).ToList();
  97. //path处理
  98. foreach (var view in input.Views)
  99. {
  100. view.Path = view.Path?.Trim().ToLower();
  101. view.ParentPath = view.ParentPath?.Trim().ToLower();
  102. }
  103. #region 执行插入
  104. //执行父级view插入
  105. var parentViews = input.Views.FindAll(a => a.ParentPath.IsNull());
  106. var pViews = (from a in parentViews where !paths.Contains(a.Path) select a).ToList();
  107. if (pViews.Count > 0)
  108. {
  109. var insertPViews = Mapper.Map<List<ViewEntity>>(pViews);
  110. insertPViews = await _viewRepository.InsertAsync(insertPViews);
  111. views.AddRange(insertPViews);
  112. }
  113. //执行子级view插入
  114. var childViews = input.Views.FindAll(a => a.ParentPath.NotNull());
  115. var cViews = (from a in childViews where !paths.Contains(a.Path) select a).ToList();
  116. if (cViews.Count > 0)
  117. {
  118. var insertCViews = Mapper.Map<List<ViewEntity>>(cViews);
  119. insertCViews = await _viewRepository.InsertAsync(insertCViews);
  120. views.AddRange(insertCViews);
  121. }
  122. #endregion
  123. #region 修改和禁用
  124. //view修改
  125. {
  126. ViewEntity a;
  127. List<string> labels;
  128. string label;
  129. string desc;
  130. foreach (var view in parentViews)
  131. {
  132. a = views.Find(a => a.Path == view.Path);
  133. if (a?.Id > 0)
  134. {
  135. labels = view.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. ViewEntity a;
  147. ViewEntity pa;
  148. List<string> labels;
  149. string label;
  150. string desc;
  151. foreach (var view in childViews)
  152. {
  153. a = views.Find(a => a.Path == view.Path);
  154. pa = views.Find(a => a.Path == view.ParentPath);
  155. if (a?.Id > 0)
  156. {
  157. labels = view.Label?.Split("\r\n")?.ToList();
  158. label = labels != null && labels.Count > 0 ? labels[0] : string.Empty;
  159. desc = labels != null && labels.Count > 1 ? string.Join("\r\n", labels.GetRange(1, labels.Count() - 1)) : string.Empty;
  160. a.ParentId = pa.Id;
  161. a.Label = label;
  162. a.Description = desc;
  163. a.Enabled = true;
  164. }
  165. }
  166. }
  167. //view禁用
  168. var inputPaths = input.Views.Select(a => a.Path).ToList();
  169. var disabledViews = (from a in views where !inputPaths.Contains(a.Path) select a).ToList();
  170. if (disabledViews.Count > 0)
  171. {
  172. foreach (var view in disabledViews)
  173. {
  174. view.Enabled = false;
  175. }
  176. }
  177. #endregion
  178. //批量更新
  179. await _viewRepository.UpdateDiy.SetSource(views)
  180. .UpdateColumns(a => new { a.ParentId, a.Label,a.Description,a.Enabled })
  181. .ExecuteAffrowsAsync();
  182. return ResponseOutput.Ok();
  183. }
  184. }
  185. }