public async Task <MessageModel <string> > Assign([FromBody] AssignView assignView) { var data = new MessageModel <string>(); if (assignView.rid > 0) { data.success = true; var roleModulePermissions = await _roleModulePermissionServices.Query(d => d.RoleId == assignView.rid); var remove = roleModulePermissions.Where(d => !assignView.pids.Contains(d.PermissionId.ObjToInt())).Select(c => (object)c.Id); data.success &= remove.Any() ? await _roleModulePermissionServices.DeleteByIds(remove.ToArray()) : true; foreach (var item in assignView.pids) { var rmpitem = roleModulePermissions.Where(d => d.PermissionId == item); var moduleid = (await _permissionServices.Query(p => p.Id == item)).FirstOrDefault()?.Mid; if (!rmpitem.Any()) { RoleModulePermission roleModulePermission = new RoleModulePermission() { IsDeleted = false, RoleId = assignView.rid, ModuleId = moduleid.ObjToInt(), PermissionId = item, }; roleModulePermission.CreateId = _user.ID; roleModulePermission.CreateBy = _user.Name; data.success &= (await _roleModulePermissionServices.Add(roleModulePermission)) > 0; } else { foreach (var role in rmpitem) { if (!role.ModuleId.Equals(moduleid)) { role.ModuleId = moduleid.Value; await _roleModulePermissionServices.Update(role, new List <string> { "ModuleId" }); } } } } if (data.success) { _requirement.Permissions.Clear(); data.response = ""; data.msg = "保存成功"; } } return(data); }
public async Task <MessageModel <string> > Assign([FromBody] AssignView assignView) { var data = new MessageModel <string>(); try { if (assignView.rid > 0) { data.success = true; var roleModulePermissions = await _roleModulePermissionServices.Query(d => d.RoleId == assignView.rid); var remove = roleModulePermissions.Where(d => !assignView.pids.Contains(d.PermissionId.ObjToInt())).Select(c => (object)c.Id); data.success |= await _roleModulePermissionServices.DeleteByIds(remove.ToArray()); foreach (var item in assignView.pids) { var rmpitem = roleModulePermissions.Where(d => d.PermissionId == item); if (!rmpitem.Any()) { var moduleid = (await _permissionServices.Query(p => p.Id == item)).FirstOrDefault()?.Mid; RoleModulePermission roleModulePermission = new RoleModulePermission() { IsDeleted = false, RoleId = assignView.rid, ModuleId = moduleid.ObjToInt(), PermissionId = item, }; roleModulePermission.CreateId = _user.ID; roleModulePermission.CreateBy = _user.Name; data.success |= (await _roleModulePermissionServices.Add(roleModulePermission)) > 0; } } if (data.success) { data.response = ""; data.msg = "保存成功"; } } } catch (Exception) { data.success = false; } return(data); }