HanvonAttendanceService/DbToDeviceSummary.cs

134 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace HanvonF710XAttendanceService
{
internal sealed class DbToDeviceFailedEmployee
{
public string SerialNumber { get; set; } = "";
public string Name { get; set; } = "";
public string Department { get; set; } = "";
public string MachineId { get; set; } = "";
public string MachineIp { get; set; } = "";
public string MachineType { get; set; } = "";
public string Reason { get; set; } = "";
}
internal sealed class DbToDeviceMissingMappingEmployee
{
public string SerialNumber { get; set; } = "";
public string MachineId { get; set; } = "";
public string MachineIp { get; set; } = "";
public string MachineType { get; set; } = "";
}
internal sealed class DbToDeviceTargetSummary
{
public string MachineId { get; set; } = "";
public string MachineIp { get; set; } = "";
public string MachineType { get; set; } = "";
public int AlreadyRegistered { get; set; }
public int NewlyRegistered { get; set; }
public int Failed { get; set; }
public List<DbToDeviceFailedEmployee> FailedEmployees { get; } = new List<DbToDeviceFailedEmployee>();
public List<DbToDeviceMissingMappingEmployee> MissingMappings { get; } = new List<DbToDeviceMissingMappingEmployee>();
public void AddFailure(string serial, string name, string department, string reason)
{
Failed++;
FailedEmployees.Add(new DbToDeviceFailedEmployee
{
SerialNumber = serial ?? "",
Name = name ?? "",
Department = department ?? "",
MachineId = MachineId,
MachineIp = MachineIp,
MachineType = MachineType,
Reason = reason ?? ""
});
}
public void AddMissingMapping(string serial)
{
MissingMappings.Add(new DbToDeviceMissingMappingEmployee
{
SerialNumber = serial ?? "",
MachineId = MachineId,
MachineIp = MachineIp,
MachineType = MachineType
});
}
public string FormatReport()
{
var sb = new StringBuilder();
sb.AppendLine("=================================");
sb.AppendLine("DB_TO_DEVICE SUMMARY");
sb.AppendLine("=================================");
sb.AppendLine();
sb.AppendLine("Target:");
sb.AppendLine("Machine ID: " + MachineId);
sb.AppendLine("IP: " + MachineIp);
sb.AppendLine("Type: " + MachineType);
sb.AppendLine();
sb.AppendLine("Already Registered: " + AlreadyRegistered);
sb.AppendLine();
sb.AppendLine("Newly Registered: " + NewlyRegistered);
sb.AppendLine();
sb.AppendLine("Failed: " + Failed);
sb.AppendLine();
sb.AppendLine("Missing Employee Mapping: " + MissingMappings.Count);
if (MissingMappings.Count > 0)
{
sb.AppendLine();
foreach (var miss in MissingMappings)
{
sb.AppendLine("Serial: " + miss.SerialNumber);
sb.AppendLine("Machine ID: " + miss.MachineId);
sb.AppendLine("Machine IP: " + miss.MachineIp);
sb.AppendLine("Machine Type: " + miss.MachineType);
sb.AppendLine("Reason: No employee row for serial_number_int");
sb.AppendLine();
}
}
else
{
sb.AppendLine();
}
if (FailedEmployees.Count > 0)
{
sb.AppendLine("Failed Employees:");
sb.AppendLine();
foreach (var fail in FailedEmployees)
{
sb.AppendLine("Serial: " + fail.SerialNumber);
sb.AppendLine("Name: " + fail.Name);
sb.AppendLine("Department: " + fail.Department);
sb.AppendLine("Machine ID: " + fail.MachineId);
sb.AppendLine("Machine IP: " + fail.MachineIp);
sb.AppendLine("Machine Type: " + fail.MachineType);
sb.AppendLine("Reason: " + fail.Reason);
sb.AppendLine();
}
}
return sb.ToString().TrimEnd() + Environment.NewLine;
}
}
internal static class DbToDeviceSkipLogic
{
public static bool ShouldSkipAlreadyRegistered(ISet<string> registeredSerials, string employeeSerial)
{
if (registeredSerials == null || string.IsNullOrWhiteSpace(employeeSerial))
{
return false;
}
return registeredSerials.Contains(employeeSerial.Trim());
}
}
}