using System;
using System.Collections.Generic;
using System.Linq;
namespace HanvonF710XAttendanceService
{
///
/// Pure planning logic for DB_TO_DEVICE jobs (testable without device/DB I/O).
///
internal static class DbToDevicePlanning
{
public const int TemplateLoadBatchSize = 50;
public static bool ShouldIgnoreSourceDevice(TemplateTransferMode mode)
{
return mode == TemplateTransferMode.DB_TO_DEVICE;
}
public static List NormalizeEmployeeIds(IEnumerable employeeIds)
{
if (employeeIds == null)
{
return new List();
}
return employeeIds
.Where(id => !string.IsNullOrWhiteSpace(id))
.Select(id => id.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
}
///
/// Resolves which employee serial numbers should be synced from HRMS DB to target devices.
///
public static List ResolveEmployeeIds(
TemplateTransferJob job,
Func, List> loadByDepartmentIds,
Func> loadRegisteredOnTargets)
{
if (job == null)
{
return new List();
}
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;
}
}
}