using AutoMapper; using Kean.Application.Command.Interfaces; using Kean.Application.Command.ViewModels; using Kean.Domain; using Kean.Domain.Material.Commands; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Kean.Application.Command.Implements { /// /// 物料信息命令服务实现 /// public class MaterialService : IMaterialService { private readonly ICommandBus _bus; // 命令总线 private readonly IMapper _mapper; // 模型映射 private readonly INotification _notifications; // 总线通知 /// /// 依赖注入 /// public MaterialService( ICommandBus bus, IMapper mapper, INotification notifications) { _bus = bus; _mapper = mapper; _notifications = notifications; } /* * 实现 Kean.Application.Command.Interfaces.IMaterialService.CreateMaterial 方法 */ public async Task<(int Id, Failure Failure)> CreateMaterial(Material material) { var command = _mapper.Map(material); await _bus.Execute(command); return (command.Id, _notifications.FirstOrDefault()); } /* * 实现 Kean.Application.Command.Interfaces.IMaterialService.ModifyMaterial 方法 */ public async Task<(bool Success, Failure Failure)> ModifyMaterial(Material material) { await _bus.Execute(_mapper.Map(material)); var failure = _notifications.FirstOrDefault(); return (failure == null, failure); } /* * 实现 Kean.Application.Command.Interfaces.IMaterialService.DeleteMaterial 方法 */ public async Task> DeleteMaterial(IEnumerable id) { var command = new DeleteMaterialCommand { Id = id }; await _bus.Execute(command); return command.Id; } /* * 实现 Kean.Application.Command.Interfaces.IMaterialService.CreateCategory 方法 */ public async Task<(int Id, Failure Failure)> CreateCategory(Category category) { var command = _mapper.Map(category); await _bus.Execute(command); return (command.Id, _notifications.FirstOrDefault()); } /* * 实现 Kean.Application.Command.Interfaces.IMaterialService.ModifyCategory 方法 */ public async Task<(bool Success, Failure Failure)> ModifyCategory(Category category) { await _bus.Execute(_mapper.Map(category)); var failure = _notifications.FirstOrDefault(); return (failure == null, failure); } /* * 实现 Kean.Application.Command.Interfaces.IMaterialService.DeleteCategory 方法 */ public async Task> DeleteCategory(IEnumerable id) { var command = new DeleteCategoryCommand { Id = id }; await _bus.Execute(command); return command.Id; } /* * 实现 Kean.Application.Command.Interfaces.IMaterialService.CreateSafety 方法 */ public async Task<(int Id, Failure Failure)> CreateSafety(Safety safety) { var command = _mapper.Map(safety); await _bus.Execute(command); return (command.Id, _notifications.FirstOrDefault()); } /* * 实现 Kean.Application.Command.Interfaces.IMaterialService.ModifySafety 方法 */ public async Task<(bool Success, Failure Failure)> ModifySafety(Safety safety) { await _bus.Execute(_mapper.Map(safety)); var failure = _notifications.FirstOrDefault(); return (failure == null, failure); } /* * 实现 Kean.Application.Command.Interfaces.IMaterialService.DeleteSafety 方法 */ public async Task> DeleteSafety(IEnumerable id) { var command = new DeleteSafetyCommand { Id = id }; await _bus.Execute(command); return command.Id; } } }