parent
84a6725eae
commit
a0db881b2c
|
|
@ -3,8 +3,8 @@ spring:
|
|||
import:
|
||||
# - classpath:nacos.yml
|
||||
# - classpath:sdk.yml
|
||||
# - classpath:nacos-prod.yml
|
||||
# - classpath:sdk-prod.yml
|
||||
- classpath:nacos-prod2.yml
|
||||
- classpath:sdk-prod2.yml
|
||||
- classpath:nacos-prod.yml
|
||||
- classpath:sdk-prod.yml
|
||||
# - classpath:nacos-prod2.yml
|
||||
# - classpath:sdk-prod2.yml
|
||||
- classpath:swagger.yml
|
||||
|
|
|
|||
|
|
@ -42,6 +42,8 @@ import org.springframework.beans.BeanUtils;
|
|||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
|
@ -49,6 +51,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDate;
|
||||
|
|
@ -203,6 +206,38 @@ public class UserAddExe {
|
|||
} catch (Exception e) {
|
||||
log.error("用户变更记录保存失败(不影响用户主体), userId: {}, error: {}", userE.getId(), e.getMessage(), e);
|
||||
}
|
||||
runAfterCommitAsync(() -> syncDaHuaPersonAndWriteBack(userE));
|
||||
} catch (BizException e) {
|
||||
log.error("新增用户业务异常: {}", e.getMessage(), e);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("新增用户系统异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (!res) {
|
||||
throw new BizException("保存失败");
|
||||
}
|
||||
cmd.setId(userE.getId());
|
||||
cmd.setUsername(userE.getUsername());
|
||||
log.info("新增用户完成, userId: {}, username: {}", userE.getId(), userE.getUsername());
|
||||
return true;
|
||||
}
|
||||
|
||||
private void runAfterCommitAsync(Runnable runnable) {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
||||
@Override
|
||||
public void afterCommit() {
|
||||
CompletableFuture.runAsync(runnable);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
CompletableFuture.runAsync(runnable);
|
||||
}
|
||||
|
||||
private void syncDaHuaPersonAndWriteBack(UserE userE) {
|
||||
try {
|
||||
DaHuaSyncResult syncResult = syncDaHuaPerson(userE);
|
||||
if (syncResult.dahuaId != null) {
|
||||
try {
|
||||
|
|
@ -220,20 +255,9 @@ public class UserAddExe {
|
|||
} else {
|
||||
log.info("大华同步未返回dahuaId,跳过写回操作, userId={}, dahuaCode={}", userE.getId(), syncResult.dahuaCode);
|
||||
}
|
||||
} catch (BizException e) {
|
||||
log.error("新增用户业务异常: {}", e.getMessage(), e);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("新增用户系统异常: {}", e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
log.error("异步同步大华异常(不影响新增用户主流程), userId={}", userE.getId(), e);
|
||||
}
|
||||
if (!res) {
|
||||
throw new BizException("保存失败");
|
||||
}
|
||||
cmd.setId(userE.getId());
|
||||
cmd.setUsername(userE.getUsername());
|
||||
log.info("新增用户完成, userId: {}, username: {}", userE.getId(), userE.getUsername());
|
||||
return true;
|
||||
}
|
||||
|
||||
private DaHuaSyncResult syncDaHuaPerson(UserE userE) {
|
||||
|
|
|
|||
|
|
@ -30,8 +30,11 @@ import org.apache.dubbo.config.annotation.DubboReference;
|
|||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
|
|
@ -77,29 +80,16 @@ public class UserRemoveExe {
|
|||
}
|
||||
// ① 先本地逻辑删除(事务内,还未提交)
|
||||
userGateway.deletedUserById(id);
|
||||
deleteBaseUser(id);
|
||||
Long userId = id;
|
||||
Integer deleteDaHuaId = dahuaId;
|
||||
boolean deleteDaHua = needDeleteDaHua;
|
||||
runAfterCommitAsync(() -> {
|
||||
|
||||
if (needDeleteDaHua) {
|
||||
if (daHuaConfig.getDockFlag() == null || daHuaConfig.getDockFlag() != 1) {
|
||||
log.info("【单个删除-大华删除跳过】dockFlag未开启, userId={}", id);
|
||||
} else {
|
||||
try {
|
||||
DaHuaPersonDeleteCmd delCmd = new DaHuaPersonDeleteCmd();
|
||||
delCmd.setIds(Collections.singletonList(dahuaId.longValue()));
|
||||
SingleResponse<Boolean> delResp = zcloudDaHuaFacade.deletePerson(delCmd);
|
||||
if (delResp == null || !delResp.isSuccess()) {
|
||||
String errCode = delResp == null ? "" : delResp.getErrCode();
|
||||
String errMessage = delResp == null ? "大华Dubbo返回为空" : delResp.getErrMessage();
|
||||
if (errMessage == null || errMessage.trim().isEmpty()) errMessage = "大华人员删除失败";
|
||||
log.warn("【单个删除-大华删除失败(不影响本地数据)】userId={}, dahuaId={}, errCode={}, errMessage={}",
|
||||
id, dahuaId, errCode, errMessage);
|
||||
} else {
|
||||
log.info("【单个删除-大华删除成功】userId={}, dahuaId={}", id, dahuaId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("【单个删除-大华删除异常(不影响本地数据)】userId={}, dahuaId={}", id, dahuaId, e);
|
||||
}
|
||||
}
|
||||
if (deleteDaHua) {
|
||||
deleteDaHuaUser(userId, deleteDaHuaId);
|
||||
}
|
||||
});
|
||||
} catch (BizException e) {
|
||||
log.error("单个删除用户业务异常: {}", e.getMessage(), e);
|
||||
throw e;
|
||||
|
|
@ -218,28 +208,7 @@ public class UserRemoveExe {
|
|||
throw new BizException("本地批量删除用户失败");
|
||||
}
|
||||
|
||||
if (!dahuaIds.isEmpty()) {
|
||||
if (daHuaConfig.getDockFlag() == null || daHuaConfig.getDockFlag() != 1) {
|
||||
log.info("【批量删除-大华删除跳过】dockFlag未开启, idList={}", idList);
|
||||
} else {
|
||||
try {
|
||||
DaHuaPersonDeleteCmd delCmd = new DaHuaPersonDeleteCmd();
|
||||
delCmd.setIds(dahuaIds.stream().map(Integer::longValue).collect(Collectors.toList()));
|
||||
SingleResponse<Boolean> delResp = zcloudDaHuaFacade.deletePerson(delCmd);
|
||||
if (delResp == null || !delResp.isSuccess()) {
|
||||
String errCode = delResp == null ? "" : delResp.getErrCode();
|
||||
String errMessage = delResp == null ? "大华Dubbo返回为空" : delResp.getErrMessage();
|
||||
if (errMessage == null || errMessage.trim().isEmpty()) errMessage = "大华人员删除失败";
|
||||
log.warn("【批量删除-大华删除失败(不影响本地数据)】localIds={}, dahuaIds={}, errCode={}, errMessage={}",
|
||||
idList, dahuaIds, errCode, errMessage);
|
||||
} else {
|
||||
log.info("【批量删除-大华删除成功】dahuaIds={}", dahuaIds);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("【批量删除-大华删除异常(不影响本地数据)】localIds={}, dahuaIds={}", idList, dahuaIds, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
runAfterCommitAsync(() -> deleteDaHuaUsers(idList, dahuaIds));
|
||||
return true;
|
||||
} catch (BizException e) {
|
||||
log.error("批量删除用户业务异常: {}", e.getMessage(), e);
|
||||
|
|
@ -249,4 +218,83 @@ public class UserRemoveExe {
|
|||
throw new BizException("批量删除失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void runAfterCommitAsync(Runnable runnable) {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
||||
@Override
|
||||
public void afterCommit() {
|
||||
CompletableFuture.runAsync(runnable);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
CompletableFuture.runAsync(runnable);
|
||||
}
|
||||
|
||||
private void deleteBaseUser(Long userId) {
|
||||
try {
|
||||
log.info("删除底座用户开始,userId:{}", userId);
|
||||
Response response = userFacade.delete(userId);
|
||||
log.info("删除底座用户结束,userId:{},结果:{}", userId, JSONUtil.toJsonStr(response));
|
||||
} catch (Exception e) {
|
||||
log.error("删除底座用户异常,userId:{}", userId, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteDaHuaUser(Long userId, Integer dahuaId) {
|
||||
if (dahuaId == null) {
|
||||
return;
|
||||
}
|
||||
if (daHuaConfig.getDockFlag() == null || daHuaConfig.getDockFlag() != 1) {
|
||||
log.info("单个删除大华删除跳过,dockFlag未开启,userId={}", userId);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
DaHuaPersonDeleteCmd delCmd = new DaHuaPersonDeleteCmd();
|
||||
delCmd.setIds(Collections.singletonList(dahuaId.longValue()));
|
||||
SingleResponse<Boolean> delResp = zcloudDaHuaFacade.deletePerson(delCmd);
|
||||
if (delResp == null || !delResp.isSuccess()) {
|
||||
String errCode = delResp == null ? "" : delResp.getErrCode();
|
||||
String errMessage = delResp == null ? "大华Dubbo返回为空" : delResp.getErrMessage();
|
||||
if (errMessage == null || errMessage.trim().isEmpty()) {
|
||||
errMessage = "大华人员删除失败";
|
||||
}
|
||||
log.warn("单个删除大华删除失败,不影响本地数据,userId={},dahuaId={},errCode={},errMessage={}",
|
||||
userId, dahuaId, errCode, errMessage);
|
||||
} else {
|
||||
log.info("单个删除大华删除成功,userId={},dahuaId={}", userId, dahuaId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("单个删除大华删除异常,不影响本地数据,userId={},dahuaId={}", userId, dahuaId, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteDaHuaUsers(List<Long> idList, List<Integer> dahuaIds) {
|
||||
if (CollUtil.isEmpty(dahuaIds)) {
|
||||
return;
|
||||
}
|
||||
if (daHuaConfig.getDockFlag() == null || daHuaConfig.getDockFlag() != 1) {
|
||||
log.info("批量删除大华删除跳过,dockFlag未开启,idList={}", idList);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
DaHuaPersonDeleteCmd delCmd = new DaHuaPersonDeleteCmd();
|
||||
delCmd.setIds(dahuaIds.stream().map(Integer::longValue).collect(Collectors.toList()));
|
||||
SingleResponse<Boolean> delResp = zcloudDaHuaFacade.deletePerson(delCmd);
|
||||
if (delResp == null || !delResp.isSuccess()) {
|
||||
String errCode = delResp == null ? "" : delResp.getErrCode();
|
||||
String errMessage = delResp == null ? "大华Dubbo返回为空" : delResp.getErrMessage();
|
||||
if (errMessage == null || errMessage.trim().isEmpty()) {
|
||||
errMessage = "大华人员删除失败";
|
||||
}
|
||||
log.warn("批量删除大华删除失败,不影响本地数据,localIds={},dahuaIds={},errCode={},errMessage={}",
|
||||
idList, dahuaIds, errCode, errMessage);
|
||||
} else {
|
||||
log.info("批量删除大华删除成功,dahuaIds={}", dahuaIds);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("批量删除大华删除异常,不影响本地数据,localIds={},dahuaIds={}", idList, dahuaIds, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -502,7 +502,6 @@ public class UserRepositoryImpl extends BaseRepositoryImpl<UserMapper, UserDO> i
|
|||
.set("employment_flag", "0")
|
||||
.set("delete_enum", "TRUE");
|
||||
update(updateWrapper);
|
||||
userFacade.delete(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in New Issue