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


namespace HRM.DA
{
    #region JVSetupCC Service

    public class JVSetupCCService : ServiceTemplate, IJVSetupCCService
    {
        #region Private functions and declaration

        public JVSetupCCService()
        {
        }

        private void MapObject(JVSetupCC oJVSetupCC, DataReader oReader)
        {
            base.SetObjectID(oJVSetupCC, oReader.GetInt32("JVSetupCCID").Value);
            oJVSetupCC.CCID = oReader.GetString("CCID") == null ? 0 : oReader.GetInt32("CCID").Value;
            oJVSetupCC.JVSetupID = oReader.GetString("JVSetupID") == null ? 0 : oReader.GetInt32("JVSetupID").Value;
            oJVSetupCC.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value;
            oJVSetupCC.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oJVSetupCC.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value;
            oJVSetupCC.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oJVSetupCC, Ease.Core.ObjectState.Saved);
        }

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

        protected JVSetupCC CreateObject(DataReader oReader)
        {
            JVSetupCC oJVSetupCC = new JVSetupCC();
            MapObject(oJVSetupCC, oReader);
            return oJVSetupCC;
        }

        #endregion

        #region Service implementation

        public JVSetupCC Get(int id)
        {
            JVSetupCC oJVSetupDetail = new JVSetupCC();

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

        public List<JVSetupCC> Get()
        {
            List<JVSetupCC> oJVSetupDetails = new List<JVSetupCC>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(JVSetupCCDA.Get(tc));
                oJVSetupDetails = this.CreateObjects<JVSetupCC>(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 oJVSetupDetails;
        }
        public List<JVSetupCC> GetLatestSetupByJVType(int payrolltypeid, int jvtypeid)
        {
            List<JVSetupCC> JVSetupDetails = new List<JVSetupCC>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(JVSetupCCDA.GetLatestSetupByJVType(tc, payrolltypeid, jvtypeid));
                JVSetupDetails = this.CreateObjects<JVSetupCC>(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 JVSetupDetails;
        }
        public List<JVSetupCC> GetByJVSetup(int JVSetupID)
        {
            List<JVSetupCC> JVSetupDetails = new List<JVSetupCC>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(JVSetupCCDA.GetByJVSetup(tc, JVSetupID));
                JVSetupDetails = this.CreateObjects<JVSetupCC>(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 JVSetupDetails;
        }

        public int Save(JVSetupCC oJVSetupCC)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oJVSetupCC.IsNew)
                {
                    int id = tc.GenerateID("JVSetupCC", "JVSetupCCID");
                    base.SetObjectID(oJVSetupCC, id);
                    JVSetupCCDA.Insert(tc, oJVSetupCC);
                }
                else
                {
                    JVSetupCCDA.Update(tc, oJVSetupCC);
                }

                tc.End();

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to GetJVSetupDetail", e);

                #endregion
            }
        }

        public void Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                JVSetupCCDA.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
}