Merge remote-tracking branch 'base-version/main' into dev

This commit is contained in:
chenbowen
2025-11-13 18:45:11 +08:00
4 changed files with 104 additions and 10 deletions

View File

@@ -2,17 +2,25 @@ package com.zt.plat.framework.business.core.util;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.CompanyDeptInfo;
import com.zt.plat.framework.common.util.json.JsonUtils;
import com.zt.plat.framework.common.util.spring.SpringUtils;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.tenant.core.context.CompanyContextHolder;
import com.zt.plat.framework.web.core.util.WebFrameworkUtils;
import com.zt.plat.module.system.api.dept.DeptApi;
import com.zt.plat.module.system.api.dept.dto.CompanyDeptInfoRespDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
@@ -23,7 +31,10 @@ import static com.zt.plat.framework.security.core.util.SecurityFrameworkUtils.ge
/**
* @author chenbowen
*/
@Slf4j
public class BusinessDeptHandleUtil {
private static final String CONTEXT_KEY_COMPANY_DEPT_INFOS = "companyDeptInfos";
public static Set<CompanyDeptInfo> getBelongCompanyAndDept(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("application/json;charset=UTF-8");
String companyIdHeader = request.getHeader(WebFrameworkUtils.HEADER_VISIT_COMPANY_ID);
@@ -37,9 +48,7 @@ public class BusinessDeptHandleUtil {
currentLoginUser.setInfo(extraInfo);
}
Set<CompanyDeptInfo> companyDeptSet = JSONUtil.parseArray(extraInfo.getOrDefault(LoginUser.INFO_KEY_COMPANY_DEPT_SET, "[]")).stream()
.map(obj -> JSONUtil.toBean((JSONObject) obj, CompanyDeptInfo.class))
.collect(Collectors.toSet());
Set<CompanyDeptInfo> companyDeptSet = resolveCompanyDeptInfos(currentLoginUser, extraInfo);
// 1. 有 companyId
if (companyIdHeader != null && !companyIdHeader.isBlank()) {
@@ -84,6 +93,94 @@ public class BusinessDeptHandleUtil {
return companyDeptSet;
}
private static Set<CompanyDeptInfo> resolveCompanyDeptInfos(LoginUser loginUser, Map<String, String> extraInfo) {
if (loginUser == null) {
return Collections.emptySet();
}
Set<CompanyDeptInfo> cached = loginUser.getContext(CONTEXT_KEY_COMPANY_DEPT_INFOS, Set.class);
if (cached != null) {
return cached;
}
Set<CompanyDeptInfo> resolved = parseFromInfo(extraInfo);
if (resolved == null || resolved.isEmpty()) {
Set<CompanyDeptInfo> fetched = fetchCompanyDeptInfos(loginUser.getId());
if (!fetched.isEmpty()) {
resolved = fetched;
} else if (resolved == null) {
resolved = Collections.emptySet();
}
}
cacheCompanyDeptInfos(loginUser, extraInfo, resolved);
return resolved;
}
private static Set<CompanyDeptInfo> parseFromInfo(Map<String, String> extraInfo) {
if (extraInfo == null || !extraInfo.containsKey(LoginUser.INFO_KEY_COMPANY_DEPT_SET)) {
return null;
}
try {
return JSONUtil.parseArray(extraInfo.getOrDefault(LoginUser.INFO_KEY_COMPANY_DEPT_SET, "[]")).stream()
.map(obj -> JSONUtil.toBean((JSONObject) obj, CompanyDeptInfo.class))
.collect(Collectors.toCollection(LinkedHashSet::new));
} catch (Exception ex) {
log.warn("[parseFromInfo][解析公司部门信息失败] raw={}", extraInfo.get(LoginUser.INFO_KEY_COMPANY_DEPT_SET), ex);
return Collections.emptySet();
}
}
private static Set<CompanyDeptInfo> fetchCompanyDeptInfos(Long userId) {
if (userId == null) {
return Collections.emptySet();
}
try {
DeptApi deptApi = SpringUtils.getBean(DeptApi.class);
CommonResult<Set<CompanyDeptInfoRespDTO>> result = deptApi.getCompanyDeptInfoListByUserId(userId);
if (result == null || !result.isSuccess() || result.getData() == null) {
return Collections.emptySet();
}
return result.getData().stream()
.map(BusinessDeptHandleUtil::convert)
.collect(Collectors.toCollection(LinkedHashSet::new));
} catch (Exception ex) {
log.warn("[fetchCompanyDeptInfos][userId({}) 获取公司部门信息失败]", userId, ex);
return Collections.emptySet();
}
}
private static void cacheCompanyDeptInfos(LoginUser loginUser, Map<String, String> extraInfo, Set<CompanyDeptInfo> infos) {
if (infos == null) {
infos = Collections.emptySet();
}
loginUser.setContext(CONTEXT_KEY_COMPANY_DEPT_INFOS, infos);
if (extraInfo == null) {
return;
}
Set<Long> companyIds = infos.stream()
.map(CompanyDeptInfo::getCompanyId)
.filter(Objects::nonNull)
.collect(Collectors.toCollection(LinkedHashSet::new));
Set<Long> deptIds = infos.stream()
.map(CompanyDeptInfo::getDeptId)
.filter(Objects::nonNull)
.collect(Collectors.toCollection(LinkedHashSet::new));
extraInfo.put(LoginUser.INFO_KEY_COMPANY_DEPT_SET, JsonUtils.toJsonString(infos));
extraInfo.put(LoginUser.INFO_KEY_COMPANY_IDS, JsonUtils.toJsonString(companyIds));
extraInfo.put(LoginUser.INFO_KEY_DEPT_IDS, JsonUtils.toJsonString(deptIds));
}
private static CompanyDeptInfo convert(CompanyDeptInfoRespDTO dto) {
CompanyDeptInfo info = new CompanyDeptInfo();
info.setCompanyId(dto.getCompanyId());
info.setCompanyName(dto.getCompanyName());
info.setCompanyCode(dto.getCompanyCode());
info.setDeptId(dto.getDeptId());
info.setDeptName(dto.getDeptName());
info.setDeptCode(dto.getDeptCode());
return info;
}
private static boolean applyAutoSelection(LoginUser loginUser, HttpServletRequest request, CompanyDeptInfo info) {
if (info == null || info.getCompanyId() == null || info.getCompanyId() <= 0
|| info.getDeptId() == null || info.getDeptId() <= 0) {

View File

@@ -155,9 +155,9 @@ class BusinessHeaderInterceptorTest {
setLoginUserForTest(loginUser);
boolean result = interceptor.preHandle(request, response, handlerMethod);
assertFalse(result);
// 可选:verify(request).setAttribute("visit-company-id", String.valueOf(deptInfo.getCompanyId()));
// 可选:verify(request).setAttribute("visit-dept-id", String.valueOf(deptInfo.getDeptId()));
assertTrue(result);
verify(request).setAttribute(eq("visit-company-id"), eq(deptInfo.getCompanyId()));
verify(request).setAttribute(eq("visit-dept-id"), eq(deptInfo.getDeptId()));
}
/**