using AutoMapper; using Microsoft.EntityFrameworkCore; using SytelineSaAppEfDataModel.Dtos; namespace SytelineSaAppEfDataModel.Services { public class CustomerOrderService(SytelineSaAppDbContext context, IMapper mapper) : ICustomerOrderService { public async Task> GetAll() { return await context.CustomerOrders.Select(x => mapper.Map(x)).ToListAsync(); } public async Task GetByOrderNumber(Guid orderNumber) { CustomerOrderDto? customerOrder = await context.CustomerOrders .Where(x => x.RowPointer == orderNumber) .Select(x => mapper.Map(x)).FirstOrDefaultAsync(); if (customerOrder == null) return null; customerOrder.CustomerOrderLines = await context.CustomerOrderLines .Where(x => x.CoNum == customerOrder.CoNum) .Select(x => mapper.Map(x)).ToListAsync(); foreach (CustomerOrderLineDto customerOrderLine in customerOrder.CustomerOrderLines) { customerOrderLine.CustomerOrderLineItems = await context.CustomerOrderLineItems .Where(x => x.CoNum == customerOrder.CoNum && x.CoLine == customerOrderLine.CoLine) .Select(x => mapper.Map(x)).ToListAsync(); } IList ediCustomerOrderTranslates = await context.EdiCustomerOrderTranslates .Where(x => x.CoCoNum == customerOrder.CoNum) .Select(x => mapper.Map(x)).ToListAsync(); customerOrder.EdiCustomerOrderTranslates = ediCustomerOrderTranslates; return customerOrder; } public async Task GetByCoNumber(string orderNumber) { CustomerOrderDto? customerOrder = await context.CustomerOrders .Where(x => x.CoNum == orderNumber) .Select(x => mapper.Map(x)).FirstOrDefaultAsync(); if (customerOrder == null) return null; customerOrder.CustomerOrderLines = await context.CustomerOrderLines .Where(x => x.CoNum == customerOrder.CoNum) .Select(x => mapper.Map(x)).ToListAsync(); foreach (CustomerOrderLineDto customerOrderLine in customerOrder.CustomerOrderLines) { customerOrderLine.CustomerOrderLineItems = await context.CustomerOrderLineItems .Where(x => x.CoNum == customerOrder.CoNum && x.CoLine == customerOrderLine.CoLine) .Select(x => mapper.Map(x)).ToListAsync(); } IList ediCustomerOrderTranslates = await context.EdiCustomerOrderTranslates .Where(x => x.CoCoNum == customerOrder.CoNum) .Select(x => mapper.Map(x)).ToListAsync(); customerOrder.EdiCustomerOrderTranslates = ediCustomerOrderTranslates; return customerOrder; } public async Task GetByCustomerAndPo(string customerNumber, int customerSequence, string poNumber) { return await context.CustomerOrders .Where(x => x.CustNum == customerNumber && x.CustSeq == customerSequence && x.CustPo == poNumber) .Select(x => mapper.Map(x)).FirstOrDefaultAsync(); } public async Task GetByPo(string poNumber) { return await context.CustomerOrders.Where(x => x.CustPo == poNumber && x.Stat == "O") .OrderByDescending(x => x.CreateDate).Select(x => mapper.Map(x)) .FirstOrDefaultAsync(); } public async Task> GetListByCustomerAndPo(string customerNumber, int customerSequence, string poNumber) { return await context.CustomerOrders .Where(x => x.CustNum == customerNumber && x.CustSeq == customerSequence && x.CustPo == poNumber) .Select(x => mapper.Map(x)).ToListAsync(); } public async Task> GetListByCustomer(string customerNumber, int customerSequence) { return await context.CustomerOrders.Where(x => x.CustNum == customerNumber && x.CustSeq == customerSequence) .Select(x => mapper.Map(x)).ToListAsync(); } public async Task?> GetLinesByCoNumber(string customerOrderNumber) { List customerOrderLines = await context.CustomerOrderLines .Where(x => x.CoNum == customerOrderNumber).Select(x => mapper.Map(x)) .ToListAsync(); return customerOrderLines; } public async Task?> GetItemsByCoNumber(string customerOrderNumber) { List customerOrderLineItems = await context.CustomerOrderLineItems .Where(x => x.CoNum == customerOrderNumber) .Select(x => mapper.Map(x)).ToListAsync(); return customerOrderLineItems; } } }