160 lines
No EOL
5.8 KiB
C#
160 lines
No EOL
5.8 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;
|
|
}
|
|
|
|
public async Task<List<LeaveProcessJobStatus>> GetPendingJobsAsync()
|
|
{
|
|
var data = await _dbContext.Set<LeaveProcessJobStatus>()
|
|
.Where(x => x.Status == "PENDING")
|
|
.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!;
|
|
}
|
|
|
|
public async Task ProcessTaskAsync(Guid id, CancellationToken cancellationToken = default)
|
|
{
|
|
Console.WriteLine($"กำลังประมวลผลงานที่มี RootDnaId: {id}");
|
|
}
|
|
|
|
public async Task ProcessPendingJobsAsync()
|
|
{
|
|
|
|
var pendingJobs = await GetPendingJobsAsync();
|
|
Console.WriteLine($"พบงานที่ค้างอยู่ในสถานะ PENDING จำนวน {pendingJobs.Count} งาน");
|
|
|
|
foreach (var job in pendingJobs)
|
|
{
|
|
try
|
|
{
|
|
// อัปเดตสถานะเป็น Processing
|
|
await UpdateToProcessingAsync(job.Id);
|
|
|
|
// ทำงานที่ต้องการที่นี่ (เช่น เรียก API, ประมวลผลข้อมูล ฯลฯ)
|
|
await ProcessTaskAsync(job.RootDnaId);
|
|
|
|
// อัปเดตสถานะเป็น Completed
|
|
await UpdateToCompletedAsync(job.Id);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// หากเกิดข้อผิดพลาด อัปเดตสถานะเป็น Failed พร้อมข้อความแสดงข้อผิดพลาด
|
|
await UpdateToFailedAsync(job.Id, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |