You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
377 lines
14 KiB
377 lines
14 KiB
4 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Configuration;
|
||
|
using System.Threading;
|
||
|
using QMAPP.FJC.Entity.Message;
|
||
|
using QMAPP.FJC.DAL.Message;
|
||
|
using QMFrameWork.Data;
|
||
|
using QMFrameWork.Log;
|
||
|
using System.IO.Ports;
|
||
|
|
||
|
namespace MsgSendService
|
||
|
{
|
||
|
public class SendClass
|
||
|
{
|
||
|
public void SendMsg()
|
||
|
{
|
||
|
//获取间隔时间
|
||
|
int internalValue = Convert.ToInt32(ConfigurationManager.AppSettings["InternalValue"]);
|
||
|
string comport = ConfigurationManager.AppSettings["Comport"];
|
||
|
//短信息过期时间(分钟) 0:不过期
|
||
|
int MessageExpire = int.Parse("0" + ConfigurationManager.AppSettings["MessageExpire"]);
|
||
|
|
||
|
//通话时长
|
||
|
int ringtime = Convert.ToInt32(ConfigurationManager.AppSettings["Ringtime"]);
|
||
|
while (true)
|
||
|
{
|
||
|
//休眠时间
|
||
|
Thread.Sleep(internalValue);
|
||
|
|
||
|
MessageDAL messageDal = new MessageDAL();
|
||
|
//获取待发信息和发送失败信息
|
||
|
List<OutBox> outBoxList = new List<OutBox>();
|
||
|
outBoxList = messageDal.GetOutBoxList();
|
||
|
|
||
|
List<BadOutBox> badOutBoxList = new List<BadOutBox>();
|
||
|
badOutBoxList = messageDal.GetBadOutBoxList();
|
||
|
|
||
|
//需要拨打电话的电话号码
|
||
|
List<string> phonenumber = new List<string>();
|
||
|
|
||
|
#region 拨号
|
||
|
|
||
|
List<string> callList = new List<string>();
|
||
|
|
||
|
//foreach (var outbox in outBoxList)
|
||
|
//{
|
||
|
// if (MessageExpire == 0 || (DateTime.Now - outbox.SENDTIME).TotalMinutes < MessageExpire)
|
||
|
// {
|
||
|
// callList.Add(outbox.MBNO);
|
||
|
// }
|
||
|
//}
|
||
|
|
||
|
//if (callList.Count > 0)
|
||
|
//{
|
||
|
// try
|
||
|
// {
|
||
|
// using (ModemLib.Voice.PhoneHelper phonehelper = new ModemLib.Voice.PhoneHelper(comport, 9600, Parity.None, 8, StopBits.One))
|
||
|
// {
|
||
|
// foreach (var no in callList)
|
||
|
// {
|
||
|
// var ring = phonehelper.DialUp(no);
|
||
|
// Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + no + " call result:" + ring.ToString());
|
||
|
// WriteLog.Write(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + no + " call result:" + ring.ToString());
|
||
|
// if (ring) //是否拨通
|
||
|
// {
|
||
|
// System.Threading.Thread.Sleep(ringtime);//等待响铃2秒后
|
||
|
// phonehelper.HangUp();//挂断
|
||
|
// }
|
||
|
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// catch (Exception ex)
|
||
|
// {
|
||
|
// WriteLog.Write(ex.Message);
|
||
|
// }
|
||
|
//}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 待发送信息
|
||
|
|
||
|
foreach (var outbox in outBoxList)
|
||
|
{
|
||
|
|
||
|
//发送短信
|
||
|
bool sendFlag = false;
|
||
|
bool expired = false;
|
||
|
//如果短信息发送时间未失效
|
||
|
if (MessageExpire == 0 || (DateTime.Now - outbox.SENDTIME).TotalMinutes < MessageExpire)
|
||
|
{
|
||
|
using (ModemLib.SMS.SMSHelper sms = new ModemLib.SMS.SMSHelper(comport))
|
||
|
{
|
||
|
sendFlag = sms.Send(outbox.MBNO, outbox.MSG);
|
||
|
}
|
||
|
Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + outbox.MBNO + " send message:" + sendFlag.ToString());
|
||
|
|
||
|
WriteLog.Write(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + outbox.MBNO + " send message:" + sendFlag.ToString());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
expired = true;
|
||
|
}
|
||
|
|
||
|
if (sendFlag || expired)
|
||
|
{
|
||
|
if (sendFlag)
|
||
|
{
|
||
|
phonenumber.Add(outbox.MBNO);
|
||
|
}
|
||
|
SendOutBox sendOutBox = new SendOutBox();
|
||
|
#region
|
||
|
|
||
|
//sendOutBox.ID = outbox.ID;
|
||
|
sendOutBox.USERNAME = outbox.USERNAME;
|
||
|
sendOutBox.MBNO = outbox.MBNO;
|
||
|
sendOutBox.MSG = outbox.MSG;
|
||
|
sendOutBox.SENDTIME = outbox.SENDTIME;
|
||
|
sendOutBox.COMPORT = outbox.COMPORT;
|
||
|
sendOutBox.TOTAL = sendFlag ? 1 : 2;//1发送成功 2短信息过期
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
session.OpenTs();
|
||
|
|
||
|
messageDal.BaseSession = session;
|
||
|
//发送成功之后
|
||
|
//删除发件箱信息
|
||
|
messageDal.DeleteOutBox(outbox);
|
||
|
|
||
|
//插入已发送信息
|
||
|
messageDal.InsertSendOutBox(sendOutBox);
|
||
|
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
BadOutBox badOutBox = new BadOutBox();
|
||
|
|
||
|
#region
|
||
|
|
||
|
//badOutBox.ID = outbox.ID;
|
||
|
badOutBox.USERNAME = outbox.USERNAME;
|
||
|
badOutBox.MBNO = outbox.MBNO;
|
||
|
badOutBox.MSG = outbox.MSG;
|
||
|
badOutBox.SENDTIME = outbox.SENDTIME;
|
||
|
badOutBox.COMPORT = outbox.COMPORT;
|
||
|
badOutBox.BADWHY = "";
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
session.OpenTs();
|
||
|
|
||
|
messageDal.BaseSession = session;
|
||
|
//发送成功之后
|
||
|
//删除发件箱信息
|
||
|
messageDal.DeleteOutBox(outbox);
|
||
|
|
||
|
//插入已发送信息
|
||
|
messageDal.InsertBadOutBox(badOutBox);
|
||
|
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 发送失败信息
|
||
|
|
||
|
foreach (var badoutbox in badOutBoxList)
|
||
|
{
|
||
|
|
||
|
//发送短信
|
||
|
bool sendFlag = false;
|
||
|
bool expired = false;
|
||
|
//如果短信息发送时间未失效
|
||
|
if (MessageExpire == 0 || (DateTime.Now - badoutbox.SENDTIME).TotalMinutes < MessageExpire)
|
||
|
{
|
||
|
using (ModemLib.SMS.SMSHelper sms = new ModemLib.SMS.SMSHelper(comport))
|
||
|
{
|
||
|
sendFlag = sms.Send(badoutbox.MBNO, badoutbox.MSG);
|
||
|
}
|
||
|
Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + badoutbox.MBNO + " resend message:" + sendFlag.ToString());
|
||
|
|
||
|
WriteLog.Write(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + badoutbox.MBNO + " resend message:" + sendFlag.ToString());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
expired = true;
|
||
|
}
|
||
|
|
||
|
if (sendFlag || expired)
|
||
|
{
|
||
|
if (sendFlag)
|
||
|
{
|
||
|
phonenumber.Add(badoutbox.MBNO);
|
||
|
}
|
||
|
SendOutBox sendOutBox = new SendOutBox();
|
||
|
|
||
|
#region
|
||
|
|
||
|
//sendOutBox.ID = badoutbox.ID;
|
||
|
sendOutBox.USERNAME = badoutbox.USERNAME;
|
||
|
sendOutBox.MBNO = badoutbox.MBNO;
|
||
|
sendOutBox.MSG = badoutbox.MSG;
|
||
|
sendOutBox.SENDTIME = badoutbox.SENDTIME;
|
||
|
sendOutBox.COMPORT = badoutbox.COMPORT;
|
||
|
sendOutBox.TOTAL = sendFlag ? 1 : 2;//1发送成功 2短信息过期;
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
session.OpenTs();
|
||
|
|
||
|
messageDal.BaseSession = session;
|
||
|
//发送成功之后
|
||
|
//删除发件箱信息
|
||
|
messageDal.DeleteBadOutBox(badoutbox);
|
||
|
|
||
|
//插入已发送信息
|
||
|
messageDal.InsertSendOutBox(sendOutBox);
|
||
|
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void Test()
|
||
|
{
|
||
|
MessageDAL messageDal = new MessageDAL();
|
||
|
//获取待发信息和发送失败信息
|
||
|
List<OutBox> outBoxList = new List<OutBox>();
|
||
|
outBoxList = messageDal.GetOutBoxList();
|
||
|
|
||
|
List<BadOutBox> badOutBoxList = new List<BadOutBox>();
|
||
|
badOutBoxList = messageDal.GetBadOutBoxList();
|
||
|
|
||
|
#region 待发送信息
|
||
|
|
||
|
foreach (var outbox in outBoxList)
|
||
|
{
|
||
|
//发送短信
|
||
|
bool sendFlag = false;
|
||
|
//using (ModemLib.SMS.SMSHelper sms = new ModemLib.SMS.SMSHelper(comport))
|
||
|
//{
|
||
|
// sendFlag = sms.Send(outbox.MBNO, outbox.MSG);
|
||
|
//}
|
||
|
|
||
|
if (sendFlag)
|
||
|
{
|
||
|
SendOutBox sendOutBox = new SendOutBox();
|
||
|
|
||
|
#region
|
||
|
|
||
|
//sendOutBox.ID = outbox.ID;//
|
||
|
sendOutBox.USERNAME = outbox.USERNAME;
|
||
|
sendOutBox.MBNO = outbox.MBNO;
|
||
|
sendOutBox.MSG = outbox.MSG;
|
||
|
sendOutBox.SENDTIME = outbox.SENDTIME;
|
||
|
sendOutBox.COMPORT = outbox.COMPORT;
|
||
|
sendOutBox.TOTAL = 1;
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
session.OpenTs();
|
||
|
|
||
|
messageDal.BaseSession = session;
|
||
|
//发送成功之后
|
||
|
//删除发件箱信息
|
||
|
messageDal.DeleteOutBox(outbox);
|
||
|
|
||
|
//插入已发送信息
|
||
|
messageDal.InsertSendOutBox(sendOutBox);
|
||
|
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
BadOutBox badOutBox = new BadOutBox();
|
||
|
|
||
|
#region
|
||
|
|
||
|
//badOutBox.ID = outbox.ID;
|
||
|
badOutBox.USERNAME = outbox.USERNAME;
|
||
|
badOutBox.MBNO = outbox.MBNO;
|
||
|
badOutBox.MSG = outbox.MSG;
|
||
|
badOutBox.SENDTIME = outbox.SENDTIME;
|
||
|
badOutBox.COMPORT = outbox.COMPORT;
|
||
|
badOutBox.BADWHY = "";
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
session.OpenTs();
|
||
|
|
||
|
messageDal.BaseSession = session;
|
||
|
//发送成功之后
|
||
|
//删除发件箱信息
|
||
|
messageDal.DeleteOutBox(outbox);
|
||
|
|
||
|
//插入已发送信息
|
||
|
messageDal.InsertBadOutBox(badOutBox);
|
||
|
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region 发送失败信息
|
||
|
|
||
|
foreach (var badoutbox in badOutBoxList)
|
||
|
{
|
||
|
//发送短信
|
||
|
bool sendFlag = true;
|
||
|
//using (ModemLib.SMS.SMSHelper sms = new ModemLib.SMS.SMSHelper(comport))
|
||
|
//{
|
||
|
// sendFlag = sms.Send(badoutbox.MBNO, badoutbox.MSG);
|
||
|
//}
|
||
|
|
||
|
if (sendFlag)
|
||
|
{
|
||
|
SendOutBox sendOutBox = new SendOutBox();
|
||
|
|
||
|
#region
|
||
|
|
||
|
//sendOutBox.ID = badoutbox.ID;
|
||
|
sendOutBox.USERNAME = badoutbox.USERNAME;
|
||
|
sendOutBox.MBNO = badoutbox.MBNO;
|
||
|
sendOutBox.MSG = badoutbox.MSG;
|
||
|
sendOutBox.SENDTIME = badoutbox.SENDTIME;
|
||
|
sendOutBox.COMPORT = badoutbox.COMPORT;
|
||
|
sendOutBox.TOTAL = 1;
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
session.OpenTs();
|
||
|
|
||
|
messageDal.BaseSession = session;
|
||
|
//发送成功之后
|
||
|
//删除发件箱信息
|
||
|
messageDal.DeleteBadOutBox(badoutbox);
|
||
|
|
||
|
//插入已发送信息
|
||
|
messageDal.InsertSendOutBox(sendOutBox);
|
||
|
|
||
|
session.CommitTs();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
}
|
||
|
}
|