/// <summary>
        /// 在 GetDeliveryReport 之後,更新大表 SendMessageHistory
        /// </summary>
        public void UpdateSendMessageHistory(int sendMessageQueueId)
        {
            var sendMessageRuleRepository    = this.unitOfWork.Repository <SendMessageRule>();
            var sendMessageQueueRepository   = this.unitOfWork.Repository <SendMessageQueue>();
            var sendMessageHistoryRepository = this.unitOfWork.Repository <SendMessageHistory>();

            SendMessageQueue sendMessageQueue = sendMessageQueueRepository.GetById(sendMessageQueueId);

            SendMessageRule sendMessageRule = sendMessageRuleRepository.GetById(sendMessageQueue.SendMessageRuleId);

            Infobip_SendMessageResult sendMessageResult = this.unitOfWork.Repository <Infobip_SendMessageResult>().Get(p => p.SourceTable == SourceTable.SendMessageQueue && p.SourceTableId == sendMessageQueueId);

            if (sendMessageResult == null)
            {
                throw new Exception(string.Format("InfobipSmsProvider(smsProviderType = {0}),無法取得 Infobip_SendMessageResult(SendMessageQueueId:{1})", smsProviderType.ToString(), sendMessageQueueId));
            }

            int?DepartmentId = null;

            if (sendMessageRule.CreatedUser.Department != null)
            {
                DepartmentId = sendMessageRule.CreatedUser.Department.Id;
            }
            string          CreatedUserId     = sendMessageRule.CreatedUser.Id;
            int             SendMessageRuleId = sendMessageQueue.SendMessageRuleId;
            SendMessageType SendMessageType   = sendMessageQueue.SendMessageType;

            DateTime     SendTime     = sendMessageQueue.SendTime;
            string       SendTitle    = sendMessageQueue.SendTitle;
            string       SendBody     = sendMessageQueue.SendBody;
            SendCustType SendCustType = sendMessageQueue.SendCustType;
            string       RequestId    = sendMessageResult.ClientCorrelator;

            string ProviderName = this.Name;

            DateTime SendMessageResultCreatedTime = sendMessageResult.CreatedTime;

            List <Infobip_SendMessageResultItem> SendMessageResults = sendMessageResult.SendMessageResults.ToList();

            foreach (var SendMessageResult in SendMessageResults)
            {
                Infobip_DeliveryReport DeliveryReport = SendMessageResult.DeliveryReport;
                if (DeliveryReport == null)
                {
                    continue;                         // 如果尚未取得派送報表,就忽略
                }
                SendMessageHistory entity = this.unitOfWork.Repository <SendMessageHistory>().Get(p => p.MessageId == SendMessageResult.MessageId);
                if (entity == null)
                {
                    continue;                 // 如果找不到對應 MessageId,就忽略
                }
                string DestinationName = SendMessageResult.DestinationName;

                ////////////////////////////////////////
                // 01 ~ 05

                entity.DepartmentId       = DepartmentId;
                entity.CreatedUserId      = CreatedUserId;
                entity.SendMessageRuleId  = SendMessageRuleId;
                entity.SendMessageQueueId = sendMessageQueueId;
                entity.SendMessageType    = SendMessageType;

                ////////////////////////////////////////
                // 06 ~ 10

                entity.SendTime     = SendTime;
                entity.SendTitle    = SendTitle;
                entity.SendBody     = SendBody;
                entity.SendCustType = SendCustType;
                entity.RequestId    = RequestId;

                ////////////////////////////////////////
                // 11 ~ 15

                entity.ProviderName        = ProviderName;
                entity.MessageId           = SendMessageResult.MessageId;
                entity.MessageStatus       = (EFunTech.Sms.Schema.MessageStatus)((int)SendMessageResult.MessageStatus);
                entity.MessageStatusString = entity.MessageStatus.ToString();
                entity.SenderAddress       = SendMessageResult.SenderAddress;

                ////////////////////////////////////////
                // 16 ~ 20

                // 20151106 Norman, Infobip 給的手機門號是 E164格式,但是沒有加上 "+",使用 【MobileUtil.GetE164PhoneNumber】會導致誤判
                var destinationAddress = SendMessageResult.DestinationAddress;
                if (!destinationAddress.StartsWith("+", StringComparison.OrdinalIgnoreCase))
                {
                    destinationAddress = "+" + destinationAddress;
                }

                entity.DestinationAddress           = MobileUtil.GetE164PhoneNumber(destinationAddress);
                entity.SendMessageResultCreatedTime = SendMessageResultCreatedTime;
                entity.SentDate       = DeliveryReport.SentDate;
                entity.DoneDate       = DeliveryReport.DoneDate;
                entity.DeliveryStatus = DeliveryReport.Status;

                ////////////////////////////////////////
                // 21 ~ 25

                entity.DeliveryStatusString = entity.DeliveryStatus.ToString();
                entity.Price = DeliveryReport.Price ?? (decimal)0.0;
                entity.DeliveryReportCreatedTime = DeliveryReport.CreatedTime;
                entity.MessageCost = (new MessageCostInfo(entity.SendBody, entity.DestinationAddress).MessageCost);
                entity.Delivered   = IsDelivered(entity.DeliveryStatus);

                ////////////////////////////////////////
                // 26
                entity.DestinationName = DestinationName;

                entity.Region = MobileUtil.GetRegionName(entity.DestinationAddress);

                sendMessageHistoryRepository.Update(entity);

                // 如果發送失敗,就回補點數
                // 20151123 Norman, Eric 要求發送失敗不回補點數
                //this.tradeService.HandleSendMessageHistory(sendMessageRule, sendMessageQueue, entity);
            }

            // (6) 出統計表
            this.sendMessageStatisticService.AddOrUpdateSendMessageStatistic(sendMessageQueueId);
        }
