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 System.IO;
using Microsoft.Data.SqlClient;


namespace HRM.DA
{
    #region InternalRecruitment Service

    public class InternalRecruitmentService : ServiceTemplate, IInternalRecruitmentService
    {
        #region InternalRecruitment

        #region Private functions and declaration

        public InternalRecruitmentService()
        {
        }

        private void MapObject(InternalRecruitment oInternalRecruitment, DataReader oReader)
        {
            base.SetObjectID(oInternalRecruitment, (oReader.GetInt32("PositionId").Value));
            oInternalRecruitment.PositionName = oReader.GetString("PositionName", null);
            oInternalRecruitment.JobDescription = oReader.GetString("JobDescription", null);
            //oInternalRecruitment.Education =(EducationEnum)oReader.GetInt32("Education",true,0);
            oInternalRecruitment.Experience = oReader.GetInt32("Experience", 0);
            oInternalRecruitment.Responsibility = oReader.GetString("Responsibility", null);
            oInternalRecruitment.OtherResponsibility = oReader.GetString("OtherResponsibility", null);
            oInternalRecruitment.SalaryRange = oReader.GetString("SalaryRange", null);
            oInternalRecruitment.Benefits = oReader.GetString("Benefits", null);
            oInternalRecruitment.ApplicationLastDate = oReader.GetString("ApplicationLastDate", null);
            oInternalRecruitment.PublishedDate = oReader.GetDateTime("PublishedDate").GetValueOrDefault();
            oInternalRecruitment.ExpectedJoiningDate = oReader.GetDateTime("ExpectedJoiningDate").GetValueOrDefault();
            oInternalRecruitment.DepartmentId = oReader.GetInt32("DepartmentId", 0);
            oInternalRecruitment.GradeId = oReader.GetInt32("GradeId", 0);
            oInternalRecruitment.DesignationId = oReader.GetInt32("DesignationId", 0);
            oInternalRecruitment.JdId = oReader.GetInt32("JdId", true, 0);
            oInternalRecruitment.BasicSalary = oReader.GetString("BasicSalary", null);
            oInternalRecruitment.BudgetedHeadCount = oReader.GetInt32("BudgetedHeadCount", 0);
            oInternalRecruitment.CurrentHeadCount = oReader.GetInt32("CurrentHeadCount", 0);
            oInternalRecruitment.TotalHeadCount = oReader.GetInt32("TotalHeadCount", 0);
            oInternalRecruitment.Type = oReader.GetString("Type") != null ? (EnumInternalRecruitmentType)oReader.GetInt32("Type").Value : EnumInternalRecruitmentType.None;
            //oInternalRecruitment.Type = oReader.GetString("Type", null);
            oInternalRecruitment.Keywords = oReader.GetString("Keywords", null);
            oInternalRecruitment.Institution = oReader.GetString("Institution", null);
            oInternalRecruitment.KPI = oReader.GetString("KPI", null);
            oInternalRecruitment.ExpectedNewJoiningDate = oReader.GetDateTime("ExpectedNewJoiningDate").GetValueOrDefault();
            oInternalRecruitment.SeparationDate = oReader.GetDateTime("SeparationDate").GetValueOrDefault();
            oInternalRecruitment.ReplacementDueTo = oReader.GetString("ReplacementReason", null);
            oInternalRecruitment.Specialization = oReader.GetString("Specialization", null);
            oInternalRecruitment.OnBoradStatus = oReader.GetString("ONBOARDSTATUS") != null ? (EnumOnBoradStatus)oReader.GetInt32("ONBOARDSTATUS").Value : EnumOnBoradStatus.None;
            //oInternalRecruitment.WorkflowRequired = oReader.GetBoolean("WorkflowRequired").GetValueOrDefault();
            oInternalRecruitment.IsClosed = oReader.GetBoolean("IsClosed").GetValueOrDefault();
            oInternalRecruitment.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oInternalRecruitment.CreatedDate = oReader.GetDateTime("CreationDate").HasValue
                ? oReader.GetDateTime("CreationDate").Value
                : DateTime.MinValue;
            oInternalRecruitment.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oInternalRecruitment.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue
                ? oReader.GetDateTime("ModifiedDate").Value
                : (DateTime?)null;
            oInternalRecruitment.PositionDate = oReader.GetDateTime("PositionDate").HasValue
               ? oReader.GetDateTime("PositionDate").Value
               : DateTime.MinValue;
            oInternalRecruitment.DesignationName = oReader.GetString("DesignationName", true, null);
            oInternalRecruitment.NumberOfStuff = oReader.GetInt32("NumberOfStuff", 0);
            oInternalRecruitment.HeadCountApprovalID = oReader.GetInt32("HeadCountApprovalID", true, 0);
            oInternalRecruitment.offerLetterApproveStatus = (EnumOfferLetterStatus)oReader.GetInt32("offerLetterApproveStatus").Value;
            oInternalRecruitment.RequisitionApprovalStatus = oReader.GetString("REQUISITIONAPPROVALSTATUS") != null ? (EnumRequisitionApprovalStatus)oReader.GetInt32("REQUISITIONAPPROVALSTATUS").Value : EnumRequisitionApprovalStatus.Not_Initiated;
            oInternalRecruitment.PositionCode = oReader.GetString("PositionCode", null);
            oInternalRecruitment.KeyResponsibilies = oReader.GetString("KEYRESPONSIBILTITIES", true, null);
            oInternalRecruitment.RaisedBy = oReader.GetInt32("RaisedBy", true, 0);
            oInternalRecruitment.PositionNo = oReader.GetString("PositionNo", true, null);
            oInternalRecruitment.ComputerRequisition = oReader.GetString("ComputerRequisition") != null ? (ComputerRequisitionEnum)oReader.GetInt32("ComputerRequisition").Value : null;
            oInternalRecruitment.LocationId = oReader.GetInt32("LocationId", 0);
            oInternalRecruitment.Remarks = oReader.GetString("Remarks", true, null);
            oInternalRecruitment.LastWorkingDate = oReader.GetDateTime("LastWorkingDate").HasValue
               ? oReader.GetDateTime("LastWorkingDate").Value
               : (DateTime?)null;
            oInternalRecruitment.JobLocation = oReader.GetString("Location",true,null);
            this.SetObjectState(oInternalRecruitment, Ease.Core.ObjectState.Saved);
        }

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

