namespace AutoSendMessage
{
class Program
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 后台监听主程序(自动发送短信)![]()
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void Main(string[] args)
{
ThreadStartLable:
Thread threadAutoSendMessage1 = new Thread(new ThreadStart(AutoSendMessage));//定义线程对象
//Thread threadAutoSendMessage2 = new Thread(new ThreadStart(AutoSendMessage));//定义线程对象
//Thread threadAutoSendMessage3 = new Thread(new ThreadStart(AutoSendMessage));//定义线程对象
//Thread threadAutoSendMessage4 = new Thread(new ThreadStart(AutoSendMessage));//定义线程对象
//Thread threadAutoSendMessage5 = new Thread(new ThreadStart(AutoSendMessage));//定义线程对象
//threadAutoSendMessage2.Start();//启动自动发送短信线程
//threadAutoSendMessage3.Start();//启动自动发送短信线程
//threadAutoSendMessage4.Start();//启动自动发送短信线程
//threadAutoSendMessage5.Start();//启动自动发送短信线程
/*if (threadAutoSendMessage.IsAlive == true)//线程挂了
{
threadAutoSendMessage.Abort();//KILL掉线程
//threadAutoSendMessage = new Thread(new ThreadStart(AutoSendMessage));//重新分配线程对象
//threadAutoSendMessage.Start();//重新启动线程
}*/
try
{
threadAutoSendMessage1.Start();//启动自动发送短信线程
}
catch
{
threadAutoSendMessage1.Abort();
goto ThreadStartLable;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 监听程序(自动发送短信)
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void AutoSendMessage()
{
long lTimes = 1;//标记监听次数
while (true)
{
Console.WriteLine("**************************************************************************\n");
Console.WriteLine("---- 后台监听程序(自动发送短信) -----\n");
Console.WriteLine("---- 第" + lTimes + "次监听 -----\n");
Console.WriteLine("---- 监听时间:" + System.DateTime.Now.ToString() + " -----\n");
Console.WriteLine("**************************************************************************\n");
//string strSQL = "select x.cf_czy,x.cf_huma,x.cf_humin,x.cf_tele,x.cf_qfje,x.cf_jfje,x.cf_bjtd_datetime,y.cf_dizi,y.cf_huzu" +
//" from dxjk_hfyd x,XJB_YHDA y where x.cf_huma=y.cf_huma and x.cf_bjtd_datetime > x.cf_bjhf_datetime";
//检索用户缴费情况表
string strSQL = "select x.cf_czy,x.cf_huma,x.cf_humin,x.cf_tele,x.cf_qfje,x.cf_jfdatetime,x.cf_sjtd_datetime,x.cf_jfje,x.cf_bjtd_datetime,y.cf_dizi," +
"y.cf_huzu from dxjk_hfyd x,XJB_YHDA y where x.cf_huma=y.cf_huma and x.cf_bjtd_datetime > x.cf_bjhf_datetime";
DbHelperOra.ConnectionString = ConfigurationManager.AppSettings["connectionstring"].ToString();//配置连接字符串
DataTable dt = DbHelperOra.Query(strSQL).Tables[0];//未完成的处理单
int second = System.DateTime.Now.Second;//系统时间-秒
foreach (DataRow dr in dt.Rows)//遍历每个处理单
{
string strLog = "";//写入的日志信息
string bjtdsj = dr["cf_bjtd_datetime"].ToString();
string sjtdsj = dr["cf_sjtd_datetime"].ToString();//实际停电时间
string jfsj = dr["cf_jfdatetime"].ToString();//用户缴费时间
string jfje = dr["cf_jfje"].ToString();//交费金额
/////////////////////////////////////////////////////////
//
// 发短给信催办员停电
//
////////////////////////////////////////////////////////
if (Convert.ToDateTime(bjtdsj) <= System.DateTime.Now)//停电期限到期
{
if (jfje != "")
{
if (Convert.ToDecimal(jfje) == 0)//还未交费
{
string czy = dr["cf_czy"].ToString();
//string phone = GetPhone(czy);
string phone = GetPhoneFromConfig(czy);
string huma = dr["cf_huma"].ToString();
string humin = dr["cf_humin"].ToString();
string address = dr["cf_dizi"].ToString();
string linkman = dr["cf_huzu"].ToString();
string tele = dr["cf_tele"].ToString();
string qfje = dr["cf_qfje"].ToString();
ShortMessageInfo smi = new ShortMessageInfo();
smi.cf_datetime = System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + second;
smi.cf_czy = czy;
smi.cf_tele = phone;
smi.cf_text = "请即刻对该用户作停电处理 用户信息:户码:" + huma + " 户名:" + humin + " 地址:" + address + " 电话:" + tele + " 欠费金额:¥" + qfje;
smi.cf_type = "停送电业务";
smi.cf_retele = phone;
smi.cf_retext = "请即刻对该用户作停电处理 用户信息:户码:" + huma + " 户名:" + humin + " 地址:" + address + " 电话:" + tele + " 欠费金额:¥" + qfje;
smi.cf_sms_subseq = qfje;
if (smi.cf_tele != "")
{
SendShortMessage(smi, false);//给催办员发送短信
strLog = "【系统日期:" + System.DateTime.Now.ToString() + " 系统发送催办员信息:催办员:" + czy +
", 手机号码:" + phone + ", 内容:对户码:" + huma + " 户名:" + humin + " 作停电处理】";
WriteToLog(strLog);//记录日志信息
}
second += 1;
if (second > 59)//秒只能再0-59之间
second = 0;
}
}
}
/////////////////////////////////////////////////////////
//
// 发短信催办员供电
//
/////////////////////////////////////////////////////////
if (sjtdsj != "" && jfsj != "" && jfje != "")
{
DateTime dtSJDTSJ = Convert.ToDateTime(sjtdsj);
DateTime dtJFSJ = Convert.ToDateTime(jfsj);
decimal dJSJE = Convert.ToDecimal(jfje);
if (dtJFSJ > dtSJDTSJ && dJSJE > 0)//用户交费时间大于停电时间并且缴费金额大于0 表示已交费
{
string czy = dr["cf_czy"].ToString();
//string phone = GetPhone(czy);
string phone = GetPhoneFromConfig(czy);
string huma = dr["cf_huma"].ToString();
string humin = dr["cf_humin"].ToString();
string address = dr["cf_dizi"].ToString();
string linkman = dr["cf_huzu"].ToString();
string tele = dr["cf_tele"].ToString();
string qfje = dr["cf_qfje"].ToString();
string fssj = System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + second;
ShortMessageInfo smi = new ShortMessageInfo();
smi.cf_datetime = fssj;
smi.cf_czy = czy;
smi.cf_tele = phone;
smi.cf_text = "请即刻对该用户作恢复供电处理 用户信息:户码:" + huma + " 户名:" + humin + " 地址:" + address + " 电话:" + tele + " 欠费金额:¥" + qfje;
smi.cf_type = "停送电业务";
smi.cf_retele = phone;
smi.cf_retext = "请即刻对该用户作恢复供电处理 用户信息:户码:" + huma + " 户名:" + humin + " 地址:" + address + " 电话:" + tele + " 欠费金额:¥" + qfje;
smi.cf_sms_subseq = qfje;
SendMessageLogInfo sml = new SendMessageLogInfo();
sml.czy = smi.cf_czy;
sml.hm = huma;
sml.jfsj = jfsj;
//if (sendShortMessageTimes < 3)//发3次
//{
if (smi.cf_tele != "")
{
if (CheckSendMessageLogBeforeInsert(sml) == true)//是否可以发送短信
{
SendShortMessage(smi, true);//给催办员发送短信
strLog = "【系统日期:" + System.DateTime.Now.ToString() + " 系统发送催办员信息:催办员:" + czy +
", 手机号码:" + phone + ", 内容:对户码:" + huma + " 户名:" + humin + " 作供电恢复电处理】";
WriteToLog(strLog);//记录日志信息
}
}
//}
second += 1;
if (second > 59)//秒只能再0-59之间
second = 0;
}//endif
}//endif
}//endforeach
lTimes += 1;
Thread.Sleep(10 * 1000);//每隔1分钟监听一次
}//endwhile
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 读取TXT文件找到催办员对应的手机号码
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static string GetPhone(string czy)
{
string path = Environment.CurrentDirectory;
FileStream fs = new FileStream("phone.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
string phone = "";
string str = sr.ReadLine();
while (str != null)
{
if (str.Contains(czy))
{
string[] a = str.Split('|');
phone = a[1];
}
str = sr.ReadLine();
}
sr.Close();
fs.Close();
return phone;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 读取App.Config文件找到催办员对应的手机号码
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static string GetPhoneFromConfig(string czy)
{
if (ConfigurationManager.AppSettings[czy] == null)
return "";
else
return ConfigurationManager.AppSettings[czy].ToString().Trim();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 发送短信
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void SendShortMessage(ShortMessageInfo smi,bool isSendRepeat)
{
if (isSendRepeat == true)//重复发送短信
{
string strSQL = "insert into AYSMS_ALARM_LSXX (CF_DATETIME,CF_TELE,CF_TEXT,CF_RETELE,CF_RETEXT,CF_CZY,CF_TYPE,CF_OK,CF_SMS_SUBSEQ,CF_SMS_TIME,CF_SMS_STATE,CF_SMS_ID) values(" +
"to_date('" + smi.cf_datetime + "','yyyy-mm-dd hh24:mi:ss'),'" + smi.cf_tele + "','" + smi.cf_text + "','" + smi.cf_retele + "','" + smi.cf_retext + "','" + smi.cf_czy +
"','" + smi.cf_type + "','" + smi.cf_ok + "','" + smi.cf_sms_subseq + "','" + smi.cf_sms_time + "','" + smi.cf_sms_state + "'," + smi.cf_sms_id + ")";
DbHelperOra.ExecuteSql(strSQL);
}
else//不重复发送短信
{
if (IsExistShortMessage(smi.cf_text) == false)//检查是否发过信息
{
string strSQL = "insert into AYSMS_ALARM_LSXX (CF_DATETIME,CF_TELE,CF_TEXT,CF_RETELE,CF_RETEXT,CF_CZY,CF_TYPE,CF_OK,CF_SMS_SUBSEQ,CF_SMS_TIME,CF_SMS_STATE,CF_SMS_ID) values(" +
"to_date('" + smi.cf_datetime + "','yyyy-mm-dd hh24:mi:ss'),'" + smi.cf_tele + "','" + smi.cf_text + "','" + smi.cf_retele + "','" + smi.cf_retext + "','" + smi.cf_czy +
"','" + smi.cf_type + "','" + smi.cf_ok + "','" + smi.cf_sms_subseq + "','" + smi.cf_sms_time + "','" + smi.cf_sms_state + "'," + smi.cf_sms_id + ")";
DbHelperOra.ExecuteSql(strSQL);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 检查已发过短信
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static bool IsExistShortMessage(string content)
{
string strSQL = "select * from AYSMS_ALARM_LSXX where cf_text='" + content + "'";
return DbHelperOra.Query(strSQL).Tables[0].Rows.Count > 0 ? true : false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 将监听的信息写入日志文件
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void WriteToLog(string strContent)
{
FileStream fs = new FileStream("log.txt", FileMode.Open, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.Begin);
sw.WriteLine(strContent);
sw.Close();
fs.Close();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 自动生成编号
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int GetMaxIDFromSendMessageLog()
{
string strSQL = "select max(id) as id from SENDMESSAGELOG";
DataSet ds = DbHelperOra.Query(strSQL);
if (ds.Tables[0].Rows[0]["id"].ToString() == "")
{
return 1;
}
else
{
return Convert.ToInt32(ds.Tables[0].Rows[0]["id"].ToString()) + 1;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 判断是否可以再次发送短信,返回true表示可以发送,返回false表示不可以发送
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static bool CheckSendMessageLogBeforeInsert(SendMessageLogInfo sml)
{
string strSQL = "select * from SENDMESSAGELOG where czy='" + sml.czy + "' and jfsj=to_date('" + sml.jfsj + "','yyyy-mm-dd hh24:mi:ss') and hm='" + sml.hm + "'";
DataSet ds = DbHelperOra.Query(strSQL);
if (ds.Tables[0].Rows.Count < 1)//没有记录数,表示系统第一次给催办员发送短信
{
sml.fscs = 1;
sml.fssj = System.DateTime.Now.ToString();//登记第一次发送的系统时间
InsertSendMessageLog(sml);
return true;
}
else//存在记录数,表示系统已经给催办员发送短信,如果小于3次,则重复发送
{
if (ds.Tables[0].Rows.Count == 1)//存在一条记录,表示发送过一次
{
DateTime lastFSSJ = Convert.ToDateTime(ds.Tables[0].Rows[0]["fssj"].ToString());//获得上次发送短信的系统时间
DateTime currFSSj = System.DateTime.Now;//本次系统时间
//if (lastFSSJ.AddHours(1) >= currFSSj)//间隔大于等于1个小时再发送否则不发送
if (lastFSSJ.AddMinutes(1) >= currFSSj)//间隔大于等于1个小时再发送否则不发送
{
sml.fscs = 2;
sml.fssj = currFSSj.ToString();
InsertSendMessageLog(sml);
return true;
}
else//间隔小于1个小时不发送
{
return false;
}
}
else if (ds.Tables[0].Rows.Count == 2)//存在两条记录,表示发送过两次
{
DateTime lastFSSJ = Convert.ToDateTime(ds.Tables[0].Rows[1]["fssj"].ToString());//获得上次发送短信的系统时间
DateTime currFSSj = System.DateTime.Now;//本次系统时间
//if (lastFSSJ.AddHours(1) >= currFSSj)//间隔大于等于1个小时再发送否则不发送
if (lastFSSJ.AddMinutes(1) >= currFSSj)//间隔大于等于1个小时再发送否则不发送
{
sml.fscs = 3;
sml.fssj = currFSSj.ToString();
InsertSendMessageLog(sml);
return true;
}
else//间隔小于1个小时不发送
{
return false;
}
}
else
{
return false;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 写入到短信日志登记表中(短信日志登记表、该表标记是否重复发送短信)
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static bool InsertSendMessageLog(SendMessageLogInfo sml)
{
string strSQL = "insert into SENDMESSAGELOG (id,czy,jfsj,hm,fscs,fssj) values (" + GetMaxIDFromSendMessageLog() +
",'" + sml.czy + "',to_date('" + sml.jfsj + "','yyyy-mm-dd hh24:mi:ss'),'" + sml.hm +
"'," + sml.fscs + ",to_date('" + sml.fssj + "','yyyy-mm-dd hh24:mi:ss'))";
return DbHelperOra.ExecuteSql(strSQL) > 0 ? true : false;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 映射短信日志登记表中
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class SendMessageLogInfo
{
public int id;//编号
public string czy;//催办员
public string jfsj;//交费时间
public string hm;//户码
public int fscs;//发送次数如果小于3次,则重复发送
public string fssj;//系统发送时间
}
}![]()
浙公网安备 33010602011771号