Files
lux/EgwCoreLib.Lux.Data/Services/Job/JobTaskService.cs
T
2026-03-17 14:54:16 +01:00

145 lines
4.6 KiB
C#

using EgwCoreLib.Lux.Data.DbModel.Job;
using EgwCoreLib.Lux.Data.Repository.Job;
using Microsoft.Extensions.Configuration;
using StackExchange.Redis;
namespace EgwCoreLib.Lux.Data.Services.Job
{
public class JobTaskService : BaseServ, IJobTaskService
{
#region Public Constructors
public JobTaskService(
IConfiguration config,
IConnectionMultiplexer redis,
IJobTaskRepository repo) : base(config, redis)
{
_className = "JobTask";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
public async Task<bool> DeleteAsync(JobTaskModel rec2del)
{
return await TraceAsync($"{_className}.Delete", async (activity) =>
{
var dbResult = await _repo.GetByIdAsync(rec2del.JobID);
if (dbResult == null) return false;
bool success = await _repo.DeleteAsync(dbResult);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}*");
await ClearCacheAsync($"{_redisBaseKey}:JobStep:*");
await ClearCacheAsync($"{_redisBaseKey}:Resources:*");
}
return success;
});
}
public async Task<List<JobTaskModel>> GetAllAsync()
{
return await TraceAsync($"{_className}.GetAll", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:ALL",
async () => await _repo.GetAllAsync(),
UltraLongCache
);
});
}
public async Task<bool> MergeTagsAsync(int JobID, List<string> reqTagList)
{
return await TraceAsync($"{_className}.MergeTags", async (activity) =>
{
var currRec = await _repo.GetByIdAsync(JobID);
if (currRec == null) return false;
string operation = "MergeTags";
bool success = await _repo.MergeTagsAsync(JobID, reqTagList);
if (currRec != null)
activity?.SetTag("db.operation", operation);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}*");
await ClearCacheAsync($"{_redisBaseKey}:JobStep:*");
await ClearCacheAsync($"{_redisBaseKey}:Resources:*");
}
return success;
});
}
public async Task<bool> MoveAsync(JobTaskModel selRec, bool moveUp)
{
return await TraceAsync($"{_className}.Move", async (activity) =>
{
var currRec = await _repo.GetByIdAsync(selRec.JobID);
if (currRec == null) return false;
string operation = "Move";
bool success = await _repo.MoveAsync(selRec, moveUp);
if (currRec != null)
activity?.SetTag("db.operation", operation);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}*");
await ClearCacheAsync($"{_redisBaseKey}:JobStep:*");
await ClearCacheAsync($"{_redisBaseKey}:Resources:*");
}
return success;
});
}
public async Task<bool> UpsertAsync(JobTaskModel upsRec)
{
return await TraceAsync($"{_className}.Upsert", async (activity) =>
{
var currRec = await _repo.GetByIdAsync(upsRec.JobID);
string operation = "UPDATE";
bool success = false;
if (currRec != null)
{
success = await _repo.UpdateAsync(upsRec);
}
else
{
operation = "INSERT";
success = await _repo.AddAsync(upsRec);
}
activity?.SetTag("db.operation", operation);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}*");
await ClearCacheAsync($"{_redisBaseKey}:JobStep:*");
await ClearCacheAsync($"{_redisBaseKey}:Resources:*");
}
return success;
});
}
#endregion Public Methods
#region Private Fields
private readonly string _className;
private readonly IJobTaskRepository _repo;
#endregion Private Fields
}
}