        private InternalRecruitment CreateObject(DataReader oReader)
        {
            InternalRecruitment oInternalRecruitment = new InternalRecruitment();
            MapObject(oInternalRecruitment, oReader);
            return oInternalRecruitment;
        }

        private void MapNotificationObject(IRNotification oIRNotification, DataReader oReader)
        {
            base.SetObjectID(oIRNotification, (oReader.GetInt32("IRNotificationID").Value));
            oIRNotification.PositionID = oReader.GetInt32("PositionId", 0);
            oIRNotification.NotificationDate = oReader.GetDateTime("NotificationDate").GetValueOrDefault();
            oIRNotification.Description = oReader.GetString("Description");
            oIRNotification.NotifiedBy = oReader.GetInt32("NotifiedBy", 0);

            this.SetObjectState(oIRNotification, Ease.Core.ObjectState.Saved);
        }

        protected List<IRNotification> CreateDetailObjects(DataReader oReader)
        {
            List<IRNotification> oDetails = new List<IRNotification>();
            while (oReader.Read())
            {
                IRNotification oIRNotification = new IRNotification();
                MapNotificationObject(oIRNotification, oReader);
                oDetails.Add(oIRNotification);
            }

            return oDetails;
        }

        private IRNotification CreateDetailObject(DataReader oReader)
        {
            IRNotification oIRNotification = new IRNotification();
            MapNotificationObject(oIRNotification, oReader);
            return oIRNotification;
        }

        private void MapIREmployeeObject(IREmployee oIREmployee, DataReader oReader)
        {
            base.SetObjectID(oIREmployee, (oReader.GetInt32("IREmployeeID").Value));
            oIREmployee.PositionID = oReader.GetInt32("PositionId", 0);
            oIREmployee.EmployeeID = oReader.GetInt32("EmployeeID", 0);
            oIREmployee.WfStatus = (EnumwfStatus)oReader.GetInt32("WfStatus").GetValueOrDefault();
            oIREmployee.AppliedDate = oReader.GetDateTime("AppliedDate").GetValueOrDefault();
            oIREmployee.Description = oReader.GetString("Description");
            oIREmployee.IsSelected = oReader.GetBoolean("IsSelected").GetValueOrDefault();

            this.SetObjectState(oIREmployee, Ease.Core.ObjectState.Saved);
        }

