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

namespace HRM.DA
{
    public class EmployeeRetirementService : ServiceTemplate, IEmployeeRetirementService
    {
        private void MapObject(EmployeeRetirement oemp, DataReader oReader)
        {
            base.SetObjectID(oemp, oReader.GetInt32("empretirementid").Value);
            oemp.retirementAge = oReader.GetInt32("RetirementAge").Value;
            oemp.employeeID = oReader.GetInt32("EmployeeID", 0);
        }

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

        #region function

        public List<EmployeeRetirement> Get()
        {
            List<EmployeeRetirement> empRetirement = new List<EmployeeRetirement>();

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

                DataReader dr = new DataReader(EmployeeRetirementDA.Get(tc));
                empRetirement = this.CreateObjects<EmployeeRetirement>(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 empRetirement;
        }

        #endregion


        #region IEmployeeRetirementService Members

        public int Save(EmployeeRetirement oEmployee)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oEmployee.IsNew)
                {
                    int id = tc.GenerateID("EmployeeRetirement", "empretirementid");
                    base.SetObjectID(oEmployee, id);
                    EmployeeRetirementDA.Insert(tc, oEmployee);
                }

                else
                {
                    EmployeeRetirementDA.Update(tc, oEmployee);
                }

                tc.End();
                return oEmployee.ID;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }


        public void SaveAll(List<EmployeeRetirement> items)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (EmployeeRetirement iteam in items)
                {
                    int id = tc.GenerateID("EmployeeRetirement", "empretirementid");
                    base.SetObjectID(iteam, id);
                    EmployeeRetirementDA.Insert(tc, iteam);
                }

                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 UpdateAll(List<EmployeeRetirement> items)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (EmployeeRetirement iteam in items)
                {
                    EmployeeRetirementDA.Update(tc, iteam);
                }

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }


        public int GetById(int id)
        {
            TransactionContext tc = null;
            int reAge = -1;
            try
            {
                tc = TransactionContext.Begin(true);
                reAge = EmployeeRetirementDA.GetAgeById(tc, id);
                if (reAge != -1) return reAge;
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return reAge;
        }

        #endregion
    }
}