public bool InsertFromExcel(CMS_MarketingModels model, ref string msg) { var result = true; using (var cxt = new CMS_Context()) { using (var trans = cxt.Database.BeginTransaction()) { try { if (string.IsNullOrEmpty(model.Id)) { var _Id = Guid.NewGuid().ToString(); model.Id = _Id; var e = new CMS_Marketing { Id = _Id, CustomerId = model.CustomerId, CustomerName = model.CustomerName, OperatorName = model.OperatorName, RunTime = model.RunTime.Value, SendFrom = model.SendFrom, SendTo = model.SendTo, SMSContent = model.SMSContent, SMSPrice = model.SMSPrice, SMSType = model.SMSType, Status = model.Status, TimeInput = model.TimeInput, IsActive = model.IsActive, SMSRate = model.SMSRate, UpdatedBy = model.UpdatedBy, UpdatedDate = DateTime.Now, CreatedBy = model.CreatedBy, CreatedDate = DateTime.Now, }; cxt.CMS_Marketing.Add(e); var customer = cxt.CMS_Customers.Where(x => x.Id.Equals(model.CustomerId)).FirstOrDefault(); customer.TotalCredit = customer.TotalCredit - model.SMSPrice; } cxt.SaveChanges(); trans.Commit(); } catch (Exception ex) { msg = "Vui lòng kiểm tra đường truyền"; result = false; trans.Rollback(); } finally { cxt.Dispose(); } } } return(result); }
public List <CMS_MarketingModels> Import(string filePath, string empId, string empName, string empphone, ref string msg) { using (var cxt = new CMS_Context()) { List <CMS_MarketingModels> listMessage = new List <CMS_MarketingModels>(); try { DataTable dtMarketing = _baseFactory.GetDataFromExcel(@filePath, 1); //DataTable dtTime = _baseFactory.GetDataFromExcel(@filePath, 2); decimal rate = GetSMSRate(cxt, (int)Commons.ConfigType.SMSMarketing); for (int i = 0; i < dtMarketing.Rows.Count; i++) { string phone = Convert.ToString(dtMarketing.Rows[i][0]); string smsContent = Convert.ToString(dtMarketing.Rows[i][1]); string subPhone = phone.Substring(0, 2); string subPhone1 = phone.Substring(0, 1); if (!subPhone.Equals("84") && !subPhone1.Equals("0")) { phone = "84" + phone; } if (!string.IsNullOrEmpty(phone)) { string smsConvert = Commons.ConvertUnicodeToWithoutAccent(smsContent); int count2 = smsConvert.Length; int smsFee = count2 / 80; // < 80 = 1sms CMS_MarketingModels model = new CMS_MarketingModels() { CustomerId = empId, CustomerName = string.Format("{0} ({1})", empName, empphone), CountMessage = count2, SendTo = phone, SMSContent = smsContent, SMSType = (int)Commons.SMSType.Marketing, Status = (int)Commons.SMSStatus.Sent, SMSPrice = (smsFee + 1) * rate, SMSRate = rate }; listMessage.Add(model); } } } catch (Exception ex) { NSLog.Logger.Error("Import marketing error: ", ex); } finally { cxt.Dispose(); } return(listMessage); } }
public ActionResult Index(CMS_MarketingModels model) { try { if (model.ExcelUpload == null) { ViewData["ErrorMessage"] = "Please choose file to import!"; return(View()); } string fileName = Path.GetFileName(model.ExcelUpload.FileName); string filePath = string.Format("{0}/{1}", System.Web.HttpContext.Current.Server.MapPath("~/Uploads"), fileName); CMS_MarketingModels importModel = new CMS_MarketingModels(); string msg = ""; //upload file to server if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } model.ExcelUpload.SaveAs(filePath); //Import data var result = _fac.Import(filePath, CurrentUser.UserId, CurrentUser.UserName, CurrentUser.Phone, ref msg); model.ListSMS = result; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } if (msg.Equals("")) { return(View(model)); } else { ViewData["ErrorMessage"] = msg; return(View(model)); } } catch (Exception ex) { NSLog.Logger.Error(ex); ViewData["ErrorMessage"] = ex.Message; return(View(model)); } }
// GET: Marketing public ActionResult Index() { if (TempData["ErrorMessage"] != null) { ViewData["ErrorMessage"] = TempData["ErrorMessage"].ToString(); } if (TempData["SuccessMessage"] != null) { ViewData["SuccessMessage"] = TempData["SuccessMessage"].ToString(); } if (TempData["DataReturnError"] != null) { CMS_MarketingModels returnModel = new CMS_MarketingModels(); returnModel.ListSMS = (List <CMS_MarketingModels>)TempData["DataReturnError"]; return(View(returnModel)); } return(View()); }
public bool CreateOrUpdate(CMS_MarketingModels model, ref string msg) { var result = true; using (var cxt = new CMS_Context()) { using (var trans = cxt.Database.BeginTransaction()) { try { if (string.IsNullOrEmpty(model.Id)) { var _Id = Guid.NewGuid().ToString(); var e = new CMS_Marketing { Id = _Id, CustomerId = model.CustomerId, CustomerName = model.CustomerName, OperatorName = model.OperatorName, RunTime = model.RunTime.Value, SendFrom = model.SendFrom, SendTo = model.SendTo, SMSContent = model.SMSContent, SMSPrice = model.SMSPrice, SMSType = model.SMSType, Status = model.Status, SMSRate = model.SMSRate, TimeInput = model.TimeInput, IsActive = model.IsActive, UpdatedBy = model.UpdatedBy, UpdatedDate = DateTime.Now, CreatedBy = model.CreatedBy, CreatedDate = DateTime.Now, }; cxt.CMS_Marketing.Add(e); } else { var e = cxt.CMS_Marketing.Find(model.Id); if (e != null) { e.CustomerId = model.CustomerId; e.CustomerName = model.CustomerName; e.OperatorName = model.OperatorName; e.RunTime = model.RunTime.Value; e.SendFrom = model.SendFrom; e.SendTo = model.SendTo; e.SMSContent = model.SMSContent; e.SMSPrice = model.SMSPrice; e.SMSType = model.SMSType; e.Status = model.Status; e.SMSRate = model.SMSRate; e.TimeInput = model.TimeInput; e.IsActive = model.IsActive; e.UpdatedDate = DateTime.Now; e.UpdatedBy = model.UpdatedBy; } } cxt.SaveChanges(); trans.Commit(); } catch (Exception ex) { msg = "Vui lòng kiểm tra đường truyền"; result = false; trans.Rollback(); } finally { cxt.Dispose(); } } } return(result); }
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)); } }