feat: 接入gbs用户信息

dev
岑溪 2026-06-25 16:31:06 +08:00
parent 03e3d35efc
commit 2f711ff22d
23 changed files with 260 additions and 276 deletions

View File

@ -1,9 +1,9 @@
package org.qinan.safetyeval.adapter.config; package org.qinan.safetyeval.adapter.config;
import org.qinan.safetyeval.infrastructure.adapter.ThreadLocalUserInfoAdapter; import org.qinan.safetyeval.infrastructure.adapter.ThreadLocalUserInfoAdapter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -15,20 +15,8 @@ import javax.servlet.http.HttpServletResponse;
* MyMetaObjectHandler org_id / tenant_id * MyMetaObjectHandler org_id / tenant_id
*/ */
@Configuration @Configuration
@Profile("local")
public class LocalDevUserContextConfig implements WebMvcConfigurer { public class LocalDevUserContextConfig implements WebMvcConfigurer {
@Value("${safety-eval.public-api.system-user-id:1}")
private Long systemUserId;
@Value("${safety-eval.public-api.system-user-name:local-dev}")
private String systemUserName;
@Value("${safety-eval.public-api.tenant-id:1001}")
private Long tenantId;
@Value("${safety-eval.public-api.org-id:1}")
private Long orgId;
private static final String[] LOCAL_DEV_PATHS = { private static final String[] LOCAL_DEV_PATHS = {
"/org-info/**", "/org-info/**",
@ -47,7 +35,10 @@ public class LocalDevUserContextConfig implements WebMvcConfigurer {
registry.addInterceptor(new HandlerInterceptor() { registry.addInterceptor(new HandlerInterceptor() {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
ThreadLocalUserInfoAdapter.set(systemUserId, systemUserName, tenantId, orgId); String orgInfoId = request.getHeader("orgInfoId");
if (StringUtils.hasText(orgInfoId)) {
ThreadLocalUserInfoAdapter.set(Long.valueOf(orgInfoId));
}
return true; return true;
} }
@ -56,6 +47,6 @@ public class LocalDevUserContextConfig implements WebMvcConfigurer {
Object handler, Exception exception) { Object handler, Exception exception) {
ThreadLocalUserInfoAdapter.clear(); ThreadLocalUserInfoAdapter.clear();
} }
}).addPathPatterns(LOCAL_DEV_PATHS); }).addPathPatterns(LOCAL_DEV_PATHS).excludePathPatterns("/**/getInfo/**");
} }
} }

View File

@ -1,44 +0,0 @@
package org.qinan.safetyeval.adapter.config;
import org.qinan.safetyeval.infrastructure.adapter.ThreadLocalUserInfoAdapter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Configuration
public class PublicApiConfig implements WebMvcConfigurer {
@Value("${safety-eval.public-api.system-user-id:0}")
private Long systemUserId;
@Value("${safety-eval.public-api.system-user-name:system}")
private String systemUserName;
@Value("${safety-eval.public-api.tenant-id:0}")
private Long tenantId;
@Value("${safety-eval.public-api.org-id:0}")
private Long orgId;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
ThreadLocalUserInfoAdapter.set(systemUserId, systemUserName, tenantId, orgId);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception exception) {
ThreadLocalUserInfoAdapter.clear();
}
}).addPathPatterns("/public/org-personnel/**");
}
}

View File

