Ejemplo n.º 1
0
        // Get the orders asynchronously.
        //  Returns number of orders updated.
        private int GetOrdersAsync(DateTime startDate, DateTime endDate, out bool canceled)
        {
            canceled = false;
            int ordersFetched = 0;

            List <AccountType> allAccounts = AccountUtil.GetAllAccounts();

            foreach (AccountType account in allAccounts)
            {
                StringCollection orderIds    = EbayTransactionBiz.GetAllOrderIds(account, startDate, endDate);
                StringCollection newOrderIds = new StringCollection();
                foreach (String orderId in orderIds)
                {
                    newOrderIds.Add(orderId);
                }

                int orderNumRetrievePerTime = 1;

                int  apiCallTimes  = newOrderIds.Count / orderNumRetrievePerTime;
                bool needExtraCall = (newOrderIds.Count % orderNumRetrievePerTime) != 0;
                if (needExtraCall)
                {
                    apiCallTimes += 1;
                }

                List <EbayTransactionType> allTrans = new List <EbayTransactionType>();

                for (int ii = 0; ii < apiCallTimes; ++ii)
                {
                    StringCollection orderIdsLoc = new StringCollection();
                    for (int jj = ii * orderNumRetrievePerTime; jj < ii * orderNumRetrievePerTime + orderNumRetrievePerTime; ++jj)
                    {
                        if (jj > newOrderIds.Count - 1)
                        {
                            break;
                        }
                        orderIdsLoc.Add(newOrderIds[jj]);
                    }

                    TimeFilter timeFilter = new TimeFilter();
                    timeFilter.TimeFrom = startDate;
                    timeFilter.TimeTo   = endDate;

                    List <EbayTransactionType> transList = EbayTransactionBiz.GetAllOrders(account, timeFilter, orderIdsLoc);
                    foreach (EbayTransactionType trans in transList)
                    {
                        allTrans.Add(trans);
                    }

                    // Update the description and progress on the modal form
                    // using Control.Invoke.  Invoke will run the anonymous
                    // function to set the label's text on the UI thread.
                    // Since it's illegal to touch the UI control on the worker
                    // thread that we're on right now.

                    int retrievedCount = (ii + 1) * orderNumRetrievePerTime;
                    if (retrievedCount > newOrderIds.Count)
                    {
                        retrievedCount = newOrderIds.Count;
                    }

                    String labelHintStr = string.Format("正在从Ebay获取交易信息, 账号 {0}, 共{1}个订单, 已获得{2}个订单信息",
                                                        account.ebayAccount,
                                                        newOrderIds.Count,
                                                        retrievedCount);
                    int progressBarValue = (int)((double)(ii + 1) / apiCallTimes * 100);
                    frmProgress.SetLabelHintAndProgressBarValue(labelHintStr, progressBarValue);

                    foreach (EbayTransactionType trans in allTrans)
                    {
                        bool result = EbayTransactionDAL.InsertOrUpdateOneTransaction(trans);
                        if (result == false)
                        {
                            canceled = true;
                            return(ordersFetched);
                        }
                    }

                    // Periodically check for a Cancellation
                    // If the user clicks the cancel button, or tries to close
                    // the progress form the m_fmProgress.Cancel flag will be set to true.
                    if (frmProgress.Cancel)
                    {
                        canceled = true;
                        return(ordersFetched);
                    }
                }   // for (int ii = 0; ii < apiCallTimes; ++ii)

                ordersFetched += newOrderIds.Count;
            }   // foreach (AccountType account in AllAccounts)

            canceled = false;
            return(ordersFetched);
        }   // GetOrdersAsync