Beispiel #1
0
        /// <summary>
        /// 任务执行的内容
        /// </summary>
        /// <param name="taskDetail">任务配置状态信息</param>
        public void Execute(TaskDetail taskDetail)
        {
            IEnumerable <IReminderInfoAccessor> reminderInfoTypeAccessors = DIContainer.Resolve <IEnumerable <IReminderInfoAccessor> >();
            //提醒信息类型查询实例集合
            IEnumerable <IReminderSender> reminderSenders = DIContainer.Resolve <IEnumerable <IReminderSender> >();
            //发送提醒实例集合
            ReminderService  reminderService  = new ReminderService();         //提醒业务逻辑实例
            ReminderSettings reminderSettings = reminderSettingsManager.Get(); //站点提醒设置
            ConcurrentDictionary <int, ConcurrentDictionary <long, List <UserReminderInfo> > > waitSendDict = new ConcurrentDictionary <int, ConcurrentDictionary <long, List <UserReminderInfo> > >();

            //遍历各种提醒信息类型
            RWLock.EnterWriteLock();
            foreach (var reminderInfoTypeAccessor in reminderInfoTypeAccessors)
            {
                int reminderInfoTypeId            = reminderInfoTypeAccessor.ReminderInfoTypeId;
                ReminderInfoType reminderInfoType = ReminderInfoType.Get(reminderInfoTypeAccessor.ReminderInfoTypeId);
                if (reminderInfoType == null)
                {
                    continue;
                }
                //查询该信息类型下的提醒信息集合
                IEnumerable <UserReminderInfo> userReminderInfos = reminderInfoTypeAccessor.GetUserReminderInfos();
                foreach (var userReminderInfo in userReminderInfos.ToList())
                {
                    if (userReminderInfo.UserId <= 0)
                    {
                        continue;
                    }
                    foreach (var reminderMode in ReminderMode.GetAll())
                    {
                        int reminderModeId = reminderMode.ModeId;

                        UserReminderSettings userReminderSettings = reminderService.GetUserReminderSettings(userReminderInfo.UserId, reminderModeId, reminderInfoTypeId);
                        if (userReminderSettings == null)
                        {
                            continue;
                        }
                        //1.	判断用户是否启用了提醒并可以使用该提醒方式;
                        if (!userReminderSettings.IsEnabled)
                        {
                            continue;
                        }

                        if (!waitSendDict.ContainsKey(reminderModeId))
                        {
                            waitSendDict[reminderModeId] = new ConcurrentDictionary <long, List <UserReminderInfo> >();
                        }
                        if (!waitSendDict[reminderModeId].ContainsKey(userReminderInfo.UserId))
                        {
                            waitSendDict[reminderModeId][userReminderInfo.UserId] = new List <UserReminderInfo>();
                        }

                        //2.	从用户设置中取发送提醒时间阈值,遍历提醒信息集合筛选出超过此值的提醒信息集合;
                        IEnumerable <ReminderInfo> reminderInfos = userReminderInfo.ReminderInfos.ToList();

                        reminderInfos = reminderInfos.Where(n => n.DateCreated.AddMinutes(userReminderSettings.ReminderThreshold) <= DateTime.UtcNow).ToList();
                        //3.	再判断是否重复提醒用户
                        if (userReminderSettings.IsRepeated)
                        {
                            //   根据相应提醒记录的最后提醒时间和当前时间求差值是否大于重复提醒时间间隔进行筛选,如果提醒记录不存在也应筛选出
                            IEnumerable <ReminderRecord> records = reminderService.GetRecords(userReminderInfo.UserId, reminderModeId, reminderInfoTypeId);
                            reminderInfos = reminderInfos.Where(n =>
                            {
                                ReminderRecord record = records.FirstOrDefault(m => m.ObjectId == n.ObjectId);
                                if (record == null)
                                {
                                    return(true);
                                }
                                TimeSpan ts = DateTime.UtcNow - record.LastReminderTime;
                                return(ts.TotalMinutes > userReminderSettings.RepeatInterval);
                            }).ToList();
                            if (reminderInfos.Count() == 0)
                            {
                                continue;
                            }
                            IEnumerable <long> objectIds = reminderInfos.Select(n => n.ObjectId);
                            //更新最后提醒时间
                            reminderService.UpdateRecoreds(userReminderInfo.UserId, reminderModeId, reminderInfoTypeId, objectIds);
                        }
                        else
                        {
                            // 根据是否提醒过筛选发送提醒信息集合
                            reminderInfos = reminderInfos.Where(n => !reminderService.IsExits(userReminderInfo.UserId, reminderModeId, reminderInfoTypeId, n.ObjectId)).ToList();
                            if (reminderInfos.Count() == 0)
                            {
                                continue;
                            }
                            //创建提醒记录
                            reminderService.CreateRecords(userReminderInfo.UserId, reminderModeId, reminderInfoTypeId, reminderInfos.Select(n => n.ObjectId));
                        }

                        userReminderInfo.SetReminderInfos(reminderInfos.ToList());
                        //生成处理地址
                        userReminderInfo.ProcessUrl = reminderInfoTypeAccessor.GetProcessUrl(userReminderInfo.UserId);

                        waitSendDict[reminderModeId][userReminderInfo.UserId].Add(userReminderInfo);
                    }
                }
            }
            RWLock.ExitWriteLock();

            if (waitSendDict != null && waitSendDict.Count > 0)
            {
                foreach (var reminderSender in reminderSenders)
                {
                    if (!waitSendDict.ContainsKey(reminderSender.ReminderModeId))
                    {
                        continue;
                    }

                    foreach (KeyValuePair <long, List <UserReminderInfo> > pair in waitSendDict[reminderSender.ReminderModeId])
                    {
                        //发送提醒
                        reminderSender.SendReminder(pair.Value);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 用户获取所有提醒设置
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns>Key:提醒方式Id,Value:用户提醒设置集合</returns>
        public Dictionary <string, IEnumerable <UserReminderSettings> > GetAllUserReminderSettings(long userId)
        {
            Dictionary <string, IEnumerable <UserReminderSettings> > userReminderSettingsesDictionary = userReminderSettingsRepository.GetAllUserReminderSettings(userId);

            if (userReminderSettingsesDictionary == null)
            {
                userReminderSettingsesDictionary = new Dictionary <string, IEnumerable <UserReminderSettings> >();
            }
            foreach (var reminderMode in ReminderMode.GetAll())
            {
                int reminderModeId = reminderMode.ModeId;
                IEnumerable <UserReminderSettings> userReminderSettingses = null;
                userReminderSettingsesDictionary.TryGetValue(reminderModeId.ToString(), out userReminderSettingses);
                ReminderSettings settings = reminderSettingsManager.Get();
                if (settings == null || settings.ReminderModeSettingses == null)
                {
                    return(new Dictionary <string, IEnumerable <UserReminderSettings> >());
                }
                var reminderModeSettings = settings.ReminderModeSettingses.FirstOrDefault(n => n.ModeId == reminderMode.ModeId);
                if (reminderModeSettings == null)
                {
                    continue;
                }

                IEnumerable <string> allowedUserRoleNames = reminderModeSettings.AllowedUserRoleNames;
                //注册用户代表所有用户
                if (!allowedUserRoleNames.Contains(RoleNames.Instance().RegisteredUsers()))
                {
                    IUser user = DIContainer.Resolve <IUserService>().GetUser(userId);
                    if (!user.IsInRoles(allowedUserRoleNames.ToArray()))
                    {
                        userReminderSettingsesDictionary[reminderModeId.ToString()] = null;
                        continue;
                    }
                }

                IEnumerable <ReminderInfoTypeSettings> defaultUserReminderSettingses = reminderModeSettings.ReminderInfoTypeSettingses;

                if (userReminderSettingses == null || userReminderSettingses.Count() == 0 || defaultUserReminderSettingses.Count() > userReminderSettingses.Count())
                {
                    //合并
                    if (userReminderSettingses == null)
                    {
                        userReminderSettingses = new List <UserReminderSettings>();
                    }
                    //1.用户没有设置过
                    if (userReminderSettingses.Count() == 0)
                    {
                        userReminderSettingses = defaultUserReminderSettingses.Select(n =>
                        {
                            UserReminderSettings userReminderSettings = UserReminderSettings.New(n);
                            userReminderSettings.UserId         = userId;
                            userReminderSettings.ReminderModeId = reminderModeId;

                            return(userReminderSettings);
                        }).ToList();
                    }
                    //2.站点新增过提醒类型,则历史用户需要新增这些类型
                    if (defaultUserReminderSettingses.Count() > userReminderSettingses.Count())
                    {
                        IList <UserReminderSettings> userReminderSettingsesList = new List <UserReminderSettings>(userReminderSettingses);
                        foreach (var setting in defaultUserReminderSettingses)
                        {
                            if (!userReminderSettingses.Any(n => n.ReminderInfoTypeId == setting.ReminderInfoTypeId))
                            {
                                UserReminderSettings userReminderSettings = UserReminderSettings.New(setting);
                                userReminderSettings.UserId         = userId;
                                userReminderSettings.ReminderModeId = reminderModeId;
                                userReminderSettingsesList.Add(userReminderSettings);
                            }
                        }
                        userReminderSettingses = userReminderSettingsesList;
                    }
                    userReminderSettingsesDictionary[reminderModeId.ToString()] = userReminderSettingses;
                }
            }
            return(userReminderSettingsesDictionary);
        }