using Domain.Entities; using Domain.Generics; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Models.Helpers; using Models.Interfaces; using Models.Models; using System.Xml.Linq; namespace Models.Repositories { public class PhSCustomerRepository(PhronCareOperationsHubContext context, ILogger logger) : IPhSCustomerRepository { #region Declaraciones y Constructor private readonly PhronCareOperationsHubContext _context = context; private readonly ILogger _logger = logger; #endregion public async Task> GetAllAsync() { var customers = await _context.PhSCustomers .Include(c => c.Accounttypes) .Include(c => c.PhSCustomerAddresses) .Include(c => c.PhSCustomerDocuments) .Take(100) .ToListAsync(); return customers.Select(EntityMapper.MapEntity); } public async Task GetByIdAsync(int id) { var customer = await _context.PhSCustomers .Include(c => c.Accounttypes) .Include(c => c.PhSCustomerAddresses) .Include(c => c.PhSCustomerDocuments) .Include(c => c.PhSQuoteHeaders) .FirstOrDefaultAsync(c => c.Id == id); return customer != null ? EntityMapper.MapEntity(customer) : null; } public async Task> SearchAsync( string? name, string? email, string? document, int page = 1, int pageSize = 50) { var query = _context.PhSCustomers .Include(c => c.Accounttypes) .Include(c => c.PhSCustomerDocuments) .Include(c => c.PhSCustomerAddresses) .AsQueryable(); if (!string.IsNullOrWhiteSpace(name)) { var lowered = name.ToLower(); query = query.Where(c => c.Name.ToLower().Contains(lowered) || c.BusinessName.ToLower().Contains(lowered)); } if (!string.IsNullOrWhiteSpace(document) && document != "?") { query = query.Where(c => c.PhSCustomerDocuments.Any(d => EF.Functions.Like(d.DocumentNumber, $"%{document}%"))); } if (!string.IsNullOrWhiteSpace(email)) { var lowered = email.ToLower(); query = query.Where(c => c.PhSCustomerAddresses.Any(a => a.Email.ToLower().Contains(lowered))); } var pagedEntities = await query.ToPagedResultAsync(page, pageSize); return new PagedResult { Items = pagedEntities.Items.Select(EntityMapper.MapEntity), TotalItems = pagedEntities.TotalItems, Page = pagedEntities.Page, PageSize = pagedEntities.PageSize }; } //public async Task> SearchAsync(string? name, string? email, string? document) //{ // var query = _context.PhSCustomers // .Include(c => c.Accounttypes) // .Include(c => c.PhSCustomerDocuments) // .Include(c => c.PhSCustomerAddresses) // .Take(100) // .AsQueryable(); // if (!string.IsNullOrWhiteSpace(name)) // { // var loweredName = name.ToLower(); // query = query.Where(c => // c.Name.ToLower().Contains(loweredName) || // c.BusinessName.ToLower().Contains(loweredName)); // } // if (!string.IsNullOrWhiteSpace(email)) // { // var loweredEmail = email.ToLower(); // query = query.Where(c => // c.PhSCustomerAddresses.Any(a => // a.Email.ToLower().Contains(loweredEmail))); // } // if (!string.IsNullOrWhiteSpace(document) && document != "?") // { // query = query.Where(c => // c.PhSCustomerDocuments.Any(a => // EF.Functions.Like(a.DocumentNumber, $"%{document}%"))); // } // var customers = await query.ToListAsync(); // Console.WriteLine($"VALOR RECIBIDO DE 'name': {name}"); // Console.WriteLine($"VALOR RECIBIDO DE 'email': {email}"); // Console.WriteLine($"VALOR RECIBIDO DE 'document': {document}"); // return customers.Select(EntityMapper.MapEntity); //} public async Task AddAsync(ECustomer entity) { var customer = EntityMapper.MapEntity(entity); await _context.PhSCustomers.AddAsync(customer); await _context.SaveChangesAsync(); return EntityMapper.MapEntity(customer); } public async Task UpdateAsync(ECustomer entity) { var customer = await _context.PhSCustomers.FindAsync(entity.Id); if (customer == null) return false; _context.Entry(customer).CurrentValues.SetValues(entity); await _context.SaveChangesAsync(); return true; } public async Task DeleteAsync(int id) { var customer = await _context.PhSCustomers.FindAsync(id); if (customer == null) return false; _context.PhSCustomers.Remove(customer); await _context.SaveChangesAsync(); return true; } } }