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 AppraisalPointService : ServiceTemplate, IAppraisalPointService
    {
        #region Object Mapping

        private void MapObject(AppraisalPoint oAppraisalPoint, DataReader oReader)
        {
            SetObjectID(oAppraisalPoint, (oReader.GetInt32("AppraisalPointID").Value));
            oAppraisalPoint.Code = oReader.GetString("Code");
            oAppraisalPoint.Name = oReader.GetString("Name");
            oAppraisalPoint.Point = oReader.GetInt32("Point", 0);
            oAppraisalPoint.Status = (EnumStatus)oReader.GetInt32("Status");

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

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

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

        #endregion

        #region Service Implementation
        public AppraisalPoint Get(int id)
        {
            AppraisalPoint oAppraisalPoint = new AppraisalPoint();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(AppraisalPointDA.Get(tc, id));
                if (oreader.Read())
                {
                    oAppraisalPoint = this.CreateObject<AppraisalPoint>(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 oAppraisalPoint;
        }
        public List<AppraisalPoint> Get(EnumStatus status)
        {
            List<AppraisalPoint> oAppraisalPoint = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(AppraisalPointDA.Get(tc, status));
                oAppraisalPoint = this.CreateObjects<AppraisalPoint>(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 oAppraisalPoint;
        }
        public List<AppraisalPoint> Get(EnumStatus status, int payrollTypeID)
        {
            List<AppraisalPoint> oAppraisalPoint = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(AppraisalPointDA.Get(tc, status));
                oAppraisalPoint = this.CreateObjects<AppraisalPoint>(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 oAppraisalPoint;
        }
        public int Save(AppraisalPoint item)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (item.IsNew)
                {
                    int id = tc.GenerateID("AppraisalPoint", "AppraisalPointID");
                    int seqNo = tc.GenerateID("AppraisalPoint", "Sequence");
                    item.Sequence = seqNo;
                    SetObjectID(item, (id));
                    AppraisalPointDA.Insert(tc, item);
                }
                else
                {
                    AppraisalPointDA.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 Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                AppraisalPointDA.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 CheckCode(string code)
        {
            bool flag = true;
            DataSet ds = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                ds = AppraisalPointDA.CheckCode(tc, code);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            if (ds.Tables[0].Rows.Count >= 1)
            {
                flag = false;
            }

            return flag;
        }

        #endregion
    }
}