@ -33,12 +33,18 @@ public class OrgInfoController {
return orgInfoApi.add(cmd); return orgInfoApi.add(cmd);
} }
@ApiOperation("查询机构信息详情") @ApiOperation("根据查询机构信息详情")
@GetMapping("/get") @GetMapping("/get")
public SingleResponse<OrgInfoCO> get(@RequestParam Long id) { public SingleResponse<OrgInfoCO> get(@RequestParam Long id) {
return orgInfoApi.get(id); return orgInfoApi.get(id);
} }
@ApiOperation("查询机构信息详情(内部查询)")
@GetMapping("/getInfo")
public SingleResponse<OrgInfoCO> getInfo() {
return orgInfoApi.getInfo();
}
@ApiOperation("修改机构信息") @ApiOperation("修改机构信息")
@PostMapping("/modify") @PostMapping("/modify")
public SingleResponse<OrgInfoCO> modify(@Validated @RequestBody OrgInfoModifyCmd cmd) { public SingleResponse<OrgInfoCO> modify(@Validated @RequestBody OrgInfoModifyCmd cmd) {

View File

@ -75,7 +75,7 @@ public class OrgDepartmentExecutor implements OrgDepartmentApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setParentId(query.getParentId()); domainQuery.setParentId(query.getParentId());
domainQuery.setDeptName(query.getDeptName()); domainQuery.setDeptName(query.getDeptName());

View File

@ -95,7 +95,7 @@ public class OrgEquipmentExecutor implements OrgEquipmentApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setDeviceName(query.getDeviceName()); domainQuery.setDeviceName(query.getDeviceName());
domainQuery.setInstrumentType(query.getInstrumentType()); domainQuery.setInstrumentType(query.getInstrumentType());
domainQuery.setDeviceType(query.getDeviceType()); domainQuery.setDeviceType(query.getDeviceType());

View File

@ -142,6 +142,12 @@ public class OrgInfoExecutor implements OrgInfoApi {
pageResult.getTotal()); pageResult.getTotal());
} }
@Override
public SingleResponse<OrgInfoCO> getInfo() {
OrgInfoEntity entity = orgInfoDomainService.getInfo();
return SingleResponse.success(toCO(entity));
}
private OrgInfoCO toCO(OrgInfoEntity entity) { private OrgInfoCO toCO(OrgInfoEntity entity) {
if (entity == null) { if (entity == null) {
return null; return null;

View File

@ -91,7 +91,7 @@ public class OrgPersonnelCertExecutor implements OrgPersonnelCertApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setPersonnelId(query.getPersonnelId()); domainQuery.setPersonnelId(query.getPersonnelId());
domainQuery.setCertName(query.getCertName()); domainQuery.setCertName(query.getCertName());
domainQuery.setCertTypeCode(query.getCertTypeCode()); domainQuery.setCertTypeCode(query.getCertTypeCode());

View File

@ -74,7 +74,7 @@ public class OrgPersonnelChangeExecutor implements OrgPersonnelChangeApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setPersonnelId(query.getPersonnelId()); domainQuery.setPersonnelId(query.getPersonnelId());
domainQuery.setChangeItem(query.getChangeItem()); domainQuery.setChangeItem(query.getChangeItem());

View File

@ -1,5 +1,8 @@
package org.qinan.safetyeval.app.executor; package org.qinan.safetyeval.app.executor;
import com.jjb.saas.system.client.user.facade.UserFacade;
import com.jjb.saas.system.client.user.request.FacadeUserAddCmd;
import org.apache.dubbo.config.annotation.DubboReference;
import org.qinan.safetyeval.app.support.OrgPersonnelChangeRecorder; import org.qinan.safetyeval.app.support.OrgPersonnelChangeRecorder;
import org.qinan.safetyeval.app.support.OrgPersonnelViewEnricher; import org.qinan.safetyeval.app.support.OrgPersonnelViewEnricher;
import org.qinan.safetyeval.client.api.OrgPersonnelApi; import org.qinan.safetyeval.client.api.OrgPersonnelApi;
@ -43,12 +46,16 @@ public class OrgPersonnelExecutor implements OrgPersonnelApi {
@Autowired(required = false) @Autowired(required = false)
private OrgResignApplyGateway orgResignApplyGateway; private OrgResignApplyGateway orgResignApplyGateway;
// @DubboReference
// private UserFacade userFacade;
@Override @Override
public SingleResponse<OrgPersonnelCO> add(OrgPersonnelAddCmd cmd) { public SingleResponse<OrgPersonnelCO> add(OrgPersonnelAddCmd cmd) {
OrgPersonnelEntity entity = toEntity(cmd); OrgPersonnelEntity entity = toEntity(cmd);
OrgPersonnelEntity result = orgPersonnelDomainService.add(entity); OrgPersonnelEntity result = orgPersonnelDomainService.add(entity);
OrgPersonnelCO co = toCO(result); OrgPersonnelCO co = toCO(result);
enrich(co); enrich(co);
// 同步用户信息GBS
return SingleResponse.success(co); return SingleResponse.success(co);
} }
@ -95,7 +102,7 @@ public class OrgPersonnelExecutor implements OrgPersonnelApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setDeptId(query.getDeptId()); domainQuery.setDeptId(query.getDeptId());
domainQuery.setPostId(query.getPostId()); domainQuery.setPostId(query.getPostId());
domainQuery.setUserName(query.getUserName()); domainQuery.setUserName(query.getUserName());
@ -103,7 +110,7 @@ public class OrgPersonnelExecutor implements OrgPersonnelApi {
domainQuery.setEmploymentStatusCode(query.getEmploymentStatusCode()); domainQuery.setEmploymentStatusCode(query.getEmploymentStatusCode());
if (query.getResignAuditStatus() != null && orgResignApplyGateway != null) { if (query.getResignAuditStatus() != null && orgResignApplyGateway != null) {
domainQuery.setPersonnelIds(orgResignApplyGateway.listPersonnelIdsByAuditStatus( domainQuery.setPersonnelIds(orgResignApplyGateway.listPersonnelIdsByAuditStatus(
query.getResignAuditStatus(), AuthUserContextAdapter.getCurrentOrgId())); query.getResignAuditStatus(), query.getOrgId()));
} }
PageResult<OrgPersonnelEntity> pageResult = orgPersonnelDomainService.page(domainQuery); PageResult<OrgPersonnelEntity> pageResult = orgPersonnelDomainService.page(domainQuery);

View File

@ -71,7 +71,7 @@ public class OrgPositionExecutor implements OrgPositionApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setDeptId(query.getDeptId()); domainQuery.setDeptId(query.getDeptId());
domainQuery.setPositionName(query.getPositionName()); domainQuery.setPositionName(query.getPositionName());

View File

@ -85,7 +85,7 @@ public class OrgQualificationExecutor implements OrgQualificationApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setCertName(query.getCertName()); domainQuery.setCertName(query.getCertName());
domainQuery.setCertNo(query.getCertNo()); domainQuery.setCertNo(query.getCertNo());
domainQuery.setEnableFlag(query.getEnableFlag()); domainQuery.setEnableFlag(query.getEnableFlag());

View File

@ -106,7 +106,7 @@ public class OrgResignApplyExecutor implements OrgResignApplyApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setPersonnelId(query.getPersonnelId()); domainQuery.setPersonnelId(query.getPersonnelId());
domainQuery.setAuditStatusCode(query.getAuditStatusCode()); domainQuery.setAuditStatusCode(query.getAuditStatusCode());
domainQuery.setApplicantName(query.getApplicantName()); domainQuery.setApplicantName(query.getApplicantName());

View File

@ -111,7 +111,7 @@ public class QualFilingChangeExecutor implements QualFilingChangeApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setOriginFilingId(query.getOriginFilingId()); domainQuery.setOriginFilingId(query.getOriginFilingId());
domainQuery.setFilingNo(query.getFilingNo()); domainQuery.setFilingNo(query.getFilingNo());
domainQuery.setFilingStatusCode(query.getFilingStatusCode()); domainQuery.setFilingStatusCode(query.getFilingStatusCode());

View File

@ -113,7 +113,7 @@ public class QualFilingExecutor implements QualFilingApi {
domainQuery.setPageNum(query.getCurrent()); domainQuery.setPageNum(query.getCurrent());
domainQuery.setPageSize(query.getSize()); domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId()); domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(AuthUserContextAdapter.getCurrentOrgId()); domainQuery.setOrgId(query.getOrgId());
domainQuery.setFilingNo(query.getFilingNo()); domainQuery.setFilingNo(query.getFilingNo());
domainQuery.setFilingUnitName(query.getFilingUnitName()); domainQuery.setFilingUnitName(query.getFilingUnitName());
domainQuery.setFilingStatusCode(query.getFilingStatusCode()); domainQuery.setFilingStatusCode(query.getFilingStatusCode());

View File

@ -23,4 +23,6 @@ public interface OrgInfoApi {
SingleResponse<Void> delete(Long id); SingleResponse<Void> delete(Long id);
PageResponse<OrgInfoCO> page(OrgInfoPageQuery query); PageResponse<OrgInfoCO> page(OrgInfoPageQuery query);
SingleResponse<OrgInfoCO> getInfo();
} }

View File

@ -22,4 +22,7 @@ public interface OrgInfoGateway {
void delete(Long id); void delete(Long id);
PageResult<OrgInfoEntity> page(OrgInfoQuery query); PageResult<OrgInfoEntity> page(OrgInfoQuery query);
OrgInfoEntity getInfo();
} }

View File

@ -48,4 +48,8 @@ public class OrgInfoDomainService {
public PageResult<OrgInfoEntity> page(OrgInfoQuery query) { public PageResult<OrgInfoEntity> page(OrgInfoQuery query) {
return orgInfoGateway.page(query); return orgInfoGateway.page(query);
} }
public OrgInfoEntity getInfo() {
return orgInfoGateway.getInfo();
}
} }

View File

@ -1,7 +1,5 @@
package org.qinan.safetyeval.infrastructure.adapter; package org.qinan.safetyeval.infrastructure.adapter;
import org.qinan.safetyeval.domain.adapter.UserInfoAdapter;
import org.springframework.stereotype.Component;
/** /**
* ThreadLocal * ThreadLocal
@ -12,41 +10,18 @@ import org.springframework.stereotype.Component;
* *
* @author safety-eval * @author safety-eval
*/ */
@Component public class ThreadLocalUserInfoAdapter {
public class ThreadLocalUserInfoAdapter implements UserInfoAdapter {
private static final ThreadLocal<Long> USER_ID = new ThreadLocal<>();
private static final ThreadLocal<String> USER_NAME = new ThreadLocal<>();
private static final ThreadLocal<Long> TENANT_ID = new ThreadLocal<>();
private static final ThreadLocal<Long> ORG_ID = new ThreadLocal<>(); private static final ThreadLocal<Long> ORG_ID = new ThreadLocal<>();
@Override public static Long get() {
public Long getCurrentUserId() {
return USER_ID.get();
}
@Override
public String getCurrentUserName() {
return USER_NAME.get();
}
@Override
public Long getCurrentTenantId() {
return TENANT_ID.get();
}
@Override
public Long getCurrentOrgId() {
return ORG_ID.get(); return ORG_ID.get();
} }
/** /**
* 线 * 线
*/ */
public static void set(Long userId, String userName, Long tenantId, Long orgId) { public static void set(Long orgId) {
USER_ID.set(userId);
USER_NAME.set(userName);
TENANT_ID.set(tenantId);
ORG_ID.set(orgId); ORG_ID.set(orgId);
} }
@ -54,9 +29,6 @@ public class ThreadLocalUserInfoAdapter implements UserInfoAdapter {
* 线 * 线
*/ */
public static void clear() { public static void clear() {
USER_ID.remove();
USER_NAME.remove();
TENANT_ID.remove();
ORG_ID.remove(); ORG_ID.remove();
} }
} }

View File

@ -9,13 +9,17 @@ import org.qinan.safetyeval.domain.query.OrgInfoQuery;
import org.qinan.safetyeval.domain.query.PageResult; import org.qinan.safetyeval.domain.query.PageResult;
import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter; import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter;
import org.qinan.safetyeval.infrastructure.dataobject.OrgInfoDO; import org.qinan.safetyeval.infrastructure.dataobject.OrgInfoDO;
import org.qinan.safetyeval.infrastructure.dataobject.OrgPersonnelDO;
import org.qinan.safetyeval.infrastructure.mapper.OrgInfoMapper; import org.qinan.safetyeval.infrastructure.mapper.OrgInfoMapper;
import org.qinan.safetyeval.infrastructure.mapper.OrgPersonnelMapper;
import org.qinan.safetyeval.infrastructure.support.InsertFieldDefaults; import org.qinan.safetyeval.infrastructure.support.InsertFieldDefaults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -28,6 +32,8 @@ public class OrgInfoGatewayImpl implements OrgInfoGateway {
@Resource @Resource
private OrgInfoMapper orgInfoMapper; private OrgInfoMapper orgInfoMapper;
@Resource
private OrgPersonnelMapper orgPersonnelMapper;
@Override @Override
public OrgInfoEntity save(OrgInfoEntity entity) { public OrgInfoEntity save(OrgInfoEntity entity) {
@ -93,6 +99,24 @@ public class OrgInfoGatewayImpl implements OrgInfoGateway {
return PageResult.of(entities, result.getTotal(), result.getCurrent(), result.getSize()); return PageResult.of(entities, result.getTotal(), result.getCurrent(), result.getSize());
} }
@Override
public OrgInfoEntity getInfo() {
// 查询人员有无机构信息
Long currentUserId = AuthUserContextAdapter.getCurrentUserId();
OrgPersonnelDO orgPersonnelDO = orgPersonnelMapper.selectById(currentUserId);
LambdaQueryWrapper<OrgInfoDO> lqw = new LambdaQueryWrapper<OrgInfoDO>()
.eq(OrgInfoDO::getCreateId, currentUserId);
if (orgPersonnelDO != null && orgPersonnelDO.getOrgId() != null) {
lqw.or().eq(OrgInfoDO::getId, orgPersonnelDO.getOrgId());
}
lqw.last("limit 1").orderByDesc(OrgInfoDO::getId);
List<OrgInfoDO> dataObject = orgInfoMapper.selectList(lqw);
if (dataObject.isEmpty()) {
return null;
}
return toEntity(dataObject.get(0));
}
private OrgInfoDO toDO(OrgInfoEntity entity) { private OrgInfoDO toDO(OrgInfoEntity entity) {
OrgInfoDO dataObject = new OrgInfoDO(); OrgInfoDO dataObject = new OrgInfoDO();
dataObject.setUnitName(entity.getUnitName()); dataObject.setUnitName(entity.getUnitName());

View File

@ -1,152 +1,152 @@
package org.qinan.safetyeval.infrastructure.handler; //package org.qinan.safetyeval.infrastructure.handler;
//
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; //import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject; //import org.apache.ibatis.reflection.MetaObject;
import org.qinan.safetyeval.domain.adapter.UserInfoAdapter; //import org.qinan.safetyeval.domain.adapter.UserInfoAdapter;
import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter; //import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter;
import org.springframework.beans.factory.annotation.Value; //import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; //import org.springframework.stereotype.Component;
//
import javax.annotation.Resource; //import javax.annotation.Resource;
import java.time.LocalDateTime; //import java.time.LocalDateTime;
//
/** ///**
* MyBatis Plus // * MyBatis Plus 自动填充处理器
* <p> // * <p>
* insert update GBS // * 在 insert 和 update 操作时自动填充 GBS 标准字段,
* AuthUserContextAdapter SSO // * 优先通过 AuthUserContextAdapter 获取 SSO 真实登录用户信息,
* UserInfoAdapterThreadLocal 退 // * 其次通过 UserInfoAdapterThreadLocal 方式)获取,最后回退到配置默认值。
* 使 strictFill null // * 使用 strictFill 策略:仅当字段值为 null 时才填充。
* </p> // * </p>
* // *
* @author safety-eval // * @author safety-eval
*/ // */
@Component //@Component
public class MyMetaObjectHandler implements MetaObjectHandler { //public class MyMetaObjectHandler implements MetaObjectHandler {
//
/** 系统环境标识 */ // /** 系统环境标识 */
private static final String DEFAULT_ENV = "prod"; // private static final String DEFAULT_ENV = "prod";
//
@Resource // @Resource
private UserInfoAdapter userInfoAdapter; // private UserInfoAdapter userInfoAdapter;
//
/** 无登录上下文时的回退值local 联调配置 org-id=1生产默认 0 不生效) */ // /** 无登录上下文时的回退值local 联调配置 org-id=1生产默认 0 不生效) */
@Value("${safety-eval.public-api.org-id:0}") // @Value("${safety-eval.public-api.org-id:0}")
private Long fallbackOrgId; // private Long fallbackOrgId;
//
@Value("${safety-eval.public-api.tenant-id:0}") // @Value("${safety-eval.public-api.tenant-id:0}")
private Long fallbackTenantId; // private Long fallbackTenantId;
//
@Value("${safety-eval.public-api.system-user-id:0}") // @Value("${safety-eval.public-api.system-user-id:0}")
private Long fallbackUserId; // private Long fallbackUserId;
//
@Value("${safety-eval.public-api.system-user-name:system}") // @Value("${safety-eval.public-api.system-user-name:system}")
private String fallbackUserName; // private String fallbackUserName;
//
/** // /**
* ID AuthUserContextAdapterSSO UserInfoAdapterThreadLocal 退 // * 解析用户ID优先 AuthUserContextAdapterSSO → UserInfoAdapterThreadLocal → 配置回退
*/ // */
private Long resolveUserId() { // private Long resolveUserId() {
Long userId = AuthUserContextAdapter.getCurrentUserId(); // Long userId = AuthUserContextAdapter.getCurrentUserId();
if (userId != null) { // if (userId != null) {
return userId; // return userId;
} // }
userId = userInfoAdapter.getCurrentUserId(); // userId = userInfoAdapter.getCurrentUserId();
if (userId == null && fallbackUserId != null && fallbackUserId > 0) { // if (userId == null && fallbackUserId != null && fallbackUserId > 0) {
userId = fallbackUserId; // userId = fallbackUserId;
} // }
return userId; // return userId;
} // }
//
/** // /**
* AuthUserContextAdapterSSO UserInfoAdapterThreadLocal 退 // * 解析用户名:优先 AuthUserContextAdapterSSO → UserInfoAdapterThreadLocal → 配置回退
*/ // */
private String resolveUserName() { // private String resolveUserName() {
String userName = AuthUserContextAdapter.getCurrentUserName(); // String userName = AuthUserContextAdapter.getCurrentUserName();
if (userName != null) { // if (userName != null) {
return userName; // return userName;
} // }
userName = userInfoAdapter.getCurrentUserName(); // userName = userInfoAdapter.getCurrentUserName();
if (userName == null && fallbackUserName != null) { // if (userName == null && fallbackUserName != null) {
userName = fallbackUserName; // userName = fallbackUserName;
} // }
return userName; // return userName;
} // }
//
/** // /**
* ID AuthUserContextAdapterSSO UserInfoAdapterThreadLocal 退 // * 解析租户ID优先 AuthUserContextAdapterSSO → UserInfoAdapterThreadLocal → 配置回退
*/ // */
private Long resolveTenantId() { // private Long resolveTenantId() {
Long tenantId = AuthUserContextAdapter.getCurrentTenantId(); // Long tenantId = AuthUserContextAdapter.getCurrentTenantId();
if (tenantId != null) { // if (tenantId != null) {
return tenantId; // return tenantId;
} // }
tenantId = userInfoAdapter.getCurrentTenantId(); // tenantId = userInfoAdapter.getCurrentTenantId();
if (tenantId == null && fallbackTenantId != null && fallbackTenantId > 0) { // if (tenantId == null && fallbackTenantId != null && fallbackTenantId > 0) {
tenantId = fallbackTenantId; // tenantId = fallbackTenantId;
} // }
return tenantId; // return tenantId;
} // }
//
/** // /**
* ID AuthUserContextAdapterSSO UserInfoAdapterThreadLocal 退 // * 解析机构ID优先 AuthUserContextAdapterSSO → UserInfoAdapterThreadLocal → 配置回退
*/ // */
private Long resolveOrgId() { // private Long resolveOrgId() {
Long orgId = AuthUserContextAdapter.getCurrentOrgId(); // Long orgId = AuthUserContextAdapter.getCurrentOrgId();
if (orgId != null) { // if (orgId != null) {
return orgId; // return orgId;
} // }
orgId = userInfoAdapter.getCurrentOrgId(); // orgId = userInfoAdapter.getCurrentOrgId();
if (orgId == null && fallbackOrgId != null && fallbackOrgId > 0) { // if (orgId == null && fallbackOrgId != null && fallbackOrgId > 0) {
orgId = fallbackOrgId; // orgId = fallbackOrgId;
} // }
return orgId; // return orgId;
} // }
//
@Override // @Override
public void insertFill(MetaObject metaObject) { // public void insertFill(MetaObject metaObject) {
LocalDateTime now = LocalDateTime.now(); // LocalDateTime now = LocalDateTime.now();
Long userId = resolveUserId(); // Long userId = resolveUserId();
String userName = resolveUserName(); // String userName = resolveUserName();
Long tenantId = resolveTenantId(); // Long tenantId = resolveTenantId();
Long orgId = resolveOrgId(); // Long orgId = resolveOrgId();
//
// 严格填充:仅当字段值为 null 时填充,优先级最低 // // 严格填充:仅当字段值为 null 时填充,优先级最低
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, now); // this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, now);
this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, now); // this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, now);
this.strictInsertFill(metaObject, "deleteEnum", String.class, "false"); // this.strictInsertFill(metaObject, "deleteEnum", String.class, "false");
this.strictInsertFill(metaObject, "env", String.class, DEFAULT_ENV); // this.strictInsertFill(metaObject, "env", String.class, DEFAULT_ENV);
this.strictInsertFill(metaObject, "version", Integer.class, 0); // this.strictInsertFill(metaObject, "version", Integer.class, 0);
//
// 用户相关字段:仅当解析值不为 null 时才填充 // // 用户相关字段:仅当解析值不为 null 时才填充
if (userId != null) { // if (userId != null) {
this.strictInsertFill(metaObject, "createId", Long.class, userId); // this.strictInsertFill(metaObject, "createId", Long.class, userId);
this.strictInsertFill(metaObject, "updateId", Long.class, userId); // this.strictInsertFill(metaObject, "updateId", Long.class, userId);
} // }
if (userName != null) { // if (userName != null) {
this.strictInsertFill(metaObject, "createName", String.class, userName); // this.strictInsertFill(metaObject, "createName", String.class, userName);
this.strictInsertFill(metaObject, "updateName", String.class, userName); // this.strictInsertFill(metaObject, "updateName", String.class, userName);
} // }
if (tenantId != null) { // if (tenantId != null) {
this.strictInsertFill(metaObject, "tenantId", Long.class, tenantId); // this.strictInsertFill(metaObject, "tenantId", Long.class, tenantId);
} // }
if (orgId != null) { // if (orgId != null) {
this.strictInsertFill(metaObject, "orgId", Long.class, orgId); // this.strictInsertFill(metaObject, "orgId", Long.class, orgId);
} // }
} // }
//
@Override // @Override
public void updateFill(MetaObject metaObject) { // public void updateFill(MetaObject metaObject) {
LocalDateTime now = LocalDateTime.now(); // LocalDateTime now = LocalDateTime.now();
Long userId = resolveUserId(); // Long userId = resolveUserId();
String userName = resolveUserName(); // String userName = resolveUserName();
//
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, now); // this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, now);
if (userId != null) { // if (userId != null) {
this.strictUpdateFill(metaObject, "updateId", Long.class, userId); // this.strictUpdateFill(metaObject, "updateId", Long.class, userId);
} // }
if (userName != null) { // if (userName != null) {
this.strictUpdateFill(metaObject, "updateName", String.class, userName); // this.strictUpdateFill(metaObject, "updateName", String.class, userName);
} // }
} // }
} //}

View File

@ -1,5 +1,6 @@
package org.qinan.safetyeval.infrastructure.support; package org.qinan.safetyeval.infrastructure.support;
import org.qinan.safetyeval.infrastructure.adapter.ThreadLocalUserInfoAdapter;
import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter; import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -41,6 +42,9 @@ public final class InsertFieldDefaults {
String userName = AuthUserContextAdapter.getCurrentUserName(); String userName = AuthUserContextAdapter.getCurrentUserName();
Long tenantId = AuthUserContextAdapter.getCurrentTenantId(); Long tenantId = AuthUserContextAdapter.getCurrentTenantId();
Long orgId = AuthUserContextAdapter.getCurrentOrgId(); Long orgId = AuthUserContextAdapter.getCurrentOrgId();
// 机构信息id
Long orgInfoId = ThreadLocalUserInfoAdapter.get();
if (userId != null) { if (userId != null) {
setIfNull(dataObject, "createId", userId); setIfNull(dataObject, "createId", userId);
@ -54,7 +58,8 @@ public final class InsertFieldDefaults {
setIfNull(dataObject, "tenantId", tenantId); setIfNull(dataObject, "tenantId", tenantId);
} }
if (orgId != null) { if (orgId != null) {
setIfNull(dataObject, "orgId", orgId); // 机构信息id
setIfNull(dataObject, "orgId", orgInfoId);
} }
} }

View File

@ -1,9 +1,12 @@
package org.qinan.safetyeval.infrastructure.support; package org.qinan.safetyeval.infrastructure.support;
import org.qinan.safetyeval.domain.adapter.UserInfoAdapter; import org.qinan.safetyeval.domain.exception.BizException;
import org.qinan.safetyeval.domain.exception.ErrorCode;
import org.qinan.safetyeval.infrastructure.adapter.ThreadLocalUserInfoAdapter;
import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter; import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -13,30 +16,14 @@ import javax.annotation.Resource;
@Component @Component
public class OrgContextResolver { public class OrgContextResolver {
@Resource
private UserInfoAdapter userInfoAdapter;
@Value("${safety-eval.public-api.org-id:0}")
private Long fallbackOrgId;
public Long resolveOrgId(Long entityOrgId) { public Long resolveOrgId(Long entityOrgId) {
if (entityOrgId != null) { if (entityOrgId != null) {
return entityOrgId; return entityOrgId;
} }
// 优先 SSO 真实登录用户的 orgId Long orgId = ThreadLocalUserInfoAdapter.get();
Long orgId = AuthUserContextAdapter.getCurrentOrgId(); if (orgId == null) {
if (orgId != null) { throw new BizException(ErrorCode.ORG_INFO_NOT_FOUND);
}
return orgId; return orgId;
} }
// 其次 ThreadLocal 方式
orgId = userInfoAdapter.getCurrentOrgId();
if (orgId != null) {
return orgId;
}
// 最后配置回退
if (fallbackOrgId != null && fallbackOrgId > 0) {
return fallbackOrgId;
}
return null;
}
} }

View File

@ -0,0 +1,21 @@
package org.qinan.safetyeval.start.config;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* ID JS Number
*/
@Configuration
public class JacksonConfig {
@Bean
public Jackson2ObjectMapperBuilderCustomizer longToStringCustomizer() {
return builder -> {
builder.serializerByType(Long.class, ToStringSerializer.instance);
builder.serializerByType(Long.TYPE, ToStringSerializer.instance);
};
}
}