Example #2
0
        /// <summary>
        /// 在 GetDeliveryReport 之後,更新大表 SendMessageHistory
        /// </summary>
        public void UpdateSendMessageHistory(int sendMessageQueueId)
        {
            var sendMessageRuleRepository    = this.unitOfWork.Repository <SendMessageRule>();
            var sendMessageQueueRepository   = this.unitOfWork.Repository <SendMessageQueue>();
            var sendMessageHistoryRepository = this.unitOfWork.Repository <SendMessageHistory>();

            SendMessageQueue sendMessageQueue = sendMessageQueueRepository.GetById(sendMessageQueueId);

            SendMessageRule sendMessageRule = sendMessageRuleRepository.GetById(sendMessageQueue.SendMessageRuleId);

            var clientTimezoneOffset = sendMessageRule.ClientTimezoneOffset;

            var messageReceivers = this.unitOfWork.Repository <MessageReceiver>().GetMany(p =>
                                                                                          p.SendMessageRuleId == sendMessageQueue.SendMessageRuleId &&
                                                                                          p.SendBody == sendMessageQueue.SendBody).ToList();

            Every8d_SendMessageResult sendMessageResult = this.unitOfWork.Repository <Every8d_SendMessageResult>().Get(p => p.SourceTable == SourceTable.SendMessageQueue && p.SourceTableId == sendMessageQueueId);
            // if (SendMessageResult == null) return; 不應該為 null

            int?DepartmentId = null;

            if (sendMessageRule.CreatedUser.Department != null)
            {
                DepartmentId = sendMessageRule.CreatedUser.Department.Id;
            }
            string          CreatedUserId     = sendMessageRule.CreatedUser.Id;
            int             SendMessageRuleId = sendMessageQueue.SendMessageRuleId;
            SendMessageType SendMessageType   = sendMessageQueue.SendMessageType;

            DateTime     SendTime     = sendMessageQueue.SendTime;
            string       SendTitle    = sendMessageQueue.SendTitle;
            string       SendBody     = sendMessageQueue.SendBody;
            SendCustType SendCustType = sendMessageQueue.SendCustType;
            string       RequestId    = sendMessageResult.BATCH_ID;

            string ProviderName = this.Name;

            DateTime SendMessageResultCreatedTime = sendMessageResult.CreatedTime;

            List <Every8d_DeliveryReport> DeliveryReports = sendMessageResult.DeliveryReports.ToList();

            //foreach (var DeliveryReport in DeliveryReports)
            for (var i = 0; i < DeliveryReports.Count; i++)
            {
                var DeliveryReport  = DeliveryReports[i];
                var messageReceiver = (0 <= i && i < messageReceivers.Count) ? messageReceivers[i] : null;

                string DestinationName = DeliveryReport.NAME;

                var entity = new SendMessageHistory();

                ////////////////////////////////////////
                // 01 ~ 05

                entity.DepartmentId       = DepartmentId;
                entity.CreatedUserId      = CreatedUserId;
                entity.SendMessageRuleId  = SendMessageRuleId;
                entity.SendMessageQueueId = sendMessageQueueId;
                entity.SendMessageType    = SendMessageType;

                ////////////////////////////////////////
                // 06 ~ 10

                entity.SendTime     = SendTime;
                entity.SendTitle    = SendTitle;
                entity.SendBody     = SendBody;
                entity.SendCustType = SendCustType;
                entity.RequestId    = RequestId;

                ////////////////////////////////////////
                // 11 ~ 15

                entity.ProviderName        = ProviderName;
                entity.MessageId           = null;
                entity.MessageStatus       = MessageStatus.Unknown;
                entity.MessageStatusString = entity.MessageStatus.ToString();
                entity.SenderAddress       = sendMessageRule.SenderAddress;

                ////////////////////////////////////////
                // 16 ~ 20

                // 20151106 Norman, Infobip 給的手機門號是 E164格式,但是沒有加上 "+",使用 【MobileUtil.GetE164PhoneNumber】會導致誤判
                var destinationAddress = DeliveryReport.MOBILE;
                if (!destinationAddress.StartsWith("+", StringComparison.OrdinalIgnoreCase))
                {
                    destinationAddress = "+" + destinationAddress;
                }

                entity.DestinationAddress           = MobileUtil.GetE164PhoneNumber(destinationAddress);
                entity.SendMessageResultCreatedTime = SendMessageResultCreatedTime;

                // TODO: 驗證 Every8d 回傳發送時間轉成 UTC 時間是否正確
                //entity.SentDate = Converter.ToDateTime(DeliveryReport.SENT_TIME, Converter.Every8d_SentTime).Value; // 2010/03/23 12:05:29
                //entity.DoneDate = Converter.ToDateTime(DeliveryReport.SENT_TIME, Converter.Every8d_SentTime).Value; // 2010/03/23 12:05:29,簡訊供應商沒有提供此資訊,因此設定與SentDate一致
                entity.SentDate = Converter.ToDateTime(DeliveryReport.SENT_TIME, Converter.Every8d_SentTime).Value.ToUniversalTime(); // 2010/03/23 12:05:29
                entity.DoneDate = Converter.ToDateTime(DeliveryReport.SENT_TIME, Converter.Every8d_SentTime).Value.ToUniversalTime(); // 2010/03/23 12:05:29,簡訊供應商沒有提供此資訊,因此設定與SentDate一致

                entity.DeliveryStatus = (DeliveryReportStatus)Convert.ToInt32(DeliveryReport.STATUS);

                ////////////////////////////////////////
                // 21 ~ 25

                entity.DeliveryStatusString = entity.DeliveryStatus.ToString();
                entity.Price = Convert.ToDecimal(DeliveryReport.COST);
                entity.DeliveryReportCreatedTime = DeliveryReport.CreatedTime;
                entity.MessageCost = (new MessageCostInfo(entity.SendBody, entity.DestinationAddress).MessageCost);
                entity.Delivered   = IsDelivered(entity.DeliveryStatus);

                ////////////////////////////////////////
                // 26
                entity.DestinationName           = DestinationName;
                entity.Region                    = MobileUtil.GetRegionName(entity.DestinationAddress);
                entity.CreatedTime               = DateTime.UtcNow;
                entity.RetryMaxTimes             = systemParameters.RetryMaxTimes;
                entity.RetryTotalTimes           = 0;
                entity.SendMessageRetryHistoryId = null;
                entity.Email = messageReceiver != null ? messageReceiver.Email : string.Empty;

                entity = sendMessageHistoryRepository.Insert(entity);

                // 如果發送失敗,就回補點數
                // 20151123 Norman, Eric 要求發送失敗不回補點數
                //this.tradeService.HandleSendMessageHistory(sendMessageRule, sendMessageQueue, entity);
            }

            // (6) 出統計表
            this.sendMessageStatisticService.AddOrUpdateSendMessageStatistic(sendMessageQueueId);
        }
        /// <summary>
        /// 在 SendSMS 之後,建立大表 SendMessageHistory
        /// </summary>
        public void CreateSendMessageHistory(SendMessageQueue sendMessageQueue)
        {
            int sendMessageQueueId = sendMessageQueue.Id;

            var sendMessageRuleRepository    = this.unitOfWork.Repository <SendMessageRule>();
            var sendMessageQueueRepository   = this.unitOfWork.Repository <SendMessageQueue>();
            var sendMessageHistoryRepository = this.unitOfWork.Repository <SendMessageHistory>();

            SendMessageRule sendMessageRule = sendMessageRuleRepository.GetById(sendMessageQueue.SendMessageRuleId);

            Infobip_SendMessageResult sendMessageResult = this.unitOfWork.Repository <Infobip_SendMessageResult>().Get(p => p.SourceTable == SourceTable.SendMessageQueue && p.SourceTableId == sendMessageQueueId);

            if (sendMessageResult == null)
            {
                throw new Exception(string.Format("InfobipSmsProvider(smsProviderType = {0}),無法取得 Infobip_SendMessageResult(SendMessageQueueId:{1})", smsProviderType.ToString(), sendMessageQueueId));
            }

            int?DepartmentId = null;

            if (sendMessageRule.CreatedUser.Department != null)
            {
                DepartmentId = sendMessageRule.CreatedUser.Department.Id;
            }
            string          CreatedUserId     = sendMessageRule.CreatedUser.Id;
            int             SendMessageRuleId = sendMessageQueue.SendMessageRuleId;
            SendMessageType SendMessageType   = sendMessageQueue.SendMessageType;

            DateTime     SendTime     = sendMessageQueue.SendTime;
            string       SendTitle    = sendMessageQueue.SendTitle;
            string       SendBody     = sendMessageQueue.SendBody;
            SendCustType SendCustType = sendMessageQueue.SendCustType;
            string       RequestId    = sendMessageResult.ClientCorrelator;

            string ProviderName = this.Name;

            DateTime SendMessageResultCreatedTime = sendMessageResult.CreatedTime;

            List <Infobip_SendMessageResultItem> SendMessageResults = sendMessageResult.SendMessageResults.ToList();

            foreach (var SendMessageResult in SendMessageResults)
            {
                string DestinationName = SendMessageResult.DestinationName;

                var entity = new SendMessageHistory();

                ////////////////////////////////////////
                // 01 ~ 05

                entity.DepartmentId       = DepartmentId;
                entity.CreatedUserId      = CreatedUserId;
                entity.SendMessageRuleId  = SendMessageRuleId;
                entity.SendMessageQueueId = sendMessageQueueId;
                entity.SendMessageType    = SendMessageType;

                ////////////////////////////////////////
                // 06 ~ 10

                entity.SendTime     = SendTime;
                entity.SendTitle    = SendTitle;
                entity.SendBody     = SendBody;
                entity.SendCustType = SendCustType;
                entity.RequestId    = RequestId;

                ////////////////////////////////////////
                // 11 ~ 15

                entity.ProviderName        = ProviderName;
                entity.MessageId           = SendMessageResult.MessageId;
                entity.MessageStatus       = (EFunTech.Sms.Schema.MessageStatus)((int)SendMessageResult.MessageStatus);
                entity.MessageStatusString = entity.MessageStatus.ToString();
                entity.SenderAddress       = SendMessageResult.SenderAddress;

                ////////////////////////////////////////
                // 16 ~ 20

                // 20151106 Norman, Infobip 給的手機門號是 E164格式,但是沒有加上 "+",使用 【MobileUtil.GetE164PhoneNumber】會導致誤判
                var destinationAddress = SendMessageResult.DestinationAddress;
                if (!destinationAddress.StartsWith("+", StringComparison.OrdinalIgnoreCase))
                {
                    destinationAddress = "+" + destinationAddress;
                }

                entity.DestinationAddress           = MobileUtil.GetE164PhoneNumber(destinationAddress);
                entity.SendMessageResultCreatedTime = SendMessageResultCreatedTime;
                entity.SentDate       = null;
                entity.DoneDate       = null;
                entity.DeliveryStatus = DeliveryReportStatus.MessageAccepted;

                ////////////////////////////////////////
                // 21 ~ 25

                entity.DeliveryStatusString = entity.DeliveryStatus.ToString();
                entity.Price = (decimal)0.0; // 尚未傳送完成
                entity.DeliveryReportCreatedTime = null;
                entity.MessageCost = (new MessageCostInfo(entity.SendBody, entity.DestinationAddress).MessageCost);
                entity.Delivered   = IsDelivered(entity.DeliveryStatus);

                ////////////////////////////////////////
                // 26
                entity.DestinationName           = DestinationName;
                entity.Region                    = MobileUtil.GetRegionName(entity.DestinationAddress);
                entity.CreatedTime               = DateTime.UtcNow;
                entity.RetryMaxTimes             = systemParameters.RetryMaxTimes;
                entity.RetryTotalTimes           = 0;
                entity.SendMessageRetryHistoryId = null;
                entity.Email = SendMessageResult.Email;

                entity = sendMessageHistoryRepository.Insert(entity);
            }

            // (6) 出統計表
            this.sendMessageStatisticService.AddOrUpdateSendMessageStatistic(sendMessageQueueId);
        }