mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 14:13:12 +00:00
367 lines
14 KiB
C#
367 lines
14 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<(IEnumerable<object> Items, int TotalCount)> GetWorkOrderListPagedAsync(
|
|
int page,
|
|
int pageSize,
|
|
string? search = null,
|
|
string? status = null,
|
|
string? assignee = null,
|
|
int? locationId = null,
|
|
string? sortBy = "lastUpdated",
|
|
string? sortDir = "desc")
|
|
{
|
|
search ??= "";
|
|
|
|
var query = _context.workOrders
|
|
.Include(w => w.Locations)
|
|
.Include(w => w.AssignToUser)
|
|
.Where(w => w.istemplate != true);
|
|
|
|
if (locationId.HasValue)
|
|
query = query.Where(w => w.LocationId == locationId.Value);
|
|
|
|
if (!string.IsNullOrWhiteSpace(status))
|
|
query = query.Where(w => w.Status == status);
|
|
|
|
if (assignee == "__unassigned")
|
|
query = query.Where(w => w.AssignTo == null || w.AssignTo == "");
|
|
else if (!string.IsNullOrWhiteSpace(assignee))
|
|
query = query.Where(w => w.AssignToUser != null && (w.AssignToUser.FirstName + " " + w.AssignToUser.LastName).Contains(assignee));
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
var s = search.ToLower();
|
|
query = query.Where(w =>
|
|
(w.InternalWONumber ?? "").ToLower().Contains(s) ||
|
|
(w.WorkerOrderNumber ?? "").ToLower().Contains(s) ||
|
|
(w.WorkerOrderTitle ?? "").ToLower().Contains(s) ||
|
|
(w.Locations.Title ?? "").ToLower().Contains(s) ||
|
|
(w.Locations.Name ?? "").ToLower().Contains(s)
|
|
);
|
|
}
|
|
|
|
var totalCount = await query.CountAsync();
|
|
|
|
var isAsc = sortDir?.ToLower() == "asc";
|
|
IOrderedQueryable<WorkOrder> ordered = (sortBy?.ToLower()) switch
|
|
{
|
|
"number" => isAsc ? query.OrderBy(w => w.InternalWONumber) : query.OrderByDescending(w => w.InternalWONumber),
|
|
"title" => isAsc ? query.OrderBy(w => w.WorkerOrderTitle) : query.OrderByDescending(w => w.WorkerOrderTitle),
|
|
"location" => isAsc ? query.OrderBy(w => w.Locations != null ? w.Locations.Name : "") : query.OrderByDescending(w => w.Locations != null ? w.Locations.Name : ""),
|
|
"priority" => isAsc ? query.OrderBy(w => w.Priority) : query.OrderByDescending(w => w.Priority),
|
|
"status" => isAsc ? query.OrderBy(w => w.Status) : query.OrderByDescending(w => w.Status),
|
|
"createddate" => isAsc ? query.OrderBy(w => w.CreatedDate) : query.OrderByDescending(w => w.CreatedDate),
|
|
"assignedto" => isAsc ? query.OrderBy(w => w.AssignToUser != null ? w.AssignToUser.FirstName : "") : query.OrderByDescending(w => w.AssignToUser != null ? w.AssignToUser.FirstName : ""),
|
|
"lastupdated" => isAsc ? query.OrderBy(w => w.LastModificationTime) : query.OrderByDescending(w => w.LastModificationTime),
|
|
_ => query.OrderByDescending(w => w.Id)
|
|
};
|
|
|
|
var data = await ordered
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.Select(w => new
|
|
{
|
|
id = w.Id,
|
|
internalWONumber = w.InternalWONumber,
|
|
workOrderNumber = w.WorkerOrderNumber,
|
|
number = w.InternalWONumber ?? w.WorkerOrderNumber,
|
|
title = w.WorkerOrderTitle,
|
|
location = w.Locations != null ? w.Locations.Name : "",
|
|
locationName = w.Locations != null ? w.Locations.Name : "",
|
|
priority = w.Priority,
|
|
status = w.Status,
|
|
createdDate = w.CreatedDate,
|
|
dueDate = w.DueDate,
|
|
assignedTo = w.AssignToUser != null ? w.AssignToUser.FirstName + " " + w.AssignToUser.LastName : "",
|
|
employeeName = w.AssignToUser != null ? w.AssignToUser.FirstName + " " + w.AssignToUser.LastName : "",
|
|
lastUpdated = new[] {
|
|
w.LastModificationTime,
|
|
w.Comments.Max(c => (DateTime?)c.CreatedDate),
|
|
_context.WorkOrderAuditLogs.Where(a => a.WorkOrderId == w.Id).Max(a => (DateTime?)a.CreatedAt)
|
|
}.Where(d => d.HasValue).Max()
|
|
})
|
|
.ToListAsync();
|
|
|
|
return (data, 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<IEnumerable<WorkOrderAuditLog>> GetWorkOrderAuditLogsAsync(int workOrderId)
|
|
{
|
|
return await _context.WorkOrderAuditLogs
|
|
.Where(a => a.WorkOrderId == workOrderId)
|
|
.OrderByDescending(a => a.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task AddWorkOrderContactsAsync(int workOrderId, IEnumerable<int> contactIds)
|
|
{
|
|
var contacts = contactIds.Select(cid => new WorkOrderContacts
|
|
{
|
|
WorkorderId = workOrderId,
|
|
ContactId = cid
|
|
});
|
|
|
|
await _context.WorkOrderContacts.AddRangeAsync(contacts);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task AddWorkOrderCategoriesAsync(int workOrderId, IEnumerable<int> categoryIds)
|
|
{
|
|
var categories = categoryIds.Select(catId => new WorkOrderCategories
|
|
{
|
|
WorkorderId = workOrderId,
|
|
CategoryId = catId
|
|
});
|
|
|
|
await _context.workOrderCategories.AddRangeAsync(categories);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task AddWorkOrderAttachmentAsync(WorkOrderAttachments attachment)
|
|
{
|
|
await _context.workOrderAttachments.AddAsync(attachment);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task AddAuditLogAsync(WorkOrderAuditLog auditLog)
|
|
{
|
|
await _context.WorkOrderAuditLogs.AddAsync(auditLog);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteWorkOrderContactsAsync(int workOrderId)
|
|
{
|
|
var contacts = await _context.WorkOrderContacts
|
|
.Where(wc => wc.WorkorderId == workOrderId)
|
|
.ToListAsync();
|
|
|
|
if (contacts.Any())
|
|
{
|
|
_context.WorkOrderContacts.RemoveRange(contacts);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task DeleteWorkOrderCategoriesAsync(int workOrderId)
|
|
{
|
|
var categories = await _context.workOrderCategories
|
|
.Where(wc => wc.WorkorderId == workOrderId)
|
|
.ToListAsync();
|
|
|
|
if (categories.Any())
|
|
{
|
|
_context.workOrderCategories.RemoveRange(categories);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task DeleteWorkOrderAttachmentsAsync(int workOrderId)
|
|
{
|
|
var attachments = await _context.workOrderAttachments
|
|
.Where(wa => wa.WorkorderId == workOrderId)
|
|
.ToListAsync();
|
|
|
|
if (attachments.Any())
|
|
{
|
|
_context.workOrderAttachments.RemoveRange(attachments);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public async Task<IEnumerable<WorkOrder>> GetAllWithDetailsAsync()
|
|
{
|
|
return await _context.workOrders
|
|
.Where(w => w.istemplate != true)
|
|
.Include(w => w.Locations)
|
|
.Include(w => w.AssignToUser)
|
|
.AsSplitQuery()
|
|
.ToListAsync();
|
|
}
|
|
}
|
|
}
|