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;
using HRM.DA;

namespace HRM.DA.Fund
{
    #region InvestmentLog Service

    [Serializable]
    public class InvestmentLogService : ServiceTemplate
    {
        #region Private functions and declaration

        public InvestmentLogService()
        {
        }

        private void MapObject(InvestmentLog oInvestmentLog, DataReader oReader)
        {
            base.SetObjectID(oInvestmentLog, oReader.GetInt32("ID").Value);

            oInvestmentLog.ActivityID = oReader.GetInt32("ActivityID").Value;
            oInvestmentLog.FMActivityID = oReader.GetInt32("FMActivityID").Value;
            oInvestmentLog.CategoryID = oReader.GetInt32("CategoryID").Value;
            oInvestmentLog.InvestmentID = oReader.GetInt32("InvestmentID").Value;
            oInvestmentLog.ProjectID = oReader.GetInt32("ProjectID").Value;
            oInvestmentLog.TranDate = oReader.GetDateTime("TranDate").Value;
            oInvestmentLog.PrincipalAmount = oReader.GetDouble("PrincipalAmount").Value;
            oInvestmentLog.AccountNo = oReader.GetString("AccountNo");
            oInvestmentLog.BankID = oReader.GetInt32("BankID").Value;
            oInvestmentLog.BranchID = oReader.GetInt32("BranchID").Value;
            oInvestmentLog.CertificateNo = oReader.GetString("CertificateNo");
            oInvestmentLog.RefID = oReader.GetInt32("RefID").Value;
            oInvestmentLog.PreStatus = oReader.GetInt32("PreStatus").Value;
            oInvestmentLog.CurrentStatus = oReader.GetInt32("CurrentStatus").Value;
            oInvestmentLog.CreatedBy = oReader.GetInt32("CreatedBy").Value;
            oInvestmentLog.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oInvestmentLog.ModifiedBy = oReader.GetInt32("ModifiedBy").Value;
            oInvestmentLog.ModifiedDate = oReader.GetDateTime("ModifiedDate").Value;

            base.SetObjectState(oInvestmentLog, ObjectState.Modified);
        }

        private InvestmentLog CreateObject(DataReader oReader)
        {
            InvestmentLog oInvestmentLog = new InvestmentLog();
            MapObject(oInvestmentLog, oReader);
            return oInvestmentLog;
        }

        #endregion

        #region Service implementation

        //public InvestmentLog Get(int id)
        //{
        //    InvestmentLog oInvestmentLog = new InvestmentLog();
        //    #region Cache Header
        //    oInvestmentLog = new InvestmentLog();
        //    if (oInvestmentLog != null)
        //        return oInvestmentLog;
        //    #endregion
        //    TransactionContext tc = null;
        //    try
        //    {
        //        tc = TransactionContext.Begin();
        //        IDataReader oreader = InvestmentLogDA.Get(tc, id);
        //        DataReader oReader = new DataReader(oreader);
        //        if (oreader.Read())
        //        {
        //            oInvestmentLog = CreateObject(oReader);
        //        }
        //        oreader.Close();
        //        tc.End();
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception
        //        if (tc != null)
        //            tc.HandleError();
        //        ExceptionLog.Write(e);
        //        throw new ServiceException("Failed to GetInvestmentLog", e);
        //        #endregion
        //    }
        //    #region Cache Footer
        //    
        //    #endregion
        //    return oInvestmentLog;
        //}

        public DataTable GetTable(int fundtypeid)
        {
            DataTable dataTable = new DataTable("InvestmentLog");
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                IDataReader iDataReader = InvestmentLogDA.Get(tc, fundtypeid);
                dataTable.Load(iDataReader);
                iDataReader.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 dataTable;
        }

        public List<InvestmentLog> Get(int fundtypeid)
        {
            #region Cache Header

            List<InvestmentLog> items = new List<InvestmentLog>();
            if (items != null)
                return items;

            #endregion

            items = new List<InvestmentLog>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dataReader = new DataReader(InvestmentLogDA.Get(tc, fundtypeid));
                while (dataReader.Read())
                {
                    InvestmentLog item = this.CreateObject(dataReader);
                    items.Add(item);
                }

                dataReader.Close();

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }

            #region Cache Footer

            #endregion

            return items;
        }

        protected override T CreateObject<T>(DataReader dataReader)
        {
            InvestmentLog oInvestmentLog = new InvestmentLog();

            MapObject(oInvestmentLog, dataReader);

            return oInvestmentLog as T;
        }

        public void Save(InvestmentLog oInvestmentLog)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oInvestmentLog.IsNew)
                {
                    int id = tc.GenerateID("InvestmentLog", "ID");
                    base.SetObjectID(oInvestmentLog, (id));
                    InvestmentLogDA.Insert(tc, oInvestmentLog);

                    //    BusinessObject.Factory.SetID(oInvestmentLog, new ID(DAInvestmentLog.GetNewID(tc)));
                    //    InvestmentLogDA.Insert(tc, oInvestmentLog);
                }
                else
                {
                    InvestmentLogDA.Update(tc, oInvestmentLog);
                }

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

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

                #endregion
            }
        }

        public void Delete(int id)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                InvestmentLogDA.Delete(tc, id);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        #endregion
    }

    #endregion
}