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

namespace HRM.DA
{
    public class BadliDailyRecruitService : ServiceTemplate
    {
        public BadliDailyRecruitService()
        {
        }

        private void MapObject(BadliDailyRecruit oBadliDailyRecruit, DataReader oReader)
        {
            base.SetObjectID(oBadliDailyRecruit, (oReader.GetInt32("BadliDailyRecruitID").Value));
            oBadliDailyRecruit.AccessCardNo = oReader.GetString("AccessCardNo");
            oBadliDailyRecruit.RecommendedBy = oReader.GetString("RecommendedBy");
            oBadliDailyRecruit.EmployeeID = oReader.GetInt32("EmployeeID", 0);
            oBadliDailyRecruit.ShiftID = oReader.GetInt32("ShiftID", 0);
            oBadliDailyRecruit.WorkPlanGroupID = oReader.GetInt32("WorkPlanGroupID", 0);
            oBadliDailyRecruit.DepartmentID = oReader.GetInt32("DepartmentID", 0);
            oBadliDailyRecruit.EntryDate = oReader.GetDateTime("EntryDate").Value;
            oBadliDailyRecruit.InTime = oReader.GetDateTime("InTime").Value;
            oBadliDailyRecruit.OutTime = oReader.GetDateTime("OutTime").Value;
            oBadliDailyRecruit.Status = (EnumBadliStatus)oReader.GetInt32("Status").Value;
            oBadliDailyRecruit.WorkDayType = (EnumWorkPlanDayType)oReader.GetInt32("WorkDayType").Value;
            oBadliDailyRecruit.OTHours = oReader.GetDouble("OTHours").Value;
            oBadliDailyRecruit.ChangedOTHours = oReader.GetDouble("ChangedOTHours").Value;
            oBadliDailyRecruit.OTRemarks = oReader.GetString("OTRemarks");
            oBadliDailyRecruit.BenefitRemarks = oReader.GetString("BenefitRemarks");
            oBadliDailyRecruit.PaymentCode = oReader.GetString("PaymentCode");
            oBadliDailyRecruit.IsPaid = oReader.GetBoolean("IsPaid").Value;
            oBadliDailyRecruit.IsPaymentCompleted = oReader.GetBoolean("IsPaymentCompleted").Value;
            oBadliDailyRecruit.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oBadliDailyRecruit.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oBadliDailyRecruit.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oBadliDailyRecruit.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oBadliDailyRecruit, Ease.Core.ObjectState.Saved);
        }

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

        #region ServiceImplementation

        public BadliDailyRecruit Get(int empID, DateTime EntryDate, EnumBadliStatus status)
        {
            BadliDailyRecruit oBadliDailyRecruit = new BadliDailyRecruit();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(BadliDailyRecruitDA.Get(tc, empID, EntryDate, status));
                if (oreader.Read())
                {
                    oBadliDailyRecruit = this.CreateObject<BadliDailyRecruit>(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 oBadliDailyRecruit;
        }


        public List<BadliDailyRecruit> Get()
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.Get(tc));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> GetByShift(int ShiftID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.GetByShift(tc, ShiftID));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> GetByDepartment(int DeptID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.GetByDepartment(tc, DeptID));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> GetByDepartment(string DeptName, DateTime entryDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.GetByDepartment(tc, DeptName, entryDate));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> Get(DateTime fromDate, DateTime toDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.Get(tc, fromDate, toDate));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> Get(DateTime fromDate, DateTime toDate, int departmentID, int shiftID,
            int relayID, EnumBadliStatus status, bool hasStatus)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.Get(tc, fromDate, toDate, departmentID, shiftID,
                    relayID, status, hasStatus));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> Get(DateTime fromDate, DateTime toDate, int departmentID, int shiftID,
            int relayID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr =
                    new DataReader(BadliDailyRecruitDA.Get(tc, fromDate, toDate, departmentID, shiftID, relayID));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> Get(string empIds, DateTime fromDate, DateTime toDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.Get(tc, empIds, fromDate, toDate));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> GetAllUnAssignedFrom(DateTime FromDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.GetAllUnAssignedFrom(tc, FromDate));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> Get(DateTime entryDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.Get(tc, entryDate));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public List<BadliDailyRecruit> Get(string empIds, DateTime entryDate)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(BadliDailyRecruitDA.Get(tc, empIds, entryDate));
                var oBadliDailyRecruits = this.CreateObjects<BadliDailyRecruit>(dr);
                dr.Close();
                tc.End();
                return oBadliDailyRecruits;
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public DataSet GetBadliDepartmentWiseWorkDay(string ids, DateTime fromdate, DateTime toDate)
        {
            DataSet oDeptWiseWorkDay = new DataSet();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oDeptWiseWorkDay = BadliDailyRecruitDA.GetBadliDepartmentWiseWorkDay(tc, ids, fromdate, toDate);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oDeptWiseWorkDay;
        }

        public DataSet GetAllBadliLineManager()
        {
            DataSet oLineManager = new DataSet();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oLineManager = BadliDailyRecruitDA.GetAllBadliLineManager(tc);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oLineManager;
        }

        public void Save(List<BadliDailyRecruit> oBadliDailyRecruits)
        {
            TransactionContext tc = null;
            string msg = "";
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (BadliDailyRecruit ac in oBadliDailyRecruits)
                {
                    msg = "";
                    if (ac.IsNew)
                    {
                        int id = tc.GenerateID("BadliDailyRecruit", "BadliDailyRecruitID");
                        base.SetObjectID(ac, (id));
                        if (BadliDailyRecruitDA.IsExist(tc, ac.EmployeeID, ac.EntryDate, ac.WorkPlanGroupID,
                            ac.ShiftID))
                        {
                            msg = "Data has been already processed for the date: " +
                                  ac.EntryDate.ToString("dd MMM yyyy");
                            BadliDailyRecruitDA.Delete(tc, ac.EmployeeID, ac.EntryDate, ac.WorkPlanGroupID, ac.ShiftID);
                        }

                        BadliDailyRecruitDA.Insert(tc, ac);
                    }
                    else
                    {
                        BadliDailyRecruitDA.Update(tc, ac);
                    }
                }

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

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

                #endregion
            }
        }

        public void Save(List<BadliDailyRecruit> oBadliDailyRecruit, TransactionContext tc)
        {
            try
            {
                foreach (BadliDailyRecruit ac in oBadliDailyRecruit)
                {
                    if (ac.IsNew)
                    {
                        int id = tc.GenerateID("BadliDailyRecruit", "BadliDailyRecruitID");
                        base.SetObjectID(ac, (id));
                        if (BadliDailyRecruitDA.IsExist(tc, ac.EmployeeID, ac.EntryDate, ac.WorkPlanGroupID,
                            ac.ShiftID))
                        {
                            BadliDailyRecruitDA.Delete(tc, ac.EmployeeID, ac.EntryDate, ac.WorkPlanGroupID, ac.ShiftID);
                        }

                        BadliDailyRecruitDA.Insert(tc, ac);
                    }
                    else
                    {
                        BadliDailyRecruitDA.Update(tc, ac);
                    }
                }
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public void UndoDepart(List<BadliDailyRecruit> oBadliDailyRecruit)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                foreach (BadliDailyRecruit bRecruit in oBadliDailyRecruit)
                {
                    BadliDailyRecruitDA.UndoDepart(tc, bRecruit);
                }

                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);
                BadliDailyRecruitDA.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
    }
}