shoc-backend/SeaHaven.Services/Implementation/DispatchService.cs
2026-05-06 10:49:33 -05:00

139 lines
5.1 KiB
C#

using Data.SeaHavenIndustries;
using SeaHaven.DataServices.Interfaces;
using SeaHaven.Services.DTOs;
using SeaHaven.Services.Interfaces;
namespace SeaHaven.Services.Implementation
{
/// <summary>
/// Dispatch service implementation
/// Contains business logic, validation, and orchestration for Dispatch operations
/// </summary>
public class DispatchService : IDispatchService
{
private readonly IDispatchDataService _dispatchDataService;
public DispatchService(IDispatchDataService dispatchDataService)
{
_dispatchDataService = dispatchDataService;
}
public async Task<DispatchDTO?> GetDispatchByIdAsync(int id)
{
var dispatch = await _dispatchDataService.GetByIdAsync(id);
return dispatch == null ? null : MapToDTO(dispatch);
}
public async Task<DispatchDTO?> GetDispatchByIdWithDetailsAsync(int id)
{
var dispatch = await _dispatchDataService.GetByIdWithDetailsAsync(id);
return dispatch == null ? null : MapToDTO(dispatch);
}
public async Task<IEnumerable<DispatchDTO>> GetAllDispatchesAsync()
{
var dispatches = await _dispatchDataService.GetAllAsync();
return dispatches.Select(MapToDTO);
}
public async Task<IEnumerable<DispatchDTO>> GetDispatchesByVendorIdAsync(int vendorId)
{
var dispatches = await _dispatchDataService.GetByVendorIdAsync(vendorId);
return dispatches.Select(MapToDTO);
}
public async Task<PagedResult<DispatchDTO>> GetDispatchesPagedAsync(int page, int pageSize, string? search = null, string? status = null)
{
var (items, totalCount) = await _dispatchDataService.GetPagedAsync(page, pageSize, search, status);
return new PagedResult<DispatchDTO>
{
Items = items.Select(MapToDTO).ToList(),
TotalCount = totalCount,
Page = page,
PageSize = pageSize
};
}
public async Task<DispatchDTO> CreateDispatchAsync(CreateDispatchDTO dto, string userId)
{
var dispatchNumber = await GenerateNextDispatchNumberAsync();
var dispatch = new Dispatch
{
VendorId = dto.VendorId,
ScheduledDate = dto.ScheduledDate,
Status = dto.Status ?? "Pending",
DispatchNumber = dispatchNumber,
CreatedDate = DateTime.UtcNow,
createdby = userId
};
var created = await _dispatchDataService.AddAsync(dispatch);
return MapToDTO(created);
}
public async Task<DispatchDTO> UpdateDispatchAsync(int id, UpdateDispatchDTO dto, string userId)
{
var dispatch = await _dispatchDataService.GetByIdAsync(id);
if (dispatch == null)
throw new InvalidOperationException($"Dispatch with ID {id} not found");
if (dto.VendorId.HasValue) dispatch.VendorId = dto.VendorId.Value;
if (dto.ScheduledDate.HasValue) dispatch.ScheduledDate = dto.ScheduledDate;
if (dto.CompletedDate.HasValue) dispatch.CompletedDate = dto.CompletedDate;
if (dto.Status != null) dispatch.Status = dto.Status;
dispatch.LastModificationTime = DateTime.UtcNow;
if (int.TryParse(userId, out int userIdInt))
dispatch.LastModifierUserId = userIdInt;
await _dispatchDataService.UpdateAsync(dispatch);
return MapToDTO(dispatch);
}
public async Task DeleteDispatchAsync(int id, string userId)
{
var dispatch = await _dispatchDataService.GetByIdAsync(id);
if (dispatch == null)
throw new InvalidOperationException($"Dispatch with ID {id} not found");
dispatch.IsDeleted = true;
dispatch.DeleterUserId = userId;
dispatch.DeletionTime = DateTime.UtcNow;
await _dispatchDataService.UpdateAsync(dispatch);
}
public async Task<bool> DispatchExistsAsync(int id)
{
return await _dispatchDataService.ExistsAsync(id);
}
public async Task<string> GenerateNextDispatchNumberAsync()
{
var count = await _dispatchDataService.CountAsync();
return $"DISP-{(count + 1):D6}";
}
public async Task<int> GetTotalDispatchCountAsync()
{
return await _dispatchDataService.CountAsync();
}
private DispatchDTO MapToDTO(Dispatch dispatch)
{
return new DispatchDTO
{
Id = dispatch.Id,
DispatchNumber = dispatch.DispatchNumber,
Status = dispatch.Status,
VendorId = dispatch.VendorId,
VendorName = dispatch.Vendor?.CompanyName,
ScheduledDate = dispatch.ScheduledDate,
CompletedDate = dispatch.CompletedDate,
CreatedDate = dispatch.CreatedDate,
CreatedBy = dispatch.createdby
};
}
}
}