예제 #1
0
        private void Run()
        {
            while (true)
            {
                try
                {
                    AppendText("Start transaction Goodi system");
                    Token token = DataBase.DBParser.Instance().GetToken();
                    if (token == null || token.Stemp_Tar < DateTime.Now || !RestApi.Instance().IsTokenValid(token.TokenNumber))
                    {
                        token = RestApi.Instance().GetNewToken();
                        if (token != null)
                        {
                            bool res = DataBase.DBParser.Instance().UpdateToken(token);
                        }
                        else
                        {
                            AppendText("Did not susseeded get token");
                            throw new Exception("Did not susseeded get token");
                        }
                    }

                    IEnumerable <ExecuteTransaction> transactions = DataBase.DBParser.Instance().GetAllTransactions();
                    List <ExecuteTransaction>        updateOk     = new List <ExecuteTransaction>();
                    List <ExecuteTransaction>        errorUpdate  = new List <ExecuteTransaction>();
                    foreach (ExecuteTransaction transaction in transactions)
                    {
                        string errorMessage = "";
                        string errorCode    = "";
                        bool   isUpdate     = false;
                        ExecuteTransactionResponse executeTransactionResponse = null;
                        bool result = RestApi.Instance().ExecuteTransaction(transaction, token.TokenNumber, out errorMessage, out errorCode, out executeTransactionResponse);
                        if (!result)
                        {
                            transaction.ERROR_MESSAGE = $"Station order  {transaction.STATION_ORDER} did not succeeded update in Goodi system";
                            AppendText(errorMessage);
                            isUpdate = DataBase.DBParser.Instance().UpdateTransaction(transaction.ID, errorMessage, errorCode);
                            errorUpdate.Add(transaction);
                        }
                        else
                        {
                            AppendText($"Successfully update Station order {transaction.STATION_ORDER}  in goodi system");
                            isUpdate = DataBase.DBParser.Instance().UpdateTransaction(transaction.ID, transactionResponse: executeTransactionResponse);
                            updateOk.Add(transaction);
                        }
                        if (isUpdate)
                        {
                            AppendText($"Successfully update Station order status {transaction.STATION_ORDER}  in data base");
                        }
                        else
                        {
                            transaction.ERROR_MESSAGE = $"Station order {transaction.STATION_ORDER} did not succeeded update order status in Data base";
                            AppendText(transaction.ERROR_MESSAGE);
                        }
                    }
                    if (updateOk.Count > 0 || errorUpdate.Count > 0)
                    {
                        AppendText($"{updateOk.Count} selers where update and {errorUpdate.Count} selers getting error");
                        //send mail
                        string title = $" ממשקי גודי דלקנים של צהל";
                        string body  = $"<div  style='direction:rtl' > שלום  <br /> מספר התדלוקים של צהל שעודכנו בגודי {updateOk.Count} <br /> מספר התדלוקים של צהל שלא עדכנו בגודי {errorUpdate.Count} </div>";
                        if (errorUpdate.Count > 0)
                        {
                            string errorSeler = "<div  style='direction:rtl' ><br />תדלוקים שלא עודכנו בגודי<br /></div>";
                            foreach (ExecuteTransaction transaction in errorUpdate)
                            {
                                errorSeler += $"<div  style='direction:rtl' ><br />{transaction.ToString()}<br /></div>";
                            }
                            body += errorSeler;
                        }
                        SendMail.Instance().Email(body, title);
                    }
                    AppendText("Finish Transaction to Goodi system");
                }
                catch (Exception ex)
                {
                    AppendText($"Error when try to update selers, error message: {ex.Message}");
                }
                if (m_stopThreadsEvent.WaitOne(_timeToWait))
                {
                    break;
                }
            }
        }