CEL_Payroll/Payroll.Service/Attendence/Service/AccessCardService.cs

441 lines
14 KiB
C#
Raw Normal View History

2024-09-17 14:30:13 +06:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ease.CoreV35.Model;
using Payroll.BO;
using Ease.CoreV35.Caching;
using Ease.CoreV35.DataAccess;
using Payroll.Service.Attendence.Service;
namespace Payroll.Service.Attendence
{
#region AccessCard Service
[Serializable]
public class AccessCardService : ServiceTemplate, IAccessCardService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(AccessCard));
#endregion
public AccessCardService() { }
private void MapObject(AccessCard oAccessCard, DataReader oReader)
{
base.SetObjectID(oAccessCard, oReader.GetID("AccessCardID"));
oAccessCard.CardNumber = oReader.GetString("CardNumber");
oAccessCard.TypeID = oReader.GetID("TypeID");
oAccessCard.Status = (EnumCardStatus)oReader.GetUInt16("Status").Value;
this.SetObjectState(oAccessCard, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
AccessCard oAccessCard = new AccessCard();
MapObject(oAccessCard, oReader);
return oAccessCard as T;
}
protected AccessCard CreateObject(DataReader oReader)
{
AccessCard oAccessCard = new AccessCard();
MapObject(oAccessCard, oReader);
return oAccessCard;
}
#region Service implementation
public AccessCard Get(ID id)
{
AccessCard oAccessCard = new AccessCard();
#region Cache Header
oAccessCard = _cache["Get", id] as AccessCard;
if (oAccessCard != null)
return oAccessCard;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(AccessCardDA.Get(tc, id));
if (oreader.Read())
{
oAccessCard = this.CreateObject<AccessCard>(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
}
#region Cache Footer
_cache.Add(oAccessCard, "Get", id);
#endregion
return oAccessCard;
}
public AccessCard Get(string CardNumber)
{
AccessCard oAccessCard = new AccessCard();
#region Cache Header
oAccessCard = _cache["Get", CardNumber] as AccessCard;
if (oAccessCard != null)
return oAccessCard;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(AccessCardDA.Get(tc, CardNumber));
if (oreader.Read())
{
oAccessCard = this.CreateObject<AccessCard>(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
}
#region Cache Footer
_cache.Add(oAccessCard, "Get", CardNumber);
#endregion
return oAccessCard;
}
public AccessCard Get(string CardNumber, TransactionContext tc)
{
AccessCard oAccessCard = new AccessCard();
#region Cache Header
oAccessCard = _cache["Get", CardNumber] as AccessCard;
if (oAccessCard != null)
return oAccessCard;
#endregion;
try
{
DataReader oreader = new DataReader(AccessCardDA.Get(tc, CardNumber));
if (oreader.Read())
{
oAccessCard = this.CreateObject<AccessCard>(oreader);
}
oreader.Close();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(oAccessCard, "Get", CardNumber);
#endregion
return oAccessCard;
}
public ObjectsTemplate<AccessCard> Get()
{
#region Cache Header
ObjectsTemplate<AccessCard> accessCards = _cache["Get"] as ObjectsTemplate<AccessCard>;
if (accessCards != null)
return accessCards;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AccessCardDA.Get(tc));
accessCards = this.CreateObjects<AccessCard>(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
}
#region Cache Footer
_cache.Add(accessCards, "Get");
#endregion
return accessCards;
}
public ObjectsTemplate<AccessCard> Get(EnumCardStatus status)
{
#region Cache Header
ObjectsTemplate<AccessCard> accessCards = _cache["Get"] as ObjectsTemplate<AccessCard>;
if (accessCards != null)
return accessCards;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AccessCardDA.Get(tc, status));
accessCards = this.CreateObjects<AccessCard>(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
}
#region Cache Footer
_cache.Add(accessCards, "Get");
#endregion
return accessCards;
}
public ObjectsTemplate<AccessCard> Get(params EnumCardStatus[] status)
{
#region Cache Header
ObjectsTemplate<AccessCard> accessCards = _cache["Get"] as ObjectsTemplate<AccessCard>;
if (accessCards != null)
return accessCards;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(AccessCardDA.Get(tc, status));
accessCards = this.CreateObjects<AccessCard>(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
}
#region Cache Footer
_cache.Add(accessCards, "Get");
#endregion
return accessCards;
}
public ID Save(AccessCard oAccessCard)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oAccessCard.IsNew)
{
int id = tc.GenerateID("AccessCard", "AccessCardID");
base.SetObjectID(oAccessCard, ID.FromInteger(id));
AccessCardDA.Insert(tc, oAccessCard);
}
else
{
AccessCardDA.Update(tc, oAccessCard);
}
tc.End();
return oAccessCard.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Save(ObjectsTemplate<AccessCard> oAccessCards)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
foreach (AccessCard ac in oAccessCards)
{
if (ac.IsNew)
{
ac.Status = EnumCardStatus.Free;
int id = tc.GenerateID("AccessCard", "AccessCardID");
base.SetObjectID(ac, ID.FromInteger(id));
AccessCardDA.Insert(tc, ac);
}
else
{
AccessCardDA.Update(tc, ac);
}
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Delete(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
AccessCardDA.Delete(tc, id);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public bool IsExist(string CardNumber)
{
bool isExist = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
isExist = AccessCardDA.IsExist(tc, CardNumber);
//accessCards = this.CreateObjects<AccessCard>(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 isExist;
}
public void UpdateStatus(TransactionContext tc, ID cardID, EnumCardStatus status)
{
try
{
AccessCardDA.UpdateStatus(tc, cardID, status);
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void UpdateStatus(ID cardID, EnumCardStatus status)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
AccessCardDA.UpdateStatus(tc, cardID, status);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
//for excel Upload
public static void SaveForExcelUpload(TransactionContext tc, ObjectsTemplate<AccessCard> accessCards)
{
ObjectsTemplate<CardOperation> cardOperations = new ObjectsTemplate<CardOperation>();
ObjectsTemplate<EmployeeWorkPlanSetup> empWorkPlans = new ObjectsTemplate<EmployeeWorkPlanSetup>();
try
{
foreach (AccessCard ac in accessCards)
{
EmployeeWorkPlanSetup empWorkPaln = new EmployeeWorkPlanSetup();
AccessCardDA.Insert(tc, ac);
ac.CardOperation.CardNumber = ac.CardNumber;
ac.CardOperation.Status = EnumCardStatus.Attached;
cardOperations.Add(ac.CardOperation);
//empWorkPaln.EmployeeID = ac.EmployeeWorkPlanSetup.EmployeeID;
//empWorkPaln.ShiftID = ac.EmployeeWorkPlanSetup.ShiftID;
//empWorkPaln.WorkPlanGroupID = ac.EmployeeWorkPlanSetup.WorkPlanGroupID;
//empWorkPaln.StartDate = ac.EmployeeWorkPlanSetup.StartDate;
//empWorkPlans.Add(empWorkPaln);
}
if (cardOperations.Count > 0)
{
CardOperationService cardOPService = new CardOperationService();
cardOPService.Save(tc, cardOperations);
}
//if (empWorkPlans.Count > 0)
//{
// EmployeeWorkPlanSetupService empWorkPlavService = new EmployeeWorkPlanSetupService();
// empWorkPlavService.Save(tc, empWorkPlans);
//}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
}
#endregion
}