123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using ZhonTai.Common.Attributes;
- using ZhonTai.Common.Domain.Dto;
- using ZhonTai.Plate.Admin.Domain.View;
- using ZhonTai.Plate.Admin.Service.View.Input;
- using ZhonTai.Plate.Admin.Service.View.Output;
- namespace ZhonTai.Plate.Admin.Service.View
- {
- public class ViewService : BaseService, IViewService
- {
- private readonly IViewRepository _viewRepository;
- public ViewService(IViewRepository moduleRepository)
- {
- _viewRepository = moduleRepository;
- }
- public async Task<IResponseOutput> GetAsync(long id)
- {
- var result = await _viewRepository.GetAsync<ViewGetOutput>(id);
- return ResponseOutput.Ok(result);
- }
- public async Task<IResponseOutput> ListAsync(string key)
- {
- var data = await _viewRepository
- .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
- .OrderBy(a => a.ParentId)
- .OrderBy(a => a.Sort)
- .ToListAsync<ViewListOutput>();
- return ResponseOutput.Ok(data);
- }
- public async Task<IResponseOutput> PageAsync(PageInput<ViewEntity> input)
- {
- var key = input.Filter?.Label;
- long total;
- var list = await _viewRepository.Select
- .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
- .Count(out total)
- .OrderByDescending(true, c => c.Id)
- .Page(input.CurrentPage, input.PageSize)
- .ToListAsync();
- var data = new PageOutput<ViewEntity>()
- {
- List = list,
- Total = total
- };
- return ResponseOutput.Ok(data);
- }
- public async Task<IResponseOutput> AddAsync(ViewAddInput input)
- {
- var entity = Mapper.Map<ViewEntity>(input);
- var id = (await _viewRepository.InsertAsync(entity)).Id;
- return ResponseOutput.Result(id > 0);
- }
- public async Task<IResponseOutput> UpdateAsync(ViewUpdateInput input)
- {
- if (!(input?.Id > 0))
- {
- return ResponseOutput.NotOk();
- }
- var entity = await _viewRepository.GetAsync(input.Id);
- if (!(entity?.Id > 0))
- {
- return ResponseOutput.NotOk("视图不存在!");
- }
- Mapper.Map(input, entity);
- await _viewRepository.UpdateAsync(entity);
- return ResponseOutput.Ok();
- }
- public async Task<IResponseOutput> DeleteAsync(long id)
- {
- var result = false;
- if (id > 0)
- {
- result = (await _viewRepository.DeleteAsync(m => m.Id == id)) > 0;
- }
- return ResponseOutput.Result(result);
- }
- public async Task<IResponseOutput> SoftDeleteAsync(long id)
- {
- var result = await _viewRepository.SoftDeleteAsync(id);
- return ResponseOutput.Result(result);
- }
- public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
- {
- var result = await _viewRepository.SoftDeleteAsync(ids);
- return ResponseOutput.Result(result);
- }
- [Transaction]
- public async Task<IResponseOutput> SyncAsync(ViewSyncInput input)
- {
- //查询所有视图
- var views = await _viewRepository.Select.ToListAsync();
- var names = views.Select(a => a.Name).ToList();
- var paths = views.Select(a => a.Path).ToList();
- //path处理
- foreach (var view in input.Views)
- {
- view.Path = view.Path?.Trim();
- }
- //批量插入
- {
- var inputViews = (from a in input.Views where !(paths.Contains(a.Path) || names.Contains(a.Name)) select a).ToList();
- if (inputViews.Count > 0)
- {
- var insertViews = Mapper.Map<List<ViewEntity>>(inputViews);
- foreach (var insertView in insertViews)
- {
- if (insertView.Label.IsNull())
- {
- insertView.Label = insertView.Name;
- }
- }
- insertViews = await _viewRepository.InsertAsync(insertViews);
- views.AddRange(insertViews);
- }
- }
- //批量更新
- {
- var inputPaths = input.Views.Select(a => a.Path).ToList();
- var inputNames = input.Views.Select(a => a.Name).ToList();
- //修改
- var updateViews = (from a in views where inputPaths.Contains(a.Path) || inputNames.Contains(a.Name) select a).ToList();
- if (updateViews.Count > 0)
- {
- foreach (var view in updateViews)
- {
- var inputView = input.Views.Where(a => a.Name == view.Name || a.Path == view.Path).FirstOrDefault();
- if (view.Label.IsNull())
- {
- view.Label = inputView.Label ?? inputView.Name;
- }
- if (view.Description.IsNull())
- {
- view.Description = inputView.Description;
- }
- view.Name = inputView.Name;
- view.Path = inputView.Path;
- view.Enabled = true;
- }
- }
- //禁用
- var disabledViews = (from a in views where (a.Path.NotNull() || a.Name.NotNull()) && (!inputPaths.Contains(a.Path) || !inputNames.Contains(a.Name)) select a).ToList();
- if (disabledViews.Count > 0)
- {
- foreach (var view in disabledViews)
- {
- view.Enabled = false;
- }
- }
- updateViews.AddRange(disabledViews);
- await _viewRepository.UpdateDiy.SetSource(updateViews)
- .UpdateColumns(a => new { a.Label, a.Name, a.Path, a.Enabled, a.Description })
- .ExecuteAffrowsAsync();
- }
-
- return ResponseOutput.Ok();
- }
- }
- }
|