        protected List<IREmployee> CreateirEmployeeObjects(DataReader oReader)
        {
            List<IREmployee> oIREmployees = new List<IREmployee>();
            while (oReader.Read())
            {
                IREmployee oIREmployee = new IREmployee();
                MapIREmployeeObject(oIREmployee, oReader);
                oIREmployees.Add(oIREmployee);
            }

            return oIREmployees;
        }

        private IREmployee CreateIREmployeeObject(DataReader oReader)
        {
            IREmployee oIREmployees = new IREmployee();
            MapIREmployeeObject(oIREmployees, oReader);
            return oIREmployees;
        }

        protected List<FileAttachment> CreateirFileAttachmentObjects(DataReader oReader)
        {
            List<FileAttachment> oIRFileAttachments = new List<FileAttachment>();
            while (oReader.Read())
            {
                FileAttachment oIRFileAtachment = new FileAttachment();
                MapIRFileAttachmentObject(oIRFileAtachment, oReader);
                oIRFileAttachments.Add(oIRFileAtachment);
            }

            return oIRFileAttachments;
        }

        private FileAttachment CreateIRFileAttcahmentObject(DataReader oReader)
        {
            FileAttachment oIRFileAtachment = new FileAttachment();
            MapIRFileAttachmentObject(oIRFileAtachment, oReader);
            return oIRFileAtachment;
        }

        private void MapIRFileAttachmentObject(FileAttachment oIRFileAttachment, DataReader oReader)
        {
            base.SetObjectID(oIRFileAttachment, (oReader.GetInt32("FILEATTACHMENTID").Value));
            oIRFileAttachment.ReferenceID = oReader.GetInt32("ReferenceID", 0);
            oIRFileAttachment.FileAsByteArray = oReader.GetLob("FileData");
            oIRFileAttachment.OriginalFileName = oReader.GetString("OriginalFileName");
            oIRFileAttachment.FileType = (EnumFileType)oReader.GetInt32("FileType").GetValueOrDefault();

            this.SetObjectState(oIRFileAttachment, Ease.Core.ObjectState.Saved);
        }
        #endregion

        #region Service implementation

        public InternalRecruitment Get(int id)
        {
            InternalRecruitment oInternalRecruitment = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(InternalRecruitmentDA.Get(tc, id));
                if (dr.Read())
                {
                    oInternalRecruitment = CreateObject(dr);
                }
                //if(oInternalRecruitment != null)
                //{
                //    oInternalRecruitment.IREmployees = new List<IREmployee>();
                //    oInternalRecruitment.IREmployees = InternalRecruitmentDA.GetIREmployeess(tc, id);
                //}


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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Get Leave Parameter :" + e.Message, e);

                #endregion
            }

            return oInternalRecruitment;
        }

