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); } } }