mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 14:13:12 +00:00
220 lines
7.2 KiB
C#
220 lines
7.2 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
|
|
namespace SeaHaven.DataServices.Implementation
|
|
{
|
|
public class DispatchDataService : IDispatchDataService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public DispatchDataService(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<Dispatch?> GetByIdAsync(int id)
|
|
{
|
|
return await _context.Dispatches.FindAsync(id);
|
|
}
|
|
|
|
public async Task<Dispatch?> GetByIdWithDetailsAsync(int id)
|
|
{
|
|
return await _context.Dispatches
|
|
.Include(d => d.Vendor)
|
|
.FirstOrDefaultAsync(d => d.Id == id);
|
|
}
|
|
|
|
public async Task<IEnumerable<Dispatch>> GetAllAsync()
|
|
{
|
|
return await _context.Dispatches.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Dispatch>> GetByVendorIdAsync(int vendorId)
|
|
{
|
|
return await _context.Dispatches
|
|
.Where(d => d.VendorId == vendorId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Dispatch>> GetByStatusAsync(string status)
|
|
{
|
|
return await _context.Dispatches
|
|
.Where(d => d.Status == status)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<(IEnumerable<Dispatch> Items, int TotalCount)> GetPagedAsync(
|
|
int page,
|
|
int pageSize,
|
|
string? search = null,
|
|
string? status = null)
|
|
{
|
|
var query = _context.Dispatches.AsQueryable();
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
query = query.Where(d => d.DispatchNumber!.Contains(search));
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(status))
|
|
{
|
|
query = query.Where(d => d.Status == status);
|
|
}
|
|
|
|
var totalCount = await query.CountAsync();
|
|
var items = await query
|
|
.OrderByDescending(d => d.CreatedDate)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
return (items, totalCount);
|
|
}
|
|
|
|
public async Task<Dispatch> AddAsync(Dispatch dispatch)
|
|
{
|
|
dispatch.CreatedDate = DateTime.Now;
|
|
await _context.Dispatches.AddAsync(dispatch);
|
|
await _context.SaveChangesAsync();
|
|
return dispatch;
|
|
}
|
|
|
|
public async Task UpdateAsync(Dispatch dispatch)
|
|
{
|
|
dispatch.LastModificationTime = DateTime.Now;
|
|
_context.Dispatches.Update(dispatch);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteAsync(int id)
|
|
{
|
|
var entity = await GetByIdAsync(id);
|
|
if (entity != null)
|
|
{
|
|
_context.Dispatches.Remove(entity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(int id)
|
|
{
|
|
return await _context.Dispatches.AnyAsync(d => d.Id == id);
|
|
}
|
|
|
|
public async Task<string> GenerateNextDispatchNumberAsync()
|
|
{
|
|
return await Task.FromResult($"DISP-{DateTime.Now:yyyyMMdd}-{new Random().Next(1000, 9999)}");
|
|
}
|
|
|
|
public async Task<int> CountAsync()
|
|
{
|
|
return await _context.Dispatches.CountAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Dispatch>> GetByWorkOrderIdAsync(int workOrderId)
|
|
{
|
|
var dispatchIds = await _context.DispatchWorkOrders
|
|
.Where(dwo => dwo.WorkOrderId == workOrderId)
|
|
.Select(dwo => dwo.DispatchId)
|
|
.Distinct()
|
|
.ToListAsync();
|
|
|
|
var directIds = await _context.Dispatches
|
|
.Where(d => d.WorkOrderId == workOrderId)
|
|
.Select(d => d.Id)
|
|
.ToListAsync();
|
|
|
|
var allIds = dispatchIds.Union(directIds).Distinct().ToList();
|
|
|
|
return await _context.Dispatches
|
|
.Where(d => allIds.Contains(d.Id))
|
|
.Include(d => d.Vendor)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Dispatch>> GetByWorkOrderIdsAsync(IEnumerable<int> workOrderIds)
|
|
{
|
|
return await _context.Dispatches
|
|
.Where(d => workOrderIds.Contains(d.WorkOrderId ?? 0))
|
|
.Include(d => d.Vendor)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<string> GenerateNextPONumberAsync()
|
|
{
|
|
var lastPO = await _context.Dispatches
|
|
.Where(d => d.PONumber != null)
|
|
.OrderByDescending(d => d.PONumber)
|
|
.Select(d => d.PONumber)
|
|
.FirstOrDefaultAsync();
|
|
|
|
int nextPO = 1000;
|
|
if (!string.IsNullOrEmpty(lastPO) && int.TryParse(lastPO, out int lastNum))
|
|
{
|
|
nextPO = lastNum + 1;
|
|
}
|
|
|
|
return nextPO.ToString();
|
|
}
|
|
|
|
public async Task<IEnumerable<DispatchChecklistItem>> GetChecklistItemsAsync(int dispatchId)
|
|
{
|
|
return await _context.DispatchChecklistItems
|
|
.Where(ci => ci.DispatchId == dispatchId)
|
|
.OrderBy(ci => ci.SortOrder)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<DispatchChecklistItem?> GetChecklistItemByIdAsync(int id)
|
|
{
|
|
return await _context.DispatchChecklistItems.FindAsync(id);
|
|
}
|
|
|
|
public async Task AddChecklistItemAsync(DispatchChecklistItem item)
|
|
{
|
|
await _context.DispatchChecklistItems.AddAsync(item);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateChecklistItemAsync(DispatchChecklistItem item)
|
|
{
|
|
_context.DispatchChecklistItems.Update(item);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<DispatchSignoff>> GetSignoffsAsync(int dispatchId)
|
|
{
|
|
return await _context.DispatchSignoffs
|
|
.Where(s => s.DispatchId == dispatchId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task AddSignoffAsync(DispatchSignoff signoff)
|
|
{
|
|
signoff.SignedAt = DateTime.UtcNow;
|
|
await _context.DispatchSignoffs.AddAsync(signoff);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<bool> HasSignoffTypeAsync(int dispatchId, string signoffType)
|
|
{
|
|
return await _context.DispatchSignoffs
|
|
.AnyAsync(s => s.DispatchId == dispatchId && s.SignoffType == signoffType);
|
|
}
|
|
|
|
public async Task<IEnumerable<DispatchWorkOrder>> GetDispatchWorkOrdersAsync(int dispatchId)
|
|
{
|
|
return await _context.DispatchWorkOrders
|
|
.Where(dwo => dwo.DispatchId == dispatchId)
|
|
.Include(dwo => dwo.WorkOrder)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task AddDispatchWorkOrderAsync(DispatchWorkOrder dispatchWorkOrder)
|
|
{
|
|
await _context.DispatchWorkOrders.AddAsync(dispatchWorkOrder);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|