public HttpResponseMessage Post([FromBody] SendSMSModel sendSMSModel)
        //public HttpResponseMessage Post([FromBody] SendSMSModel sendSMSModel)
        {
            try
            {
                this.logService.Debug("BasicAuthApi::SendSMSController,Post({0})", JsonConvert.SerializeObject(sendSMSModel));

                DateTime?sendTime = sendSMSModel.sendTime;
                string   subject  = sendSMSModel.subject;
                string   content  = sendSMSModel.content;
                string[] mobiles  = sendSMSModel.mobiles;

                HttpResponseMessage returnVal = null;

                using (TransactionScope scope = context.CreateTransactionScope())
                {
                    bool isImmediately = !sendTime.HasValue;

                    var model = new SendMessageRuleModel();

                    model.SendTitle                = subject;
                    model.SendBody                 = content;
                    model.RecipientFromType        = RecipientFromType.ManualInput;
                    model.RecipientFromManualInput = new RecipientFromManualInputModel
                    {
                        PhoneNumbers = string.Join(",", mobiles)
                    };
                    model.SendTimeType = isImmediately ? SendTimeType.Immediately : SendTimeType.Deliver;
                    model.SendDeliver  = isImmediately
                        ? null
                        : new SendDeliverModel
                    {
                        SendTime             = sendTime.Value,
                        ClientTimezoneOffset = ClientTimezoneOffset,
                    };
                    model.SendCycleEveryDay   = null;
                    model.SendCycleEveryWeek  = null;
                    model.SendCycleEveryMonth = null;
                    model.SendCycleEveryYear  = null;
                    model.SendCustType        = SendCustType.OneWay;
                    model.UseParam            = false;
                    model.SendMessageType     = SendMessageType.SmsMessage;
                    model.TotalReceiverCount  = 0;
                    model.TotalMessageCost    = 0;
                    model.RemainingSmsBalance = 0;
                    model.CreatedTime         = DateTime.UtcNow;

                    model.UpdateClientTimezoneOffset(ClientTimezoneOffset);

                    var rules = this.sendMessageRuleService.CreateSendMessageRuleFromWeb(CurrentUser, model);

                    var result = rules.Select(p => new
                    {
                        balance  = p.RemainingSmsBalance, // 發送後剩餘點數
                        smsCount = p.TotalReceiverCount,  // 發送通數
                        price    = p.TotalMessageCost,    // 本次發送扣除點數
                        bulkId   = p.Id,                  // 發送識別碼
                    });

                    returnVal = this.Request.CreateResponse(HttpStatusCode.OK, result);

                    scope.Complete();
                }

                return(returnVal);
            }
            catch (Exception ex)
            {
                this.logService.Error(ex);

                return(this.Request.CreateResponse(HttpStatusCode.InternalServerError, new
                {
                    ErrorMessage = ex.Message,
                }));
            }
        }
