mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 07:13:12 +00:00
190 lines
6.1 KiB
C#
190 lines
6.1 KiB
C#
|
|
using Data.SeaHavenIndustries;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using SeaHaven.DataServices.Interfaces;
|
||
|
|
|
||
|
|
namespace SeaHaven.DataServices.Implementation
|
||
|
|
{
|
||
|
|
public class WorkOrderDataService : IWorkOrderDataService
|
||
|
|
{
|
||
|
|
private readonly ApplicationDbContext _context;
|
||
|
|
|
||
|
|
public WorkOrderDataService(ApplicationDbContext context)
|
||
|
|
{
|
||
|
|
_context = context;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<WorkOrder?> GetByIdAsync(int id)
|
||
|
|
{
|
||
|
|
return await _context.workOrders.FindAsync(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<WorkOrder?> GetByIdWithDetailsAsync(int id)
|
||
|
|
{
|
||
|
|
return await _context.workOrders
|
||
|
|
.Include(w => w.Locations)
|
||
|
|
.Include(w => w.AssignToUser)
|
||
|
|
.FirstOrDefaultAsync(w => w.Id == id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<WorkOrder?> GetByInternalNumberAsync(string internalNumber)
|
||
|
|
{
|
||
|
|
return await _context.workOrders
|
||
|
|
.FirstOrDefaultAsync(w => w.InternalWONumber == internalNumber);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<WorkOrder>> GetAllAsync()
|
||
|
|
{
|
||
|
|
return await _context.workOrders.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<WorkOrder>> GetByLocationIdAsync(int locationId)
|
||
|
|
{
|
||
|
|
return await _context.workOrders
|
||
|
|
.Where(w => w.LocationId == locationId)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<WorkOrder>> GetByAssigneeAsync(string assignee)
|
||
|
|
{
|
||
|
|
return await _context.workOrders
|
||
|
|
.Where(w => w.AssignTo == assignee)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<WorkOrder>> GetByStatusAsync(string status)
|
||
|
|
{
|
||
|
|
return await _context.workOrders
|
||
|
|
.Where(w => w.Status == status)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<WorkOrder>> GetByPriorityAsync(string priority)
|
||
|
|
{
|
||
|
|
return await _context.workOrders
|
||
|
|
.Where(w => w.Priority == priority)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(IEnumerable<WorkOrder> Items, int TotalCount)> GetPagedAsync(
|
||
|
|
int page,
|
||
|
|
int pageSize,
|
||
|
|
string? search = null,
|
||
|
|
string? status = null,
|
||
|
|
string? priority = null)
|
||
|
|
{
|
||
|
|
var query = _context.workOrders.AsQueryable();
|
||
|
|
|
||
|
|
if (!string.IsNullOrWhiteSpace(search))
|
||
|
|
{
|
||
|
|
query = query.Where(w =>
|
||
|
|
w.WorkerOrderNumber!.Contains(search) ||
|
||
|
|
w.WorkerOrderTitle!.Contains(search));
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!string.IsNullOrWhiteSpace(status))
|
||
|
|
{
|
||
|
|
query = query.Where(w => w.Status == status);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!string.IsNullOrWhiteSpace(priority))
|
||
|
|
{
|
||
|
|
query = query.Where(w => w.Priority == priority);
|
||
|
|
}
|
||
|
|
|
||
|
|
var totalCount = await query.CountAsync();
|
||
|
|
var items = await query
|
||
|
|
.OrderByDescending(w => w.CreatedDate)
|
||
|
|
.Skip((page - 1) * pageSize)
|
||
|
|
.Take(pageSize)
|
||
|
|
.ToListAsync();
|
||
|
|
|
||
|
|
return (items, totalCount);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<WorkOrder> AddAsync(WorkOrder workOrder)
|
||
|
|
{
|
||
|
|
workOrder.CreatedDate = DateTime.Now;
|
||
|
|
await _context.workOrders.AddAsync(workOrder);
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
return workOrder;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task UpdateAsync(WorkOrder workOrder)
|
||
|
|
{
|
||
|
|
workOrder.LastModificationTime = DateTime.Now;
|
||
|
|
_context.workOrders.Update(workOrder);
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task DeleteAsync(int id)
|
||
|
|
{
|
||
|
|
var entity = await GetByIdAsync(id);
|
||
|
|
if (entity != null)
|
||
|
|
{
|
||
|
|
_context.workOrders.Remove(entity);
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<WorkOrderContacts>> GetWorkOrderContactsAsync(int workOrderId)
|
||
|
|
{
|
||
|
|
return await _context.WorkOrderContacts
|
||
|
|
.Where(wc => wc.WorkorderId == workOrderId)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<WorkOrderCategories>> GetWorkOrderCategoriesAsync(int workOrderId)
|
||
|
|
{
|
||
|
|
return await _context.workOrderCategories
|
||
|
|
.Where(wc => wc.WorkorderId == workOrderId)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<WorkOrderAttachments>> GetWorkOrderAttachmentsAsync(int workOrderId)
|
||
|
|
{
|
||
|
|
return await _context.workOrderAttachments
|
||
|
|
.Where(wa => wa.WorkorderId == workOrderId)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<Comments>> GetWorkOrderCommentsAsync(int workOrderId)
|
||
|
|
{
|
||
|
|
return await _context.Comments
|
||
|
|
.Where(c => c.WorkerOrderId == workOrderId)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> ExistsAsync(int id)
|
||
|
|
{
|
||
|
|
return await _context.workOrders.AnyAsync(w => w.Id == id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> InternalNumberExistsAsync(string internalNumber, int? excludeId = null)
|
||
|
|
{
|
||
|
|
return await _context.workOrders.AnyAsync(w =>
|
||
|
|
w.InternalWONumber == internalNumber &&
|
||
|
|
(excludeId == null || w.Id != excludeId));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<string> GenerateNextInternalWONumberAsync()
|
||
|
|
{
|
||
|
|
return await Task.FromResult($"WO-{DateTime.Now:yyyyMMdd}-{new Random().Next(1000, 9999)}");
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<int> CountAsync()
|
||
|
|
{
|
||
|
|
return await _context.workOrders.CountAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<int> CountByStatusAsync(string status)
|
||
|
|
{
|
||
|
|
return await _context.workOrders.CountAsync(w => w.Status == status);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<int> CountByLocationAsync(int locationId)
|
||
|
|
{
|
||
|
|
return await _context.workOrders.CountAsync(w => w.LocationId == locationId);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|