Ejemplo n.º 1
0
        // private IArticleInfoService _articleInfoService = EngineContext.Current.Resolve<IArticleInfoService>();

        public void Execute()
        {
            log.Debug("TimerMessageJob Execute Start");

            //查询7分钟之内的待发消息
            DateTime dtStart = DateTime.Now.AddMinutes(-2);
            DateTime dtEnd   = DateTime.Now.AddSeconds(30);

            Expression <Func <ArticleInfo, bool> > predicate = (a) => a.IsDeleted == false && a.ArticleStatus != "Published" && a.ScheduleSendTime > dtStart && a.ScheduleSendTime < dtEnd;
            var list = articleInfoService.GetListWithContent <ArticleInfoView>(predicate, new Infrastructure.Utility.Data.PageCondition()
            {
                PageSize = 100, PageIndex = 1
            });

            log.Debug("TimerMessageJob Execute:{0}", list.Count);
            System.Net.IPAddress myIPAddress = (System.Net.IPAddress)System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).Where(aa => aa.AddressFamily.ToString() == "InterNetwork").ToList().FirstOrDefault();

            //已经发送成功的信息不会重新发布
            list.RemoveAll(a => lstSent.Exists(b => b == a.Id));

            //保存到已经发送列表
            lstSent.AddRange(list.Select(aa => aa.Id).ToArray());

            //消息分组
            var listArticle = list.GroupBy(a => a.ArticleCode);

            foreach (var a in listArticle)
            {
                //消息排序
                var lstTemp = a.ToList().OrderBy(c => c.OrderID).ToList();
                var content = JsonConvert.DeserializeObject <List <NewsInfoView> >(lstTemp[0].ArticleContentEdit);
                lstTemp.ForEach(b => b.NewsInfo = content.Find(c => c.Id == b.OrderID));

                // System.Net.IPAddress myIPAddress = (System.Net.IPAddress)System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).Where(aa => aa.AddressFamily.ToString() == "InterNetwork");

                //消息发送
                using (var transactionScope = new TransactionScope(TransactionScopeOption.Suppress,
                                                                   new TransactionOptions {
                    IsolationLevel = IsolationLevel.RepeatableRead
                }))
                {
                    Weixin.QY.AdvancedAPIs.Mass.MassResult result = null;
                    try
                    {
                        result = WechatCommon.SendMsgToUser(lstTemp[0].AppId.Value, ((WechatMessageLogType)lstTemp[0].ContentType.Value).ToString(), "", lstTemp);

                        //如果发送失败,把已经发送列表中数据去除,这样可以重新发送。
                        if (result.errcode != Weixin.ReturnCode_QY.请求成功)
                        {
                            lstSent.RemoveAll(bb => lstTemp.Exists(aa => aa.Id == bb));
                        }

                        foreach (var b in lstTemp)
                        {
                            b.PublishDate   = DateTime.Now;
                            b.ArticleStatus = "Published";

                            //记录IP
                            if (myIPAddress != null)
                            {
                                b.Previewers = myIPAddress.ToString();
                            }
                            articleInfoService.UpdateView(b, new List <string>()
                            {
                                "PublishDate", "ArticleStatus"
                            });
                        }

                        if (result != null && result.errcode == Weixin.ReturnCode_QY.请求成功)
                        {
                            transactionScope.Complete();
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex, "Auto SendMsg error");

                        //如果发送失败,把已经发送列表中数据去除,这样可以重新发送。
                        if (result == null || result.errcode != Weixin.ReturnCode_QY.请求成功)
                        {
                            lstSent.RemoveAll(bb => lstTemp.Exists(aa => aa.Id == bb));
                        }
                    }
                }
            }

            log.Debug("TimerMessageJob Execute");
        }