mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 13:03:12 +00:00
WorkOrderDataService.AddAsync overwrote the UTC CreatedDate set by WorkOrderService with local server time, offsetting the SLA response clock on any host not running in UTC. Data services now stamp CreatedDate, LastModificationTime and DeletionTime with DateTime.UtcNow, and a work order keeps the creation time its caller set.
257 lines
9 KiB
C#
257 lines
9 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
|
|
namespace SeaHaven.DataServices.Implementation
|
|
{
|
|
public class FollowUpDataService : IFollowUpDataService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public FollowUpDataService(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<FollowUps?> GetByIdAsync(int id)
|
|
{
|
|
return await _context.FollowUps.FindAsync(id);
|
|
}
|
|
|
|
public async Task<FollowUps?> GetByIdWithDetailsAsync(int id)
|
|
{
|
|
return await _context.FollowUps
|
|
.Include(x => x.Employee)
|
|
.Include(x => x.Account)
|
|
.Include(x => x.Location)
|
|
// Removed .Include(x => x.WorkOrder) due to schema mismatch - WorkOrder has columns not in DB
|
|
.FirstOrDefaultAsync(f => f.Id == id);
|
|
}
|
|
|
|
public async Task<IEnumerable<FollowUps>> GetAllAsync()
|
|
{
|
|
return await _context.FollowUps.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<FollowUps>> GetByWorkOrderIdAsync(int workOrderId)
|
|
{
|
|
return await _context.FollowUps
|
|
.Where(f => f.WorkOrderId == workOrderId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<FollowUps>> GetByUserIdAsync(string userId)
|
|
{
|
|
return await _context.FollowUps
|
|
.Where(f => f.createdby == userId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<(IEnumerable<FollowUps> Items, int TotalCount)> GetPagedAsync(
|
|
int page,
|
|
int pageSize,
|
|
string? search = null,
|
|
string? status = null)
|
|
{
|
|
search ??= "";
|
|
status = string.IsNullOrWhiteSpace(status) ? "Open" : status;
|
|
|
|
var query = _context.FollowUps
|
|
.Include(x => x.Employee)
|
|
.Include(x => x.Account)
|
|
.Include(x => x.Location)
|
|
// Removed .Include(x => x.WorkOrder) due to schema mismatch - WorkOrder has columns not in DB
|
|
.Where(x => x.Status == status);
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
var s = search.ToLower();
|
|
query = query.Where(x =>
|
|
(x.Reason ?? "").ToLower().Contains(s) ||
|
|
(x.Account != null && (x.Account.Name ?? "").ToLower().Contains(s)) ||
|
|
(x.Employee != null &&
|
|
(((x.Employee.FirstName ?? "") + " " + (x.Employee.LastName ?? ""))
|
|
.ToLower().Contains(s))
|
|
)
|
|
);
|
|
}
|
|
|
|
var totalCount = await query.CountAsync();
|
|
var items = await query
|
|
.OrderByDescending(f => f.Id)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
return (items, totalCount);
|
|
}
|
|
|
|
public async Task<FollowUps> AddAsync(FollowUps followUp)
|
|
{
|
|
followUp.CreatedDate = DateTime.UtcNow;
|
|
await _context.FollowUps.AddAsync(followUp);
|
|
await _context.SaveChangesAsync();
|
|
return followUp;
|
|
}
|
|
|
|
public async Task UpdateAsync(FollowUps followUp)
|
|
{
|
|
followUp.LastModificationTime = DateTime.UtcNow;
|
|
_context.FollowUps.Update(followUp);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteAsync(int id)
|
|
{
|
|
var entity = await GetByIdAsync(id);
|
|
if (entity != null)
|
|
{
|
|
_context.FollowUps.Remove(entity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(int id)
|
|
{
|
|
return await _context.FollowUps.AnyAsync(f => f.Id == id);
|
|
}
|
|
|
|
public async Task<int> CountAsync()
|
|
{
|
|
return await _context.FollowUps.CountAsync();
|
|
}
|
|
|
|
public async Task<List<FollowUps>> GetListPagedAsync(
|
|
int page,
|
|
int pageSize,
|
|
string? search,
|
|
string? status,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
search ??= "";
|
|
status = string.IsNullOrWhiteSpace(status) ? "Open" : status;
|
|
|
|
var query = _context.FollowUps
|
|
.Include(x => x.Employee)
|
|
.Include(x => x.Account)
|
|
.Include(x => x.Location)
|
|
.AsNoTracking()
|
|
.Where(x => x.Status == status);
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
var s = search.ToLower();
|
|
query = query.Where(x =>
|
|
(x.Reason ?? "").ToLower().Contains(s) ||
|
|
(x.Account != null && (x.Account.Name ?? "").ToLower().Contains(s)) ||
|
|
(x.Employee != null &&
|
|
(((x.Employee.FirstName ?? "") + " " + (x.Employee.LastName ?? ""))
|
|
.ToLower().Contains(s))
|
|
)
|
|
);
|
|
}
|
|
|
|
return await query
|
|
.OrderByDescending(f => f.Id)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<int> GetListTotalAsync(string? search, string? status, CancellationToken cancellationToken)
|
|
{
|
|
search ??= "";
|
|
status = string.IsNullOrWhiteSpace(status) ? "Open" : status;
|
|
|
|
var query = _context.FollowUps
|
|
.AsNoTracking()
|
|
.Where(x => x.Status == status);
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
var s = search.ToLower();
|
|
query = query.Where(x =>
|
|
(x.Reason ?? "").ToLower().Contains(s) ||
|
|
(x.Account != null && (x.Account.Name ?? "").ToLower().Contains(s)) ||
|
|
(x.Employee != null &&
|
|
(((x.Employee.FirstName ?? "") + " " + (x.Employee.LastName ?? ""))
|
|
.ToLower().Contains(s))
|
|
)
|
|
);
|
|
}
|
|
|
|
return await query.CountAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<FollowUps?> GetDetailByIdAsync(int id, CancellationToken cancellationToken)
|
|
{
|
|
return await _context.FollowUps
|
|
.Include(x => x.Employee)
|
|
.Include(x => x.Account)
|
|
.Include(x => x.Location)
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(f => f.Id == id, cancellationToken);
|
|
}
|
|
|
|
public async Task<FollowUps?> GetByIdForUpdateAsync(int id, CancellationToken cancellationToken)
|
|
{
|
|
return await _context.FollowUps.FindAsync(new object[] { id }, cancellationToken);
|
|
}
|
|
|
|
public async Task<string> GetUserNameAsync(string? userId, CancellationToken cancellationToken)
|
|
{
|
|
if (string.IsNullOrEmpty(userId))
|
|
return "";
|
|
|
|
var u = await _context.Users
|
|
.AsNoTracking()
|
|
.Where(x => x.Id == userId)
|
|
.Select(x => new { x.FirstName, x.LastName, x.UserName })
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
|
|
if (u == null)
|
|
return "";
|
|
|
|
var n = ((u.FirstName ?? "") + " " + (u.LastName ?? "")).Trim();
|
|
return string.IsNullOrWhiteSpace(n) ? (u.UserName ?? "") : n;
|
|
}
|
|
|
|
public async Task<FollowUps> AddAsync(FollowUps followUp, CancellationToken cancellationToken)
|
|
{
|
|
followUp.CreatedDate = DateTime.UtcNow;
|
|
await _context.FollowUps.AddAsync(followUp, cancellationToken);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
return followUp;
|
|
}
|
|
|
|
public async Task UpdateAsync(FollowUps followUp, CancellationToken cancellationToken)
|
|
{
|
|
followUp.LastModificationTime = DateTime.UtcNow;
|
|
_context.FollowUps.Update(followUp);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<bool> SetStatusAsync(int id, string status, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.FollowUps.FindAsync(new object[] { id }, cancellationToken);
|
|
if (entity == null)
|
|
return false;
|
|
|
|
entity.Status = status;
|
|
entity.LastModificationTime = DateTime.UtcNow;
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> RemoveByIdAsync(int id, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.FollowUps.FindAsync(new object[] { id }, cancellationToken);
|
|
if (entity == null)
|
|
return false;
|
|
|
|
_context.FollowUps.Remove(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
return true;
|
|
}
|
|
}
|
|
}
|