using Ease.Core.DataAccess;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using HRM.BO;

namespace HRM.DA
{
    #region LoanEmployeeDoc Service

    public class LoanEmployeeDocService : ServiceTemplate, ILoanEmployeeDocService
    {
        #region Private functions and declaration

        #endregion

        public LoanEmployeeDocService()
        {
        }

        #region MapObject For LoanEmployeeDoc

        private void MapObject(LoanEmployeeDoc oLoanEmployeeDoc, DataReader oReader)
        {
            base.SetObjectID(oLoanEmployeeDoc, oReader.GetInt32("LoanEmpDocID").Value);
            oLoanEmployeeDoc.HRDocID = oReader.GetString("HRDocID") == null ? 0 : oReader.GetInt32("HRDocID").Value;
            oLoanEmployeeDoc.LoanIssueID =
                oReader.GetString("LoanIssueID") == null ? 0 : oReader.GetInt32("LoanIssueID").Value;
            oLoanEmployeeDoc.EmployeeID =
                oReader.GetString("EmployeeID") == null ? 0 : oReader.GetInt32("EmployeeID").Value;
            oLoanEmployeeDoc.HRID = oReader.GetString("HRID") == null ? 0 : oReader.GetInt32("HRID").Value;
            oLoanEmployeeDoc.HRReceiveDate = oReader.GetDateTime("HRReceivedDate").Value;
            oLoanEmployeeDoc.FileNo = oReader.GetString("FileNo");
            oLoanEmployeeDoc.SafeNo = oReader.GetString("SafeNo");
            oLoanEmployeeDoc.HRRemarks = oReader.GetString("HRRemarks");
            oLoanEmployeeDoc.EmpReceiveDate = oReader.GetDateTime("EmpReceiveDate").Value;
            oLoanEmployeeDoc.EmpReceiveRemarks = oReader.GetString("EmpReceiveRemarks");
            oLoanEmployeeDoc.CreatedBy =
                oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value;
            oLoanEmployeeDoc.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oLoanEmployeeDoc.ModifiedBy =
                oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value;
            ;
            oLoanEmployeeDoc.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oLoanEmployeeDoc, Ease.Core.ObjectState.Saved);
        }

        protected override T CreateObject<T>(DataReader oReader)
        {
            LoanEmployeeDoc oLoanEmployeeDoc = new LoanEmployeeDoc();
            MapObject(oLoanEmployeeDoc, oReader);
            return oLoanEmployeeDoc as T;
        }

        protected LoanEmployeeDoc CreateObject(DataReader oReader)
        {
            LoanEmployeeDoc oLoanEmployeeDoc = new LoanEmployeeDoc();
            MapObject(oLoanEmployeeDoc, oReader);
            return oLoanEmployeeDoc;
        }

        #endregion

        #region Service Implementation of LoanEmployeeDoc

        public LoanEmployeeDoc Get(int id)
        {
            LoanEmployeeDoc oLoanEmpDoc = new LoanEmployeeDoc();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(LoanEmployeeDocDA.Get(tc, id));
                if (oreader.Read())
                {
                    oLoanEmpDoc = this.CreateObject<LoanEmployeeDoc>(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 oLoanEmpDoc;
        }

        public List<LoanEmployeeDoc> GetbyIssueId(int issueId)
        {
            List<LoanEmployeeDoc> LoanEmpDocs = new List<LoanEmployeeDoc>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(LoanEmployeeDocDA.GetbyIssueId(tc, issueId));
                LoanEmpDocs = this.CreateObjects<LoanEmployeeDoc>(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 LoanEmpDocs;
        }


        public int Save(TransactionContext tc, LoanEmployeeDoc oLoanEmpDoc)
        {
            try
            {
                if (oLoanEmpDoc.IsNew)
                {
                    int id = tc.GenerateID("LoanEmpDoc", "LoanEmpDocID");
                    base.SetObjectID(oLoanEmpDoc, id);

                    LoanEmployeeDocDA.Insert(tc, oLoanEmpDoc);
                }
                else
                {
                    LoanEmployeeDocDA.Update(tc, oLoanEmpDoc);
                }

                return oLoanEmpDoc.ID;
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }
        }

        public void DeletebyIssueid(TransactionContext tc, int id)
        {
            try
            {
                LoanEmployeeDocDA.DeletebyIssueid(tc, 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);
                LoanEmployeeDocDA.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
    }

    #endregion
}