HanvonAttendanceService/MachineUserDeleteSync.cs

84 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
namespace HanvonF710XAttendanceService
{
internal delegate bool MachineUserDeleteBatchDelegate(IReadOnlyList<string> deviceEnrollIds, out string error);
internal delegate void MachineUserDeletedDelegate(string serialNumber);
internal sealed class MachineUserDeleteSyncResult
{
public int TotalPending { get; set; }
public int SuccessCount { get; set; }
public int FailedCount { get; set; }
}
internal static class MachineUserDeleteSync
{
public static MachineUserDeleteSyncResult Execute(
string machineId,
string targetIp,
IReadOnlyList<MachineUserDeleteItem> pendingItems,
int batchSize,
MachineUserDeleteBatchDelegate deleteBatch,
MachineUserDeletedDelegate markDeleted,
Action<string> log)
{
var result = new MachineUserDeleteSyncResult();
if (pendingItems == null || pendingItems.Count == 0)
{
return result;
}
result.TotalPending = pendingItems.Count;
var batches = MachineUserDeleteBatcher.SplitIntoBatches(pendingItems, batchSize);
int totalBatches = batches.Count;
log?.Invoke($"[USER_DELETE] machine={machineId} target={targetIp} pending={result.TotalPending} batchSize={batchSize} batches={totalBatches}");
for (int batchIndex = 0; batchIndex < batches.Count; batchIndex++)
{
var batch = batches[batchIndex];
int batchNumber = batchIndex + 1;
var deviceIds = new List<string>(batch.Count);
foreach (var item in batch)
{
if (!string.IsNullOrWhiteSpace(item.DeviceEnrollId))
{
deviceIds.Add(item.DeviceEnrollId);
}
}
log?.Invoke($"[USER_DELETE] batch={batchNumber}/{totalBatches} count={batch.Count}");
if (deviceIds.Count == 0)
{
log?.Invoke($"[USER_DELETE] batch={batchNumber}/{totalBatches} result=FAILED err=no_device_ids");
result.FailedCount += batch.Count;
continue;
}
string error = null;
if (deleteBatch == null || !deleteBatch(deviceIds, out error))
{
log?.Invoke($"[USER_DELETE] batch={batchNumber}/{totalBatches} result=FAILED err={error ?? "deleteusers failed"}");
result.FailedCount += batch.Count;
continue;
}
foreach (var item in batch)
{
markDeleted?.Invoke(item.SerialNumber);
}
result.SuccessCount += batch.Count;
log?.Invoke($"[USER_DELETE] batch={batchNumber}/{totalBatches} result=SUCCESS");
}
log?.Invoke($"[USER_DELETE] machine={machineId} total={result.TotalPending} success={result.SuccessCount} failed={result.FailedCount}");
return result;
}
}
}