public async Task <ActionResult> Edit(HanEdit model) { var hg = _db.HanGroups.Include("blogs").Include("members").SingleOrDefault(m => m.HanGroupID == model.ID); if (hg == null) { return(NotFound()); } byte[] imgbtye = null; if (model.HanImage != null) { imgbtye = Convert.FromBase64String(model.HanImage); if (imgbtye.Length > 4194304) { ModelState.AddModelError("", "头像必须是图片,且小于4MB。"); } } if (ModelState.IsValid) { string[] newBlogIDstr = null; if (model.blogIDs == null) { newBlogIDstr = new string[] { }; } else { newBlogIDstr = model.blogIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); } var newBlogIDs = new List <int>(newBlogIDstr.Length); foreach (var blogid in newBlogIDstr) { int id; if (int.TryParse(blogid, out id)) { newBlogIDs.Add(id); } } var actual = _db.Blogs.Where(blog => newBlogIDs.Contains(blog.BlogID)).Select(blog => blog.BlogID).ToList(); if (actual.Count != newBlogIDs.Count) { newBlogIDs = newBlogIDs.Intersect(actual).ToList(); } var blogIDs = hg.blogs.Select(h => h.BlogID); var Members = hg.members.Select(h => h.Username); string[] newmembers = null; if (string.IsNullOrWhiteSpace(model.Members)) { newmembers = new string[] { } } ; else { newmembers = model.Members.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); } var errnames = string.Empty; var errids = string.Empty; var adminm = hg.members.Where(hm => hm.GroupLvl == 1).ToList(); var m = new List <HanGroupMember>(); if (imgbtye != null) { if (!string.IsNullOrEmpty(hg.Logo)) { await _uploadUtil.DeleteFileAsync(hg.Logo); } hg.Logo = _uploadUtil.GenerateImageName(hg.GroupUri); await _uploadUtil.saveImageAsync(imgbtye, hg.Logo); } hg.Intro = model.Intro; hg.Title = model.Title; var existnames = _udb.Users.Where(u => newmembers.Contains(u.UserName)).Select(u => u.UserName).ToList(); foreach (var n in newmembers) { var name = n.Trim(); if (existnames.Contains(name)) { var member = new HanGroupMember(); member.HanGroupID = hg.HanGroupID; member.Username = name; member.GroupLvl = 2; m.Add(member); } else { errnames += name + ','; } } var comp = new HanGroupMemberComparer(); hg.members = adminm.Union(hg.members.Where(hm => hm.GroupLvl == 2).Intersect(m, comp).Union(m, comp), comp).ToList(); //member = Group1 Union newGroup2 var b = new List <HanGroupBlog>(); foreach (var blogid in newBlogIDs) { var blog = new HanGroupBlog(); blog.BlogID = blogid; blog.HanGroupID = hg.HanGroupID; b.Add(blog); } var comparer = new HanGroupBlogComparer(); hg.blogs = hg.blogs.Intersect(b, comparer).Union(b, comparer).ToList(); _db.SaveChanges(); return(Json(new { success = true })); } string error = string.Empty; foreach (var err in ModelState.Values.SelectMany(v => v.Errors)) { error += "<li>" + err.ErrorMessage + "</li>"; } return(Json(new { success = false, err = "<ul>" + error + "</ul>" })); }
public async Task <ActionResult> ManageHangroup(string id, string title, string members, string uri, string action) { int gid; HanGroup g; switch (action) { case "new": if (string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(uri)) { TempData["HgMsg"] = "请输入名称和识别码"; } else { g = new HanGroup(); g.Title = title; g.GroupUri = uri; var errnames = string.Empty; if (members != null) { var names = members.Split(new char[] { ',', ',' }, StringSplitOptions.RemoveEmptyEntries); g.members = new List <HanGroupMember>(); foreach (var n in names) { var name = n.Trim(); var user = await _userManager.FindByNameAsync(name); if (user != null) { var member = new HanGroupMember(); member.Username = name; member.GroupLvl = 1; g.members.Add(member); } else { errnames += name + ','; } } } _db.HanGroups.Add(g); _db.SaveChanges(); TempData["HgMsg"] = errnames == string.Empty ? "添加成功" : "添加成功,以下用户不存在:" + errnames; } break; case "edit": if (string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(uri)) { TempData["HgMsg"] = "请输入名称和识别码"; } else if (int.TryParse(id, out gid)) { g = _db.HanGroups.Find(gid); g.Title = title; g.GroupUri = uri; var errnames = string.Empty; if (members != null) { var names = members.Split(new char[] { ',', ',' }, StringSplitOptions.RemoveEmptyEntries); var m = new List <HanGroupMember>(); var userm = g.members.Where(hm => hm.GroupLvl != 1); var comp = new HanGroupMemberComparer(); foreach (var n in names) { var name = n.Trim(); var user = await _userManager.FindByNameAsync(name); if (user != null) { //HanGroupMember member = userm.SingleOrDefault(um => um.Username == name); //if (member == null) //{ var member = new HanGroupMember(); member.HanGroupID = g.HanGroupID; member.Username = name; //} member.GroupLvl = 1; m.Add(member); } else { errnames += name + ','; } } g.members = g.members.Where(hm => hm.GroupLvl == 1).Intersect(m, comp).Union(m, comp).Union(userm, comp).ToList(); //member = newGroup1 Union Group2 } _db.SaveChanges(); TempData["HgMsg"] = errnames == string.Empty ? "编辑成功" : "编辑成功,以下用户不存在:" + errnames; } break; case "del": if (int.TryParse(id, out gid)) { g = _db.HanGroups.Find(gid); if (g.blogs == null || g.blogs.Count == 0) { _db.HanGroups.Remove(g); _db.SaveChanges(); TempData["HgMsg"] = "删除成功"; } else { TempData["HgMsg"] = "不可删除非空汉化组"; } } break; } _cache.Remove(CacheService.HanGroupListCacheKey); return(RedirectToAction("Manage", new { context = "Category" })); }