Пример #1
0
        }   // GetOrdersAsync

        // Get messages from ebay async.
        //  Returns number of new messages retrieved.
        private int GetMessagesAsync(DateTime startDate, DateTime endDate, out bool canceled)
        {
            canceled = false;
            int messagesFetched = 0;

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

            foreach (AccountType account in allAccounts)
            {
                StringCollection messageIds    = EbayMessageBiz.GetAllMessageIds(account, startDate, endDate);
                StringCollection newMessageIds = new StringCollection();
                foreach (String messageId in messageIds)
                {
                    EbayMessageType messageType = EbayMessageDAL.GetOneMessage(messageId);
                    if (messageType == null)
                    {
                        newMessageIds.Add(messageId);
                    }
                }

                int messageNumRetrievePerTime = 1;

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

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

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

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

                    // 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) * messageNumRetrievePerTime;
                    if (retrievedCount > newMessageIds.Count)
                    {
                        retrievedCount = newMessageIds.Count;
                    }

                    List <EbayMessageType> messageList = EbayMessageBiz.GetAllMessageByIds(account, messageIdsLoc);

                    foreach (EbayMessageType messageType in messageList)
                    {
                        // We are pretty sure the message didn't exist.
                        EbayMessageDAL.InsertOneMessage(messageType);

                        // Update the transaction message status.
                        String recipientUserId = messageType.RecipientUserId;
                        String sender          = messageType.Sender;
                        String sellerName      = messageType.SellerName;
                        String buyerId         = sellerName == sender ? recipientUserId : sender;

                        TransactionMessageStatus messageStatus = EbayMessageDAL.GetTransactionMessageStatus(buyerId, sellerName, messageType.ItemID);

                        List <EbayTransactionType> transList = EbayTransactionDAL.GetTransactionsBySellerBuyerItem(sellerName, buyerId, messageType.ItemID);
                        foreach (EbayTransactionType trans in transList)
                        {
                            EbayTransactionDAL.UpdateTransactionMessageStatus(trans.TransactionId, messageStatus);
                        }
                    }

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

                    // 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(messagesFetched);
                    }
                }   // for (int ii = 0; ii < apiCallTimes; ++ii)

                messagesFetched += newMessageIds.Count;
            }   // foreach (AccountType account in AllAccounts)

            canceled = false;
            return(messagesFetched);
        }   // GetMessagesAsync
Пример #2
0
        private void SendMessageAsync(object sender, DoWorkEventArgs e)
        {
            object[]  paramArr      = e.Argument as object[];
            DataTable dtOrders      = (DataTable)paramArr[0];
            String    messageToSend = (String)paramArr[1];

            int curIdx = 0;

            foreach (DataRow row in dtOrders.Rows)
            {
                curIdx++;

                String sellerName      = StringUtil.GetSafeString(row["SellerName"]);
                String buyerId         = StringUtil.GetSafeString(row["BuyerId"]);
                String itemId          = StringUtil.GetSafeString(row["ItemId"]);
                String orderLineItemId = StringUtil.GetSafeString(row["OrderLineItemId"]);

                EbayTransactionType transLoc = EbayTransactionDAL.GetOneTransaction(orderLineItemId);
                if (transLoc == null)
                {
                    continue;
                }

                String subject = String.Format("{0} sent a message on item {1} with itemId {2}",
                                               sellerName,
                                               buyerId,
                                               itemId);

                AccountType account = AccountUtil.GetAccount(sellerName);
                if (account == null)
                {
                    continue;
                }

                String labelHintStr = String.Format("正在向买家 {0} 发送消息.... 进度 {1} / {2}",
                                                    buyerId, curIdx, dtOrders.Rows.Count);
                int progressBarValue = (int)((double)(curIdx) / dtOrders.Rows.Count * 100);
                frmProgress.SetLabelHintAndProgressBarValue(labelHintStr, progressBarValue);

                if (frmProgress.Cancel)
                {
                    e.Cancel = true;
                    e.Result = curIdx;
                    return;
                }

                messageToSend = replaceMessageMacros(messageToSend, transLoc);

                bool result = EbayMessageBiz.SendMessageToBuyer(account,
                                                                buyerId,
                                                                itemId,
                                                                subject,
                                                                messageToSend,
                                                                true /*emailCopyToSender*/,
                                                                eBay.Service.Core.Soap.QuestionTypeCodeType.General);

                if (result)
                {
                    DateTime startTime = DateTime.Now;
                    DateTime endTime   = startTime;
                    startTime = startTime.Subtract(new TimeSpan(0, 5, 0));

                    startTime = startTime.ToUniversalTime();
                    endTime   = endTime.ToUniversalTime();

                    // Get all message ids within this five minutes.
                    StringCollection msgIds = EbayMessageBiz.GetAllMessageIds(account, startTime, endTime);

                    // Skip the messages we have retrieved.
                    StringCollection newMsgIds = new StringCollection();
                    {
                        foreach (String msgId in msgIds)
                        {
                            EbayMessageType existedMessage = EbayMessageDAL.GetOneMessage(msgId);
                            if (existedMessage != null)
                            {
                                continue;
                            }

                            newMsgIds.Add(msgId);
                        }
                    }

                    List <EbayMessageType> newMessages = EbayMessageBiz.GetAllMessageByIds(account, newMsgIds);

                    Logger.WriteSystemLog(String.Format("Retrieved new messages count={0}", newMsgIds.Count));

                    foreach (EbayMessageType messageType in newMessages)
                    {
                        // We are pretty sure the message didn't exist.
                        EbayMessageDAL.InsertOneMessage(messageType);

                        // Update the transaction message status.
                        String recipientUserId = messageType.RecipientUserId;
                        String senderName      = messageType.Sender;
                        String sellerNameLoc   = messageType.SellerName;
                        String buyerIdLoc      = sellerNameLoc == senderName ? recipientUserId : senderName;

                        // [ZHI_TODO]
                        TransactionMessageStatus messageStatus = EbayMessageDAL.GetTransactionMessageStatus(buyerIdLoc, sellerNameLoc, messageType.ItemID);

                        List <EbayTransactionType> transList = EbayTransactionDAL.GetTransactionsBySellerBuyerItem(sellerNameLoc, buyerIdLoc, messageType.ItemID);
                        foreach (EbayTransactionType trans in transList)
                        {
                            EbayTransactionDAL.UpdateTransactionMessageStatus(trans.TransactionId, messageStatus);
                        }
                    }
                }
                else
                {
                    Logger.WriteSystemUserLog("发送消息失败!");
                }
            }
            e.Result = OrdersDataTable.Rows.Count;
        } // SendMessageAsync