using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Web; using System.Web.Mvc; using WX.CRM.Common; using WX.CRM.IBLL.Res; using WX.CRM.IBLL.Sms; using WX.CRM.WebHelper; namespace WX.CRM.WEB.Controllers.Sms { public class BatchMsgController : BaseController { private readonly ISMS_BATCHMSG BatchMsgBiz; private readonly ISMS_BATCHMSG_Q BatchMsgBiz_Q; private readonly ISMS_MSGSUBTYPE_Q MsgSubTypeBiz_Q; private readonly ISMS_ACCOUNT_Q SmsAccountBiz_Q; private readonly IRES_CUSTOMER _customer; private readonly IRES_CUSTOMER_Q _customer_Q; public BatchMsgController(ISMS_BATCHMSG _BatchMsgBiz, ISMS_BATCHMSG_Q _BatchMsgBiz_Q, ISMS_MSGSUBTYPE_Q _MsgSubTypeBiz_Q, ISMS_ACCOUNT_Q _SmsAccountBiz_Q, IRES_CUSTOMER customer, IRES_CUSTOMER_Q customer_Q) { BatchMsgBiz = _BatchMsgBiz; BatchMsgBiz_Q = _BatchMsgBiz_Q; MsgSubTypeBiz_Q = _MsgSubTypeBiz_Q; SmsAccountBiz_Q = _SmsAccountBiz_Q; _customer = customer; _customer_Q = customer_Q; } [AuthorizeRedirect(Roles = InitRights.CONST_批量短信)] public ActionResult Index() { Pager pager = new Pager() { order = "BATCHID", page = 1, rows = 10 }; string tableId = "tablist"; Table tab = new Table(tableId); tab.AddHeadCol("map_BATCHID", "100", "批次ID"); tab.AddHeadCol("map_MESSAGE", "", "短信内容"); tab.AddHeadCol("map_TYPECODENAME", "120", "短信类型"); tab.AddHeadCol("map_CLIENTENAME", "120", "短信平台"); tab.AddHeadCol("map_CTIME", "120", "提交时间"); tab.AddHeadCol("map_CREATEUSERNAME", "120", "提交人"); tab.AddHeadRow(); ViewBag.gridTable = tab.GetTable() + Pagination.GetPage(pager, tableId, "15,30,50,100"); return View(); } [AuthorizeRedirect(Roles = InitRights.CONST_批量短信)] public JsonResult GetHtmlList(Pager pager, string columns) { int c = 0; List lis = BatchMsgBiz_Q.GetList(pager.page, pager.rows, ref c); pager.totalRows = c; Table table = new Table(columns, true); table.gridPager = pager; foreach (WX.CRM.Model.Entity.SMS_BATCHMSG model in lis) { table.AddCol(model.map_BATCHID); table.AddCol(model.map_MESSAGE); table.AddCol(model.map_TYPECODENAME); table.AddCol(model.map_CLIENTENAME); table.AddCol(model.map_CTIME); table.AddCol(model.map_CREATEUSERNAME); table.AddRow(); } var json = new { totalPages = pager.totalPages, totalRows = pager.totalRows, rowsList = table.GetRows() }; return Json(json, JsonRequestBehavior.AllowGet); } public string RecFile(string id) { string result = "ok"; string tn = string.Format("{0}", id); List lis = new List(); try { WX.CRM.Model.Entity.SMS_BATCHMSG info = new WX.CRM.Model.Entity.SMS_BATCHMSG(); info.SUBTYPEID = decimal.Parse(string.Format("{0}", Request.Form["hidSubTypeID"])); info.CLIENTCODE = string.Format("{0}", Request.Form["hidClientCode"]); info.CREATEUSER = UserId; info.CTIME = DateTime.Now; switch (tn) { case "客户ID批量提交": lis = ReciveFile1(); info.MESSAGE = string.Format("{0}", Request.Form["txtContent1"]).Trim().TrimEnd('\r').TrimEnd('\n'); info.TYPECODE = "BATSMS0001"; //未定义 break; case "客户ID文件批量提交": lis = ReciveFile2(); info.MESSAGE = string.Format("{0}", Request.Form["txtContent2"]).Trim().TrimEnd('\r').TrimEnd('\n'); info.TYPECODE = "BATSMS0001"; //未定义 break; case "号码批量提交": lis = ReciveFile4(); info.MESSAGE = string.Format("{0}", Request.Form["bmtxtContent2"]).Trim().TrimEnd('\r').TrimEnd('\n'); info.TYPECODE = "BATSMS0001"; //未定义 break; case "号码文件批量提交": lis = ReciveFile5(); info.MESSAGE = string.Format("{0}", Request.Form["bmtxtContent1"]).Trim().TrimEnd('\r').TrimEnd('\n'); info.TYPECODE = "BATSMS0001"; //未定义 break; case "短信地址推送与反馈客户ID批量提交": lis = ReciveFile3(); info.MESSAGE = string.Format("{0}", Request.Form["txtFKResIDS"]); if (info.MESSAGE.IndexOf("@@code ") == -1) info.MESSAGE = info.MESSAGE.Replace("@@code", "@@code "); info.TYPECODE = "BATSMS0002"; //未定义 break; default: result = string.Format("【{0}】当前标签不正确!", tn); break; } //提交短信 if (lis == null || lis.Count == 0) throw new Exception("请提交正确的客户ID!"); if (string.IsNullOrEmpty(info.MESSAGE)) throw new Exception("短信内容能为空!"); if (info.MESSAGE.Length > 500) throw new Exception("短信内容建议不要超过200个字!"); BatchMsgBiz.ImpotDt(info, lis, 1000); } catch (Exception ex) { result = ex.Message; } return result; } #region 处理提交上来的数据文件 public List ReciveFile1() { List lis = new List(); string mobileStr = string.Format("{0}", Request.Form["txtResIdFromSql"]); string[] mbs = mobileStr.Trim().Split(','); for (int i = 0; i < mbs.Length; i++) { if (!string.IsNullOrEmpty(mbs[i])) lis.Add(mbs[i].Trim()); } return lis; } public List ReciveFile2() { string vPath = System.Configuration.ConfigurationManager.AppSettings["UploadTemFileDic"]; List lis = new List(); HttpFileCollectionBase files = Request.Files; HttpPostedFileBase file = files["FileMobile"]; if (file != null && file.ContentLength > 0) { string fileName = file.FileName; //判断文件名字是否包含路径名,如果有则提取文件名 if (fileName.LastIndexOf("\\") > -1) { fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); } if (fileName.ToLower().IndexOf(".txt") > -1) { if (!System.IO.Directory.Exists(Server.MapPath(vPath))) { System.IO.Directory.CreateDirectory(Server.MapPath(vPath)); } string path = Server.MapPath((vPath + "/") + fileName); file.SaveAs(path); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default); while (!sr.EndOfStream) { string number = sr.ReadLine(); if (!string.IsNullOrEmpty(number)) lis.Add(number.Trim()); } sr.Close(); fs.Close(); System.IO.File.Delete(path); } else { throw new Exception("文件格式不正确!"); } } else { throw new Exception("请选择正确的文件!"); } return lis; } public List ReciveFile3() { string vPath = System.Configuration.ConfigurationManager.AppSettings["UploadTemFileDic"]; List lis = new List(); HttpFileCollectionBase files = Request.Files; HttpPostedFileBase file = files["FileMobile2"]; if (file != null && file.ContentLength > 0) { string fileName = file.FileName; //判断文件名字是否包含路径名,如果有则提取文件名 if (fileName.LastIndexOf("\\") > -1) { fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); } if (fileName.ToLower().IndexOf(".txt") > -1) { if (!System.IO.Directory.Exists(Server.MapPath(vPath))) { System.IO.Directory.CreateDirectory(Server.MapPath(vPath)); } string path = Server.MapPath((vPath + "/") + fileName); file.SaveAs(path); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default); while (!sr.EndOfStream) { string number = sr.ReadLine(); if (!string.IsNullOrEmpty(number)) lis.Add(number.Trim()); } sr.Close(); fs.Close(); System.IO.File.Delete(path); } else { throw new Exception("文件格式不正确!"); } } else { throw new Exception("请选择正确的文件!"); } return lis; } //private const string mobileReg = @"^(0|86|17951)?(13[0-9]|17[0-9]|15[012356789]|18[012356789]|14[57])[0-9]{8}$"; private const string mobileReg = @"^(0|86|17951)?(1)[0-9]{10}$"; public List ReciveFile4() { List lis = new List(); string mobileStr = string.Format("{0}", Request.Form["bmtxtResIDS"]); mobileStr = mobileStr.Replace("\r\n", "&"); mobileStr = mobileStr.Replace("\r", "&"); mobileStr = mobileStr.Replace("\n", "&"); string[] mbs = mobileStr.Trim().Split('&'); string mobile = string.Empty; var customerFrom = "BatchMsg_CRM"; //来源标签,是否改成从配置表中取 string resId = string.Empty; for (int i = 0; i < mbs.Length; i++) { mobile = mbs[i].Trim(); if (!string.IsNullOrEmpty(mobile) && System.Text.RegularExpressions.Regex.IsMatch(mobile, mobileReg))//判断是否为手机号码 { //对手机号码进行处理 resId = ResUtil.CreateResId(mobile); int result = _customer.ResgisterCustomer(mobile, resId, customerFrom, null); lis.Add(resId); } } return lis; } public List ReciveFile5() { string vPath = System.Configuration.ConfigurationManager.AppSettings["UploadTemFileDic"]; List lis = new List(); HttpFileCollectionBase files = Request.Files; HttpPostedFileBase file = files["bmFileMobile"]; string resId = string.Empty; var customerFrom = "BatchMsg_CRM"; //来源标签,是否改成从配置表中取 if (file != null && file.ContentLength > 0) { string fileName = file.FileName; //判断文件名字是否包含路径名,如果有则提取文件名 if (fileName.LastIndexOf("\\") > -1) { fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); } if (fileName.ToLower().IndexOf(".txt") > -1) { if (!System.IO.Directory.Exists(Server.MapPath(vPath))) { System.IO.Directory.CreateDirectory(Server.MapPath(vPath)); } string path = Server.MapPath((vPath + "/") + fileName); file.SaveAs(path); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default); while (!sr.EndOfStream) { string mobile = sr.ReadLine().Trim(); if (!string.IsNullOrEmpty(mobile) && System.Text.RegularExpressions.Regex.IsMatch(mobile, mobileReg))//判断是否为手机号码 { //对手机号码进行处理 resId = ResUtil.CreateResId(mobile); int result = _customer.ResgisterCustomer(mobile, resId, customerFrom, null); lis.Add(resId); } } sr.Close(); fs.Close(); System.IO.File.Delete(path); } else { throw new Exception("文件格式不正确!"); } } else { throw new Exception("请选择正确的文件!"); } return lis; } #endregion #region 获取客户ID public JsonResult GetAllResId(string resIds) { resIds = resIds.Trim().Replace("\r\n", ","); resIds = resIds.Replace("\r\n", ","); resIds = resIds.Replace("\r", ","); resIds = resIds.Replace("\n", ","); DataTable tab = _customer_Q.GetList_CustomerByResIds(resIds); List obj = new List(); string residLastChar = string.Empty; foreach (DataRow row in tab.Rows) { residLastChar = string.Format("{0}", row["RESID"]); residLastChar = residLastChar.Substring(residLastChar.Length - 1, 1); obj.Add(new { RESID = row["RESID"], LASTNUM3 = row["LASTNUM3"], ISPRIMARYNUM = row["ISPRIMARYNUM"], isCallPhone = (Convert.ToInt32(residLastChar) % 2 == 0) }); } return Json(new { result = tab.Rows.Count > 0 ? 1 : 0, message = tab.Rows.Count > 0 ? "" : "请输入正确的客户ID!", ResIds = obj }); } #endregion #region combox数据列表,未加载实际数据 public JsonResult SmsSubTypeList(string id) { List lis = new List(); List tem = new List(); switch (id) { case "客户ID批量提交": tem = MsgSubTypeBiz_Q.GetList("BATSMS0001"); break; case "客户ID文件批量提交": tem = MsgSubTypeBiz_Q.GetList("BATSMS0001"); break; case "号码批量提交": tem = MsgSubTypeBiz_Q.GetList("BATSMS0001"); break; case "号码文件批量提交": tem = MsgSubTypeBiz_Q.GetList("BATSMS0001"); break; case "短信地址推送与反馈客户ID批量提交": tem = MsgSubTypeBiz_Q.GetList("BATSMS0002"); break; default: break; } lis.Clear(); lis.Add(new WX.CRM.Model.Entity.SMS_MSGSUBTYPE() { SUBTYPEID = 0, SUBTYPENAME = "-请选择-" }); foreach (var obj in tem) { lis.Add(new WX.CRM.Model.Entity.SMS_MSGSUBTYPE() { SUBTYPEID = obj.SUBTYPEID, SUBTYPENAME = obj.SUBTYPENAME }); } return Json(lis, JsonRequestBehavior.AllowGet); } public JsonResult SmsCountList(string id) { List lis = new List(); List tem = new List(); decimal typeid = decimal.Parse(id); tem = SmsAccountBiz_Q.GetList(typeid); lis.Clear(); lis.Add(new WX.CRM.Model.Entity.SMS_ACCOUNT() { CLIENTCODE = "0", CLIENTNAME = "-请选择-" }); foreach (var obj in tem) { lis.Add(new WX.CRM.Model.Entity.SMS_ACCOUNT() { CLIENTCODE = obj.CLIENTCODE, CLIENTNAME = obj.CLIENTNAME }); } return Json(lis, JsonRequestBehavior.AllowGet); } #endregion } }