public RecordList<WorkingListRecord> GetWorkingList(string applicationName, string search, int limit, int offset, IDbTransaction transaction)
{
StringBuilder cb = new StringBuilder(
@"SELECT CAST(COUNT(wg.[Id]) AS bigint)
FROM [BlueCollarWorking] wg
INNER JOIN [BlueCollarWorker] w ON wg.[WorkerId] = w.[Id]
LEFT OUTER JOIN [BlueCollarSchedule] s ON wg.[ScheduleId] = s.[Id]
WHERE
wg.[ApplicationName] = @ApplicationName");
StringBuilder sb = new StringBuilder(
@"SELECT
wg.[Id],
wg.[JobName],
wg.[JobType],
wg.[QueuedOn],
wg.[QueueName],
wg.[Signal],
wg.[StartedOn],
wg.[TryNumber],
w.[Name] AS [WorkerName],
w.[MachineAddress] AS [WorkerMachineAddress],
w.[MachineName] AS [WorkerMachineName],
s.[Name] AS [ScheduleName]
FROM [BlueCollarWorking] wg
INNER JOIN [BlueCollarWorker] w ON wg.[WorkerId] = w.[Id]
LEFT OUTER JOIN [BlueCollarSchedule] s ON wg.[ScheduleId] = s.[Id]
WHERE
wg.[ApplicationName] = @ApplicationName");
if (!string.IsNullOrEmpty(search))
{
const string Search = @"
AND
(
wg.[QueueName] LIKE @Search
OR wg.[JobType] LIKE @Search
OR wg.[JobName] LIKE @Search
OR w.[Name] LIKE @Search
OR w.[MachineName] LIKE @Search
OR s.[Name] LIKE @Search
)";
cb.Append(Search);
sb.Append(Search);
}
cb.Append(";\n\n");
sb.Append("\n");
sb.Append(
@"ORDER BY [QueuedOn] DESC
LIMIT @Limit OFFSET @Offset;");
sb.Append("\n");
sb.Append(CountsSql);
var p = new
{
ApplicationName = applicationName,
Search = !string.IsNullOrEmpty(search) ? string.Concat("%", search, "%") : null,
Limit = limit,
Offset = offset
};
var list = new RecordList<WorkingListRecord>();
using (var multi = this.connection.QueryMultiple(cb.ToString() + sb.ToString(), p, transaction, null, null))
{
list.SetPaging(multi.Read<long>().First(), limit, offset);
foreach (WorkingListRecord record in multi.Read<WorkingListRecord>())
{
list.Records.Add(record);
}
list.Counts = CreateCounts(multi);
}
return list;
}