public ActionResult SendSMS(List <CMS_MarketingModels> model, int RunTime) { /* * ok 0.Check user login * ok 1. Check total credit of customer * ok 2. Check phone number get Operator * ok 3. Insert to DB tru credit * ok 4. send sms to Centri server */ if (CurrentUser == null || string.IsNullOrEmpty(CurrentUser.UserId)) { return(RedirectToAction("Index", "Login")); } string msg = ""; if (model != null) { decimal totalPrice = 0; totalPrice = model.Sum(x => x.SMSPrice); if (!_fac.CheckTotalCredit(CurrentUser.UserId, totalPrice)) { TempData["ErrorMessage"] = "Your credit is not enough to run this service!"; TempData["DataReturnError"] = model; return(RedirectToAction("Index")); } else { List <MessageSMSModels> listData = new List <MessageSMSModels>(); List <CMS_SimOperatorModels> listOp = _simOperator.GetList(); List <string> listGSMName = _gsmFac.GetList().Where(x => x.IsActive).Select(x => x.GSMName).ToList(); var random = new Random(); int index = random.Next(listGSMName.Count); string GSMName = listGSMName[index]; string channelName = Commons.centriSMSChannel + (GSMName == null? "" : "#" + GSMName); foreach (CMS_MarketingModels item in model) { string newPhone = ""; string operatorName = _fac.GetOperatorName(item.SendTo, listOp, ref newPhone); item.OperatorName = operatorName; item.RunTime = RunTime; item.SendFrom = GSMName; item.TimeInput = DateTime.Now; item.UpdatedBy = CurrentUser.UserId; item.CreatedBy = CurrentUser.UserId; item.SendTo = newPhone; _fac.InsertFromExcel(item, ref msg); MessageSMSModels modelCentri = new MessageSMSModels() { id = item.Id, operatorName = operatorName, phone = item.SendTo, text = item.SMSContent }; listData.Add(modelCentri); } bool isRunSuccess = true; if (listData.Count > 0) { MainSMSModels mod = new MainSMSModels() { type = "SEND_SMS", messages = listData, callbackURL = Url.Action("UpdateSMSStatus", "Centrifuge", null, HttpContext.Request.Url.Scheme), delay = RunTime }; isRunSuccess = CMSCentrifugoFactory.PublishApiToCentri("publish", Commons.centriURL, Commons.centriApiKey, channelName, mod); } if (!isRunSuccess) { foreach (MessageSMSModels item in listData) { _fac.UpdateSMSStatus(item.id, (int)Commons.SMSStatus.Fail, ref msg); } msg = "Run SMS marketing fail!"; } } } if (string.IsNullOrEmpty(msg)) { TempData["SuccessMessage"] = "Run SMS marketing successfully!"; } else { TempData["DataReturnError"] = model; TempData["ErrorMessage"] = msg; } return(RedirectToAction("Index")); }
public ActionResult Index(CMS_TestServiceModels model) { try { if (!ModelState.IsValid) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(View(model)); } List <CMS_SimOperatorModels> listOp = _simOperator.GetList(); List <string> listGSMName = _gsmFac.GetList().Where(x => x.IsActive).Select(x => x.GSMName).ToList(); var random = new Random(); int index = random.Next(listGSMName.Count); string GSMName = listGSMName[index]; string channelName = Commons.centriSMSChannel + (GSMName == null ? "" : "#" + GSMName); decimal rate = _fac.GetSMSRate((int)Commons.ConfigType.SMSOTP); string strSMSConvert = Commons.ConvertUnicodeToWithoutAccent(model.Content); int smsFee = strSMSConvert.Length / 80; string newPhone = ""; string operatorName = _fac.GetOperatorName(model.Phone, listOp, ref newPhone); CMS_MarketingModels importModel = new CMS_MarketingModels() { OperatorName = operatorName, SendFrom = GSMName, CreatedBy = CurrentUser.UserId, CustomerId = CurrentUser.UserId, CustomerName = string.Format("{0} ({1})", CurrentUser.UserName, CurrentUser.Phone), RunTime = 60, SendTo = newPhone, SMSContent = model.Content, SMSType = (int)Commons.SMSType.OTP, Status = (int)Commons.SMSStatus.Sent, TimeInput = DateTime.Now, UpdatedBy = CurrentUser.UserId, SMSRate = rate, SMSPrice = (smsFee + 1) * rate }; string msg = ""; var result = _fac.InsertFromExcel(importModel, ref msg); if (string.IsNullOrEmpty(msg)) { List <MessageSMSModels> listData = new List <MessageSMSModels>(); MessageSMSModels modelCentri = new MessageSMSModels() { id = importModel.Id, operatorName = operatorName, phone = importModel.SendTo, text = importModel.SMSContent }; listData.Add(modelCentri); bool isRunSuccess = true; if (listData.Count > 0) { MainSMSModels mod = new MainSMSModels() { type = "SEND_SMS", messages = listData, callbackURL = Url.Action("UpdateSMSStatus", "Centrifuge", null, HttpContext.Request.Url.Scheme), delay = 10 }; isRunSuccess = CMSCentrifugoFactory.PublishApiToCentri("publish", Commons.centriURL, Commons.centriApiKey, channelName, mod); } if (!isRunSuccess) { _fac.UpdateSMSStatus(importModel.Id, (int)Commons.SMSStatus.Fail, ref msg); msg = "Service is fail!"; } } if (msg.Equals("")) { ViewData["SuccessMessage"] = "Service is successfully!"; return(View(new CMS_TestServiceModels())); } else { ViewData["ErrorMessage"] = msg; return(View(model)); } } catch (Exception ex) { NSLog.Logger.Error(ex); ViewData["ErrorMessage"] = ex.Message; return(View(model)); } }