大华口门对接

(cherry picked from commit 604d96b3fa)
dahua_koumen
zhangxiongfeng 2026-07-29 17:15:00 +08:00
parent 8b5a9d6f7f
commit 460679ea62
1 changed files with 121 additions and 5 deletions

View File

@ -235,6 +235,10 @@ public class UserAddExe {
}
private DaHuaSyncResult syncDaHuaPerson(UserE userE) {
return syncDaHuaPerson(userE, userE.getFaceFile(), true);
}
private DaHuaSyncResult syncDaHuaPerson(UserE userE, Object avatarSource, boolean throwOnFail) {
Map<String, Object> personMap = new HashMap<>();
personMap.put("name", userE.getName());
String idCard = userE.getUserIdCard();
@ -254,16 +258,28 @@ public class UserAddExe {
personMap.put("departmentId", 1L);
personMap.put("departmentType", 2);
personMap.put("type", 3);
personMap.put("nation", 1);
personMap.put("nationName", "汉族");
if (userE.getNation() != null && !userE.getNation().trim().isEmpty()) {
try {
personMap.put("nation", Integer.valueOf(userE.getNation().trim()));
} catch (Exception e) {
log.warn("用户民族编码转Integer失败用默认值1nation={}", userE.getNation());
personMap.put("nation", 1);
}
} else {
personMap.put("nation", 1);
}
personMap.put("nationName", (userE.getNationName() != null && !userE.getNationName().trim().isEmpty()) ? userE.getNationName().trim() : "汉族");
personMap.put("service", "evo-thirdParty");
Object faceFile = userE.getFaceFile();
Object faceFile = avatarSource != null ? avatarSource : userE.getFaceFile();
java.util.function.Function<Object, String> extractThumbFromSingle = (singleObj) -> {
if (singleObj == null) {
return null;
}
if (singleObj instanceof String) {
return singleObj.toString();
}
if (singleObj instanceof Map) {
Map<String, Object> faceFileMap = (Map<String, Object>) singleObj;
Object thumbUrlObj = faceFileMap.get("thumbUrl");
@ -447,10 +463,18 @@ public class UserAddExe {
if (plainIdCard != null && plainIdCard.length() >= 14) {
maskedIdCard = plainIdCard.substring(0, 6) + "********" + plainIdCard.substring(plainIdCard.length() - 4);
}
result.setDahuaCode(plainIdCard);
userE.setDahuaCode(plainIdCard);
if (success == null || !success) {
String errCode = (String) syncResult.getOrDefault("errCode", "");
String errMessage = (String) syncResult.getOrDefault("errMessage", "大华人员同步失败");
throw new BizException("同步大华平台失败:" + errMessage);
if (throwOnFail) {
throw new BizException("同步大华平台失败:" + errMessage);
} else {
log.warn("同步大华平台未成功(重复信息或其他原因,不影响本地数据保存): errCode={}, errMessage={}, userId={}, idCardMask={}",
errCode, errMessage, userE.getId(), maskedIdCard);
return result;
}
}
Object data = syncResult.get("data");
if (data != null) {
@ -850,12 +874,56 @@ public class UserAddExe {
@Transactional(rollbackFor = Exception.class)
public boolean executeXgf(UserXgfAddCmd cmd) {
log.info("开始新增相关方用户name: {}, corpinfoId: {}, phone: {}", cmd.getName(), cmd.getCorpinfoId(), cmd.getPhone());
try {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attrs != null) {
HttpServletRequest req = attrs.getRequest();
String orgidHeader = req.getHeader("orgid");
if (orgidHeader != null && cmd.getCorpinfoId() == null) {
try {
cmd.setCorpinfoId(Long.parseLong(orgidHeader.trim()));
} catch (Exception e) {
log.warn("orgid 转 Long 失败: {}", orgidHeader);
}
}
}
} catch (Exception e) {
log.warn("获取请求头异常: {}", e.getMessage());
}
Long tenantId = null;
SSOUser ssoUser = AuthContext.getCurrentUser();
Long tenantId = ssoUser.getTenantId();
if (ssoUser != null) {
tenantId = ssoUser.getTenantId();
}
UserE userE = new UserE();
BeanUtils.copyProperties(cmd, userE);
// tenantId 优先级cmd 传值 -> AuthContext 上下文 -> ssoUser
if (!ObjectUtils.isEmpty(userE.getTenantId())) {
tenantId = userE.getTenantId();
} else if (!ObjectUtils.isEmpty(userE.getCorpinfoId())) {
tenantId = userE.getCorpinfoId();
} else {
try {
Long ctxTenantId = AuthContext.getTenantId();
if (ctxTenantId != null) {
tenantId = ctxTenantId;
}
} catch (Exception e) {
log.warn("从 AuthContext 获取 tenantId 失败: {}", e.getMessage());
}
}
if (tenantId == null && ObjectUtils.isEmpty(userE.getCorpinfoId())) {
throw new BizException("无法获取租户信息,请登录后重试或在请求中指定 corpinfoId");
}
if (ObjectUtils.isEmpty(userE.getCorpinfoId())) {
userE.setCorpinfoId(tenantId);
}
userE.initAdd(tenantId, userE);
CorpInfoDO corpInfoDO = corpInfoRepository.getById(userE.getCorpinfoId());
if (corpInfoDO == null) {
throw new BizException("企业信息不存在corpinfoId=" + userE.getCorpinfoId());
}
userE.setUserType(CorpTypeEnum.getUserTypeByCode(corpInfoDO.getType()));
Long roleId = userRepository.getDefaultRoleId();
if (roleId == null) {
@ -928,6 +996,54 @@ public class UserAddExe {
userExpandInfoRepository.updateByPhone(userE.getPhone(),userE.getFlowFlag());
}
// ========== 相关方用户同步大华(统一用 /person/sync重复信息返回false直接跳过不影响本地==========
Long xgfUserId = addFlag ? userE.getId() : userDOUpdate.getId();
if (xgfUserId == null) {
log.warn("相关方用户保存后未获取到userId跳过大华同步及dahua信息写回, addFlag={}", addFlag);
} else {
Object xgfAvatarSource = cmd.getUserImg();
if (xgfAvatarSource == null) {
xgfAvatarSource = cmd.getUserAvatarUrl();
}
DaHuaSyncResult xgfSyncResult = null;
try {
xgfSyncResult = syncDaHuaPerson(userE, xgfAvatarSource, false);
} catch (Exception e) {
log.error("相关方用户同步大华异常不影响本地保存继续执行写回身份证号为dahuaCode, userId={}, addFlag={}, error={}",
xgfUserId, addFlag, e.getMessage(), e);
}
// 无论大华同步成功/失败/返回false/抛异常都尝试写回dahuaId/dahuaCode
// dahuaId = 大华返回的id成功时才有失败/异常/重复时为null
// dahuaCode = 前端传的身份证号经过Base64解码的明文syncDaHuaPerson里即使失败也已经set到result了
Integer xgfDahuaId = xgfSyncResult != null ? xgfSyncResult.dahuaId : null;
String xgfDahuaCode = xgfSyncResult != null ? xgfSyncResult.dahuaCode : null;
// 如果xgfSyncResult异常或dahuaCode为空兜底直接把userE.getUserIdCard()做一次Base64解码写入dahuaCode保证100%写回
if ((xgfDahuaCode == null || xgfDahuaCode.trim().isEmpty()) && userE.getUserIdCard() != null && !userE.getUserIdCard().trim().isEmpty()) {
String idCardRaw = userE.getUserIdCard().trim();
try {
xgfDahuaCode = new String(java.util.Base64.getDecoder().decode(idCardRaw), java.nio.charset.StandardCharsets.UTF_8);
} catch (Exception e) {
xgfDahuaCode = idCardRaw;
}
}
if (xgfDahuaId != null || (xgfDahuaCode != null && !xgfDahuaCode.trim().isEmpty())) {
try {
UserE updateDahua = new UserE();
updateDahua.setId(xgfUserId);
updateDahua.setDahuaId(xgfDahuaId);
updateDahua.setDahuaCode(xgfDahuaCode);
userRepository.updateDahuaInfo(updateDahua.getId(), updateDahua.getDahuaId(), updateDahua.getDahuaCode());
log.info("相关方用户写回dahuaId/dahuaCode成功, userId={}, addFlag={}, dahuaId={}, dahuaCode={}",
xgfUserId, addFlag, xgfDahuaId, xgfDahuaCode);
} catch (Exception e) {
log.error("相关方用户写回dahuaId/dahuaCode失败, userId={}, addFlag={}, dahuaId={}, dahuaCode={}",
xgfUserId, addFlag, xgfDahuaId, xgfDahuaCode, e);
}
} else {
log.warn("相关方用户既无dahuaId也无dahuaCode可写回跳过update, userId={}, addFlag={}", xgfUserId, addFlag);
}
}
// =========================================================================
//页面相关保存user_corp
/**