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

namespace HRM.DA
{
    public class EmpAppraisalRatingService : ServiceTemplate, IEmpAppraisalRatingService
    {
        #region Object Mapping

        private void MapObject(EmpAppraisalRating oEmpAppraisalRating, DataReader oReader)
        {
            SetObjectID(oEmpAppraisalRating, (oReader.GetInt32("EmpAppraislRatingID").Value));
            oEmpAppraisalRating.AppraisalPointRatingID = oReader.GetInt32("AppraisalPointRatingID", 0);
            if (oReader.GetDateTime("AppraisalYear") != null)
            {
                oEmpAppraisalRating.AppraisalYear = (DateTime)oReader.GetDateTime("AppraisalYear");
            }
            else
            {
                oEmpAppraisalRating.AppraisalYear = DateTime.MinValue;
            }

            oEmpAppraisalRating.EmployeeID = oReader.GetInt32("EmployeeID", 0);

            oEmpAppraisalRating.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oEmpAppraisalRating.CreatedDate = oReader.GetDateTime("CreatedDate").HasValue
                ? oReader.GetDateTime("CreatedDate").Value
                : DateTime.MinValue;
            oEmpAppraisalRating.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oEmpAppraisalRating.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue
                ? oReader.GetDateTime("ModifiedDate").Value
                : (DateTime?)null;


            this.SetObjectState(oEmpAppraisalRating, ObjectState.Saved);
        }

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

        #endregion

        #region Service Implementation

        public List<EmpAppraisalRating> Get(DateTime appraisalyear)
        {
            List<EmpAppraisalRating> oEmpAppraisalRating = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(EmpAppraisalRatingDA.Get(tc, appraisalyear));
                oEmpAppraisalRating = this.CreateObjects<EmpAppraisalRating>(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 oEmpAppraisalRating;
        }


        public int Save(EmpAppraisalRating item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (item.IsNew)
                {
                    int id = tc.GenerateID("EmpAppraisalRating", "EmpAppraislRatingID");
                    int sNo = tc.GenerateID("EmpAppraisalRating", "Sequence");
                    SetObjectID(item, (id));
                    EmpAppraisalRatingDA.Insert(tc, item);
                }

                else
                {
                    EmpAppraisalRatingDA.Update(tc, item);
                }

                tc.End();

                return item.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(List<EmpAppraisalRating> items)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (EmpAppraisalRating item in items)
                {
                    EmpAppraisalRatingDA.DeleteByEmpIdAndYear(tc, item);
                    //if (item.IsNew)
                    //{
                    //    int id = tc.GenerateID("EmpAppraisalRating", "EmpAppraislRatingID");
                    //    SetObjectID(item, (id));
                    //    EmpAppraisalRatingDA.Insert(tc, item);
                    //}

                    //else
                    //{
                    //    EmpAppraisalRatingDA.Update(tc, item);
                    //}
                    int id = tc.GenerateID("EmpAppraisalRating", "EmpAppraislRatingID");
                    SetObjectID(item, (id));
                    EmpAppraisalRatingDA.Insert(tc, item);
                }

                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(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                EmpAppraisalRatingDA.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 void Update(EmpAppraisalRating item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                EmpAppraisalRatingDA.Update(tc, item);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        #endregion
    }
}