123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- using System.Linq;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using AutoMapper;
- using Admin.Core.Common;
- using Admin.Core.Model.Input;
- using Admin.Core.Model.Output;
- using Admin.Core.Model.Admin;
- using Admin.Core.Repository.Admin;
- using Admin.Core.Service.Admin.Api.Input;
- using Admin.Core.Service.Admin.Api.Output;
- namespace Admin.Core.Service.Admin.Api
- {
- public class ApiService : IApiService
- {
- private readonly IMapper _mapper;
- private readonly IApiRepository _apiRepository;
- public ApiService(IMapper mapper, IApiRepository moduleRepository)
- {
- _mapper = mapper;
- _apiRepository = moduleRepository;
- }
- public async Task<IResponseOutput> GetAsync(long id)
- {
- var result = await _apiRepository.GetAsync<ApiGetOutput>(id);
- return ResponseOutput.Ok(result);
- }
- public async Task<IResponseOutput> ListAsync(string key)
- {
- var data = await _apiRepository
- .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
- .ToListAsync<ApiListOutput>();
- return ResponseOutput.Ok(data);
- }
- public async Task<IResponseOutput> PageAsync(PageInput<ApiEntity> input)
- {
- var key = input.Filter?.Label;
- var list = await _apiRepository.Select
- .WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
- .Count(out var total)
- .OrderByDescending(true, c => c.Id)
- .Page(input.CurrentPage, input.PageSize)
- .ToListAsync();
- var data = new PageOutput<ApiEntity>()
- {
- List = list,
- Total = total
- };
- return ResponseOutput.Ok(data);
- }
- public async Task<IResponseOutput> AddAsync(ApiAddInput input)
- {
- var entity = _mapper.Map<ApiEntity>(input);
- var id = (await _apiRepository.InsertAsync(entity)).Id;
- return ResponseOutput.Result(id > 0);
- }
- public async Task<IResponseOutput> UpdateAsync(ApiUpdateInput input)
- {
- if (!(input?.Id > 0))
- {
- return ResponseOutput.NotOk();
- }
- var entity = await _apiRepository.GetAsync(input.Id);
- if (!(entity?.Id > 0))
- {
- return ResponseOutput.NotOk("接口不存在!");
- }
- _mapper.Map(input, entity);
- await _apiRepository.UpdateAsync(entity);
- return ResponseOutput.Ok();
- }
- public async Task<IResponseOutput> DeleteAsync(long id)
- {
- var result = false;
- if (id > 0)
- {
- result = (await _apiRepository.DeleteAsync(m => m.Id == id)) > 0;
- }
- return ResponseOutput.Result(result);
- }
- public async Task<IResponseOutput> SoftDeleteAsync(long id)
- {
- var result = await _apiRepository.SoftDeleteAsync(id);
- return ResponseOutput.Result(result);
- }
- public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
- {
- var result = await _apiRepository.SoftDeleteAsync(ids);
- return ResponseOutput.Result(result);
- }
- [Transaction]
- public async Task<IResponseOutput> SyncAsync(ApiSyncInput input)
- {
- //查询所有api
- var apis = await _apiRepository.Select.ToListAsync();
- var paths = apis.Select(a => a.Path).ToList();
- //path处理
- foreach (var api in input.Apis)
- {
- api.Path = api.Path?.Trim().ToLower();
- api.ParentPath = api.ParentPath?.Trim().ToLower();
- }
- #region 执行插入
- //执行父级api插入
- var parentApis = input.Apis.FindAll(a => a.ParentPath.IsNull());
- var pApis = (from a in parentApis where !paths.Contains(a.Path) select a).ToList();
- if (pApis.Count > 0)
- {
- var insertPApis = _mapper.Map<List<ApiEntity>>(pApis);
- insertPApis = await _apiRepository.InsertAsync(insertPApis);
- apis.AddRange(insertPApis);
- }
- //执行子级api插入
- var childApis = input.Apis.FindAll(a => a.ParentPath.NotNull());
- var cApis = (from a in childApis where !paths.Contains(a.Path) select a).ToList();
- if (cApis.Count > 0)
- {
- var insertCApis = _mapper.Map<List<ApiEntity>>(cApis);
- insertCApis = await _apiRepository.InsertAsync(insertCApis);
- apis.AddRange(insertCApis);
- }
- #endregion
- #region 修改和禁用
- {
- //api修改
- ApiEntity a;
- List<string> labels;
- string label;
- string desc;
- foreach (var api in parentApis)
- {
- a = apis.Find(a => a.Path == api.Path);
- if (a?.Id > 0)
- {
- labels = api.Label?.Split("\r\n")?.ToList();
- label = labels != null && labels.Count > 0 ? labels[0] : string.Empty;
- desc = labels != null && labels.Count > 1 ? string.Join("\r\n", labels.GetRange(1, labels.Count() - 1)) : string.Empty;
- a.ParentId = 0;
- a.Label = label;
- a.Description = desc;
- a.Enabled = true;
- }
- }
- }
- {
- //api修改
- ApiEntity a;
- ApiEntity pa;
- List<string> labels;
- string label;
- string desc;
- foreach (var api in childApis)
- {
- a = apis.Find(a => a.Path == api.Path);
- pa = apis.Find(a => a.Path == api.ParentPath);
- if (a?.Id > 0)
- {
- labels = api.Label?.Split("\r\n")?.ToList();
- label = labels != null && labels.Count > 0 ? labels[0] : string.Empty;
- desc = labels != null && labels.Count > 1 ? string.Join("\r\n", labels.GetRange(1, labels.Count() - 1)) : string.Empty;
- a.ParentId = pa?.Id;
- a.Label = label;
- a.Description = desc;
- a.HttpMethods = api.HttpMethods;
- a.Enabled = true;
- }
- }
- }
- {
- //api禁用
- var inputPaths = input.Apis.Select(a => a.Path).ToList();
- var disabledApis = (from a in apis where !inputPaths.Contains(a.Path) select a).ToList();
- if (disabledApis.Count > 0)
- {
- foreach (var api in disabledApis)
- {
- api.Enabled = false;
- }
- }
- }
- #endregion
- //批量更新
- await _apiRepository.UpdateDiy.SetSource(apis)
- .UpdateColumns(a => new { a.ParentId, a.Label, a.HttpMethods,a.Description,a.Enabled })
- .ExecuteAffrowsAsync();
- return ResponseOutput.Ok();
- }
- }
- }
|