Example #1
0
        /// <summary>
        /// <para>发送之前必须调用该方法,构造发送标题、内容,读取接收者列表等操作</para>
        /// <para>使用Title, Content, Receivers属性就可以获得发送需要的完整信息</para>
        /// </summary>
        /// <param name="session"></param>
        /// <returns>待发送数量</returns>
        public int ReadyToSend()
        {
            this._title   = "";
            this._content = "";

            //接收者
            IList <NotificationSubscriber> subscribers = NotificationSubscriber.GetList(this._session, this.CatID);

            foreach (NotificationSubscriber subscriber in subscribers)
            {
                NotificationReceiver receiver = NotificationReceiver.Retrieve(this._session, this.NotifyID, subscriber.UserID);
                if (receiver == null)
                {
                    //当前订阅者是否已经添加到接收者列表中了?
                    receiver = new NotificationReceiver(this.NotifyID, subscriber.UserID, subscriber.UserName, subscriber.PostCode);
                    receiver.Create(this._session);
                }
            }
            //获取接收者列表
            this._receiver = NotificationReceiver.ToSendList(this._session, this.NotifyID);
            if (this._receiver.Count <= 0)
            {
                return(0);
            }

            //参数
            this._param = new Dictionary <string, object>();
            IList <NotificationParam> paramList = this._session.CreateEntityQuery <NotificationParam>()
                                                  .Where(Exp.Eq("NotifyID", this.NotifyID) & Exp.Eq("ParentID", 0))
                                                  .List <NotificationParam>();

            foreach (NotificationParam p in paramList)
            {
                this._param.Add(p.ParamName, this.BuildParam(p));
            }

            NotificationCategory category = Notification.GetCategory(this._session, this.CatID);

            //消息标题(对于邮件消息)
            if (category.Type == NotificationType.Mail && !string.IsNullOrEmpty(category.TitleTemplate) && category.TitleTemplate.Trim().Length > 0)
            {
                StringTemplate st = new StringTemplate(category.TitleTemplate);
                SetAttribute(st, this._param);
                this._title = st.ToString();
            }

            //消息内容
            if (!System.IO.File.Exists(category.TemplateFile))
            {
                log.ErrorFormat("category {0}: template file {1} not exists", category.CatID, category.TemplateFile);
                return(this._receiver.Count);
            }
            string templateContent = null;

            using (System.IO.StreamReader reader = new System.IO.StreamReader(category.TemplateFile, System.Text.Encoding.UTF8))
            {
                templateContent = reader.ReadToEnd();
            }
            if (string.IsNullOrEmpty(templateContent) || templateContent.Length <= 0)
            {
                return(this._receiver.Count);
            }
            StringTemplate stContent = new StringTemplate(templateContent);

            SetAttribute(stContent, this._param);
            this._content = stContent.ToString();

            return(this._receiver.Count);
        }