用Timer定时发送短信,调用webserver发短信

   protected override void OnStart(string[] args)
        {
            atimer = new System.Timers.Timer(10000);
            atimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
            atimer.Interval = 1000;
            atimer.Enabled = true;
            Console.ReadLine();
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }
        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            atimer.Enabled = false;  //Add by equn 防止该过程被多次执行
            try
            {
                WFIT.BLL.SendMessageTime bllsendtime = new WFIT.BLL.SendMessageTime();
                DataTable dt = bllsendtime.GetSendMesTime().Tables[0];
                DateTime dttest = DateTime.Now;
                if (dt.Rows.Count > 0)
                {
                    DateTime SendTime = Convert.ToDateTime(dt.Rows[0]["SendTime"].ToString());
                    DateTime dtnow = DateTime.Now;


                    if (dtnow.ToString("yyyy-MM-dd hh:mm") == SendTime.ToString("yyyy-MM-dd hh:mm"))
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            int ApplicationTypeID = Convert.ToInt32(dr["ApplicationTypeID"].ToString());

                            SendMessage(ApplicationTypeID);
                        }

                    }
                    System.TimeSpan ts = SendTime.Subtract(dtnow);
                    if (ts.TotalSeconds > 0)
                    {
                        atimer.Interval = ts.TotalSeconds * 1000;
                    }
                    else
                    {
                        atimer.Interval = 60000;
                    }
                }
                else
                {
                    atimer.Interval = 60000;
                }
            }
            finally
            {
                atimer.Enabled = true;
            }

        }
        private DataTable CreateDT()
        {
            DataTable dt = null;
            dt = new DataTable();
            dt.Columns.Add("ReceivePerson", System.Type.GetType("System.String"));
            dt.Columns.Add("PhoneNumber", System.Type.GetType("System.String"));
            return dt;
        }
        private void AddDTRow(string ReceivePerson, string PhoneNumber, ref DataTable dt)
        {
            DataRow dr = null;
            dr = dt.NewRow();
            dr[0] = ReceivePerson;
            dr[1] = PhoneNumber;
            dt.Rows.Add(dr);
        }
        public void SendMessage(int TypeID)
        {
            try
            {
                //string strbody = "Staff Web短信提醒"+TypeID.ToString();
                //string telphone = "13760538565";
                //DataTable dtSentPhone = CreateDT();
                //AddDTRow("011960", telphone, ref dtSentPhone);
                //AddDTRow("011960", "13750307426", ref dtSentPhone);
                //DataSet dtset = new DataSet();
                //dtset.Tables.Add(dtSentPhone);
                //SMSServiceApply.SMSServicesSoapClient ss = new WinService.SMSServiceApply.SMSServicesSoapClient();
                //ss.SendSMSMessageByDefaultAccount(strbody, "", dtset);

                WFIT.BLL.ApplicationFormAccount bll = new WFIT.BLL.ApplicationFormAccount();
                DataTable dt = bll.GetHRAlertTable(TypeID);
                DataTable dtdistinct = dt.DefaultView.ToTable(true, new string[] { "AgentAccount" });

                if (dtdistinct.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtdistinct.Rows)
                    {
                        DataRow[] drAccAll = dt.Select("AgentAccount='" + dr["AgentAccount"].ToString() + "'", "IsAgent,ATypeID");
                        int flag1 = 0;
                        int flag2 = 0;
                        string strbody = "[Staff Web提醒]";
                        foreach (DataRow drow in drAccAll)
                        {
                            if (flag1 == 0 && drow["IsAgent"].ToString() == "0")
                            {
                                strbody += "等待您审批的申请单:";
                                flag1 = 1;
                            }
                            if (flag2 == 0 && drow["IsAgent"].ToString() == "1")
                            {
                                strbody += "您可代理审批的申请单:";
                                flag2 = 1;
                            }
                            strbody += drow["ATypeName"].ToString() + ":" + drow["ACount"].ToString() + "条,";
                        }
                        strbody += "请访问http://staff.wwtt.hk 登录进入个人信息中心审批";

                        WFIT.BLL.WFEmpInfo bllWFE = new WFIT.BLL.WFEmpInfo();
                        WFIT.Model.WFEmpInfo modelWFE = new WFIT.Model.WFEmpInfo();
                        modelWFE = bllWFE.GetModel(dr["AgentAccount"].ToString());
                        if (modelWFE == null || modelWFE.Telphone == null || modelWFE.Telphone == "")
                        {
                            continue;
                        }

                        string telphone = modelWFE.Telphone;
                        //*********lxl添加**********
                        DataTable dtSentPhone = CreateDT();
                        AddDTRow(dr["AgentAccount"].ToString(), telphone, ref dtSentPhone);
                        //AddDTRow("011960", "13750307426", ref dtSentPhone);
                        DataSet dtset = new DataSet();
                        dtset.Tables.Add(dtSentPhone);

                        //************

                        SMSServiceApply.SMSServicesSoapClient ss = new WinService.SMSServiceApply.SMSServicesSoapClient();
                        ss.SendSMSMessageByDefaultAccount(strbody, "", dtset);
                    }
                }
            }
            catch //(Exception ex)
            {
                //
            }
        }
        public void start(string[] args)
        {
            this.OnStart(args);
        }
        public void stop()
        {
            this.OnStop();
        }

posted @ 2012-10-19 11:29  洗耳恭听兼烂笔头  阅读(462)  评论(0编辑  收藏  举报