197 lines
8.8 KiB
C#
197 lines
8.8 KiB
C#
using MySql.Data.MySqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace HanvonF710XAttendanceService
|
|
{
|
|
class AttendanceMachineDAO
|
|
{
|
|
|
|
public void update(AttendanceMachine attendanceMachine, MySqlConnection connection) {
|
|
string query = "UPDATE `hrms`.`attendance_machine` SET `machine_ip` = @machine_ip, `port_number` = @port_number, `machine_name` = @machine_name, `site_id` = @site_id, `status` = @status, `last_sync_date` = @last_sync_date WHERE `machine_id` = @machine_id";
|
|
MySqlCommand cmd = new MySqlCommand(query, connection);
|
|
cmd.Parameters.AddWithValue("@machine_ip", attendanceMachine.MachineIp);
|
|
cmd.Parameters.AddWithValue("@port_number", attendanceMachine.PortNumber);
|
|
cmd.Parameters.AddWithValue("@machine_name", attendanceMachine.MachineName);
|
|
cmd.Parameters.AddWithValue("@site_id", attendanceMachine.SiteId);
|
|
cmd.Parameters.AddWithValue("@status", attendanceMachine.Status);
|
|
cmd.Parameters.AddWithValue("@last_sync_date", attendanceMachine.LastSyncDate);
|
|
cmd.Parameters.AddWithValue("@machine_id", attendanceMachine.MachineId);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
public List<AttendanceMachine> getAttendanceMachines(MySqlConnection connection, String siteId) {
|
|
List<AttendanceMachine> attendanceMachines = null;
|
|
using (var cmd = new MySqlCommand())
|
|
{
|
|
cmd.Connection = connection;
|
|
cmd.CommandTimeout = 120;
|
|
string typeClause = MachineScope.BuildMachineTypeWhereClause(cmd);
|
|
cmd.CommandText = "SELECT * FROM hrms.attendance_machine WHERE site_id = @siteId AND " + typeClause;
|
|
cmd.Parameters.AddWithValue("@siteId", siteId);
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader != null)
|
|
{
|
|
attendanceMachines = getAttendanceMachineList(reader);
|
|
}
|
|
}
|
|
}
|
|
return attendanceMachines ?? new List<AttendanceMachine>();
|
|
}
|
|
|
|
public List<AttendanceMachine> getAllAttendanceMachines(MySqlConnection connection)
|
|
{
|
|
List<AttendanceMachine> attendanceMachines = null;
|
|
using (var cmd = new MySqlCommand())
|
|
{
|
|
cmd.Connection = connection;
|
|
cmd.CommandTimeout = 120;
|
|
string typeClause = MachineScope.BuildMachineTypeWhereClause(cmd);
|
|
cmd.CommandText =
|
|
"SELECT machine_id, machine_ip, port_number, machine_name, site_id, status, last_sync_date " +
|
|
"FROM hrms.attendance_machine WHERE " + typeClause;
|
|
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader != null)
|
|
{
|
|
attendanceMachines = getAttendanceMachineList(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
return attendanceMachines ?? new List<AttendanceMachine>();
|
|
}
|
|
|
|
/// <summary>Normalize IP for comparison (trim + remove spaces) so "192.168. 0.50" matches "192.168.0.50".</summary>
|
|
private static string NormalizeIp(string ip)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(ip)) return "";
|
|
return new string(ip.Trim().Where(c => !char.IsWhiteSpace(c)).ToArray());
|
|
}
|
|
|
|
public bool TryGetAttendanceMachineByIp(MySqlConnection connection, string machineIp, out AttendanceMachine machine, out string machineType)
|
|
{
|
|
machine = null;
|
|
machineType = null;
|
|
if (string.IsNullOrWhiteSpace(machineIp)) return false;
|
|
|
|
string normalizedIp = NormalizeIp(machineIp);
|
|
|
|
using (var cmd = new MySqlCommand())
|
|
{
|
|
cmd.Connection = connection;
|
|
cmd.CommandTimeout = 60;
|
|
string typeClause = MachineScope.BuildMachineTypeWhereClause(cmd, "machine_type", "mtype");
|
|
cmd.CommandText =
|
|
"SELECT machine_id, machine_ip, port_number, machine_name, site_id, status, last_sync_date, machine_type " +
|
|
"FROM hrms.attendance_machine WHERE machine_ip = @machine_ip AND " + typeClause + " LIMIT 1";
|
|
cmd.Parameters.AddWithValue("@machine_ip", machineIp);
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
FillMachineFromReader(reader, out machine, out machineType);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
using (var cmd = new MySqlCommand())
|
|
{
|
|
cmd.Connection = connection;
|
|
cmd.CommandTimeout = 120;
|
|
string typeClause = MachineScope.BuildMachineTypeWhereClause(cmd, "machine_type", "mtype2");
|
|
cmd.CommandText =
|
|
"SELECT machine_id, machine_ip, port_number, machine_name, site_id, status, last_sync_date, machine_type " +
|
|
"FROM hrms.attendance_machine WHERE " + typeClause;
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
string dbIp = reader.IsDBNull(1) ? "" : reader.GetString("machine_ip");
|
|
if (NormalizeIp(dbIp) == normalizedIp)
|
|
{
|
|
FillMachineFromReader(reader, out machine, out machineType);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
private static void FillMachineFromReader(MySqlDataReader reader, out AttendanceMachine machine, out string machineType)
|
|
{
|
|
machine = new AttendanceMachine(
|
|
reader.IsDBNull(0) ? "" : reader.GetString("machine_id"),
|
|
reader.IsDBNull(1) ? "" : reader.GetString("machine_ip"),
|
|
reader.IsDBNull(2) ? default(int) : reader.GetInt16("port_number"),
|
|
reader.IsDBNull(3) ? "" : reader.GetString("machine_name"),
|
|
reader.IsDBNull(4) ? default(int) : reader.GetInt16("site_id"),
|
|
reader.IsDBNull(5) ? "" : reader.GetString("status"),
|
|
reader.IsDBNull(6) ? default(DateTime) : reader.GetDateTime("last_sync_date")
|
|
);
|
|
machineType = reader.IsDBNull(7) ? null : reader.GetString("machine_type");
|
|
}
|
|
|
|
private List<AttendanceMachine> getAttendanceMachineList(MySqlDataReader reader) {
|
|
List<AttendanceMachine> attendanceMachines = new List<AttendanceMachine>();
|
|
while (reader.Read()) {
|
|
attendanceMachines.Add(toAttendanceMachine(reader));
|
|
}
|
|
return attendanceMachines;
|
|
}
|
|
|
|
private AttendanceMachine toAttendanceMachine(MySqlDataReader reader) {
|
|
return new AttendanceMachine(
|
|
reader.IsDBNull(0) ? "" : reader.GetString("machine_id"),
|
|
reader.IsDBNull(1) ? "" : reader.GetString("machine_ip"),
|
|
reader.IsDBNull(2) ? default(int) : reader.GetInt16("port_number"),
|
|
reader.IsDBNull(3) ? "" : reader.GetString("machine_name"),
|
|
reader.IsDBNull(4) ? default(int) : reader.GetInt16("site_id"),
|
|
reader.IsDBNull(5) ? "" : reader.GetString("status"),
|
|
reader.IsDBNull(6) ? default(DateTime) : reader.GetDateTime("last_sync_date")
|
|
|
|
) ;
|
|
}
|
|
|
|
public void UpdateTotalEmpInMachines(string machine_id, int total_users,MySqlConnection connection)
|
|
{
|
|
string query = "UPDATE `hrms`.`attendance_machine` SET `total_users` ="+ total_users + " WHERE `machine_id` = '"+ machine_id+ "'";
|
|
MySqlCommand cmd = new MySqlCommand(query, connection);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
|
|
public string CheckEmpInDB(string machine_id, int emp_cd, MySqlConnection connection)
|
|
{
|
|
string result = "FALSE";
|
|
string query = "Select * from attendance_machine_user a where a.machine_id='"+ machine_id + "' and a.serial_number = "+ emp_cd + "";
|
|
MySqlCommand cmd = new MySqlCommand(query, connection);
|
|
DataTable dt = new DataTable();
|
|
MySqlDataAdapter MySqlAdt = new MySqlDataAdapter();
|
|
MySqlAdt.SelectCommand = cmd;
|
|
MySqlAdt.Fill(dt);
|
|
if(dt.Rows.Count > 0)
|
|
{
|
|
result = "TRUE";
|
|
}
|
|
else
|
|
{
|
|
result = "FALSE";
|
|
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
}
|