/// <summary>
        /// Process - update batch status
        /// </summary>
        /// <param name="result"></param>
        public void BatchUpdateStatus(SECOM_AJIS.Common.Util.doBatchProcessResult result)
        {
            base.UpdateBatchResult(
                result.BatchCode
                , result.BatchStatus
                , null
                , result.Total
                , result.Complete
                , result.Failed
                , result.BatchUser);

            ILogHandler handLog = ServiceContainer.GetService <ILogHandler>() as ILogHandler;

            if (result.BatchStatus == BatchStatus.C_BATCH_STATUS_SUCCEEDED)
            {
                string message = "Night batch of " + result.BatchName + " is finished";
                base.InsertTbt_BatchLog(result.BatchDate, result.BatchCode, message, FlagType.C_FLAG_OFF, result.BatchUser);
                handLog.WriteWindowLog(EventType.C_EVENT_TYPE_INFORMATION, message, EventID.C_EVENT_ID_BATCH_FINISH);
            }

            else if (result.BatchStatus == BatchStatus.C_BATCH_STATUS_FAILED)
            {
                string message = "Night batch of " + result.BatchName + " is finished with error : \n\n" + result.ErrorMessage;
                base.InsertTbt_BatchLog(result.BatchDate, result.BatchCode, message, FlagType.C_FLAG_ON, result.BatchUser);
                handLog.WriteWindowLog(EventType.C_EVENT_TYPE_ERROR, message, EventID.C_EVENT_ID_BATCH_ERROR);
            }
            else
            {
                string message = "Night batch of " + result.BatchName + " is processing";
                base.InsertTbt_BatchLog(result.BatchDate, result.BatchCode, message, FlagType.C_FLAG_OFF, result.BatchUser);
                handLog.WriteWindowLog(EventType.C_EVENT_TYPE_INFORMATION, message, EventID.C_EVENT_ID_BATCH_START);
            }
        }
        public void Execute(JobExecutionContext context)
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            List <BatchProcessRunAll_Result> list = (List <BatchProcessRunAll_Result>)dataMap.Get("BatchList");
            string           UserId    = dataMap.GetString("UserId");
            DateTime         BatchDate = dataMap.GetDateTime("BatchDate");
            BatchWriteLogDel writeLog  = (BatchWriteLogDel)dataMap.Get("WriteLog");

            try
            {
                //=== start 'run batch all' ===
                doBatchProcessResult allResult_start = new doBatchProcessResult();
                allResult_start.BatchName = "Run All Batch";
                allResult_start.BatchUser = UserId;

                allResult_start.BatchStatus  = BatchStatus.C_BATCH_STATUS_PROCESSING;
                allResult_start.BatchCode    = "AL";
                allResult_start.BatchJobName = "";
                allResult_start.BatchDate    = DateTime.Now;

                writeLog(allResult_start);


                foreach (BatchProcessRunAll_Result s in list)
                {
                    doBatchProcessResult result = CommonUtil.CloneObject <BatchProcessRunAll_Result, doBatchProcessResult>(s);

                    try
                    {
                        string[] batchJobName = s.BatchJobName.Split(',');
                        string   assemblyName = batchJobName[0];
                        string   typeName     = assemblyName + '.' + batchJobName[1];
                        result.BatchStatus = BatchStatus.C_BATCH_STATUS_PROCESSING;
                        writeLog(result);

                        Assembly assembly = Assembly.Load(assemblyName + ", Version=0.0.0.0, PublicKeyToken=null,Culture=neutral");
                        Type     type     = assembly.GetType(typeName);

                        IBatchProcess process = (IBatchProcess)Activator.CreateInstance(type);
                        result = process.WorkProcess(UserId, BatchDate);

                        // == add by Narupon ==

                        result.BatchCode    = s.BatchCode;
                        result.BatchName    = s.BatchName;
                        result.BatchJobName = s.BatchJobName;
                        result.BatchDate    = DateTime.Now;


                        if (result.Result)
                        {
                            result.BatchStatus = BatchStatus.C_BATCH_STATUS_SUCCEEDED;
                        }
                        else
                        {
                            result.BatchStatus = BatchStatus.C_BATCH_STATUS_FAILED;
                        }


                        // == (end) add by Narupon ==

                        result.BatchUser = UserId;
                        writeLog(result);
                    }
                    catch (Exception ex)
                    {
                        result.ErrorMessage += string.Format("Error: {0} {1}\n", ex.Message, ex.InnerException != null ? ex.InnerException.Message : "");
                        result.BatchStatus   = BatchStatus.C_BATCH_STATUS_FAILED;
                        writeLog(result);
                    }
                }

                //=== finish 'run batch all' ===
                doBatchProcessResult allResult_finish = new doBatchProcessResult();
                allResult_finish.BatchName = "Run All Batch";
                allResult_finish.BatchUser = UserId;

                allResult_finish.BatchStatus  = BatchStatus.C_BATCH_STATUS_SUCCEEDED;
                allResult_finish.BatchCode    = "AL";
                allResult_finish.BatchJobName = "";
                allResult_finish.BatchDate    = DateTime.Now;

                writeLog(allResult_finish);
            }
            catch (Exception ex)
            {
                doBatchProcessResult errorResult = new doBatchProcessResult();
                errorResult.BatchCode    = null;
                errorResult.ErrorMessage = ex.Message;
                errorResult.BatchUser    = UserId;
                errorResult.BatchStatus  = BatchStatus.C_BATCH_STATUS_FAILED;
                writeLog(errorResult);
                throw ex;
            }
            finally
            {
                context.Scheduler.Shutdown();
            }
        }
        public void Execute(JobExecutionContext context)
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;

            doBatchProcessResult      result         = new doBatchProcessResult();
            BatchCallBackDel          callback       = (BatchCallBackDel)dataMap.Get("Callback");
            BatchProcessRunAll_Result doBatchProcess = (BatchProcessRunAll_Result)dataMap.Get("doBatchProcess");

            try
            {
                if (callback != null)
                {
                    result.BatchName   = doBatchProcess.BatchName;
                    result.BatchCode   = doBatchProcess.BatchCode;
                    result.BatchStatus = BatchStatus.C_BATCH_STATUS_PROCESSING;
                    result.BatchUser   = doBatchProcess.BatchUser;
                    result.BatchDate   = doBatchProcess.BatchDate;
                    callback(result);
                }

                string assemblyName = dataMap.GetString("assemblyName");
                string typeName     = dataMap.GetString("typeName");

                Assembly      assembly = Assembly.Load(assemblyName + ", Version=0.0.0.0, PublicKeyToken=null,Culture=neutral");
                Type          type     = assembly.GetType(typeName);
                IBatchProcess process  = (IBatchProcess)Activator.CreateInstance(type);
                result = process.WorkProcess(doBatchProcess.BatchUser, (DateTime)doBatchProcess.BatchDate);

                // Added by Non A. 30/Mar/2012 : Set BatchStatus only if it's null.
                if (string.IsNullOrWhiteSpace(result.BatchStatus))
                {
                    if (result.Result == true)
                    {
                        result.BatchStatus = BatchStatus.C_BATCH_STATUS_SUCCEEDED;
                    }
                    else
                    {
                        result.BatchStatus = BatchStatus.C_BATCH_STATUS_FAILED;
                    }
                }
            }
            catch (Exception ex)
            {
                result.ErrorMessage = string.Format("{0} {1}", ex.Message, ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                result.BatchStatus  = BatchStatus.C_BATCH_STATUS_FAILED;

                throw ex;
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(result.BatchUser))
                {
                    result.BatchUser = doBatchProcess.BatchUser;
                }
                result.BatchJobName = doBatchProcess.BatchCode;
                result.BatchName    = doBatchProcess.BatchName;
                result.BatchCode    = doBatchProcess.BatchCode;
                result.BatchDate    = doBatchProcess.BatchDate;
                if (callback != null)
                {
                    callback(result);
                }

                bool keepScheduler = dataMap.GetBoolean("KeepScheduler");
                if (!keepScheduler)
                {
                    context.Scheduler.Shutdown();
                }
            }
        }
        /// <summary>
        /// To send notify email for change contract fee
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="BatchDate"></param>
        /// <returns></returns>
        public SECOM_AJIS.Common.Util.doBatchProcessResult SendNotifyEmailForChangeFee(string UserId, DateTime BatchDate)
        {
            SECOM_AJIS.Common.Util.doBatchProcessResult doResult = new SECOM_AJIS.Common.Util.doBatchProcessResult();
            try
            {
                ILogHandler handLog = ServiceContainer.GetService <ILogHandler>() as ILogHandler;

                //1. Get list of unsent email
                List <tbt_ContractEmail> dtTbt_ContractEmail = this.GetUnsentNotifyEmail();

                //2. For each unsent email in dtTbt_ContractEmail
                int failedItem    = 0;
                int completdeItem = 0;
                foreach (tbt_ContractEmail email in dtTbt_ContractEmail)
                {
                    //2.1 Prepare destination email
                    //2.1.1 Get employee data of destination email
                    IEmployeeMasterHandler handMaster = ServiceContainer.GetService <IEmployeeMasterHandler>() as IEmployeeMasterHandler;
                    List <tbm_Employee>    dtEmployee = handMaster.GetTbm_Employee(email.ToEmpNo);

                    //2.1.2 If dtEmployee is empty
                    if (dtEmployee.Count <= 0)
                    {
                        handLog.WriteWindowLog(EventType.C_EVENT_TYPE_ERROR, (MessageUtil.GetMessage(MessageUtil.MODULE_CONTRACT, MessageUtil.MessageList.MSG3077, email.ToEmpNo)).Message, EventID.C_EVENT_ID_NOTIFY_EMAIL_ERROR);
                        failedItem += 1;
                        continue;
                    }

                    //2.1.3 Get default email if endDate is not empty
                    String strEmailTo = (dtEmployee[0].EndDate != null) ?
                                        this.getEmailsOfDefaultDepartment() :
                                        dtEmployee[0].EmailAddress;

                    //2.2 Prepare email object for sending
                    doEmailProcess emailProc = new doEmailProcess();
                    emailProc.MailTo   = strEmailTo;
                    emailProc.MailFrom = email.EmailFrom;
                    emailProc.Subject  = email.EmailSubject;
                    emailProc.Message  = email.EmailContent;

                    //2.3 Send email
                    //2.3.1 Retry for 3 times while strStatus is still fails
                    int retry = 0;
                    do
                    {
                        try
                        {
                            ICommonHandler commonHand = ServiceContainer.GetService <ICommonHandler>() as ICommonHandler;
                            commonHand.SendMail(emailProc);
                            break;
                        }
                        catch (Exception)
                        {
                            retry += 1;
                        }
                    } while (retry < 3);

                    ICommonContractHandler comContractHand = ServiceContainer.GetService <ICommonContractHandler>() as ICommonContractHandler;
                    if (retry == 3)
                    {
                        failedItem += 1;
                        email.FailSendingCounter = (email.FailSendingCounter == null) ? 1 : email.FailSendingCounter + 1;
                        email.UpdateBy           = UserId;
                        //Update to database
                        comContractHand.UpdateTbt_ContractEmail(email);

                        //Check the number of fail for reporting error
                        if (email.FailSendingCounter >= 6 && email.FailSendingCounter % 3 == 0)
                        {
                            handLog.WriteWindowLog(EventType.C_EVENT_TYPE_ERROR, (MessageUtil.GetMessage(MessageUtil.MODULE_CONTRACT, MessageUtil.MessageList.MSG3078, strEmailTo)).Message, EventID.C_EVENT_ID_NOTIFY_EMAIL_ERROR);
                        }
                    }
                    else
                    {
                        completdeItem += 1;

                        //Delete sent email
                        comContractHand.DeleteTbt_ContractEmail(email.ContractEmailID, UserId);
                    }
                }

                //3. Prepare process result for returning
                doResult.Result   = FlagType.C_FLAG_ON;
                doResult.Total    = dtTbt_ContractEmail.Count;
                doResult.Complete = completdeItem;
                doResult.Failed   = failedItem;
            }
            catch (Exception)
            {
                throw;
            }
            return(doResult);
        }
        /// <summary>
        /// Process - update batch result
        /// </summary>
        /// <param name="result"></param>
        public void BatchUpdateResult(SECOM_AJIS.Common.Util.doBatchProcessResult result)
        {
            bool flag;

            flag = (result.BatchStatus == BatchStatus.C_BATCH_STATUS_FAILED);
            if (!string.IsNullOrEmpty(result.BatchName))
            {
                ILogHandler handLog = ServiceContainer.GetService <ILogHandler>() as ILogHandler;
                if (result.BatchStatus == BatchStatus.C_BATCH_STATUS_FAILED)
                {
                    handLog.WriteWindowLog(EventType.C_EVENT_TYPE_INFORMATION, LogMessage.C_LOG_NIGHT_BATCH_ERROR, EventID.C_EVENT_ID_BATCH_ERROR);
                }
                else
                {
                    handLog.WriteWindowLog(EventType.C_EVENT_TYPE_INFORMATION, LogMessage.C_LOG_NIGHT_BATCH_FINISH, EventID.C_EVENT_ID_BATCH_FINISH);
                }
            }

            if (result.BatchCode != null)
            {
                base.UpdateBatchResult(
                    result.BatchCode
                    , result.BatchStatus
                    , null
                    , result.Total
                    , result.Complete
                    , result.Failed
                    , result.BatchUser);

                //base.InsertTbt_BatchLog(result.BatchDate, result.BatchCode, result.BatchName, flag, result.BatchUser);

                // == add by Narupon ==

                ILogHandler handLog = ServiceContainer.GetService <ILogHandler>() as ILogHandler;
                if (result.BatchStatus == BatchStatus.C_BATCH_STATUS_SUCCEEDED)
                {
                    string message = "Night batch of " + result.BatchName + " is finished";
                    base.InsertTbt_BatchLog(result.BatchDate, result.BatchCode, message, FlagType.C_FLAG_OFF, result.BatchUser);
                    handLog.WriteWindowLog(EventType.C_EVENT_TYPE_INFORMATION, message, EventID.C_EVENT_ID_BATCH_FINISH);
                }

                else if (result.BatchStatus == BatchStatus.C_BATCH_STATUS_FAILED)
                {
                    string message = "Night batch of " + result.BatchName + " is finished with error : \n\n" + result.ErrorMessage;
                    base.InsertTbt_BatchLog(result.BatchDate, result.BatchCode, message, FlagType.C_FLAG_ON, result.BatchUser);
                    handLog.WriteWindowLog(EventType.C_EVENT_TYPE_ERROR, message, EventID.C_EVENT_ID_BATCH_ERROR);
                }
                else
                {
                    string message = "Night batch of " + result.BatchName + " is processing";
                    base.InsertTbt_BatchLog(result.BatchDate, result.BatchCode, message, FlagType.C_FLAG_OFF, result.BatchUser);
                    handLog.WriteWindowLog(EventType.C_EVENT_TYPE_INFORMATION, message, EventID.C_EVENT_ID_BATCH_START);
                }

                // == (end) add by Narupon ==
            }
            else
            {
                base.UpdateBatchResult(
                    null
                    , BatchStatus.C_BATCH_STATUS_FAILED
                    , null
                    , -1
                    , -1
                    , -1
                    , result.BatchUser);
            }
        }