static void impfile(string file, string tag) { var msgs = File.ReadAllLines(file); var count = 0; using (var db = new messageContext()) { foreach (var msg in msgs) { var fields = msg.Split('|'); if (fields.Length < 5) { Console.WriteLine("bad line: {0}", msg); continue; } var ordinal = fields[0]; short busitype = 0; short.TryParse(fields[1], out busitype); var success = true; if (fields[2] == "2") { success = false; } var phone = fields[3]; var content = fields[4]; var dbmsg = db.Abmsg.FirstOrDefault(c => c.Ordinal == ordinal); if (dbmsg == null) { count++; db.Abmsg.Add(new Abmsg { Ordinal = ordinal, Content = content, Busiflag = success, Busitype = busitype, Timestamp = DateTime.Now, Phone = phone, }); db.SaveChanges(); } } } Console.WriteLine("{2},{0} of {3} message added this time,{1}", count, DateTime.Now, tag, msgs.Count()); }
static void Main(string[] args) { //import msg data if (!File.Exists(msgfile)) { Console.WriteLine("file {0} doesn't exist,{1}", msgfile, DateTime.Now); } else { impfile(msgfile, "over"); } if (!File.Exists(msgfilenew)) { Console.WriteLine("file {0} doesn't exist,{1}", msgfilenew, DateTime.Now); } else { impfile(msgfilenew, "new"); } //send message if (DateTime.Now.Hour > 21 || DateTime.Now.Hour < 6) { return; } var sendcount = 0; using (var msgdb = new messageContext()) { var needsend = msgdb.Abmsg.Where(c => c.Sendflag == false && c.Count < 100 && c.Timestamp.CompareTo(DateTime.Now.AddMonths(-1)) >= 0 ); foreach (var m in needsend) { try { send(m.Phone, m.Content); m.Sendflag = true; sendcount++; } catch (Exception ex) { Console.WriteLine("{2},send msg {0} error, {1}", m.Content, ex, m.Phone); m.Count++; } msgdb.SaveChanges(); } Console.WriteLine("{0} of {2} messages sended this time,{1}", sendcount, DateTime.Now, needsend.Count()); } if (sendcount < 1) { return; } var recap = string.Format("本次发送ab短信{0}条,{1}", sendcount, DateTime.Now); var phone = "18521561581"; try { send(phone, recap); } catch (Exception ex) { Console.WriteLine("{2},send msg {0} error, {1}", recap, ex, phone); } }
static void Main(string[] args) { //import msg data if (!File.Exists(msgfile)) { Console.WriteLine("file {0} doesn't exist,{1}", msgfile, DateTime.Now); } else { var msgs = File.ReadAllLines(msgfile); var count = 0; using (var db = new messageContext()) { foreach (var msg in msgs) { var fields = msg.Split('|'); if (fields.Length < 5) { Console.WriteLine("bad line: {0}", msg); continue; } var ordinal = 0; if (!int.TryParse(fields[0], out ordinal)) { Console.WriteLine("bad ordinal line: {0}", msg); continue; } short busitype = 0; short.TryParse(fields[1], out busitype); var success = true; if (fields[2] == "2") { success = false; } var phone = fields[3]; var content = fields[4]; var dbmsg = db.Drivermsg.FirstOrDefault(c => c.Ordinal == ordinal); if (dbmsg == null) { count++; db.Drivermsg.Add(new Drivermsg { Ordinal = ordinal, Content = content, Busiflag = success, Busitype = busitype, Timestamp = DateTime.Now, Phone = phone, }); db.SaveChanges(); } } } Console.WriteLine("{0} of {2} message added this time,{1}", count, DateTime.Now, msgs.Count()); } if (DateTime.Now.Hour > 21 || DateTime.Now.Hour < 6) { return; } //send message using (var msgdb = new messageContext()) { var sendcount = 0; var needsend = msgdb.Drivermsg.Where(c => c.Sendflag == false && c.Count < 100 && c.Timestamp.CompareTo(DateTime.Now.AddMonths(-1)) >= 0 ); foreach (var m in needsend) { try { send(m.Phone, m.Content); m.Sendflag = true; sendcount++; } catch (Exception ex) { Console.WriteLine("{2},send msg {0} error, {1}", m.Content, ex, m.Phone); m.Count++; } msgdb.SaveChanges(); } Console.WriteLine("{0} of {2} messages sended this time,{1}", sendcount, DateTime.Now, needsend.Count()); if (sendcount < 1) { return; } var recap = string.Format("本次发送驾管业务短信{0}条,{1}", sendcount, DateTime.Now); var phoneinfo = "18521561581"; try { send(phoneinfo, recap); } catch (Exception ex) { Console.WriteLine("{2},send msg {0} error, {1}", recap, ex, phoneinfo); } } }
public msgres CheckMsg() { var ret = new msgres { status = 0, aball = 0, driverall = 0, carall = 0, absuccess = 0, carsuccess = 0, driversuccess = 0, aballtoday = 0, driveralltoday = 0, caralltoday = 0, absuccesstoday = 0, carsuccesstoday = 0, driversuccesstoday = 0, abchartdata = new chartdata { values = new List <values>(), labels = new List <labels>() }, carchartdata = new chartdata { values = new List <values>(), labels = new List <labels>() }, driverchartdata = new chartdata { values = new List <values>(), labels = new List <labels>() }, recap = string.Empty }; try { using (var msgdb = new messageContext()) { ret.aball = msgdb.Abmsg.Count(); ret.driverall = msgdb.Drivermsg.Count(); ret.carall = msgdb.Carmsg.Count(); ret.absuccess = msgdb.Abmsg.Count(c => c.Sendflag); ret.carsuccess = msgdb.Carmsg.Count(c => c.Sendflag); ret.driversuccess = msgdb.Drivermsg.Count(c => c.Sendflag); var today = DateTime.Now; var tod = new DateTime(today.Year, today.Month, today.Day); ret.aballtoday = msgdb.Abmsg.Count(c => c.Timestamp.CompareTo(tod) >= 0); ret.driveralltoday = msgdb.Drivermsg.Count(c => c.Timestamp.CompareTo(tod) >= 0); ret.caralltoday = msgdb.Carmsg.Count(c => c.Timestamp.CompareTo(tod) >= 0); ret.absuccesstoday = msgdb.Abmsg.Count(c => c.Sendflag && c.Timestamp.CompareTo(tod) >= 0); ret.carsuccesstoday = msgdb.Carmsg.Count(c => c.Sendflag && c.Timestamp.CompareTo(tod) >= 0); ret.driversuccesstoday = msgdb.Drivermsg.Count(c => c.Sendflag && c.Timestamp.CompareTo(tod) >= 0); { var ab = msgdb.Abmsg.Where(c => c.Sendflag).Select(c => c.Timestamp);//.ToList(); var aaaaa = from one in ab group one by one.ToString("yyyy-MM-dd") into onegroup orderby onegroup.Key descending select new aaa { day = onegroup.Key, count = onegroup.Count() }; foreach (var cc in aaaaa) { ret.abchartdata.labels.Add(new labels { label = cc.day }); ret.abchartdata.values.Add(new values { value = cc.count.ToString() }); } } { var ab = msgdb.Carmsg.Where(c => c.Sendflag).Select(c => c.Timestamp);//.ToList(); var aaaaa = from one in ab group one by one.ToString("yyyy-MM-dd") into onegroup orderby onegroup.Key descending select new aaa { day = onegroup.Key, count = onegroup.Count() }; foreach (var cc in aaaaa) { ret.carchartdata.labels.Add(new labels { label = cc.day }); ret.carchartdata.values.Add(new values { value = cc.count.ToString() }); } } { var ab = msgdb.Drivermsg.Where(c => c.Sendflag).Select(c => c.Timestamp);//.ToList(); var aaaaa = from one in ab group one by one.ToString("yyyy-MM-dd") into onegroup orderby onegroup.Key descending select new aaa { day = onegroup.Key, count = onegroup.Count() }; foreach (var cc in aaaaa) { ret.driverchartdata.labels.Add(new labels { label = cc.day }); ret.driverchartdata.values.Add(new values { value = cc.count.ToString() }); } } ret.recap += "<li>车管总短信量: " + ret.carall + "</li>"; ret.recap += "<li>车管总成功量: " + ret.carsuccess + "</li>"; ret.recap += "<li>驾管总短信量: " + ret.driverall + "</li>"; ret.recap += "<li>驾管总成功量: " + ret.driversuccess + "</li>"; ret.recap += "<li>AB总短信量: " + ret.aball + "</li>"; ret.recap += "<li>AB总成功量: " + ret.absuccess + "</li>"; ret.recap += "<li>车管今日短信量: " + ret.caralltoday + "</li>"; ret.recap += "<li>车管今日成功量: " + ret.carsuccesstoday + "</li>"; ret.recap += "<li>驾管今日短信量: " + ret.driveralltoday + "</li>"; ret.recap += "<li>驾管今日成功量: " + ret.driversuccesstoday + "</li>"; ret.recap += "<li>AB今日短信量: " + ret.aballtoday + "</li>"; ret.recap += "<li>AB今日成功量: " + ret.absuccesstoday + "</li>"; } } catch (Exception ex) { ret.content += ex.Message; } return(ret); }