        public List<InternalRecruitment> Get(DateTime? fromDate, DateTime? toDate, EnumInternalRecruitmentType type)
        {
            List<InternalRecruitment> oInternalRecruitments = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(InternalRecruitmentDA.Get(tc, fromDate, toDate, type));
                oInternalRecruitments = this.CreateObjects<InternalRecruitment>(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 oInternalRecruitments;
        }

        public List<InternalRecruitment> GetApprovedUserRequisitions(int userId)
        {
            List<InternalRecruitment> oInternalRecruitments = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(InternalRecruitmentDA.GetApprovedUserRequisitions(tc, userId));
                oInternalRecruitments = this.CreateObjects<InternalRecruitment>(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 oInternalRecruitments;
        }

        public List<InternalRecruitment> GetApprovedRequisitionsPicker(EnumRequisitionApprovalStatus? approvalStatus,
            string onBoradStatus)
        {
            List<InternalRecruitment> oInternalRecruitments = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(InternalRecruitmentDA.GetApprovedRequisitions(tc, approvalStatus, onBoradStatus));
                oInternalRecruitments = this.CreateObjects<InternalRecruitment>(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 oInternalRecruitments;
        }


        public List<InternalRecruitment> Get(bool isClosed)
        {
            List<InternalRecruitment> oInternalRecruitments = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(InternalRecruitmentDA.Get(tc, isClosed));
                oInternalRecruitments = this.CreateObjects<InternalRecruitment>(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 oInternalRecruitments;
        }

        public int Save(InternalRecruitment oInternalRecruitment, string connectionString)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oInternalRecruitment.IsNew)
                {
                    int id = tc.GenerateID("InternalReqruitment", "PositionId");
                    base.SetObjectID(oInternalRecruitment, (id));
                    // var time24 = DateTime.Now.ToString("HH_mm");
                    oInternalRecruitment.PositionNo = DateTime.Now.ToString("yyyyMMdd") + "-" + id.ToString("00000");
                    InternalRecruitmentDA.Insert(tc, oInternalRecruitment);
                }
                else
                {
                    InternalRecruitmentDA.Update(tc, oInternalRecruitment);
                }

                InternalRecruitmentDA.DeleteNotifications(tc, oInternalRecruitment.ID);
                InternalRecruitmentDA.DeleteIREmployees(tc, oInternalRecruitment.ID);
                InternalRecruitmentDA.DeleteIRAttachment(tc, oInternalRecruitment.ID);
                if (oInternalRecruitment.Notifications != null && oInternalRecruitment.Notifications.Count > 0)
                {
                    foreach (IRNotification oDetail in oInternalRecruitment.Notifications)
                    {
                        oDetail.PositionID = oInternalRecruitment.ID;
                        this.SetObjectID(oDetail, (InternalRecruitmentDA.GetNewNotificationID(tc)));
                        InternalRecruitmentDA.InsertNotification(tc, oDetail);
                    }
                }

                if (oInternalRecruitment.IREmployees != null && oInternalRecruitment.IREmployees.Count > 0)
                {
                    foreach (IREmployee oemployee in oInternalRecruitment.IREmployees)
                    {
                        if(oemployee.ID > 0)
                        {
                            oemployee.PositionID = oInternalRecruitment.ID;
                            oemployee.AppliedDate = DateTime.Today;
                            this.SetObjectID(oemployee, (InternalRecruitmentDA.GetNewIREmployeeID(tc)));
                            InternalRecruitmentDA.InsertIREmployee(tc, oemployee);
                        }
                    }
                }
                tc.End();

                if (oInternalRecruitment.IRFileAttacments != null && oInternalRecruitment.IRFileAttacments.Count > 0)
                {
                    foreach (var uploadfile in oInternalRecruitment.IRFileAttacments)
                    {
                        if (uploadfile.ID > 0 && uploadfile.PreviousFileTobase64 != null)
                        {
                            byte[] newBytes = Convert.FromBase64String(uploadfile.PreviousFileTobase64);
                            uploadfile.FileAsByteArray = newBytes;
                            uploadfile.ReferenceID = oInternalRecruitment.ID;
                            uploadfile.FileType = EnumFileType.InternalRecruitment;
                            FileAttachmentDA.Insert(uploadfile, connectionString);
                        }
                        else
                        {
                            FileStream stream = new FileStream(uploadfile.FilePath, FileMode.Open, FileAccess.Read);
                            BinaryReader reader = new BinaryReader(stream);
                            byte[] buff = reader.ReadBytes((int)stream.Length);
                            reader.Close();
                            stream.Close();
                            uploadfile.FileAsByteArray = buff;
                            uploadfile.ReferenceID = oInternalRecruitment.ID;
                            uploadfile.FileType = EnumFileType.InternalRecruitment;
                            FileAttachmentDA.Insert(uploadfile, connectionString);
                        }

                    }
                }

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Insert Internal Recruitment :" + e.Message, e);

                #endregion
            }

            return oInternalRecruitment.ID;
        }

        public void InitiateOfferLetterWorkFlow(InternalRecruitment item, int initiateEmpID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                InternalRecruitmentDA.UpdateOfferLetterApproveStatus(tc, item.ObjectID, EnumOfferLetterStatus.not_Initiated, initiateEmpID);

                if (item.offerLetterApproveStatus == EnumOfferLetterStatus.not_Initiated)
                {
                    string subject = " Candidate has been selected for the offer letter of the position: " + item.PositionName + ". Your Approval is required";
                    WFManager<IworkflowInterface> om = new WFManager<IworkflowInterface>(item);
                    om.InitiateProcess(tc, initiateEmpID, 23, item.ObjectID, "", subject);
                }
                item.offerLetterApproveStatus = EnumOfferLetterStatus.InProcess;
                InternalRecruitmentDA.UpdateOfferLetterApproveStatus(tc, item.ObjectID, EnumOfferLetterStatus.InProcess, initiateEmpID);

                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 InitiateRecruitmentRequisitionWorkFlow(InternalRecruitment item, int initiateEmpID, int userID)
        {
            TransactionContext tc = null;

            try
            {
                tc = TransactionContext.Begin(true);

                if (item.RequisitionApprovalStatus == EnumRequisitionApprovalStatus.Not_Initiated)
                {
                    item.RequisitionApprovalStatus = EnumRequisitionApprovalStatus.InProcess;
                    InternalRecruitmentDA.UpdateRequisitionApproveStatus(tc, item.ObjectID, EnumRequisitionApprovalStatus.InProcess, userID);

                    string subject = " Requisition has raised: " + item.PositionName + ". Your Approval is required";
                    WFManager<IworkflowInterface> om = new WFManager<IworkflowInterface>(item);
                    om.InitiateProcess(tc, initiateEmpID, item.SetupID, item.ObjectID, "", subject);
                }
                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 UpdateRequisitionApproveStatusAdmin(InternalRecruitment item, int userID)
        {
            TransactionContext tc = null;

            try
            {
                tc = TransactionContext.Begin(true);
                InternalRecruitmentDA.UpdateRequisitionApproveStatus(tc, item.ObjectID, EnumRequisitionApprovalStatus.Approved, userID);
                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 ApproveOfferLetter(WFMovementTran approver)
        {
            InternalRecruitment item = this.Get(approver.ObjectID);
            TransactionContext tc = null;
            try
            {
                SearchEmployee s = new SearchEmployeeService().get(approver.FromEmployeeID);

                tc = TransactionContext.Begin(true);
                WFMovementTran newTran = new WFManager<IworkflowInterface>().Approve(tc, approver.ID,
                    approver.FromEmployeeID, approver.Remarks, "");


                if (newTran.Status == EnumwfStatus.End)
                {
                    //     string subject = " Requisition has raised: " + item.PositionName + ". Your Approval is required";
                    InternalRecruitmentDA.UpdateOfferLetterApproveStatus(tc, newTran.ObjectID, EnumOfferLetterStatus.Approved, 0);
                }

                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 ApproveRecruitmentRequisition(WFMovementTran approver)
        {
            InternalRecruitment item = this.Get(approver.ObjectID);
            TransactionContext tc = null;
            try
            {
                SearchEmployee s = new SearchEmployeeService().get(approver.FromEmployeeID);

                tc = TransactionContext.Begin(true);

                string subject = " Requisition has raised: " + item.PositionName + ". Your Approval is required";
                WFMovementTran newTran = new WFManager<IworkflowInterface>().Approve(tc, approver.ID,
                    approver.FromEmployeeID, approver.Remarks, subject);


                if (newTran.Status == EnumwfStatus.End)
                {
                    InternalRecruitmentDA.UpdateRequisitionApproveStatus(tc, newTran.ObjectID, EnumRequisitionApprovalStatus.Approved, approver.UserID);
                }

                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 RecruitmentRequisitionReject(WFMovementTran rejectTran)
        {
            TransactionContext tc = null;
            try
            {
                SearchEmployee s = new SearchEmployeeService().get(rejectTran.FromEmployeeID);
                //LeaveEntry le = new LeaveEntryService().Get(rejectTran.ObjectID);
                //Leave oleave = new LeaveService().Get(le.LeaveID);
                InternalRecruitment internalRecruitment = new InternalRecruitmentService().Get(rejectTran.ObjectID);

                tc = TransactionContext.Begin(true);
                string rejectString = s.EmployeeNo + "-" + s.Name + ",  has rejected your " + internalRecruitment.PositionName;
                new WFManager<IworkflowInterface>().Reject(tc, rejectTran.ID, rejectTran.FromEmployeeID, rejectTran.Remarks, rejectString);

                //LeaveEntryDA.UpdateLeaveStatus(tc, le.ID, EnumLeaveStatus.Declined);
                InternalRecruitmentDA.UpdateRequisitionApproveStatus(tc, internalRecruitment.ID, EnumRequisitionApprovalStatus.Decline, 0);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public int SaveIrEmployee(IREmployee iREmployee)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (iREmployee.IsNew)
                {
                    int id = tc.GenerateID("IREmployee", "IREmployeeID");
                    base.SetObjectID(iREmployee, (id));
                    InternalRecruitmentDA.InsertIREmployee(tc, iREmployee);
                }
                else
                {
                    InternalRecruitmentDA.DeleteIREmployees(tc, iREmployee.PositionID, iREmployee.EmployeeID);
                    int id = tc.GenerateID("IREmployee", "IREmployeeID");
                    base.SetObjectID(iREmployee, (id));
                    InternalRecruitmentDA.InsertIREmployee(tc, iREmployee);
                }

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Insert IREmployee :" + e.Message, e);

                #endregion
            }

            return iREmployee.ID;
        }

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Delete Internal Recruitment: " + e.Message, e);

                #endregion
            }
        }

        public List<IRNotification> GetNotifications(int PositionId)
        {
            List<IRNotification> oDetails = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(InternalRecruitmentDA.GetNotifications(tc, PositionId));
                oDetails = this.CreateDetailObjects(oreader);
                oreader.Close();
            }
            catch (Exception ex)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(ex);
                throw new ServiceException("Failed to Get IRNotifications: " + ex.Message, ex);

                #endregion
            }

            return oDetails;
        }

        public IRNotification GetWithPositionID(int PositionId)
        {
            IRNotification oIRNotification = null;

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(InternalRecruitmentDA.Get(tc, PositionId));
                if (dr.Read())
                {
                    oIRNotification = CreateDetailObject(dr);
                }

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Get IRNotification: " + e.Message, e);

                #endregion
            }

            return oIRNotification;
        }

        public void UpdateCompleteStatus(int positionID)
        {
            TransactionContext tc = null;
            int oID = 0;
            try
            {
                tc = TransactionContext.Begin(true);
                InternalRecruitmentDA.UpdateRequisitionStatus(tc, positionID, EnumOnBoradStatus.CVCollection);
                tc.End();

            }
            catch (Exception ex)
            {
                #region Handle Exception

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

                #endregion
            }
        }


        public List<IREmployee> GetIREmployeess(int PositioniD)
        {
            List<IREmployee> oDetails = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(InternalRecruitmentDA.GetIREmployeess(tc, PositioniD));
                oDetails = this.CreateirEmployeeObjects(oreader);
                oreader.Close();
            }
            catch (Exception ex)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(ex);
                throw new ServiceException("Failed to Get IRNotifications: " + ex.Message, ex);

                #endregion
            }

            return oDetails;
        }

        public List<FileAttachment> GetIRFileAttachments(int PositioniD)
        {
            List<FileAttachment> oFileAttachments = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(FileAttachmentDA.GetByReferenceId(tc, PositioniD, EnumFileType.InternalRecruitment));
                oFileAttachments = this.CreateirFileAttachmentObjects(oreader);
                oreader.Close();
            }
            catch (Exception ex)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(ex);
                throw new ServiceException("Failed to Get IRNotifications: " + ex.Message, ex);

                #endregion
            }

            return oFileAttachments;
        }

        public List<FileAttachment> GetIRFileAttachmentbyID(int PositioniD, int id)
        {
            List<FileAttachment> oFileAttachments = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(FileAttachmentDA.GetByReferenceandFileId(tc, PositioniD, EnumFileType.InternalRecruitment, id));
                oFileAttachments = this.CreateirFileAttachmentObjects(oreader);
                oreader.Close();
            }
            catch (Exception ex)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(ex);
                throw new ServiceException("Failed to Get IRNotifications: " + ex.Message, ex);

                #endregion
            }

