CEL_Payroll/Payroll.Service/Attendence/Service/ShiftService.cs
2024-09-17 14:30:13 +06:00

269 lines
9.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ease.CoreV35.Caching;
using Ease.CoreV35.DataAccess;
using Payroll.Service.Attendence.DA;
using Ease.CoreV35.Model;
using Payroll.BO;
namespace Payroll.Service.Attendence.Service
{
#region Shift Service
[Serializable]
public class ShiftService : ServiceTemplate, IShiftService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(Shift));
#endregion
public ShiftService() { }
private void MapObject(Shift oShift, DataReader oReader)
{
base.SetObjectID(oShift, oReader.GetID("ShiftID"));
oShift.Code = oReader.GetString("Code");
oShift.Name = oReader.GetString("Name");
oShift.InTime = oReader.GetDateTime("InTime").Value;
oShift.OutTime = oReader.GetDateTime("OutTime").Value;
oShift.hasAbsentTime = oReader.GetBoolean("hasAbsentTime").Value;
oShift.AbsentTime = oShift.hasAbsentTime? oReader.GetDateTime("AbsentTime").Value : DateTime.MinValue;
oShift.LateCalcualtion = oReader.GetDouble("LateCalcualtion").Value;
oShift.DelayCalcualtion = oReader.GetDouble("DelayCalcualtion").HasValue ? oReader.GetDouble("DelayCalcualtion").Value : 0.0;
oShift.EarlyExitBefore = oReader.GetDouble("EarlyExitBefore").Value;
oShift.MinimumOTHour = oReader.GetDouble("MinimumOTHour").Value;
oShift.IsOverlapingDay = oReader.GetBoolean("IsOverlapingDay").Value;
oShift.ShiftType = (EnumWorkPlanGroup) oReader.GetInt16("ShiftType").Value;
oShift.ShortName = oReader.GetString("ShortName");
oShift.Sequence = oReader.GetInt32("SequenceNo").Value;
oShift.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oShift.CreatedBy = oReader.GetID("CreatedBy");
oShift.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oShift.ModifiedBy = oReader.GetID("ModifiedBy");
oShift.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oShift, Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Shift oShift = new Shift();
MapObject(oShift, oReader);
return oShift as T;
}
protected Shift CreateObject(DataReader oReader)
{
Shift oShift = new Shift();
MapObject(oShift, oReader);
return oShift;
}
#region Service implementation
public Shift Get(ID id)
{
Shift oShift = new Shift();
#region Cache Header
oShift = _cache["Get", id] as Shift;
if (oShift != null)
return oShift;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(ShiftDA.Get(tc, id));
if (oreader.Read())
{
oShift = this.CreateObject<Shift>(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(oShift, "Get", id);
#endregion
return oShift;
}
public Shift Get(string shiftName)
{
Shift oShift = new Shift();
#region Cache Header
oShift = _cache["Get", shiftName] as Shift;
if (oShift != null)
return oShift;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(ShiftDA.Get(tc, shiftName));
if (oreader.Read())
{
oShift = this.CreateObject<Shift>(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(oShift, "Get", shiftName);
#endregion
return oShift;
}
public ObjectsTemplate<Shift> Get()
{
#region Cache Header
ObjectsTemplate<Shift> shifts = _cache["Get"] as ObjectsTemplate<Shift>;
if (shifts != null)
return shifts;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(ShiftDA.Get(tc));
shifts = this.CreateObjects<Shift>(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(shifts, "Get");
#endregion
return shifts;
}
public ObjectsTemplate<Shift> Get(bool isCounterClock)
{
#region Cache Header
ObjectsTemplate<Shift> shifts = _cache["Get", isCounterClock] as ObjectsTemplate<Shift>;
if (shifts != null)
return shifts;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(ShiftDA.Get(tc, isCounterClock));
shifts = this.CreateObjects<Shift>(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(shifts, "Get", isCounterClock);
#endregion
return shifts;
}
public ID Save(Shift oShift)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oShift.IsNew)
{
int id = tc.GenerateID("Shift", "ShiftID");
base.SetObjectID(oShift, ID.FromInteger(id));
ShiftDA.Insert(tc, oShift);
}
else
{
ShiftDA.Update(tc, oShift);
}
tc.End();
return oShift.ID;
}
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);
ShiftDA.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 shiftCode, string shortName)
{
bool isExist = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
isExist = ShiftDA.IsExist(tc, shiftCode, shortName);
//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;
}
#endregion
}
#endregion
}