コード例 #1
0
        private static void WorkProcessor()
        {
            try
            {
                TimeSpan tsTimeout = TimeSpan.FromSeconds(Convert.ToDouble(transactionTimeout * batchSize));
                SmsSend  smsBll    = new SmsSend();

                while (true)
                {
                    TimeSpan datetimeStarting = new TimeSpan(DateTime.Now.Ticks);
                    double   elapsedTime      = 0;
                    int      processedItems   = 0;

                    List <SmsSendInfo> smsList = new List <Model.SmsSendInfo>();
                    SmsTemplate        smstBll = new SmsTemplate();
                    Order oBll = new Order();

                    for (int j = 0; j < batchSize; j++)
                    {
                        try
                        {
                            if ((elapsedTime + queueTimeout + transactionTimeout) < tsTimeout.TotalSeconds)
                            {
                                smsList.Add(smsBll.ReceiveFromQueue(queueTimeout));
                            }
                            else
                            {
                                j = batchSize;   // exit loop
                            }

                            //update elapsed time
                            elapsedTime = new TimeSpan(DateTime.Now.Ticks).TotalSeconds - datetimeStarting.TotalSeconds;
                        }
                        catch (TimeoutException)
                        {
                            //exit loop because no more messages are waiting
                            j = batchSize;
                        }
                    }

                    if (smsList.Count > 0)
                    {
                        foreach (var smsModel in smsList)
                        {
                            try
                            {
                                if (!string.IsNullOrEmpty(smsModel.MobilePhone))
                                {
                                    string sendContent = "";

                                    Guid templateId = Guid.Empty;
                                    if (smsModel.SmsTemplateId != null)
                                    {
                                        Guid.TryParse(smsModel.SmsTemplateId.ToString(), out templateId);
                                    }
                                    if (!templateId.Equals(Guid.Empty))
                                    {
                                        #region 使用模板发送

                                        sendContent = smstBll.GetTemplateContent(templateId, "");

                                        if (sendContent == "")
                                        {
                                            WriteLog log = new WriteLog();
                                            log.Write("手机号:“" + smsModel.MobilePhone + "”,使用了模板(ID:" + templateId + "),但是模板不存在或已被删除,无法发送短信");
                                            continue;
                                        }

                                        #endregion
                                    }
                                    else if (!string.IsNullOrEmpty(smsModel.SmsContent))
                                    {
                                        sendContent = smsModel.SmsContent;
                                    }

                                    if (string.IsNullOrEmpty(sendContent))
                                    {
                                        WriteLog log = new WriteLog();
                                        log.Write("手机号:“" + smsModel.MobilePhone + "”,短信内容为空,无法发送短信");
                                        continue;
                                    }

                                    //发送短信并写入数据库
                                    smsModel.SmsContent = sendContent;
                                    SmsSendAndInsert(smsModel, null);
                                }

                                else if (!string.IsNullOrEmpty(smsModel.OrderCode))
                                {
                                    DataTable dt         = null;
                                    string    sendMobile = "";

                                    smsBll.GetSmsSendMobile(smsModel.OrderCode, smsModel.TranNode, out sendMobile, out dt);

                                    if (string.IsNullOrEmpty(sendMobile))
                                    {
                                        WriteLog log = new WriteLog();
                                        log.Write("订单号:" + smsModel.OrderCode + ",运输环节:" + smsModel.TranNodeText + ",异常:发货方或收货方手机号为空值或无效,无法发送短信");
                                        continue;
                                    }

                                    string[]      mobiles     = sendMobile.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    List <string> contentList = GetSmsContent(smsModel, dt);
                                    if (contentList.Count != mobiles.Length)
                                    {
                                        WriteLog log = new WriteLog();
                                        log.Write("订单号:" + smsModel.OrderCode + ",运输环节:" + smsModel.TranNodeText + ",异常:发货方或收货方短信模板找不到或已被删除,无法发送短信");
                                        continue;
                                    }

                                    for (var i = 0; i < mobiles.Length; i++)
                                    {
                                        smsModel.MobilePhone = mobiles[i];
                                        smsModel.SmsContent  = contentList[i];

                                        SmsSendAndInsert(smsModel, dt);
                                    }
                                }
                                else if (!string.IsNullOrEmpty(smsModel.CarScanCode))
                                {
                                    string[] orders = oBll.GetOrderByCarcode(smsModel.CarScanCode);
                                    if (orders.Length == 0)
                                    {
                                        WriteLog log = new WriteLog();
                                        log.Write("派车单号:" + smsModel.CarScanCode + ",没有任何订单号,无法发送短信");
                                        continue;
                                    }
                                    foreach (string orderCode in orders)
                                    {
                                        DataTable dt         = null;
                                        string    sendMobile = "";

                                        smsBll.GetSmsSendMobile(orderCode, smsModel.TranNode == null ? "" : smsModel.TranNode, out sendMobile, out dt);

                                        if (string.IsNullOrEmpty(sendMobile))
                                        {
                                            WriteLog log = new WriteLog();
                                            log.Write("派车单号:“" + smsModel.CarScanCode + "”,订单号:" + orderCode + ",异常:发货方或收货方手机号为空值或无效,无法发送短信");
                                            continue;
                                        }

                                        string[]      mobiles     = sendMobile.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                        List <string> contentList = GetSmsContent(smsModel, dt);
                                        if (contentList.Count != mobiles.Length)
                                        {
                                            WriteLog log = new WriteLog();
                                            log.Write("订单号:" + smsModel.OrderCode + ",运输环节:" + smsModel.TranNodeText + ",异常:发货方或收货方短信模板找不到或已被删除,无法发送短信");
                                            continue;
                                        }

                                        for (var i = 0; i < mobiles.Length; i++)
                                        {
                                            smsModel.MobilePhone = mobiles[i];
                                            smsModel.SmsContent  = contentList[i];

                                            SmsSendAndInsert(smsModel, dt);
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                WriteLog log         = new WriteLog();
                                string   errorAppend = "";
                                if (!string.IsNullOrEmpty(smsModel.MobilePhone))
                                {
                                    errorAppend += "手机号:" + smsModel.MobilePhone + "";
                                }
                                if (!string.IsNullOrEmpty(smsModel.OrderCode))
                                {
                                    errorAppend += "订单号:" + smsModel.OrderCode + "";
                                }
                                if (!string.IsNullOrEmpty(smsModel.CarScanCode))
                                {
                                    errorAppend += "派车单号:" + smsModel.CarScanCode + "";
                                }
                                log.Write("异常:" + ex.Message + "," + errorAppend + "");
                            }

                            processedItems++;
                        }
                    }

                    Thread.Sleep(2000);
                }
            }
            catch (Exception ex)
            {
                WriteLog log = new WriteLog();
                log.Write(ex.Message);
            }
        }
コード例 #2
0
        private static void WorkProcessor()
        {
            Random   rnd         = new Random();
            bool     isPassNight = false;             //是否已到深夜0点
            DateTime startTime   = DateTime.MinValue; //开始时间
            DateTime endTime     = DateTime.MinValue; //结束时间

            try
            {
                TyUser  bll   = new TyUser();
                SysEnum seBll = new SysEnum();

                while (true)
                {
                    if (!isPassNight)
                    {
                        if (DateTime.Now.Hour == 0)
                        {
                            isPassNight = true;
                        }
                    }

                    List <SysEnumInfo> seList     = seBll.GetList("and t2.EnumCode = 'UserProcessor'");
                    double             runTimeout = 0;
                    double.TryParse(seList.Find(m => m.EnumCode == "RunTimeout").EnumValue.Trim(), out runTimeout);
                    bool isOff = seList.Find(m => m.EnumCode == "On/Off").EnumValue.Trim().ToLower() == "off" ? true : false;
                    if (isOff)
                    {
                        Thread.Sleep(5000);
                        continue;
                    }

                    string sStartTime = seList.Find(m => m.EnumCode.Trim() == "StartTime").EnumValue;
                    string sEndTime   = seList.Find(m => m.EnumCode.Trim() == "EndTime").EnumValue;

                    if ((sStartTime.IndexOf(':') != -1) && (sEndTime.IndexOf(':') != -1))
                    {
                        DateTime currTime = DateTime.Now;
                        sStartTime = string.Format("{0} {1}", currTime.ToString("yyyy-MM-dd"), sStartTime);
                        sEndTime   = string.Format("{0} {1}", currTime.ToString("yyyy-MM-dd"), sEndTime);

                        DateTime.TryParse(sStartTime, out startTime);
                        DateTime.TryParse(sEndTime, out endTime);
                        DateTime maxTime = DateTime.Parse(string.Format("{0} {1}", currTime.ToString("yyyy-MM-dd"), "23:59:59"));

                        if ((startTime != DateTime.MinValue) && (endTime != DateTime.MinValue))
                        {
                            if (currTime >= endTime || currTime <= startTime)
                            {
                                Thread.Sleep(5000);
                                continue;
                            }
                        }
                    }

                    #region  步用户表开始

                    TyUser tyuserBll = new TyUser();

                    List <string>     newUsers = new List <string>();
                    string[]          oldUsers = tyuserBll.GetTyUsers();
                    List <TyUserInfo> userList = tyuserBll.GetList();

                    foreach (string userName in oldUsers)
                    {
                        if (!userList.Exists(m => m.UserName == userName))
                        {
                            newUsers.Add(userName);
                        }
                    }

                    if (newUsers.Count > 0)
                    {
                        using (TransactionScope scope = new TransactionScope())
                        {
                            foreach (string userName in newUsers)
                            {
                                string psw = (rnd.NextDouble() * Int32.MaxValue).ToString().PadLeft(6, '0').Substring(0, 6);
                                Membership.CreateUser(userName, psw, "" + userName + "@tygaweb.com");
                                Roles.AddUserToRole(userName, "Users");
                                tyuserBll.Insert(new TyUserInfo {
                                    UserName = userName, Password = psw, IsEnable = true, LastUpdatedDate = DateTime.Now
                                });
                            }

                            scope.Complete();
                        }
                    }

                    #endregion

                    if (runTimeout > 0)
                    {
                        TimeSpan ts = DateTime.Now.AddMinutes(runTimeout) - DateTime.Now;
                        Thread.Sleep(ts);
                    }
                    else
                    {
                        Thread.Sleep(5000);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog log = new WriteLog();
                log.Write(ex.Message);
            }
        }
コード例 #3
0
        private static List <string> GetSmsContent(SmsSendInfo smsModel, DataTable dt)
        {
            List <string> list    = new List <string>();
            string        content = "";

            try
            {
                Guid templateId = Guid.Empty;
                if (smsModel.SmsTemplateId != null)
                {
                    Guid.TryParse(smsModel.SmsTemplateId.ToString(), out templateId);
                }
                if (!templateId.Equals(Guid.Empty))
                {
                    SmsTemplate smstBll = new SmsTemplate();
                    content = smstBll.GetTemplateContent(templateId, dt);
                    list.Add(content);
                }
                else if (!string.IsNullOrEmpty(smsModel.TemplateType))
                {
                    if (smsModel.TemplateType == "auto")
                    {
                        DataRow dr = dt.Rows[0];
                        DataColumnCollection dcc = dt.Columns;

                        string[] paramsCodeArr = smsModel.ParamsCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        string[] tParmsValues  = new string[paramsCodeArr.Length];
                        for (int i = 0; i < paramsCodeArr.Length; i++)
                        {
                            if (dcc.Contains(paramsCodeArr[i]))
                            {
                                if (dr[paramsCodeArr[i]] != DBNull.Value)
                                {
                                    tParmsValues[i] = dr[paramsCodeArr[i]].ToString();
                                }
                                else
                                {
                                    tParmsValues[i] = "";
                                }
                            }
                            else
                            {
                                tParmsValues[i] = "";
                            }
                        }

                        content = string.Format(smsModel.SmsContent, tParmsValues);
                        list.Add(content);
                    }
                    else if (smsModel.TemplateType == "custom")
                    {
                        content = string.Format(smsModel.SmsContent, smsModel.ParamsValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                        list.Add(content);
                    }
                }
                else if (!string.IsNullOrEmpty(smsModel.SmsContent))
                {
                    content = smsModel.SmsContent;
                    list.Add(content);
                }
                else
                {
                    SmsTemplate smstBll = new SmsTemplate();
                    return(smstBll.GetTemplateContentByTranNode(dt, smsModel.TranNode));
                }
            }
            catch (Exception ex)
            {
                string error = "";
                error += "手机号:" + smsModel.MobilePhone == null ? "无" : smsModel.MobilePhone + ",";
                error += "订单号:" + smsModel.OrderCode == null ? "无" : smsModel.OrderCode + ",";
                error += "运输节点:" + smsModel.TranNodeText == null ? "无" : smsModel.TranNodeText + ",";
                error += "派车单号:" + smsModel.CarScanCode == null ? "无" : smsModel.CarScanCode + ",";
                error += "模板ID:" + smsModel.SmsTemplateId == null ? "无" : smsModel.SmsTemplateId.ToString() + ",";
                error += "参数代码:" + smsModel.ParamsCode == null ? "无" : smsModel.ParamsCode + ",";
                error += "参数名称:" + smsModel.ParamsName == null ? "无" : smsModel.ParamsName + ",";
                error += "参数值:" + smsModel.ParamsValue == null ? "无" : smsModel.ParamsValue + ",";
                error += "短信内容:" + smsModel.SmsContent == null ? "无" : smsModel.SmsContent;
                WriteLog log = new WriteLog();
                log.Write("执行获取短信内容时异常(" + ex.Message + "),详情:" + error + "");
            }

            return(list);
        }