110,578
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CommonLib;
namespace SmsSender
{
public partial class FormMain : CommonLib.FormBase
{
private SmsBase sms;
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
this.mobilePhones = new string[1]; //手机号码数组(目前只能装一个元素)
mobilePhones[0] = string.Empty;
tmr.Interval = Int32.Parse(FileConfig.GetConfigValue("TimerInterval")) * 1000;
int smsVersion = Int32.Parse(FileConfig.GetConfigValue("SmsVersion"));
switch (smsVersion)
{
case 1:
sms = new SmsV1();
break;
case 2:
sms = new SmsV2();
break;
default:
Common.MsgStop2(string.Format("未定义的短信接口版本[{0}]。", smsVersion));
sms = new SmsBase();
break;
}
}
private void ShowInfo(string info)
{
if (edInfo.Lines.Length > 2000)
edInfo.Text = string.Empty;
if (info != "")
edInfo.AppendText(DateTime.Now.ToString() + " " + info + "\r\n");
else
edInfo.AppendText("\r\n");
}
private void tmr_Tick(object sender, EventArgs e)
{
tmr.Enabled = false;
if (CheckSmsService())
{
if (Int32.Parse(FileConfig.GetConfigValue("SmsVersion")) == 2)// add by tansy 2010 7 29
{
RecSmsMessage();
SmsOperation();
SendSmsMessage();
}
if (Int32.Parse(FileConfig.GetConfigValue("SmsVersion")) == 1)
{
SendSmsMessage();
}
if (Int32.Parse(FileConfig.GetConfigValue("SmsVersion")) == 0)
{
return;
}
}
tmr.Enabled = true;
}
//检查短信接口工作是否正常
private bool CheckSmsService()
{
ShowInfo("检测短信接口......");
if(sms.CheckInterface())
{
ShowInfo("短信接口工作正常");
return true;
}
else
{
ShowInfo(sms.ErrInfo);
Common.WriteLog(sms.ErrInfo);
return false;
}
}
//发送短信
DataTable table = null;
int recID; //待发信息表记录ID
string[] mobilePhones; //手机号码数组(目前只能装一个元素)
string smsMessage; //待发的短信内容
private void SendSmsMessage()
{
//检索待发短信(发送失败次数超过3次的短信,将不再发送)
ShowInfo("检索待发短信......");
string commandText = @"SELECT * FROM SmsMessage WHERE SendTime IS NULL AND RetryTimes <= 3 and SendMessage <> ''"; //and SendMessage='"+string.Empty+"' ";
if (CommonDB.OpenSql(commandText, ref table) != MyConst.RET_OK)
{
ShowInfo("检索待发短信失败:" + CommonDB.ErrorInfo);
Common.WriteLog("检索待发短信失败:" + CommonDB.ErrorInfo);
return;
}
if (table.Rows.Count < 1)
{
ShowInfo("没有需要发送的短信");
return;
}
//循环发送待发短信
string msgInfo;
foreach (DataRow row in table.Rows)
{
recID = row.Field<int>("RecID");
this.mobilePhones[0] = row.Field<string>("MobilePhone").Trim();
this.smsMessage = row.Field<string>("SendMessage").Trim();
msgInfo = string.Format(@"[手机号] {0} [待发短信] {1}......", this.mobilePhones[0], this.smsMessage.Substring(0, 20));
//手机号或待发短信有一个为空,则跳过该待发记录
if (this.mobilePhones[0] == string.Empty || this.smsMessage == string.Empty)
{
ShowInfo(msgInfo + "跳过");
this.MarkOK();
continue;
}
//正式发送短信
if(sms.Send(this.mobilePhones, this.smsMessage))
{
ShowInfo(msgInfo + "成功");
this.MarkOK();
}
else
{
ShowInfo(msgInfo + "失败");
Common.WriteLog(sms.ErrInfo);
this.MarkFail();
}
}
}
//接收短信
private void RecSmsMessage()
{
ShowInfo("正在监听用户短信......");
string Smessage = string.Empty;
sms.Receive(out Smessage);
if (Smessage !=null)
{
char[] splitchar = { '+', ',' };
string[] ArryMessage;
ArryMessage = Smessage.Split(splitchar);
ShowInfo("接收到" +(ArryMessage.Length/2) + "条短信,正在保存......");
for (int m = 0; m < ArryMessage.Length; m++)
{
if (m % 2 == 0)//ArryMessage[偶数],ArryMessage[偶数+1]分别存储了手机号,手机短信
{
string StrCount = @"select * from Customer A,Card B where A.MobilePhone='" + ArryMessage[m] + "'and A.State in ('1','2') and B.CustomerID=A.CustomerID";
//int exist =CommonDB.ExecSQLGetCount(StrCount);//判断是否有记录
DataTable countdt = null;
CommonDB.OpenSql(StrCount, ref countdt);
int exist = countdt.Rows.Count;
if (exist != 0)
{
string Selectinfo = @"select A.PhysicalCode,A.CardID,B.CustomerID from Card A,Customer B where B.MobilePhone='"+ArryMessage[m]+"' and A.State=B.State and A.CustomerID=B.CustomerID";//1取出表中相关信息记录;
DataTable dt = null;
CommonDB.OpenSql(Selectinfo,ref dt);
string Insertinfo = @"insert into SmsMessage(ArisesTime,PhysicalCode,CardID,CustomerID,MobilePhone,RetryTimes,RecvMessage) values('" + DateTime.Now + "','" + dt.Rows[0].ItemArray[0].ToString() + "','" + dt.Rows[0].ItemArray[1].ToString() + "','" + dt.Rows[0].ItemArray[2].ToString() + "','" + ArryMessage[m] + "','0','" + ArryMessage[m + 1] + "')";
if (CommonDB.ExecSQL(Insertinfo) == MyConst.RET_OK)//2插入表SmsMessage;
{
if (m/2==0)
ShowInfo("第"+(m+1)+"条短信已经成功保存");
else
ShowInfo("第" + (m/2+1) + "条短信已经成功保存");
}
else
{
continue;
}
}
else
{
DataTable nobinddt = null;
string sqlnobind = "select Content from SmsMessageBoard where Code='NO_BINDING'";
CommonDB.OpenSql(sqlnobind, ref nobinddt);
string[] ph={ArryMessage[m].ToString()};
sms.Send(ph, nobinddt.Rows[0].ItemArray[0].ToString());//发短信通知用户无手机记录,先到一卡通中心登记手机号
}
}
else//跳过
{
continue;
}
}
}
else
{
ShowInfo("未有新短信发送过来");
}
}
//标记记录已经发送成功
private bool MarkOK()
{
string commandText = @"UPDATE SmsMessage SET SendTime = '{0}' WHERE RecID = {1}";
commandText = string.Format(commandText, DateTime.Now, recID);
bool ret = (CommonDB.ExecSQL(commandText) == MyConst.RET_OK);
if (!ret)
{
ShowInfo("更新记录行失败:" + CommonDB.ErrorInfo);
Common.WriteLog("更新记录行失败(MarkOK):" + CommonDB.ErrorInfo);
}
return ret;
}
//标记记录发送失败
private bool MarkFail()
{
string commandText = @"UPDATE SmsMessage SET RetryTimes = RetryTimes + 1 WHERE RecID = {0}";
commandText = string.Format(commandText, recID);
bool ret = (CommonDB.ExecSQL(commandText) == MyConst.RET_OK);
if (!ret)
{
ShowInfo("更新记录行失败:" + CommonDB.ErrorInfo);
Common.WriteLog("更新记录行失败(MarkFail):" + CommonDB.ErrorInfo);
}
return ret;
}