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
///
/// Eliminazione record
///
///
///
public async Task 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;
});
}
///
/// Elenco completo JobTask da DB
///
///
public async Task> GetAllAsync()
{
return await TraceAsync($"{_className}.GetAll", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:ALL",
async () => await _repo.GetAllAsync(),
UltraLongCache
);
});
}
///
/// Unisci tag a JobTask
///
///
/// Lista di tag richiesti
///
public async Task MergeTagsAsync(int JobID, List 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;
});
}
///
/// Sposta record JobTask (up/down)
///
///
///
///
public async Task 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;
});
}
///
/// Upsert record JobTask (aggiorna o inserisce)
///
///
///
public async Task 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
}
}