EchoTex_Payroll/HRM.DA/Service/Basic/CompanyService.cs

188 lines
5.1 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
using HRM.BO;
using Ease.Core;
using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HRM.DA
{
public class CompanyService : ServiceTemplate //, ICompanyService
{
private void MapObject(Company oCompany, DataReader oReader)
{
base.SetObjectID(oCompany, oReader.GetInt32("CompanyID").Value);
oCompany.Code = oReader.GetString("code");
oCompany.Name = oReader.GetString("description");
oCompany.Sequence = oReader.GetInt32("SequenceNo").Value;
oCompany.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oCompany.CreatedBy = oReader.GetInt32("CreatedBy").Value;
oCompany.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
oCompany.ModifiedBy = oReader.GetInt32("ModifiedBy");
oCompany.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oCompany, ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Company oCompany = new Company();
MapObject(oCompany, oReader);
return oCompany as T;
}
#region Service implementation
public Company Get(int id)
{
Company oCompany = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(CompanyDA.Get(tc, id));
if (oreader.Read())
{
oCompany = this.CreateObject<Company>(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 oCompany;
}
public Company Get(TransactionContext tc, int id)
{
Company oCompany = null;
try
{
DataReader oreader = new DataReader(CompanyDA.Get(tc, id));
if (oreader.Read())
{
oCompany = this.CreateObject<Company>(oreader);
}
oreader.Close();
}
catch (Exception e)
{
#region Handle Exception
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCompany;
}
public List<Company> Get(EnumStatus status)
{
List<Company> Companys = new List<Company>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(CompanyDA.Get(tc, status));
Companys = this.CreateObjects<Company>(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 Companys;
}
public int Save(Company oCompany)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oCompany.IsNew)
{
int id = tc.GenerateID("Company", "CompanyID");
base.SetObjectID(oCompany, id);
int seqNo = tc.GenerateID("Company", "SequenceNo");
oCompany.Sequence = seqNo;
CompanyDA.Insert(tc, oCompany);
}
else
{
CompanyDA.Update(tc, oCompany);
}
tc.End();
return oCompany.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(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
CompanyDA.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
}
}
#endregion
}
}