using System.Threading.Tasks; using GxPress.Repository.Interface.Navigation; using GxPress.Service.Interface.Navigation; using System.Transactions; using GxPress.Common.Tools; using System.Linq; using System.Collections.Generic; using GxPress.Request.Navigation; using AutoMapper; using GxPress.Result.MiddleLable; using GxPress.Repository.Interface.SystemLabel; namespace GxPress.Service.Implement.Navigation { public class MiddleLableService : IMiddleLableService { private readonly IMiddleLableRepository middleLableRepository; private readonly IMiddleLableNexusRepository middleLableNexusRepository; private readonly IMapper _mapper; private readonly ISystemLabelRepository systemLabelRepository; public MiddleLableService(IMiddleLableRepository middleLableRepository, IMiddleLableNexusRepository middleLableNexusRepository, IMapper mapper, ISystemLabelRepository systemLabelRepository) { this.middleLableRepository = middleLableRepository; this.middleLableNexusRepository = middleLableNexusRepository; _mapper = mapper; this.systemLabelRepository = systemLabelRepository; } /// /// 添加中间件页面 /// /// /// public async Task InsertAsync(MiddleLableInRequest request) { try { using (var transactionScope = new System.Transactions.TransactionScope()) { var model = _mapper.Map(request); var middleLableId = await middleLableRepository.InsertAsync(model); foreach (var item in request.MiddleLableNexusUpRequests) item.MiddleLableId = middleLableId; await middleLableNexusRepository.InsertAsync(request.MiddleLableNexusUpRequests); transactionScope.Complete(); } } catch { return false; } return true; } /// /// 删除 /// /// /// public async Task DeleteAsync(int id) { try { using (var transactionScope = new System.Transactions.TransactionScope()) { await middleLableRepository.DeleteAsync(id); await middleLableNexusRepository.DeleteByMiddleLableIdAsync(id); transactionScope.Complete(); } } catch { return false; } return true; } public async Task UpdateAsync(MiddleLableUpRequest request) { try { using (var transactionScope = new System.Transactions.TransactionScope()) { await middleLableRepository.UpdateAsync(request); await middleLableNexusRepository.UpdateAsync(request.Id, request.MiddleLableNexusUpRequests); transactionScope.Complete(); } } catch { return false; } return true; } /// /// 详情 /// /// /// public async Task GetMiddleLableResultAsync(int middleLableId) { var middleLable = await middleLableRepository.GetAsync(middleLableId); var middleLableResult = new MiddleLableResult() { Id = middleLable.Id, Name = middleLable.Name, Sort = middleLable.Sort, IsDisable = middleLable.IsDisable, IsSearch = middleLable.IsSearch, IsSlide = middleLable.IsSlide, TypeId = middleLable.TypeId }; middleLableResult.MiddleLableNexusResults = new List(); var middleLableNexusList = await middleLableNexusRepository.GetAllTaskAsync(middleLableId); var systemLabels = await systemLabelRepository.GetAllAsync(middleLableNexusList.Select(n => n.LableId).ToList()); foreach (var middleLableNexus in middleLableNexusList) { foreach (var item in systemLabels) { if (middleLableNexus.LableId == item.Id) { var middleLableNexusResult = new MiddleLableNexusResult() { Id = middleLableNexus.Id, LableId = item.Id, MiddleLableId = middleLableId, MiddleLableName = item.LabelName, Sort = middleLableNexus.Sort }; middleLableResult.MiddleLableNexusResults.Add(middleLableNexusResult); } } } return middleLableResult; } } }