public void Add(MsgAdmin MsgAdmin, int?IsObj, int?IsAdmin, int?IsPId, int?IsLevel) { string sendUsers = ""; //获取发送对象 if (IsObj.HasValue) { switch (IsObj) { case 0: //管理员 MsgAdmin.IsAdmin = 0; #region 给管理员发送 if (IsAdmin.HasValue) { if (IsAdmin.Value == 0) { //选择了所有的管理员 IList <SysAdmin> ListAdmin = BasicAgent.GetAgentAdmins(Entity); //获取所有的管理员 if (ListAdmin.Count > 1) { MsgAdmin.AId = 0; foreach (var item in ListAdmin) { sendUsers += item.Id + ","; } } else { MsgAdmin.AId = ListAdmin[0].Id; } } else { //如果选择了单个管理员 MsgAdmin.AId = IsAdmin.Value; } } #endregion break; case 1: //代理商 MsgAdmin.IsAdmin = 1; #region 给代理商发送 int isLevel = IsLevel.HasValue ? IsLevel.Value : 0; if (IsPId.HasValue) { if (IsPId.Value == 0) //如果选择的所有代理商 { IList <SysAgent> ListAgent = BasicAgent.GetSupAgent(Entity); if (isLevel == 1) { //包含下级 ListAgent = ListAgent.GetSupAgent(Entity); } IList <SysAdmin> modelAdmin = ListAgent.GetAgentAdmins(Entity); if (modelAdmin.Count > 1) { MsgAdmin.AId = 0; foreach (var adminItem in modelAdmin) { sendUsers += adminItem.Id + ","; } } else { MsgAdmin.AId = modelAdmin[0].Id; } } else //如果选择的单个代理商 { SysAgent agentModel = Entity.SysAgent.FirstOrDefault(x => x.Id == IsPId); IList <SysAdmin> modelAdmin = null; if (isLevel == 0) //不包含下级 { modelAdmin = agentModel.GetAgentAdmins(Entity); } else //包含下级 { //获取下级代理商 IList <SysAgent> ListAgent = agentModel.GetSupAgent(Entity); modelAdmin = ListAgent.GetAgentAdmins(Entity); } if (modelAdmin.Count > 1) { MsgAdmin.AId = 0; foreach (var item in modelAdmin) { sendUsers += item.Id + ","; } } else { MsgAdmin.AId = modelAdmin[0].Id; } } } #endregion break; } } MsgAdmin.PId = AdminUser.Id; MsgAdmin.SendUsers = !string.IsNullOrWhiteSpace(sendUsers) ? "," + sendUsers : ",0,"; MsgAdmin.Name = MsgAdmin.Name; MsgAdmin.Info = MsgAdmin.Info; MsgAdmin.State = MsgAdmin.State; MsgAdmin.AddTime = DateTime.Now; Entity.MsgAdmin.AddObject(MsgAdmin); Entity.SaveChanges(); BaseRedirect(); }