/// <summary> /// 发送消息 /// </summary> /// <param name="isNotify">是否发送通知,true,返回需要发送的消息列表,需要在web层调用notifyHub发送</param> /// <param name="messageData">消息内容</param> /// <returns>需要发送通知的对象列表</returns> public List <NotifyInfo> SendMessageToNotify(bool isNotify, MessagesPostData messageData) { List <NotifyInfo> result = new List <NotifyInfo>(); MessageData data = new MessageData(); EntityTools.EntityCopy(messageData.Message, data, ""); data.CreateDate = DateTime.Now; if (data.ActiveDate.Year == 1) { data.ActiveDate = Convert.ToDateTime("1900-1-1"); } if (data.NotActiveDate.Year == 1) { data.NotActiveDate = Convert.ToDateTime("9999-1-1"); } //data.MessageSourceType =DictionaryCache.Get()[data.MessageSourceTypeId]; //data.MessageType =DictionaryCache.Get()[data.MessageTypeId]; List <string> ids = new List <string>(); IQueryable <User> users = userBLL.Filter(o => o.IsResignOrGraduate != true && o.SecurityStamp != null); //List<string> userList = userBLL.Filter(o => o.IsResignOrGraduate != true).Select(o => o.Id).ToList(); List <string> userList = null; if (messageData.IdsByCategory != null && messageData.IdsByCategory.Count() != 0) { foreach (var idsByCategory in messageData.IdsByCategory) { switch (idsByCategory.Key) { case CategoryDictionary.Building: var usersByBuilding = new List <string>(); foreach (var r in idsByCategory.Value) { int id = Convert.ToInt32(r); string treeId = buildingBLL.Find(id).TreeId; var list = new List <string>(); list = users.Where(o => !o.IsResignOrGraduate && o.Buildings.Any(x => x.TreeId.StartsWith(treeId + "-") || x.TreeId == treeId)).Select(o => o.Id).ToList(); usersByBuilding = usersByBuilding.Union(list).ToList(); } if (userList == null) { userList = usersByBuilding; } else { userList = userList.Intersect(usersByBuilding).ToList(); } break; case CategoryDictionary.Role: var usersByRole = new List <string>(); foreach (var r in idsByCategory.Value) { var list = new List <string>(); list = users.Where(o => !o.IsResignOrGraduate && o.Roles.Any(x => x.Id == r)).Select(o => o.Id).ToList(); usersByRole = usersByRole.Union(list).ToList(); } if (userList == null) { userList = usersByRole; } else { userList = userList.Intersect(usersByRole).ToList(); } break; case CategoryDictionary.Organization: var usersByOrganization = new List <string>(); foreach (var r in idsByCategory.Value) { int id = Convert.ToInt32(r); var list = new List <string>(); string treeId = organizationBLL.Find(id).TreeId; list = users.Where(o => !o.IsResignOrGraduate && (o.Organizations.Any(c => c.TreeId.StartsWith(treeId) || c.TreeId == treeId)) || (o.OrganizationId == id || userBLL.db.Organizations.FirstOrDefault(c => c.Id == id).TreeId.StartsWith(treeId))).Select(o => o.Id).ToList(); usersByOrganization = usersByOrganization.Union(list).ToList(); } if (userList == null) { userList = usersByOrganization; } else { userList = userList.Intersect(usersByOrganization).ToList(); } break; case CategoryDictionary.Org: var usersByOrg = new List <string>(); foreach (var r in idsByCategory.Value) { int id = Convert.ToInt32(r); var list = new List <string>(); list = users.Where(o => !o.IsResignOrGraduate && o.OrganizationId == id).Select(o => o.Id).ToList(); usersByOrg = usersByOrg.Union(list).ToList(); } if (userList == null) { userList = usersByOrg; } else { userList = userList.Intersect(usersByOrg).ToList(); } break; case CategoryDictionary.User: if (userList == null) { userList = idsByCategory.Value; } else { userList = userList.Intersect(idsByCategory.Value).ToList(); } break; } } } var node = Create(data.ToModel()); if (userList.Count > 0) { foreach (var id in userList) { var toUser = userBLL.Find(id); if (toUser == null) { continue; } List <string> roles = new List <string>(); if (toUser.Roles != null) { foreach (var role in toUser.Roles) { roles.Add(role.Id); } } var roleSub = subscribeBLL.Filter(o => o.TypeId == 290001 && o.MessageTypeId == messageData.Message.MessageTypeId && o.Enabled == true && roles.Contains(o.TargetId)).ToList(); //角色订阅 var userSub = subscribeBLL.Filter(o => o.TypeId == 290002 && o.MessageTypeId == messageData.Message.MessageTypeId && o.Enabled == true && o.TargetId == toUser.Id).ToList(); //用户退订 if (!((roleSub != null && roleSub.Count() > 0) && (userSub == null || (userSub != null && userSub.Count() == 0)))) { continue; } if (isNotify) { List <int> receiveModels = new List <int>(); foreach (var m in userSub) { receiveModels.Add(m.ReceivingModelId); } var sub = roleSub.Where(o => !receiveModels.Contains(o.ReceivingModelId)).ToList(); if (sub.Count > 0) { //NotifyHub hub = new Hubs.NotifyHub(); //hub.Notify(data.ToViewData(), id, sub); NotifyInfo info = new NotifyInfo(); info.msg = data; info.userId = id; info.subs = sub; result.Add(info); } } MessageRecord record = new MessageRecord(); record.MessageId = node.Id; record.IsDeleted = false; record.IsEnable = false; record.IsReaded = false; record.ReadedTime = null; record.UserId = id; messageRecordBLL.Create(record); } return(result); } else { return(null); } }
/// <summary> /// 用于postsearch中,转换旧版本搜索格式支持新版本格式 /// </summary> /// <param name="me"></param> /// <param name="targetType"></param> /// <param name="targetId"></param> /// <param name="startTime"></param> /// <param name="endTime"></param> /// <param name="templateIds"></param> /// <returns></returns> public IList <MonitoringConfigData> GetConfigsByTargetAndTime(DAL.EmpModels.User me, int targetType, int targetId, DateTime?startTime, DateTime?endTime, List <int?> templateIds) { IQueryable <ConfigDetail> details; if (targetId != -1) { details = db.ConfigDetail.Where(o => templateIds.Contains(o.Id) && ((targetType == DictionaryCache.ConfigToBuilding.Id && o.BuildingId == targetId) || (targetType == DictionaryCache.ConfigToMeter.Id && o.MeterId == targetId) || (targetType == DictionaryCache.ConfigToOrg.Id && o.OrganizationId == targetId)) ); } else { details = this.db.ConfigDetail.Where(o => templateIds.Contains(o.Id) ); } if (startTime != null) { details = details.Where(o => o.Template.ConfigCycleSettings.Any(c => c.BeginTime <= startTime)); } if (endTime != null) { details = details.Where(o => o.Template.ConfigCycleSettings.Any(c => c.EndTime >= endTime)); } var detailsList = details.ToList(); IList <MonitoringConfigData> results = new List <MonitoringConfigData>(); List <string> hasRightUserIds = new List <string>(); List <string> notHasRightUserIds = new List <string>(); var tempIDs = detailsList.Select(o => o.TemplateId).ToList(); var monitoringConfigs = this.Filter(o => tempIDs.Contains((int)o.TemplateId)).ToList(); foreach (var detail in detailsList) { bool isHasRight = true; if (notHasRightUserIds.Contains(detail.OperatorId)) { isHasRight = false; } else if (hasRightUserIds.Contains(detail.OperatorId)) { isHasRight = true; } else { //修改是否允许配置参数 var editUser = this.db.Users.FirstOrDefault(o => o.Id == detail.OperatorId); //如果配置者有权限的机构存在任意一是当前用户有权限机构的上级机构,则不允许修改 var orgsForEditUser = editUser.Organizations; var orgsForMe = me.Organizations; var count = orgsForEditUser.Where(o => orgsForMe.Any(c => c.TreeId.StartsWith(o.TreeId + "-"))).Count(); if (count > 0) { isHasRight = false; notHasRightUserIds.Add(editUser.Id); } else { hasRightUserIds.Add(editUser.Id); } } var templates = monitoringConfigs.Where(o => o.TemplateId == detail.TemplateId).ToList(); foreach (var template in templates) { foreach (var cycleSetting in template.ConfigCycleSettings) { MonitoringConfigData config = new MonitoringConfigData(); EntityTools.EntityCopy(template.ToViewData(), config, ""); if (config.TargetTypeId == DictionaryCache.ConfigToBuilding.Id) { detail.BuildingId = config.TargetId; } else if (config.TargetTypeId == DictionaryCache.ConfigToOrg.Id) { detail.OrganizationId = config.TargetId; } else if (config.TargetTypeId == DictionaryCache.ConfigToMeter.Id) { detail.MeterId = config.TargetId; } config.StartTime = cycleSetting.BeginTime; config.EndTime = cycleSetting.EndTime; config.isAlowEdit = isHasRight; results.Add(config); } } } return(results); }