|
|
@@ -10,6 +10,7 @@ import com.fs.common.core.page.TableDataInfo;
|
|
|
import com.fs.common.enums.BusinessType;
|
|
|
import com.fs.common.utils.poi.ExcelUtil;
|
|
|
import com.fs.framework.datasource.DynamicDataSourceContextHolder;
|
|
|
+import com.fs.framework.datasource.TenantDataSourceContextHelper;
|
|
|
import com.fs.framework.datasource.TenantDataSourceManager;
|
|
|
import com.fs.proxy.domain.ProxyTenantRel;
|
|
|
import com.fs.proxy.service.ProxyTenantRelService;
|
|
|
@@ -42,6 +43,9 @@ public class CompanyAdminController extends BaseController {
|
|
|
@Autowired
|
|
|
private TenantDataSourceManager tenantDataSourceManager;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private TenantDataSourceContextHelper tenantContextHelper;
|
|
|
+
|
|
|
@Autowired(required = false)
|
|
|
private ProxyTenantRelService proxyTenantRelService;
|
|
|
|
|
|
@@ -263,68 +267,83 @@ public class CompanyAdminController extends BaseController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 编辑租户菜单(勾选/取消勾选/新增)
|
|
|
+ * 编辑租户菜单(全量替换:主库取模板 → 租户库删旧插新)。
|
|
|
+ * params.selected 须为当前勾选的完整菜单 ID 列表。
|
|
|
*/
|
|
|
@PreAuthorize("@ss.hasPermi('admin:company:edit')")
|
|
|
@Log(title = "编辑租户菜单", businessType = BusinessType.UPDATE)
|
|
|
@PostMapping("/{id}/menu/edit")
|
|
|
public R tenantMenuEdit(@PathVariable String id, @RequestBody Map<String, Object> params) {
|
|
|
- tenantDataSourceManager.clear();
|
|
|
String flag = (String) params.getOrDefault("flag", "sys");
|
|
|
List<Long> selected = toLongList(params.get("selected"));
|
|
|
- List<Long> unSelected = toLongList(params.get("unSelected"));
|
|
|
- try {
|
|
|
- TenantInfo tenantInfo = tenantInfoMapper.selectTenantInfoById(id);
|
|
|
- if (tenantInfo == null) return R.error("租户不存在");
|
|
|
- if (tenantInfo.getStatus() != null && tenantInfo.getStatus() == 2) return R.error("租户初始化中");
|
|
|
|
|
|
- if ("sys".equals(flag)) {
|
|
|
- List<SysMenu> addSysMenu = getAddSysMenu(tenantInfo, selected);
|
|
|
- tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
- return tenantInfoService.menuEdit(selected, unSelected, flag, addSysMenu, null);
|
|
|
- }
|
|
|
- List<TenantCompanyMenu> addCompanyMenu = getAddCompanyMenu(tenantInfo, selected);
|
|
|
- tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
- return tenantInfoService.menuEdit(selected, unSelected, flag, null, addCompanyMenu);
|
|
|
- } finally {
|
|
|
- tenantDataSourceManager.clear();
|
|
|
+ TenantInfo tenantInfo = tenantContextHelper.executeInMaster(() -> tenantInfoMapper.selectTenantInfoById(id));
|
|
|
+ if (tenantInfo == null) {
|
|
|
+ return R.error("租户不存在");
|
|
|
+ }
|
|
|
+ if (tenantInfo.getStatus() != null && tenantInfo.getStatus() == 2) {
|
|
|
+ return R.error("租户初始化中");
|
|
|
}
|
|
|
+
|
|
|
+ if ("sys".equals(flag)) {
|
|
|
+ List<SysMenu> assignSysMenu = tenantContextHelper.executeInMaster(() -> loadMasterSysMenus(selected));
|
|
|
+ return tenantContextHelper.executeInTenant(tenantInfo,
|
|
|
+ () -> tenantInfoService.menuAssignReplace(selected, flag, assignSysMenu, null));
|
|
|
+ }
|
|
|
+ List<TenantCompanyMenu> assignCompanyMenu = tenantContextHelper.executeInMaster(() -> loadMasterCompanyMenus(selected));
|
|
|
+ return tenantContextHelper.executeInTenant(tenantInfo,
|
|
|
+ () -> tenantInfoService.menuAssignReplace(selected, flag, null, assignCompanyMenu));
|
|
|
}
|
|
|
|
|
|
- private List<SysMenu> getAddSysMenu(TenantInfo tenantInfo, List<Long> selected) {
|
|
|
+ /** 主库 tenant_sys_menu 模板 */
|
|
|
+ private List<SysMenu> loadMasterSysMenus(List<Long> selected) {
|
|
|
if (selected == null || selected.isEmpty()) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
- tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
- List<Long> existIds = tenantInfoMapper.selectExistMenuIds();
|
|
|
- DynamicDataSourceContextHolder.setDataSourceType(com.fs.common.enums.DataSourceType.MASTER.name());
|
|
|
- List<Long> needAddIds = selected.stream().filter(mid -> !existIds.contains(mid)).collect(Collectors.toList());
|
|
|
- if (!needAddIds.isEmpty()) {
|
|
|
- return tenantInfoMapper.getTenSysMenuByIds(needAddIds);
|
|
|
- }
|
|
|
- return new ArrayList<>();
|
|
|
+ return tenantInfoMapper.getTenSysMenuByIds(selected);
|
|
|
}
|
|
|
|
|
|
- private List<TenantCompanyMenu> getAddCompanyMenu(TenantInfo tenantInfo, List<Long> selected) {
|
|
|
+ /** 主库 tenant_company_menu 模板 */
|
|
|
+ private List<TenantCompanyMenu> loadMasterCompanyMenus(List<Long> selected) {
|
|
|
if (selected == null || selected.isEmpty()) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
- tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
- List<Long> existIds = tenantInfoMapper.selectExistComMenuIds();
|
|
|
- DynamicDataSourceContextHolder.setDataSourceType(com.fs.common.enums.DataSourceType.MASTER.name());
|
|
|
- List<Long> needAddIds = selected.stream().filter(mid -> !existIds.contains(mid)).collect(Collectors.toList());
|
|
|
- if (!needAddIds.isEmpty()) {
|
|
|
- return tenantInfoMapper.getTenComMenuByIds(needAddIds);
|
|
|
+ return tenantInfoMapper.getTenComMenuByIds(selected);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Long> toLongList(Object raw) {
|
|
|
+ if (raw == null || !(raw instanceof List)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ List<?> list = (List<?>) raw;
|
|
|
+ List<Long> result = new ArrayList<>(list.size());
|
|
|
+ for (Object item : list) {
|
|
|
+ Long id = toLongId(item);
|
|
|
+ if (id != null) {
|
|
|
+ result.add(id);
|
|
|
+ }
|
|
|
}
|
|
|
- return new ArrayList<>();
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- private List<Long> toLongList(Object obj) {
|
|
|
- if (obj instanceof List) {
|
|
|
- return ((List<Number>) obj).stream().map(Number::longValue).collect(Collectors.toList());
|
|
|
+ private static Long toLongId(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (value instanceof Long) {
|
|
|
+ return (Long) value;
|
|
|
+ }
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).longValue();
|
|
|
+ }
|
|
|
+ if (value instanceof String) {
|
|
|
+ String s = ((String) value).trim();
|
|
|
+ if (s.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return Long.parseLong(s);
|
|
|
}
|
|
|
- return new ArrayList<>();
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|