private void UpdateDb(SendMessageQueue sendMessageQueue, SEND_SMS_RESULT sendMessageResult) { // 寫入對應的 SendMessageResult var every8d_SendMessageResult = new Every8d_SendMessageResult(); every8d_SendMessageResult.SourceTable = SourceTable.SendMessageQueue; every8d_SendMessageResult.SourceTableId = sendMessageQueue.Id; every8d_SendMessageResult.SendTime = sendMessageQueue.SendTime; every8d_SendMessageResult.Subject = sendMessageQueue.SendTitle; every8d_SendMessageResult.Content = sendMessageQueue.SendBody; every8d_SendMessageResult.CreatedTime = DateTime.UtcNow; every8d_SendMessageResult.CREDIT = sendMessageResult.CREDIT; every8d_SendMessageResult.SENDED = sendMessageResult.SENDED; every8d_SendMessageResult.COST = sendMessageResult.COST; every8d_SendMessageResult.UNSEND = sendMessageResult.UNSEND; every8d_SendMessageResult.BATCH_ID = sendMessageResult.BATCH_ID; every8d_SendMessageResult = this.unitOfWork.Repository <Every8d_SendMessageResult>().Insert(every8d_SendMessageResult); // Every8d 在此階段無法取得寫入 SendMessageHistory,無對應資料結構 // CreateSendMessageHistory(sendMessageQueue.Id); // 在 Thread 中等待 30 秒,再寫入 DeliveryReportQueue var delayMilliseconds = (int)30 * 1000; FaFTaskFactory.StartNew(delayMilliseconds, () => { using (var context = new ApplicationDbContext()) { var _unitOfWork = new UnitOfWork(context); var _repository = _unitOfWork.Repository <DeliveryReportQueue>(); // 寫入簡訊派送結果等待取回序列 var deliveryReportQueue = new DeliveryReportQueue(); deliveryReportQueue.SourceTableId = sendMessageQueue.Id; deliveryReportQueue.SourceTable = SourceTable.SendMessageQueue; deliveryReportQueue.RequestId = every8d_SendMessageResult.BATCH_ID; deliveryReportQueue.ProviderName = this.Name; deliveryReportQueue.CreatedTime = DateTime.UtcNow; deliveryReportQueue.SendMessageResultItemCount = every8d_SendMessageResult.SENDED.HasValue ? every8d_SendMessageResult.SENDED.Value : 0; deliveryReportQueue.DeliveryReportCount = 0; deliveryReportQueue = _repository.Insert(deliveryReportQueue); } }); }
public void RetrySMS(int sendMessageHistoryId) { var sendMessageHistory = this.unitOfWork.Repository <SendMessageHistory>().GetById(sendMessageHistoryId); using (var smsClient = new SMSClient(userName, password)) { DateTime?sendTime = null; // NULL 表示立刻發送 string subject = sendMessageHistory.SendTitle; string message = sendMessageHistory.SendBody; var every8dMessageReceiver = new List <Every8d_MessageReceiver> { new Every8d_MessageReceiver { NAME = sendMessageHistory.DestinationName, MOBILE = sendMessageHistory.DestinationAddress, EMAIL = string.Empty, // p.Email, // 不要依賴 Every8d 寄送 Email SENDTIME = null, CONTENT = message, } }; this.logService.Debug("Every8dSmsProvider(smsProviderType = {0}),重試簡訊(簡訊發送結果歷史紀錄編號:{1},發送內容:{2},發送名單:[{3}])", smsProviderType.ToString(), sendMessageHistory.Id, message, string.Join("、", every8dMessageReceiver.Select(p => p.MOBILE))); if (systemParameters.AllowSendMessage) { SEND_SMS_RESULT sendMessageResult = smsClient.SendParamSMS(sendTime, subject, every8dMessageReceiver); this.logService.Debug("Every8dSmsProvider(smsProviderType = {0}),重試簡訊(簡訊發送結果歷史紀錄編號:{1},回傳簡訊發送識別碼:{2},回傳結果:{3})", smsProviderType.ToString(), sendMessageHistory.Id, sendMessageResult.BATCH_ID, sendMessageResult.ToString()); UpdateDb(sendMessageHistory, sendMessageResult); } } }
public void SendSMS(int sendMessageQueueId) { var sendMessageQueue = this.unitOfWork.Repository <SendMessageQueue>().GetById(sendMessageQueueId); using (var smsClient = new SMSClient(userName, password)) { DateTime?sendTime = null; // NULL 表示立刻發送 string subject = sendMessageQueue.SendTitle; string message = sendMessageQueue.SendBody; var messageReceiver = this.unitOfWork.Repository <MessageReceiver>().GetMany(p => p.SendMessageRuleId == sendMessageQueue.SendMessageRuleId && p.SendBody == message) .ToList(); var every8dMessageReceiver = messageReceiver .Select(p => new Every8d_MessageReceiver { NAME = p.Name, MOBILE = p.E164Mobile, EMAIL = string.Empty, // p.Email, // 不要依賴 Every8d 寄送 Email SENDTIME = null, CONTENT = message, }).ToList(); this.logService.Debug("Every8dSmsProvider(smsProviderType = {0}),發送簡訊(簡訊編號:{1},簡訊序列編號:{2},發送內容:{3},發送名單(共{4}筆):[{5}])", smsProviderType.ToString(), sendMessageQueue.SendMessageRuleId, sendMessageQueue.Id, message, every8dMessageReceiver.Count, string.Join("、", every8dMessageReceiver.Select(p => p.MOBILE))); if (systemParameters.AllowSendMessage) { SEND_SMS_RESULT sendMessageResult = smsClient.SendParamSMS(sendTime, subject, every8dMessageReceiver); this.logService.Debug("Every8dSmsProvider(smsProviderType = {0}),發送簡訊(簡訊編號:{1},簡訊序列編號:{2},回傳簡訊發送識別碼:{3},回傳結果:{4})", smsProviderType.ToString(), sendMessageQueue.SendMessageRuleId, sendMessageQueue.Id, sendMessageResult.BATCH_ID, sendMessageResult.ToString()); // Send Email string body = message; string[] emails = messageReceiver.Where(p => !string.IsNullOrEmpty(p.Email)).Select(p => p.Email).ToArray(); if (emails.Any()) { this.logService.Debug("Every8dSmsProvider(smsProviderType = {0}),發送Email(簡訊編號:{1},簡訊序列編號:{2},主旨:{3},內容:{4},發送名單:[{5}])", smsProviderType.ToString(), sendMessageQueue.SendMessageRuleId, sendMessageQueue.Id, subject, body, string.Join("、", emails)); BackgroundJob.Enqueue <CommonMailService>(x => x.Send(subject, body, emails)); } else { this.logService.Debug("Every8dSmsProvider(smsProviderType = {0}),無須發送Email(簡訊編號:{1},簡訊序列編號:{2})", smsProviderType.ToString(), sendMessageQueue.SendMessageRuleId, sendMessageQueue.Id); } UpdateDb(sendMessageQueue, sendMessageResult); } } }