// 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
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