public bool Validate(UploadedMessageReceiver entity, IEnumerable <Blacklist> blacklists, out string error) { var errors = new List <string>(); // Mobile if (string.IsNullOrEmpty(entity.Mobile)) { errors.Add("手機門號必填"); } else { if (!MobileUtil.IsPossibleNumber(entity.Mobile)) { errors.Add("手機門號格式錯誤"); } else { // 檢驗是否為黑名單 if (blacklists.Any(p => p.Mobile == entity.Mobile)) { errors.Add("黑名單"); } } } // Email if (!string.IsNullOrEmpty(entity.Email)) { var rgx = new Regex(RegularExpressionPatterns.Email); if (!rgx.IsMatch(entity.Email)) { errors.Add("電子郵件格式錯誤"); } } if (!string.IsNullOrEmpty(entity.SendTimeString)) { if (entity.SendTime.HasValue) { if (entity.SendTime.Value < DateTime.UtcNow) { errors.Add("指定時間必須大於目前時間"); } } else { // 不為空的時間字串必須格式正確 if (!string.IsNullOrEmpty(entity.SendTimeString)) { errors.Add("時間格式錯誤"); } } } error = string.Join(", ", errors); entity.IsValid = string.IsNullOrEmpty(error); entity.InvalidReason = error; return(string.IsNullOrEmpty(error)); }
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, })); } }