ViewService.cs 7.4 KB

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