EchoTex_Payroll/HRM.DA/Service/Attendance/AttnMobileRawDataService.cs
2024-10-14 10:01:49 +06:00

240 lines
8.7 KiB
C#

using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using HRM.BO;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HRM.DA
{
[Serializable]
public class AttnMobileRawDataService : ServiceTemplate, IAttnMobileRawDataService
{
#region Object Mapping
protected override T CreateObject<T>(DataReader dr)
{
AttnMobileRawData oAttnMobileRawData = new AttnMobileRawData();
MapObject(oAttnMobileRawData, dr);
return oAttnMobileRawData as T;
}
private void MapObject(AttnMobileRawData oAttnMobileRawData, DataReader oReader)
{
base.SetObjectID(oAttnMobileRawData, (oReader.GetInt32("AttnMobileRawDataID").Value));
oAttnMobileRawData.EmpID = oReader.GetInt32("EmpID", 0);
oAttnMobileRawData.AttnDate = oReader.GetDateTime("AttnDate").Value;
oAttnMobileRawData.PunchTime = oReader.GetDateTime("PunchTime").Value;
oAttnMobileRawData.Latitude = oReader.GetFloat("Latitude", 0);
oAttnMobileRawData.Longitude = oReader.GetFloat("Longitude", 0);
oAttnMobileRawData.IsSynced = oReader.GetBoolean("IsSynced", false);
oAttnMobileRawData.InRemarks = oReader.GetString("InRemarks");
oAttnMobileRawData.OutRemarks = oReader.GetString("OutRemarks");
oAttnMobileRawData.PunchType = (EnumMobilePunchType)oReader.GetInt32("PunchType").Value;
oAttnMobileRawData.SyncRemarks = oReader.GetString("SyncRemarks");
oAttnMobileRawData.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oAttnMobileRawData.CreatedBy = oReader.GetInt32("CreatedBy", 0);
this.SetObjectState(oAttnMobileRawData, Ease.Core.ObjectState.Saved);
}
#endregion Object Mapping
#region Interface Implementation
public AttnMobileRawData Get(int id)
{
TransactionContext tc = null;
AttnMobileRawData oAttnMobileRawData = new AttnMobileRawData();
try
{
tc = TransactionContext.Begin(true);
DataReader oreader = new DataReader(AttnMobileRawDataDA.Get(tc, id));
if (oreader.Read())
{
oAttnMobileRawData = this.CreateObject<AttnMobileRawData>(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 oAttnMobileRawData;
}
public List<AttnMobileRawData> Get()
{
TransactionContext tc = null;
List<AttnMobileRawData> oAttnMobileRawData = new List<AttnMobileRawData>();
try
{
tc = TransactionContext.Begin(true);
DataReader oreader = new DataReader(AttnMobileRawDataDA.Get(tc));
oAttnMobileRawData = this.CreateObjects<AttnMobileRawData>(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 oAttnMobileRawData;
}
public int Delete(AttnMobileRawData obj)
{
TransactionContext tc = null;
int oId = 0;
try
{
tc = TransactionContext.Begin(true);
oId = AttnMobileRawDataDA.Delete(tc, obj);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oId;
}
public int Save(AttnMobileRawData obj)
{
TransactionContext tc = null;
int oId = 0;
try
{
tc = TransactionContext.Begin(true);
if (obj.IsNew)
{
int id = tc.GenerateID("AttnMobileRawData", "AttnMobileRawDataID");
base.SetObjectID(obj, id);
obj.SetAuditTrails();
oId = AttnMobileRawDataDA.Insert(tc, obj);
}
else
{
oId = AttnMobileRawDataDA.Update(tc, obj);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oId;
}
public void SaveBulkMobileAttnRawData( List<AttnMobileRawData> oAttnMobileRawData)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oAttnMobileRawData.Count > 0)
{
int id = tc.GenerateID("AttnMobileRawData", "AttnMobileRawDataID");
DataTable attnMobileRawData = new DataTable("AttnMobileRawData");
attnMobileRawData.Columns.Add(new DataColumn("AttnMobileRawDataID", typeof(int)));
attnMobileRawData.Columns.Add(new DataColumn("EmpID", typeof(int)));
attnMobileRawData.Columns.Add(new DataColumn("AttnDate", typeof(DateTime)));
attnMobileRawData.Columns.Add(new DataColumn("Latitude", typeof(float)));
attnMobileRawData.Columns.Add(new DataColumn("Longitude", typeof(float)));
attnMobileRawData.Columns.Add(new DataColumn("IsSynced", typeof(bool)));
attnMobileRawData.Columns.Add(new DataColumn("InRemarks", typeof(string)));
attnMobileRawData.Columns.Add(new DataColumn("OutRemarks", typeof(string)));
attnMobileRawData.Columns.Add(new DataColumn("PunchType", typeof(int)));
attnMobileRawData.Columns.Add(new DataColumn("SyncRemarks", typeof(string)));
attnMobileRawData.Columns.Add(new DataColumn("PunchTime", typeof(DateTime)));
attnMobileRawData.Columns.Add(new DataColumn("CreatedDate",typeof(DateTime)));
attnMobileRawData.Columns.Add(new DataColumn("CreatedBy", typeof(string)));
foreach (AttnMobileRawData aMobileRawData in oAttnMobileRawData)
{
attnMobileRawData.Rows.Add(
id++,
aMobileRawData.EmpID,
aMobileRawData.AttnDate,
aMobileRawData.Latitude,
aMobileRawData.Longitude,
aMobileRawData.IsSynced,
aMobileRawData.InRemarks,
aMobileRawData.OutRemarks,
aMobileRawData.PunchType,
aMobileRawData.SyncRemarks,
aMobileRawData.PunchTime,
DateTime.Today,
aMobileRawData.EmpID
);
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy((SqlConnection)tc.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)tc.Transaction))
{
bulkCopy.BulkCopyTimeout = 6000; // in seconds
bulkCopy.DestinationTableName = "AttnMobileRawData";
bulkCopy.WriteToServer(attnMobileRawData);
}
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion Interface Implementation
}
}