HRM-10/HRM.DA/Service/Basic/UserLogInInfoService.cs

375 lines
12 KiB
C#
Raw Normal View History

2026-11-03 13:06:00 +06:00
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using HRM.BO;
namespace HRM.DA
{
public class UserLogInInfoService : ServiceTemplate, IUserLogInInfoService
{
public UserLogInInfoService()
{
}
private void MapObject(UserLogInInfo oUserLogInInfo, DataReader oReader)
{
base.SetObjectID(oUserLogInInfo, oReader.GetInt32("SlID").Value);
oUserLogInInfo.UserID = oReader.GetInt32("UserID", 0);
oUserLogInInfo.LoginTime = oReader.GetDateTime("LoginTime").Value;
oUserLogInInfo.PCNumber = oReader.GetString("PCNumber");
2026-11-12 14:58:04 +06:00
oUserLogInInfo.IPAddress = oReader.GetString("IPAddress");
oUserLogInInfo.DeviceId = oReader.GetString("DeviceId");
2026-11-03 13:06:00 +06:00
if (oReader.GetDateTime("LogoutTime") != null)
oUserLogInInfo.LogoutTime = Convert.ToDateTime(oReader.GetString("LogoutTime"));
oUserLogInInfo.LogoutType = oReader.GetString("LogoutType") == null
? EnumLogoutType.NotInitiate
: (EnumLogoutType)oReader.GetInt32("LogoutType").Value;
}
protected override T CreateObject<T>(DataReader oReader)
{
UserLogInInfo oUserLogInInfo = new UserLogInInfo();
MapObject(oUserLogInInfo, oReader);
return oUserLogInInfo as T;
}
#region Service implementation
public UserLogInInfo Get(int id)
{
UserLogInInfo oUserLogInInfo = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserLogInInfoDA.Get(tc, id));
if (oreader.Read())
{
oUserLogInInfo = this.CreateObject<UserLogInInfo>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUserLogInInfo;
}
public List<UserLogInInfo> Get()
{
List<UserLogInInfo> UserLogInInfos = new List<UserLogInInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserLogInInfoDA.Get(tc));
UserLogInInfos = this.CreateObjects<UserLogInInfo>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return UserLogInInfos;
}
public List<UserLogInInfo> GetLoggedUsers()
{
List<UserLogInInfo> UserLogInInfos = new List<UserLogInInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserLogInInfoDA.GetLoggedUsers(tc));
UserLogInInfos = this.CreateObjects<UserLogInInfo>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return UserLogInInfos;
}
public List<UserLogInInfo> Get(string sLoginID)
{
List<UserLogInInfo> UserLogInInfos = new List<UserLogInInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserLogInInfoDA.Get(tc, sLoginID));
UserLogInInfos = this.CreateObjects<UserLogInInfo>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return UserLogInInfos;
}
public List<UserLogInInfo> GetLastLogin(string sLoginID, bool isWebLogin)
{
List<UserLogInInfo> UserLogInInfos = new List<UserLogInInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = null;
if (!isWebLogin)
dr = new DataReader(UserLogInInfoDA.GetLastLogin(tc, sLoginID));
else
dr = new DataReader(UserLogInInfoDA.GetLastWebLogin(tc, sLoginID));
UserLogInInfos = this.CreateObjects<UserLogInInfo>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return UserLogInInfos;
}
public void UpdateLogoutTime(int nLoginID)
{
TransactionContext tc = null;
tc = TransactionContext.Begin();
UserLogInInfoDA.UpdateLogoutTime(tc, nLoginID);
tc.End();
}
public List<UserLogInInfo> Get(DateTime dtFrom, DateTime dtTo)
{
List<UserLogInInfo> UserLogInInfos = new List<UserLogInInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserLogInInfoDA.Get(tc, dtFrom, dtTo));
UserLogInInfos = this.CreateObjects<UserLogInInfo>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return UserLogInInfos;
}
2026-11-12 14:58:04 +06:00
public List<UserLogInInfo> GetAuditTrail(DateTime dtFrom, DateTime dtTo)
{
List<UserLogInInfo> items = new List<UserLogInInfo>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserLogInInfoDA.GetAuditTrail(tc, dtFrom, dtTo));
while (dr.Read())
{
UserLogInInfo item = this.CreateObject<UserLogInInfo>(dr);
item.LoginID = dr.GetString("LoginID");
item.UserName = dr.GetString("UserName");
items.Add(item);
}
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return items;
}
2026-11-03 13:06:00 +06:00
public long NoofLogin(int nUserID)
{
long nCount = 0;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
nCount = UserLogInInfoDA.NoofLogin(tc, nUserID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return nCount;
}
public int Save(UserLogInInfo oUserLogInInfo)
{
TransactionContext tc = null;
try
{
2026-11-12 14:58:04 +06:00
// Before the transaction: the computer-name lookup can take a moment.
if (oUserLogInInfo.IsNew)
FillClientInfo(oUserLogInInfo);
2026-11-03 13:06:00 +06:00
tc = TransactionContext.Begin(true);
if (oUserLogInInfo.IsNew)
{
int id = tc.GenerateID("UserLoginInfo", "SlID");
base.SetObjectID(oUserLogInInfo, id);
UserLogInInfoDA.Insert(tc, oUserLogInInfo);
}
else
UserLogInInfoDA.Update(tc, oUserLogInInfo);
tc.End();
return oUserLogInInfo.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public int Save(TransactionContext tc, UserLogInInfo oUserLogInInfo)
{
try
{
if (oUserLogInInfo.IsNew)
{
2026-11-12 14:58:04 +06:00
FillClientInfo(oUserLogInInfo);
2026-11-03 13:06:00 +06:00
UserLogInInfoDA.Insert(tc, oUserLogInInfo);
}
else UserLogInInfoDA.Update(tc, oUserLogInInfo);
return oUserLogInInfo.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
2026-11-12 14:58:04 +06:00
/// <summary>
/// Fills IPAddress and DeviceId from the current HTTP request, keeping whatever the
/// caller already set (e.g. the mobile login knows the DeviceID from its request body).
/// Call it on the request thread BEFORE handing the login to a background thread
/// (UserService.SaveLoginfo): once the request is over there is nothing left to read.
/// </summary>
public static void CaptureRequestInfo(UserLogInInfo oUserLogInInfo)
{
if (string.IsNullOrWhiteSpace(oUserLogInInfo.IPAddress))
oUserLogInInfo.IPAddress = ClientRequestInfo.GetIPAddress();
if (string.IsNullOrWhiteSpace(oUserLogInInfo.DeviceId))
oUserLogInInfo.DeviceId = ClientRequestInfo.GetDeviceId();
}
/// <summary>
/// IP / device id from the request (when still reachable), then the computer name:
/// reverse DNS of that IP, or the IP itself when the network has no name for it.
/// </summary>
private static void FillClientInfo(UserLogInInfo oUserLogInInfo)
{
CaptureRequestInfo(oUserLogInInfo);
if (string.IsNullOrWhiteSpace(oUserLogInInfo.PCNumber))
oUserLogInInfo.PCNumber = ClientRequestInfo.ResolveComputerName(oUserLogInInfo.IPAddress);
}
2026-11-03 13:06:00 +06:00
public void Delete(int id)
{
//Not Needed
}
#endregion
}
}