大华口门对接

(cherry picked from commit bcb9d32b95)
dahua_koumen
zhangxiongfeng 2026-07-28 16:58:06 +08:00
parent 566e2c9158
commit 70da6bacad
2 changed files with 107 additions and 4 deletions

View File

@ -33,6 +33,7 @@ import com.zcloud.basic.info.persistence.dataobject.*;
import com.zcloud.basic.info.persistence.repository.*; import com.zcloud.basic.info.persistence.repository.*;
import com.zcloud.gbscommon.utils.*; import com.zcloud.gbscommon.utils.*;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -64,6 +65,11 @@ import java.util.Base64;
@AllArgsConstructor @AllArgsConstructor
@Slf4j @Slf4j
public class UserAddExe { public class UserAddExe {
@Data
public static class DaHuaSyncResult {
private Integer dahuaId;
private String dahuaCode;
}
private final UserGateway userGateway; private final UserGateway userGateway;
private final UserExpandInfoGateway userExpandInfoGateway; private final UserExpandInfoGateway userExpandInfoGateway;
private UserCoConvertor userCoConvertor; private UserCoConvertor userCoConvertor;
@ -179,8 +185,7 @@ public class UserAddExe {
userE.setUserType(CorpTypeEnum.getUserTypeByCode(corpInfoDO.getType())); userE.setUserType(CorpTypeEnum.getUserTypeByCode(corpInfoDO.getType()));
userE.resetPassword(corpInfoDO.getType()); userE.resetPassword(corpInfoDO.getType());
//先同步大华人脸平台,成功后再保存本地 // ① 先本地落库 + 底座GBS新增。GBS失败抛 RuntimeException整个事务回滚
syncDaHuaPerson(userE);
res = userGateway.add(userE); res = userGateway.add(userE);
if (corpInfoDO != null && !ObjectUtils.isEmpty(corpInfoDO.getCorpName())) { if (corpInfoDO != null && !ObjectUtils.isEmpty(corpInfoDO.getCorpName())) {
corpName = corpInfoDO.getCorpName(); corpName = corpInfoDO.getCorpName();
@ -196,6 +201,23 @@ public class UserAddExe {
} catch (Exception e) { } catch (Exception e) {
log.error("用户变更记录保存失败(不影响用户主体), userId: {}, error: {}", userE.getId(), e.getMessage(), e); log.error("用户变更记录保存失败(不影响用户主体), userId: {}, error: {}", userE.getId(), e.getMessage(), e);
} }
// ② 本地和底座都成功后,最后才同步大华。失败→抛异常→回滚本地+GBS避免留下"大华人成功但本地没数据"
DaHuaSyncResult syncResult = syncDaHuaPerson(userE);
if (syncResult.dahuaId != null || (syncResult.dahuaCode != null && !syncResult.dahuaCode.trim().isEmpty())) {
try {
UserE updateDahua = new UserE();
updateDahua.setId(userE.getId());
updateDahua.setDahuaId(syncResult.dahuaId);
updateDahua.setDahuaCode(syncResult.dahuaCode);
userRepository.updateDahuaInfo(updateDahua.getId(), updateDahua.getDahuaId(), updateDahua.getDahuaCode());
log.info("写回dahuaId/dahuaCode成功, userId={}, dahuaId={}, dahuaCode={}",
userE.getId(), syncResult.dahuaId, syncResult.dahuaCode);
} catch (Exception e) {
log.error("写回dahuaId/dahuaCode失败, userId={}, dahuaId={}, dahuaCode={}",
userE.getId(), syncResult.dahuaId, syncResult.dahuaCode, e);
throw new BizException("写回大华信息到本地失败:" + e.getMessage());
}
}
} catch (BizException e) { } catch (BizException e) {
log.error("新增用户业务异常: {}", e.getMessage(), e); log.error("新增用户业务异常: {}", e.getMessage(), e);
throw e; throw e;
@ -212,7 +234,7 @@ public class UserAddExe {
return true; return true;
} }
private void syncDaHuaPerson(UserE userE) { private DaHuaSyncResult syncDaHuaPerson(UserE userE) {
Map<String, Object> personMap = new HashMap<>(); Map<String, Object> personMap = new HashMap<>();
personMap.put("name", userE.getName()); personMap.put("name", userE.getName());
String idCard = userE.getUserIdCard(); String idCard = userE.getUserIdCard();
@ -416,8 +438,10 @@ public class UserAddExe {
Map<String, Object> syncResult = daHuaConfig.syncPerson(personMap, extraHeaders); Map<String, Object> syncResult = daHuaConfig.syncPerson(personMap, extraHeaders);
Boolean success = (Boolean) syncResult.get("success"); Boolean success = (Boolean) syncResult.get("success");
Boolean skipped = (Boolean) syncResult.get("skipped"); Boolean skipped = (Boolean) syncResult.get("skipped");
DaHuaSyncResult result = new DaHuaSyncResult();
result.setDahuaCode(plainIdCard);
if (skipped != null && skipped) { if (skipped != null && skipped) {
return; return result;
} }
String maskedIdCard = plainIdCard; String maskedIdCard = plainIdCard;
if (plainIdCard != null && plainIdCard.length() >= 14) { if (plainIdCard != null && plainIdCard.length() >= 14) {
@ -436,6 +460,7 @@ public class UserAddExe {
if (idObj != null) { if (idObj != null) {
try { try {
Integer dahuaId = Integer.valueOf(idObj.toString()); Integer dahuaId = Integer.valueOf(idObj.toString());
result.setDahuaId(dahuaId);
userE.setDahuaId(dahuaId); userE.setDahuaId(dahuaId);
log.info("设置大华dahuaId成功, dahuaId={}", dahuaId); log.info("设置大华dahuaId成功, dahuaId={}", dahuaId);
} catch (Exception e) { } catch (Exception e) {
@ -448,6 +473,7 @@ public class UserAddExe {
if (idObj != null) { if (idObj != null) {
try { try {
Integer dahuaId = Integer.valueOf(idObj.toString()); Integer dahuaId = Integer.valueOf(idObj.toString());
result.setDahuaId(dahuaId);
userE.setDahuaId(dahuaId); userE.setDahuaId(dahuaId);
log.info("设置大华dahuaId成功, dahuaId={}", dahuaId); log.info("设置大华dahuaId成功, dahuaId={}", dahuaId);
} catch (Exception e) { } catch (Exception e) {
@ -457,6 +483,7 @@ public class UserAddExe {
} }
} }
userE.setDahuaCode(plainIdCard); userE.setDahuaCode(plainIdCard);
return result;
} }
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)

View File

@ -188,4 +188,80 @@ public class DaHuaConfig {
return resultMap; return resultMap;
} }
} }
public Map<String, Object> deletePerson(List<Integer> ids) {
return deletePerson(ids, null);
}
public Map<String, Object> deletePerson(List<Integer> ids, Map<String, String> extraHeaders) {
Map<String, Object> resultMap = new HashMap<>();
log.info("========== 大华人员(删除)调用开始 ==========");
log.info("dockFlag配置值={}, primeportUrl={}, primeportGateway={}, ids={}", dockFlag, primeportUrl, primeportGateway, ids);
if (dockFlag == null || dockFlag != 1) {
log.info("大华对接未开启(dockFlag={}), 跳过删除大华人员", dockFlag);
resultMap.put("success", true);
resultMap.put("skipped", true);
return resultMap;
}
if (ids == null || ids.isEmpty()) {
log.info("待删除大华ids为空, 跳过删除");
resultMap.put("success", true);
resultMap.put("skipped", true);
return resultMap;
}
String baseUrl = primeportUrl;
if (baseUrl == null || baseUrl.trim().isEmpty()) {
throw new RuntimeException("大华对接网关地址未配置(dahua.config.primeportUrl)");
}
baseUrl = baseUrl.trim();
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}
String gw = (primeportGateway == null || primeportGateway.trim().isEmpty()) ? "basicInfo" : primeportGateway.trim();
String url = baseUrl + "/" + gw + "/dahua/person/delete";
Map<String, Object> reqMap = new HashMap<>();
reqMap.put("ids", ids);
String reqJson = JSONUtil.toJsonStr(reqMap);
log.info("【实际请求URL】method=POST, url={}", url);
log.info("【传给大华的请求体(JSON)】{}", reqJson);
try {
HttpRequest request = HttpRequest.post(url)
.header("Content-Type", "application/json;charset=UTF-8")
.timeout(30000)
.body(reqJson);
if (extraHeaders != null && !extraHeaders.isEmpty()) {
extraHeaders.forEach((k, v) -> {
if (k != null && v != null) {
request.header(k, v);
}
});
log.info("【透传请求头】{}", extraHeaders);
}
String respBody = request.execute().body();
log.info("【大华接口原始返回】{}", respBody);
if (respBody == null || respBody.trim().isEmpty()) {
resultMap.put("success", false);
resultMap.put("errCode", "EMPTY_RESPONSE");
resultMap.put("errMessage", "大华人员删除接口返回为空");
return resultMap;
}
JSONObject respJson = JSONUtil.parseObj(respBody);
Boolean success = respJson.getBool("success", false);
String errCode = respJson.getStr("errCode", "");
String errMessage = respJson.getStr("errMessage", "");
Object data = respJson.get("data");
resultMap.put("success", success);
resultMap.put("errCode", errCode);
resultMap.put("errMessage", errMessage);
resultMap.put("data", data);
log.info("【解析后返回】success={}, errCode={}, errMessage={}, data={}", success, errCode, errMessage, data);
return resultMap;
} catch (Exception e) {
log.error("调用大华人员删除接口异常: {}", e.getMessage(), e);
resultMap.put("success", false);
resultMap.put("errCode", "HTTP_EXCEPTION");
resultMap.put("errMessage", "调用大华人员删除接口异常: " + e.getMessage());
return resultMap;
}
}
} }