public ActionResult SelRoleSave(ViewRole r) { var b = new ViewBase(); var pemits = Request["cbPemit"]; List <selrole_pemit> role_nav = new List <selrole_pemit>(); using (var db = new EFContext()) { try { if (r.ID.IsNull()) { #region 添加 selrole entity = new selrole(); entity.ID = WebTools.getGUID(); entity.CreateTime = DateTime.Now; entity.IsAvailable = 1; entity.ShopID = ShopId; entity.CreateManagerID = ManagerId; entity.IsDefault = 0; entity.Title = r.Title; entity.Introduction = r.Introduction; db.selrole.Add(entity); if (pemits.IsNotNull()) { var str = pemits.Split(','); for (int i = 0; i < str.Length; i++) { var pr = new selrole_pemit(); pr.SelPemitID = str[i]; pr.SelRoleID = entity.ID; pr.ID = WebTools.getGUID(); db.selrole_pemit.Add(pr); } } #endregion } else { #region 修改 var model = db.selrole.Find(r.ID); if (model != null) { model.Title = r.Title; model.Introduction = r.Introduction; var selrolepemitlist = db.selrole_pemit.Where(x => x.SelRoleID == r.ID); if (selrolepemitlist.Count() > 0) { db.selrole_pemit.RemoveRange(selrolepemitlist); } if (pemits.IsNotNull()) { var str = pemits.Split(','); for (int i = 0; i < str.Length; i++) { var pr = new selrole_pemit(); pr.SelPemitID = str[i]; pr.SelRoleID = r.ID; pr.ID = WebTools.getGUID(); db.selrole_pemit.Add(pr); } //var str = pemits.Split(','); //for (int i = 0; i < str.Length; i++) //{ // var pr = new selrole_pemit(); // pr.SelPemitID = str[i]; // pr.SelRoleID = entity.ID; // pr.ID = WebTools.getGUID(); // role_nav.Add(pr); //} //var oldNav = db.selrole_pemit.Where(p => p.SelRoleID == entity.ID); ////先删除原来多余的 //foreach (var item in oldNav) //{ // var exsit = role_nav.Any(a => a.SelPemitID == item.SelPemitID); // if (exsit == false) // { // db.selrole_pemit.Remove(item); // } //} ////再添加原来没有的 //foreach (var item in role_nav) //{ // var exsit = oldNav.FirstOrDefault(a => a.SelPemitID == item.SelPemitID); // if (exsit == null) // { // db.selrole_pemit.Add(item); // } //} } } #endregion } db.SaveChanges(); b.IsSuccess = true; } catch (Exception e) { LogHelper.Error("添加角色:" + WebTools.getFinalException(e)); b.Message = "添加角色失败"; } if (b.IsSuccess) { b.Message = "操作成功"; b.Url = "/Permission/RoleSet"; //成功后跳转 } } return(Json(b)); }
public void TestInitDefaultRole() { using (var db = new EFContext()) { var role1011 = new selrole() { ID = "1011", Title = "云掌柜", Introduction = "云掌柜(免费版)", CreateManagerID = "", CreateTime = DateTime.Now, IsAvailable = 1, IsDefault = 1, ShopID = "", Sort = 0 }; var role1021 = new selrole() { ID = "1021", Title = "云掌柜", Introduction = "云掌柜(微店版)", CreateManagerID = "", CreateTime = DateTime.Now, IsAvailable = 1, IsDefault = 1, ShopID = "", Sort = 0 }; var role1031 = new selrole() { ID = "1031", Title = "云掌柜", Introduction = "云掌柜(聚餐版)", CreateManagerID = "", CreateTime = DateTime.Now, IsAvailable = 1, IsDefault = 1, ShopID = "", Sort = 0 }; var role1041 = new selrole() { ID = "1041", Title = "云掌柜", Introduction = "云掌柜(宴会版)", CreateManagerID = "", CreateTime = DateTime.Now, IsAvailable = 1, IsDefault = 1, ShopID = "", Sort = 0 }; var role1012 = new selrole() { ID = "1012", Title = "云掌柜", Introduction = "云掌柜(免费版+云库存)", CreateManagerID = "", CreateTime = DateTime.Now, IsAvailable = 1, IsDefault = 1, ShopID = "", Sort = 0 }; var role1022 = new selrole() { ID = "1022", Title = "云掌柜", Introduction = "云掌柜(微店版+云库存)", CreateManagerID = "", CreateTime = DateTime.Now, IsAvailable = 1, IsDefault = 1, ShopID = "", Sort = 0 }; var role1032 = new selrole() { ID = "1032", Title = "云掌柜", Introduction = "云掌柜(聚餐版+云库存)", CreateManagerID = "", CreateTime = DateTime.Now, IsAvailable = 1, IsDefault = 1, ShopID = "", Sort = 0 }; var role1042 = new selrole() { ID = "1042", Title = "云掌柜", Introduction = "云掌柜(宴会版+云库存)", CreateManagerID = "", CreateTime = DateTime.Now, IsAvailable = 1, IsDefault = 1, ShopID = "", Sort = 0 }; List <selrole> list = new List <selrole>(); list.Add(role1011); list.Add(role1012); list.Add(role1021); list.Add(role1022); list.Add(role1031); list.Add(role1032); list.Add(role1041); list.Add(role1042); foreach (var item in list) { //先删除默认角色数据 var queryrole = db.selrole.FirstOrDefault(x => x.ID == item.ID); if (queryrole != null) { db.selrole.Remove(queryrole); } var queryrolepemit = db.selrole_pemit.Where(x => x.SelRoleID == item.ID); if (queryrolepemit.Any()) { db.selrole_pemit.RemoveRange(queryrolepemit); } db.SaveChanges(); } { var selrolemodel = role1011; db.selrole.Add(selrolemodel); var queryrolepemit = db.selpemit.Where(x => x.PemitState.Contains("1")).ToList(); foreach (var item in queryrolepemit) { db.selrole_pemit.Add(new selrole_pemit() { ID = WebTools.getGUID(), SelRoleID = selrolemodel.ID, SelPemitID = item.ID }); } db.SaveChanges(); } { var selrolemodel = role1012; db.selrole.Add(selrolemodel); var queryrolepemit = db.selpemit.Where(x => x.PemitState.Contains("1") || x.PemitState.Contains("6")).ToList(); foreach (var item in queryrolepemit) { db.selrole_pemit.Add(new selrole_pemit() { ID = WebTools.getGUID(), SelRoleID = selrolemodel.ID, SelPemitID = item.ID }); } db.SaveChanges(); } { var selrolemodel = role1021; db.selrole.Add(selrolemodel); var queryrolepemit = db.selpemit.Where(x => x.PemitState.Contains("2")).ToList(); foreach (var item in queryrolepemit) { db.selrole_pemit.Add(new selrole_pemit() { ID = WebTools.getGUID(), SelRoleID = selrolemodel.ID, SelPemitID = item.ID }); } db.SaveChanges(); } { var selrolemodel = role1022; db.selrole.Add(selrolemodel); var queryrolepemit = db.selpemit.Where(x => x.PemitState.Contains("2") || x.PemitState.Contains("6")).ToList(); foreach (var item in queryrolepemit) { db.selrole_pemit.Add(new selrole_pemit() { ID = WebTools.getGUID(), SelRoleID = selrolemodel.ID, SelPemitID = item.ID }); } db.SaveChanges(); } { var selrolemodel = role1031; db.selrole.Add(selrolemodel); var queryrolepemit = db.selpemit.Where(x => x.PemitState.Contains("3")).ToList(); foreach (var item in queryrolepemit) { db.selrole_pemit.Add(new selrole_pemit() { ID = WebTools.getGUID(), SelRoleID = selrolemodel.ID, SelPemitID = item.ID }); } db.SaveChanges(); } { var selrolemodel = role1032; db.selrole.Add(selrolemodel); var queryrolepemit = db.selpemit.Where(x => x.PemitState.Contains("3") || x.PemitState.Contains("6")).ToList(); foreach (var item in queryrolepemit) { db.selrole_pemit.Add(new selrole_pemit() { ID = WebTools.getGUID(), SelRoleID = selrolemodel.ID, SelPemitID = item.ID }); } db.SaveChanges(); } { var selrolemodel = role1041; db.selrole.Add(selrolemodel); var queryrolepemit = db.selpemit.Where(x => x.PemitState.Contains("4")).ToList(); foreach (var item in queryrolepemit) { db.selrole_pemit.Add(new selrole_pemit() { ID = WebTools.getGUID(), SelRoleID = selrolemodel.ID, SelPemitID = item.ID }); } db.SaveChanges(); } { var selrolemodel = role1042; db.selrole.Add(selrolemodel); var queryrolepemit = db.selpemit.Where(x => x.PemitState.Contains("4") || x.PemitState.Contains("6")).ToList(); foreach (var item in queryrolepemit) { db.selrole_pemit.Add(new selrole_pemit() { ID = WebTools.getGUID(), SelRoleID = selrolemodel.ID, SelPemitID = item.ID }); } db.SaveChanges(); } } }