public async Task <MessageModel <string> > GetRMPMaps() { var data = new MessageModel <string>() { success = true, msg = "" }; if (_env.IsDevelopment()) { // 获取数据,当然,你可以做个where查询 var rmps = await _roleModulePermissionServices.GetRMPMaps(); var rmpSerializeStr = JsonConvert.SerializeObject(rmps); // 将序列化的json生成到tsv文件 var roleModulePermissions = JsonConvert.DeserializeObject <List <RoleModulePermission> >(rmpSerializeStr); data.success = true; data.msg = "生成成功!"; } else { data.success = false; data.msg = "当前不处于开发模式,代码生成不可用!"; } return(data); }
public async Task <MessageModel <string> > DataMigrateFromOld2New() { var data = new MessageModel <string>() { success = true, msg = "" }; if (_env.IsDevelopment()) { try { // 获取权限集合数据 var rmps = await _roleModulePermissionServices.GetRMPMaps(); // 当然,你可以做个where查询 //rmps = rmps.Where(d => d.ModuleId > 88).ToList(); // 开启事务,保证数据一致性 _unitOfWork.BeginTran(); var rid = 0; var pid = 0; var mid = 0; var rpmid = 0; // 注意信息的完整性,不要重复添加,确保主库没有要添加的数据 foreach (var item in rmps) { // 角色信息,防止重复添加,做了判断 if (item.Role != null) { var isExit = (await _roleServices.Query(d => d.Name == item.Role.Name && d.IsDeleted == false)).FirstOrDefault(); if (isExit == null) { rid = await _roleServices.Add(item.Role); Console.WriteLine($"Role Added:{item.Role.Name}"); } else { rid = isExit.Id; } } // 菜单 if (item.Permission != null) { pid = await _permissionServices.Add(item.Permission); Console.WriteLine($"Permission Added:{item.Permission.Name}"); } // 接口 if (item.Module != null) { mid = await _moduleServices.Add(item.Module); Console.WriteLine($"Module Added:{item.Module.LinkUrl}"); } // 关系 if (rid > 0 && pid > 0 && mid > 0) { rpmid = await _roleModulePermissionServices.Add(new RoleModulePermission() { IsDeleted = false, CreateTime = DateTime.Now, ModifyTime = DateTime.Now, ModuleId = mid, PermissionId = pid, RoleId = rid, }); Console.WriteLine($"RMP Added:{rpmid}"); } } _unitOfWork.CommitTran(); data.success = true; data.msg = "导入成功!"; } catch (Exception) { _unitOfWork.RollbackTran(); } } else { data.success = false; data.msg = "当前不处于开发模式,代码生成不可用!"; } return(data); }
public async Task <MessageModel <string> > DataMigrateFromOld2New() { var data = new MessageModel <string>() { success = true, msg = "" }; var filterPermissionId = 122; if (_env.IsDevelopment()) { try { var apiList = await _moduleServices.Query(d => d.IsDeleted == false); var permissionsAllList = await _permissionServices.Query(d => d.IsDeleted == false); var permissions = permissionsAllList.Where(d => d.Pid == 0).ToList(); var rmps = await _roleModulePermissionServices.GetRMPMaps(); List <PM> pms = new(); // 当然,你可以做个where查询 rmps = rmps.Where(d => d.PermissionId >= filterPermissionId).ToList(); InitPermissionTree(permissions, permissionsAllList, apiList); var actionPermissionIds = permissionsAllList.Where(d => d.Id >= filterPermissionId).Select(d => d.Id).ToList(); List <int> filterPermissionIds = new(); FilterPermissionTree(permissionsAllList, actionPermissionIds, filterPermissionIds); permissions = permissions.Where(d => filterPermissionIds.Contains(d.Id)).ToList(); // 开启事务,保证数据一致性 _unitOfWork.BeginTran(); // 注意信息的完整性,不要重复添加,确保主库没有要添加的数据 // 1、保持菜单和接口 await SavePermissionTreeAsync(permissions, pms); var rid = 0; var pid = 0; var mid = 0; var rpmid = 0; // 2、保存关系表 foreach (var item in rmps) { // 角色信息,防止重复添加,做了判断 if (item.Role != null) { var isExit = (await _roleServices.Query(d => d.Name == item.Role.Name && d.IsDeleted == false)).FirstOrDefault(); if (isExit == null) { rid = await _roleServices.Add(item.Role); Console.WriteLine($"Role Added:{item.Role.Name}"); } else { rid = isExit.Id; } } pid = (pms.FirstOrDefault(d => d.PidOld == item.PermissionId)?.PidNew).ObjToInt(); mid = (pms.FirstOrDefault(d => d.MidOld == item.ModuleId)?.MidNew).ObjToInt(); // 关系 if (rid > 0 && pid > 0) { rpmid = await _roleModulePermissionServices.Add(new RoleModulePermission() { IsDeleted = false, CreateTime = DateTime.Now, ModifyTime = DateTime.Now, ModuleId = mid, PermissionId = pid, RoleId = rid, }); Console.WriteLine($"RMP Added:{rpmid}"); } } _unitOfWork.CommitTran(); data.success = true; data.msg = "导入成功!"; } catch (Exception) { _unitOfWork.RollbackTran(); } } else { data.success = false; data.msg = "当前不处于开发模式,代码生成不可用!"; } return(data); }