119 lines
No EOL
4 KiB
C#
119 lines
No EOL
4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BMA.EHR.Application.Common.Interfaces;
|
|
using BMA.EHR.Domain.Models.Leave.TimeAttendants;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
|
|
{
|
|
public class LeaveProcessJobStatusRepository: GenericLeaveRepository<Guid, LeaveProcessJobStatus>
|
|
{
|
|
#region " Fields "
|
|
|
|
private readonly ILeaveDbContext _dbContext;
|
|
|
|
#endregion
|
|
|
|
#region " Constructor and Destructor "
|
|
|
|
public LeaveProcessJobStatusRepository(ILeaveDbContext dbContext,
|
|
IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " Methods "
|
|
|
|
/// <summary>
|
|
/// ดึงข้อมูล Job Status จาก TaskId
|
|
/// </summary>
|
|
public async Task<LeaveProcessJobStatus?> GetByTaskIdAsync(Guid id)
|
|
{
|
|
var data = await _dbContext.Set<LeaveProcessJobStatus>()
|
|
.Where(x => x.Id == id)
|
|
.FirstOrDefaultAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ดึงข้อมูล Job Status จาก UserId และสถานะ
|
|
/// </summary>
|
|
public async Task<List<LeaveProcessJobStatus>> GetByUserIdAndStatusAsync(Guid userId, string status)
|
|
{
|
|
var data = await _dbContext.Set<LeaveProcessJobStatus>()
|
|
.Where(x => x.CreatedUserId == userId.ToString("D") && x.Status == status)
|
|
.OrderByDescending(x => x.CreatedDate)
|
|
.ToListAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ดึงข้อมูล Job Status ที่ยัง pending หรือ processing
|
|
/// </summary>
|
|
public async Task<List<LeaveProcessJobStatus>> GetPendingOrProcessingJobsAsync(Guid userId)
|
|
{
|
|
var data = await _dbContext.Set<LeaveProcessJobStatus>()
|
|
.Where(x => x.CreatedUserId == userId.ToString("D") &&
|
|
(x.Status == "PENDING" || x.Status == "PROCESSING"))
|
|
//.OrderByDescending(x => x.CreatedDate)
|
|
.ToListAsync();
|
|
|
|
return data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// อัปเดตสถานะเป็น Processing
|
|
/// </summary>
|
|
public async Task<LeaveProcessJobStatus> UpdateToProcessingAsync(Guid id)
|
|
{
|
|
var job = await GetByTaskIdAsync(id);
|
|
if (job != null)
|
|
{
|
|
job.Status = "PROCESSING";
|
|
job.ProcessingDate = DateTime.Now;
|
|
await UpdateAsync(job);
|
|
}
|
|
return job!;
|
|
}
|
|
|
|
/// <summary>
|
|
/// อัปเดตสถานะเป็น Completed
|
|
/// </summary>
|
|
public async Task<LeaveProcessJobStatus> UpdateToCompletedAsync(Guid id, string? additionalData = null)
|
|
{
|
|
var job = await GetByTaskIdAsync(id);
|
|
if (job != null)
|
|
{
|
|
job.Status = "COMPLETED";
|
|
job.CompletedDate = DateTime.Now;
|
|
await UpdateAsync(job);
|
|
}
|
|
return job!;
|
|
}
|
|
|
|
/// <summary>
|
|
/// อัปเดตสถานะเป็น Failed
|
|
/// </summary>
|
|
public async Task<LeaveProcessJobStatus> UpdateToFailedAsync(Guid id, string errorMessage)
|
|
{
|
|
var job = await GetByTaskIdAsync(id);
|
|
if (job != null)
|
|
{
|
|
job.Status = "FAILED";
|
|
job.CompletedDate = DateTime.Now;
|
|
job.ErrorMessage = errorMessage;
|
|
await UpdateAsync(job);
|
|
}
|
|
return job!;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |