HanvonAttendanceService/DbToDevicePlanning.cs

89 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace HanvonF710XAttendanceService
{
/// <summary>
/// Pure planning logic for DB_TO_DEVICE jobs (testable without device/DB I/O).
/// </summary>
internal static class DbToDevicePlanning
{
public const int TemplateLoadBatchSize = 50;
public static bool ShouldIgnoreSourceDevice(TemplateTransferMode mode)
{
return mode == TemplateTransferMode.DB_TO_DEVICE;
}
public static List<string> NormalizeEmployeeIds(IEnumerable<string> employeeIds)
{
if (employeeIds == null)
{
return new List<string>();
}
return employeeIds
.Where(id => !string.IsNullOrWhiteSpace(id))
.Select(id => id.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
}
/// <summary>
/// Resolves which employee serial numbers should be synced from HRMS DB to target devices.
/// </summary>
public static List<string> ResolveEmployeeIds(
TemplateTransferJob job,
Func<IReadOnlyList<string>, List<string>> loadByDepartmentIds,
Func<List<string>> loadRegisteredOnTargets)
{
if (job == null)
{
return new List<string>();
}
if (job.EmpIds != null && job.EmpIds.Count > 0)
{
return NormalizeEmployeeIds(job.EmpIds);
}
if (job.DepartmentIds != null && job.DepartmentIds.Count > 0)
{
if (loadByDepartmentIds != null)
{
var fromDepartments = loadByDepartmentIds(job.DepartmentIds);
if (fromDepartments != null && fromDepartments.Count > 0)
{
return NormalizeEmployeeIds(fromDepartments);
}
}
}
return NormalizeEmployeeIds(loadRegisteredOnTargets != null ? loadRegisteredOnTargets() : null);
}
public static int ComputeTemplateLoadBatchCount(int employeeCount)
{
if (employeeCount <= 0)
{
return 0;
}
return (employeeCount + TemplateLoadBatchSize - 1) / TemplateLoadBatchSize;
}
public static int ComputeLastTemplateLoadBatchSize(int employeeCount)
{
if (employeeCount <= 0)
{
return 0;
}
int remainder = employeeCount % TemplateLoadBatchSize;
return remainder == 0 ? TemplateLoadBatchSize : remainder;
}
}
}