ViewService.cs 7.0 KB

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