/// <summary> /// 添加接收者 /// </summary> /// <param name="userId">接收者标识,例如用户ID、会员ID等</param> /// <param name="userName">接收者姓名</param> /// <param name="postTo">邮件消息该属性指定接收者邮箱地址,短信消息该属性指定接收者手机号码</param> /// <returns></returns> public Notification AddReceiver(string userId, string userName, string postTo) { NotificationReceiver receiver = new NotificationReceiver(this.NotifyID, userId, userName, postTo); receiver.Create(this._session); return(this); }
/// <summary> /// 检查是否满足发送条件 /// </summary> /// <param name="receiver">接收者</param> /// <returns></returns> public bool CheckForSend(NotificationReceiver receiver) { if (receiver == null) { return(false); } if (string.IsNullOrEmpty(this._content)) { log.ErrorFormat("content is null, notification {0}, receiver {1} not sent", this.NotifyID, receiver.ReceiverID); return(false); } if (string.IsNullOrEmpty(receiver.PostTo) || receiver.PostTo.Trim().Length <= 0) { log.ErrorFormat("receiver's PostTo is null or empty, notification {0}, receiver {1} not sent", this.NotifyID, receiver.ReceiverID); return(false); } return(true); }
/// <summary> /// 给消息通知中某个接收者发送完毕时的处理 /// </summary> /// <param name="receiver">接收者</param> /// <param name="code">发送结果状态码</param> /// <param name="text">如果发送时发生异常,该文本传入异常信息</param> /// <returns>如果发送结果表明发送成功,返回true,否则返回false(短信的发送是否成功,还得延迟一段时间查询后才能获知)</returns> public bool AfterSendDo(NotificationReceiver receiver, string code, string text) { //查询发送结果状态码对象 NotificationCodeDef codeDef = GetCodeDef(this._session, GetCategory(this._session, this.CatID).Type, code); //记录一条发送日志 NotificationLog ntflog = new NotificationLog(); ntflog.NotifyID = this.NotifyID; ntflog.ReceiverID = receiver.ReceiverID; ntflog.ResultCode = code; if (codeDef != null && codeDef.IsSuccess) { ntflog.ErrorText = text; } else { ntflog.ErrorText = codeDef == null ? text : codeDef.Text + text; } ntflog.LogTime = DateTime.Now; ntflog.Create(this._session); //接收者状态更新 //TODO: 等待查询短信发送状态之后再做这个处理 if (codeDef != null && codeDef.IsSuccess) { receiver.ResultStatus = NotificationResultStatus.AllSuccess; } else { receiver.ResultStatus = NotificationResultStatus.AllError; } receiver.FinishTime = DateTime.Now; receiver.Update(this._session, "ResultStatus", "FinishTime"); return(codeDef != null && codeDef.IsSuccess); }
/// <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; }
/// <summary> /// ����Ƿ����㷢������ /// </summary> /// <param name="receiver">������</param> /// <returns></returns> public bool CheckForSend(NotificationReceiver receiver) { if (receiver == null) return false; if (string.IsNullOrEmpty(this._content)) { log.ErrorFormat("content is null, notification {0}, receiver {1} not sent", this.NotifyID, receiver.ReceiverID); return false; } if (string.IsNullOrEmpty(receiver.PostTo) || receiver.PostTo.Trim().Length <= 0) { log.ErrorFormat("receiver's PostTo is null or empty, notification {0}, receiver {1} not sent", this.NotifyID, receiver.ReceiverID); return false; } return true; }
/// <summary> /// ����Ϣ֪ͨ��ij�������߷������ʱ�Ĵ��� /// </summary> /// <param name="receiver">������</param> /// <param name="code">���ͽ��״̬��</param> /// <param name="text">�������ʱ�����쳣�����ı������쳣��Ϣ</param> /// <returns>������ͽ���������ͳɹ�������true������false�����ŵķ����Ƿ�ɹ��������ӳ�һ��ʱ���ѯ����ܻ�֪��</returns> public bool AfterSendDo(NotificationReceiver receiver, string code, string text) { //��ѯ���ͽ��״̬����� NotificationCodeDef codeDef = GetCodeDef(this._session, GetCategory(this._session, this.CatID).Type, code); //��¼һ��������־ NotificationLog ntflog = new NotificationLog(); ntflog.NotifyID = this.NotifyID; ntflog.ReceiverID = receiver.ReceiverID; ntflog.ResultCode = code; if (codeDef != null && codeDef.IsSuccess) ntflog.ErrorText = text; else ntflog.ErrorText = codeDef == null ? text : codeDef.Text + text; ntflog.LogTime = DateTime.Now; ntflog.Create(this._session); //������״̬���� //TODO: �ȴ���ѯ���ŷ���״̬֮������������� if (codeDef!=null && codeDef.IsSuccess) receiver.ResultStatus = NotificationResultStatus.AllSuccess; else receiver.ResultStatus = NotificationResultStatus.AllError; receiver.FinishTime = DateTime.Now; receiver.Update(this._session, "ResultStatus", "FinishTime"); return codeDef != null && codeDef.IsSuccess; }
/// <summary> /// ��ӽ����� /// </summary> /// <param name="userId">�����߱�ʶ�������û�ID����ԱID��</param> /// <param name="userName">����������</param> /// <param name="postTo">�ʼ���Ϣ������ָ�������������ַ��������Ϣ������ָ���������ֻ�����</param> /// <returns></returns> public Notification AddReceiver(string userId, string userName, string postTo) { NotificationReceiver receiver = new NotificationReceiver(this.NotifyID, userId, userName, postTo); receiver.Create(this._session); return this; }
/// <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); }