Exemplo n.º 1
0
        public static NotificationCategory GetCategory(ISession session, int catId)
        {
            if (!_categoryLoaded)
            {
                lock (_lock)
                {
                    _categories.Clear();
                    IList <NotificationCategory> categories = session.CreateEntityQuery <NotificationCategory>().List <NotificationCategory>();
                    foreach (NotificationCategory cat in categories)
                    {
                        _categories.Add(cat.CatID, cat);
                    }
                }
            }
            NotificationCategory result = null;

            if (_categories.TryGetValue(catId, out result))
            {
                return(result);
            }
            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建消息通知对象
        /// </summary>
        /// <param name="session"></param>
        /// <param name="triggerId">触发消息通知的对象标识,例如会员注册使用会员ID、订单确认使用订单号或订单ID等</param>
        /// <param name="categoryId">消息通知分类ID,参考表BC_NOTIFY_CAT中的设置</param>
        /// <returns>创建的消息通知对象</returns>
        public static Notification Create(ISession session, string triggerId, int categoryId)
        {
            Notification notification = new Notification();

            notification.CatID        = categoryId;
            notification.TriggerID    = triggerId;
            notification.CreateTime   = DateTime.Now;
            notification.FinishTime   = new DateTime(1900, 1, 1);
            notification.RefCount     = 0;
            notification.Status       = NotificationStatus.New;
            notification.ResultStatus = NotificationResultStatus.New;

            notification._session = session;
            NotificationCategory category = Notification.GetCategory(session, categoryId);

            if (category == null || !category.IsEnabled)
            {
                return(null);
            }

            notification.Create(session);

            return(notification);
        }
Exemplo n.º 3
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);
        }