            return oFileAttachments;
        }

        public IREmployee GetIrempID(int irEmpID)
        {
            IREmployee oIREmployee = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(InternalRecruitmentDA.Get(tc, irEmpID));
                if (dr.Read())
                {
                    oIREmployee = CreateIREmployeeObject(dr);
                }

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Get IRNotification: " + e.Message, e);

                #endregion
            }

            return oIREmployee;
        }

        public IREmployee Get(int PositionId, int empId)
        {
            IREmployee oIREmployee = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(InternalRecruitmentDA.Get(tc, PositionId, empId));
                if (dr.Read())
                {
                    oIREmployee = CreateIREmployeeObject(dr);
                }

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

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to Get IRNotification: " + e.Message, e);

                #endregion
            }

            return oIREmployee;
        }

        public List<FileAttachment> GetIRFileAttachmentbyIDCV(int PositioniD, int id, EnumFileType type)
        {
            List<FileAttachment> oFileAttachments = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(FileAttachmentDA.GetByReferenceandFileId(tc, PositioniD, type, id));
                oFileAttachments = this.CreateirFileAttachmentObjects(oreader);
                oreader.Close();
            }
            catch (Exception ex)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(ex);
                throw new ServiceException("Failed to Get IRNotifications: " + ex.Message, ex);

                #endregion
            }

            return oFileAttachments;
        }

        public DataSet GetRequsitionCountChart(DateTime fromDate, DateTime toDate)
        {
            DataSet oEmpBasicInfos = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oEmpBasicInfos = InternalRecruitmentDA.GetRequsitionCountChart(tc, 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 oEmpBasicInfos;
        }

        public DataSet GetRecruitmentTrackerReport(DateTime? fromDate, DateTime? toDate, int departmentId, int sbuId, EnumInternalRecruitmentType type)
        {
            DataSet oEmpBasicInfos = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oEmpBasicInfos = InternalRecruitmentDA.GetRecruitmentTrackerReport(tc, fromDate, toDate, departmentId, sbuId, type);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oEmpBasicInfos;
        }

        public DataSet GetWeeklyMonthlyRequisitionTypeChart(DateTime fromDate, DateTime toDate)
        {
            DataSet oEmpBasicInfos = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oEmpBasicInfos = InternalRecruitmentDA.GetWeeklyMonthlyRequisitionTypeChart(tc, 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 oEmpBasicInfos;
        }

        public DataSet GetMonthlyRequisitionByLocationChart(DateTime fromDate, DateTime toDate)
        {
            DataSet oEmpBasicInfos = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oEmpBasicInfos = InternalRecruitmentDA.GetMonthlyRequisitionByLocationChart(tc, 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 oEmpBasicInfos;
        }

        public DataSet GetIssuedOfferLetterbyTypeChart(DateTime fromDate, DateTime toDate)
        {
            DataSet oEmpBasicInfos = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oEmpBasicInfos = InternalRecruitmentDA.GetIssuedOfferLetterbyTypeChart(tc, 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 oEmpBasicInfos;
        }

        public DataSet GetDesignationIssueLetterRequisitionCount(DateTime fromDate, DateTime toDate, int designationId)
        {
            DataSet oEmpBasicInfos = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                oEmpBasicInfos = InternalRecruitmentDA.GetDesignationIssueLetterRequisitionCount(tc, fromDate, toDate, designationId);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return oEmpBasicInfos;
        }

        public DataSet GetRequisitionChart(DateTime fromDate, DateTime toDate)
        {
            DataSet recTracking = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                recTracking = InternalRecruitmentDA.GetRequisitionChart(tc, fromDate, toDate);
                tc.End();
            }
            catch (Exception e)
            {
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
            }

            return recTracking;
        }

        public DataSet GetRecruitment(DateTime? startDate, DateTime? endDate, EnumOnBoradStatus? onBoardStatus, int recruitmentId, int recruiterId,int currentuserId)
        {
            DataSet recTracking = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                recTracking = InternalRecruitmentDA.GetRecruitment(tc, startDate, endDate, onBoardStatus, recruitmentId, recruiterId, currentuserId);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }

            return recTracking;
        }

        public DataSet GetRequisitionReports(DateTime fromDate, DateTime toDate)
        {
            DataSet recTracking = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                recTracking = InternalRecruitmentDA.GetRequisitionReports(tc, fromDate, toDate);
                tc.End();
            }
            catch (Exception e)
            {
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
            }

            return recTracking;
        }

        public DataSet GetRequisitionAllCountReport(DateTime fromDate, DateTime toDate)
        {
            DataSet recTracking = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                recTracking = InternalRecruitmentDA.GetRequisitionAllCountReport(tc, fromDate, toDate);
                tc.End();
            }
            catch (Exception e)
            {
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
            }

            return recTracking;
        }

        #endregion

        #endregion
    }

    

    #endregion
}