public override IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage)
 {
     try
     {
         var opid        = requestMessage.FromUserName;
         var entityevent = new MpEventScanLog();
         entityevent.ID           = Formula.FormulaHelper.CreateGuid();
         entityevent.MpID         = account.ID;
         entityevent.OpenID       = opid;
         entityevent.EventContent = requestMessage.EventKey;
         entityevent.EventType    = "已关注";
         entityevent.MsgID        = requestMessage.MsgId.ToString();
         entityevent.CreateDate   = System.DateTime.Now;
         entities.Set <MpEventScanLog>().Add(entityevent);
         entities.SaveChanges();
     }
     catch (Exception ex)
     {
         LogWriter.Info(string.Format("MPID{0}记录扫码事件出错:原因{1}", account.ID, ex.Message));
     }
     return(null);
 }
        /// <summary>
        /// 订阅(关注)事件
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
        {
            var rs     = CacheHelper.Get(string.Format("SubscribeRequest_{0}", account.AppID));
            var rstype = CacheHelper.Get(string.Format("SubscribeRequest_{0}_Type", account.AppID));
            var opid   = requestMessage.FromUserName;

            #region 记录日志
            try
            {
                var entityevent = new MpEventScanLog();
                entityevent.ID           = Formula.FormulaHelper.CreateGuid();
                entityevent.MpID         = account.ID;
                entityevent.OpenID       = opid;
                entityevent.EventContent = requestMessage.EventKey.StartsWith("qrscene_") ? requestMessage.EventKey.Substring(8) : requestMessage.EventKey;;
                entityevent.EventType    = "未关注";
                entityevent.MsgID        = requestMessage.MsgId.ToString();
                entityevent.CreateDate   = System.DateTime.Now;
                entities.Set <MpEventScanLog>().Add(entityevent);
                entities.SaveChanges();
            }
            catch (Exception ex)
            {
                LogWriter.Info(string.Format("MPID{0}记录扫码事件出错:原因{1}", account.ID, ex.Message));
            }
            #endregion

            #region 更新粉丝
            WxFO wxfo = new WxFO();
            try
            {
                UserInfoJson wxinfo = null;
                try
                {
                    wxinfo = UserApi.Info(wxfo.GetAccessToken(account.ID), opid);
                }
                catch (Exception ex)
                {
                    LogWriter.Error(ex, string.Format("获取MpID为{0},openid为{1}的用户信息报错", account.ID, opid));
                    wxinfo = UserApi.Info(wxfo.GetAccessToken(account.ID, true), opid);
                }
                if (wxinfo.errcode != ReturnCode.请求成功)
                {
                    throw new Exception(string.Format("获取MpID为{0},openid为{1}的用户信息报错,错误编号:{2},错误消息:{3}", account.ID, opid, wxinfo.errcode, wxinfo.errmsg));
                }

                var group      = entities.Set <MpGroup>().Where(c => c.MpID == account.ID && c.WxGroupID == wxinfo.groupid).FirstOrDefault();
                var entityfans = entities.Set <MpFans>().Where(c => c.MpID == account.ID && c.OpenID == opid).FirstOrDefault();

                #region 保存分组
                if (group == null)
                {
                    var rootgroup = entities.Set <MpGroup>().Where(c => c.MpID == account.ID && c.Length == 1).FirstOrDefault();
                    if (rootgroup == null)
                    {
                        rootgroup            = new MpGroup();
                        rootgroup.ID         = Formula.FormulaHelper.CreateGuid();
                        rootgroup.MpID       = account.ID;
                        rootgroup.Name       = "全部";
                        rootgroup.ParentID   = "-1";
                        rootgroup.FullPath   = rootgroup.ID;
                        rootgroup.Length     = 1;
                        rootgroup.ChildCount = 0;
                        entities.Set <MpGroup>().Add(rootgroup);
                    }
                    var g = new MpGroup();
                    g.ID         = Formula.FormulaHelper.CreateGuid();
                    g.MpID       = account.ID;
                    g.ParentID   = rootgroup.ID;
                    g.FullPath   = string.Format("{0}.{1}", rootgroup.ID, g.ID);
                    g.Length     = 2;
                    g.ChildCount = 0;
                    g.WxGroupID  = wxinfo.groupid;

                    GroupsJson groups = null;
                    try
                    {
                        groups = GroupsApi.Get(wxfo.GetAccessToken(account.ID));
                    }
                    catch (Exception ex)
                    {
                        LogWriter.Error(ex, string.Format("获取MpID为{0}的分组报错", account.ID));
                        groups = GroupsApi.Get(wxfo.GetAccessToken(account.ID, true));
                    }
                    if (groups.errcode != ReturnCode.请求成功)
                    {
                        throw new Exception(string.Format("获取MpID为{0}的分组报错,错误编号:{1},错误消息:{2}", account.ID, groups.errcode, groups.errmsg));
                    }

                    var wg = groups.groups.Where(c => c.id == wxinfo.groupid).FirstOrDefault();
                    if (wg != null)
                    {
                        g.Name      = wg.name;
                        g.FansCount = wg.count;
                    }
                    entities.Set <MpGroup>().Add(g);
                }
                #endregion

                #region 保存粉丝
                if (entityfans == null)
                {
                    entityfans               = new MpFans();
                    entityfans.ID            = Formula.FormulaHelper.CreateGuid();
                    entityfans.City          = wxinfo.city;
                    entityfans.Country       = wxinfo.country;
                    entityfans.HeadImgUrl    = wxinfo.headimgurl;
                    entityfans.IsFans        = "1";
                    entityfans.Language      = wxinfo.language;
                    entityfans.MpID          = account.ID;
                    entityfans.NickName      = wxinfo.nickname;
                    entityfans.OpenID        = wxinfo.openid;
                    entityfans.Province      = wxinfo.province;
                    entityfans.Remark        = wxinfo.remark;
                    entityfans.Sex           = wxinfo.sex.ToString();
                    entityfans.SubscribeTime = DateTimeHelper.GetDateTimeFromXml(wxinfo.subscribe_time);
                    entityfans.UniionID      = wxinfo.unionid;
                    entityfans.WxGroupID     = wxinfo.groupid;
                    entityfans.GroupID       = group.ID;
                    entityfans.UpdateTime    = DateTime.Now;
                    entities.Set <MpFans>().Add(entityfans);
                }
                else
                {
                    entityfans.City          = wxinfo.city;
                    entityfans.Country       = wxinfo.country;
                    entityfans.HeadImgUrl    = wxinfo.headimgurl;
                    entityfans.IsFans        = "1";
                    entityfans.Language      = wxinfo.language;
                    entityfans.MpID          = account.ID;
                    entityfans.NickName      = wxinfo.nickname;
                    entityfans.OpenID        = wxinfo.openid;
                    entityfans.Province      = wxinfo.province;
                    entityfans.Remark        = wxinfo.remark;
                    entityfans.Sex           = wxinfo.sex.ToString();
                    entityfans.SubscribeTime = DateTimeHelper.GetDateTimeFromXml(wxinfo.subscribe_time);
                    entityfans.UniionID      = wxinfo.unionid;
                    entityfans.WxGroupID     = wxinfo.groupid;
                    entityfans.GroupID       = group.ID;
                    entityfans.UpdateTime    = DateTime.Now;
                }
                #endregion

                entities.SaveChanges();
            }
            catch (Exception ex)
            {
                LogWriter.Error(string.Format("粉丝订阅更新数据库失败,原因:{0}", ex.Message));
            }
            #endregion

            #region 推送消息
            if (rs == null || rstype == null)
            {
                var eventtype = MpEventType.Subscribe.ToString();
                var entity    = entities.Set <MpEvent>().Where(c => c.MpID == account.ID && c.IsDelete == 0 && c.EventType == eventtype).FirstOrDefault();
                if (entity != null)
                {
                    CacheHelper.Set(string.Format("SubscribeRequest_{0}_Type", account.AppID), entity.ReplyType, cachesecond);
                    if (entity.ReplyType == MpMessageType.none.ToString())
                    {
                        return(null);
                    }
                    else if (entity.ReplyType == MpMessageType.image.ToString())
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageImage>();
                        responseMessage.Image.MediaId = entity.ImageMediaID;
                        CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond);
                        return(responseMessage);
                    }
                    else if (entity.ReplyType == MpMessageType.text.ToString())
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageText>();
                        responseMessage.Content = entity.Content;
                        CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond);
                        return(responseMessage);
                    }
                    else if (entity.ReplyType == MpMessageType.voice.ToString())
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageVoice>();
                        responseMessage.Voice.MediaId = entity.VoiceMediaID;
                        CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond);
                        return(responseMessage);
                    }
                    else if (entity.ReplyType == MpMessageType.video.ToString())
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageVideo>();
                        var video           = entities.Set <MpMediaVideo>().Where(c => c.MpID == account.ID && c.IsDelete == 0 && c.ID == entity.VideoID).FirstOrDefault();
                        if (video == null)
                        {
                            return(null);
                        }
                        responseMessage.Video.MediaId     = video.MediaID;
                        responseMessage.Video.Title       = video.Title;
                        responseMessage.Video.Description = video.Description;
                        CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond);
                        return(responseMessage);
                    }
                    else if (entity.ReplyType == MpMessageType.mpnews.ToString())
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageNews>();
                        var article         = entities.Set <MpSelfArticle>().Where(c => c.MpID == account.ID && c.IsDelete == 0 && c.ID == entity.ArticleID).FirstOrDefault();
                        if (article == null)
                        {
                            return(null);
                        }
                        responseMessage.Articles.Add(new Article()
                        {
                            Title       = article.Title,
                            Description = article.Description,
                            Url         = article.Url,
                            PicUrl      = string.Format("http://{0}/wechatservice/api/Image/Get/{1}", domain, article.PicFileID),
                        });
                        CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond);
                        return(responseMessage);
                    }
                    else if (entity.ReplyType == MpMessageType.mpmultinews.ToString())
                    {
                        var responseMessage = base.CreateResponseMessage <ResponseMessageNews>();
                        var article         = entities.Set <MpSelfArticleGroup>().Where(c => c.MpID == account.ID && c.IsDelete == 0 && c.ID == entity.ArticleGroupID).FirstOrDefault();
                        if (article == null || article.MpSelfArticleGroupItem == null || article.MpSelfArticleGroupItem.Count(c => c.MpSelfArticle != null) < 2)
                        {
                            return(null);
                        }
                        foreach (var item in article.MpSelfArticleGroupItem.Where(c => c.MpSelfArticle != null))
                        {
                            responseMessage.Articles.Add(new Article()
                            {
                                Title       = item.MpSelfArticle.Title,
                                Description = item.MpSelfArticle.Description,
                                Url         = item.MpSelfArticle.Url,
                                PicUrl      = string.Format("http://{0}/wechatservice/api/Image/Get/{1}", domain, item.MpSelfArticle.PicFileID),
                            });
                        }
                        CacheHelper.Set(string.Format("SubscribeRequest_{0}", account.AppID), responseMessage, cachesecond);
                        return(responseMessage);
                    }
                    else
                    {
                        return(null);
                    }
                }
                //其他回复
                else
                {
                    return(null);
                }
            }
            else
            {
                var rstp = rstype.ToString();
                if (rstp == MpMessageType.image.ToString())
                {
                    return(rs as ResponseMessageImage);
                }
                else if (rstp == MpMessageType.mpmultinews.ToString())
                {
                    return(rs as ResponseMessageNews);
                }
                else if (rstp == MpMessageType.mpnews.ToString())
                {
                    return(rs as ResponseMessageNews);
                }
                else if (rstp == MpMessageType.text.ToString())
                {
                    return(rs as ResponseMessageText);
                }
                else if (rstp == MpMessageType.video.ToString())
                {
                    return(rs as ResponseMessageVideo);
                }
                else if (rstp == MpMessageType.voice.ToString())
                {
                    return(rs as ResponseMessageVoice);
                }
                return(rs as IResponseMessageBase);
            }
            #endregion
        }