using AutoMapper; using Gestor.Infrastructure.Entities.Generic; using Gestor.Infrastructure.Entities.Seguros; using Gestor.Infrastructure.Mappers; using Gestor.Infrastructure.Repository.Generic; using Gestor.Infrastructure.Repository.Interface; using Gestor.Infrastructure.UnitOfWork.Generic; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Runtime.CompilerServices; namespace Gestor.Infrastructure.Repository.Logic { public class ClienteVinculoRepository : GenericRepository, IClienteVinculoRepository, IGenericRepository { public ClienteVinculoRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) { } public void Delete(long id) { base.Delete(base.FindEntityById(id)); } public List DeleteVinculo(long clienteId, List vinculos) { if (vinculos == null) { return null; } List list = ( from x in base.All() where x.Cliente1.Id == clienteId || x.Cliente2.Id == clienteId select x).ToList().Where((ClienteVinculoDb vinculo) => { List clienteVinculos = vinculos; Func u003cu003e9_32 = ClienteVinculoRepository.u003cu003ec.u003cu003e9__3_2; if (u003cu003e9_32 == null) { u003cu003e9_32 = (ClienteVinculo t) => t.Id; ClienteVinculoRepository.u003cu003ec.u003cu003e9__3_2 = u003cu003e9_32; } return !clienteVinculos.Select(u003cu003e9_32).Contains(vinculo.Id); }).ToList(); ClienteVinculoRepository clienteVinculoRepository = this; list.ForEach(new Action(clienteVinculoRepository.Delete)); return ApplicationMapper.Mapper.Map, List>(list); } public List FindByCliente(long id) { List list = ( from x in base.All() where x.Cliente1.Id == id || x.Cliente2.Id == id select x).ToList(); return ApplicationMapper.Mapper.Map, List>(list); } public ClienteVinculo FindById(long id) { ClienteVinculoDb clienteVinculoDb = base.FindEntityById(id); return ApplicationMapper.Mapper.Map(clienteVinculoDb); } public List Inserir(List vinculos, Cliente cliente) { foreach (ClienteVinculo vinculo in vinculos) { if (vinculo.Cliente1.Id == cliente.Id || vinculo.Cliente1.Id == 0) { vinculo.Cliente1 = cliente; } else { if (vinculo.Cliente2.Id != cliente.Id && vinculo.Cliente2.Id != 0) { continue; } vinculo.Cliente2 = cliente; } } List clienteVinculoDbs = ApplicationMapper.Mapper.Map, List>(vinculos); base.AddRange(clienteVinculoDbs); return ApplicationMapper.Mapper.Map, List>(clienteVinculoDbs); } public List Merge(List vinculos, Cliente cliente) { List clienteVinculoDbs = ApplicationMapper.Mapper.Map, List>(vinculos); clienteVinculoDbs.ForEach((ClienteVinculoDb x) => { if (x.Id != 0) { base.Merge(x); return; } x.Cliente1 = ApplicationMapper.Mapper.Map(cliente); this.SaveOrUpdate(x); }); return ApplicationMapper.Mapper.Map, List>(clienteVinculoDbs); } public ClienteVinculo SaveOrUpdate(ClienteVinculo clienteVinculo) { ClienteVinculoDb clienteVinculoDb = ApplicationMapper.Mapper.Map(clienteVinculo); this.SaveOrUpdate(clienteVinculoDb); return ApplicationMapper.Mapper.Map(clienteVinculoDb); } } }