Beispiel #2
0
        public HttpResponseMessage Post([FromBody] SendParamSMSModel sendParamSMSModel)
        //public HttpResponseMessage Post([FromBody] SendParamSMSModel sendParamSMSModel)
        {
            try
            {
                this.logService.Debug("BasicAuthApi::SendParamSMSController,Post({0})", JsonConvert.SerializeObject(sendParamSMSModel));

                DateTime?sendTime = sendParamSMSModel.sendTime;
                string   subject  = sendParamSMSModel.subject;
                string   content  = sendParamSMSModel.content;
                List <UploadedMessageReceiverModel> messageReceivers = sendParamSMSModel.messageReceivers;

                HttpResponseMessage returnVal = null;

                var ErrorMessages = new List <string>();

                using (TransactionScope scope = context.CreateTransactionScope())
                {
                    bool isImmediately = !sendTime.HasValue;

                    // Simulate SaveUploadedFile

                    var entityUploadedFile = new UploadedFile();
                    entityUploadedFile.FileName         = "FakeFileName";
                    entityUploadedFile.FilePath         = "FakeFilePath";
                    entityUploadedFile.UploadedFileType = UploadedFileType.SendParamMessage;
                    entityUploadedFile.CreatedUser      = CurrentUser;
                    entityUploadedFile.CreatedTime      = DateTime.UtcNow;
                    entityUploadedFile = AsyncHelper.RunSync(() => context.InsertAsync(entityUploadedFile));

                    // Simulate Insert UploadedMessageReceivers

                    var blacklists = context.Set <Blacklist>().Where(p => p.CreatedUserId == CurrentUserId).ToList();

                    for (int i = 0; i < messageReceivers.Count; i++)
                    {
                        var _model = messageReceivers[i];

                        if (string.IsNullOrEmpty(_model.Mobile))
                        {
                            continue;
                        }

                        var entity = new UploadedMessageReceiver();
                        entity.RowNo  = i + 1;
                        entity.Name   = _model.Name;
                        entity.Mobile = _model.Mobile;
                        entity.Email  = _model.Email;

                        if (_model.SendTime.HasValue)
                        {
                            entity.SendTime = Converter.ToUniversalTime(_model.SendTime.Value, ClientTimezoneOffset);
                        }
                        else
                        {
                            entity.SendTime = null;
                        }

                        entity.ClientTimezoneOffset = ClientTimezoneOffset;
                        entity.UseParam             = true;
                        entity.Param1            = _model.Param1;
                        entity.Param2            = _model.Param2;
                        entity.Param3            = _model.Param3;
                        entity.Param4            = _model.Param4;
                        entity.Param5            = _model.Param5;
                        entity.CreatedUserId     = CurrentUserId;
                        entity.CreatedTime       = entityUploadedFile.CreatedTime;
                        entity.UploadedFile      = entityUploadedFile;
                        entity.UploadedSessionId = entityUploadedFile.Id;

                        var error   = string.Empty;
                        var isValid = this.validationService.Validate(entity, blacklists, out error);

                        if (!isValid)
                        {
                            ErrorMessages.Add(string.Format("{0}: {1}", entity.Mobile, error));
                        }

                        // 目前就算驗證不過也沒關係,仍然可以存檔
                        entity = AsyncHelper.RunSync(() => context.InsertAsync(entity));
                    }

                    var model = new SendMessageRuleModel();

                    model.SendTitle               = subject;
                    model.SendBody                = content;
                    model.RecipientFromType       = RecipientFromType.FileUpload;
                    model.RecipientFromFileUpload = new RecipientFromFileUploadModel
                    {
                        UploadedFileId = entityUploadedFile.Id,
                        AddSelfToMessageReceiverList = false,
                    };

                    model.SendTimeType = isImmediately ? SendTimeType.Immediately : SendTimeType.Deliver;
                    model.SendDeliver  = isImmediately
                        ? null
                        : new SendDeliverModel
                    {
                        SendTime             = sendTime.Value,
                        ClientTimezoneOffset = ClientTimezoneOffset,
                    };
                    model.SendCycleEveryDay   = null;
                    model.SendCycleEveryWeek  = null;
                    model.SendCycleEveryMonth = null;
                    model.SendCycleEveryYear  = null;
                    model.SendCustType        = SendCustType.OneWay;
                    model.UseParam            = true;
                    model.SendMessageType     = SendMessageType.SmsMessage;
                    model.TotalReceiverCount  = 0;
                    model.TotalMessageCost    = 0;
                    model.RemainingSmsBalance = 0;
                    model.CreatedTime         = DateTime.UtcNow;

                    model.UpdateClientTimezoneOffset(ClientTimezoneOffset);

                    var rules = this.sendMessageRuleService.CreateSendMessageRuleFromWeb(CurrentUser, model);

                    var result = rules.Select(p => new
                    {
                        balance  = p.RemainingSmsBalance, // 發送後剩餘點數
                        smsCount = p.TotalReceiverCount,  // 發送通數
                        price    = p.TotalMessageCost,    // 本次發送扣除點數
                        bulkId   = p.Id,                  // 發送識別碼
                    });

                    returnVal = this.Request.CreateResponse(HttpStatusCode.OK, new {
                        result = result,
                        errors = ErrorMessages,
                    });

                    scope.Complete();
                }

                return(returnVal);
            }
            catch (Exception ex)
            {
                this.logService.Error(ex);

                return(this.Request.CreateResponse(HttpStatusCode.InternalServerError, new
                {
                    ErrorMessage = ex.Message,
                }));
            }
        }