fix: 修复用户添加逻辑并增加MQTT定时连接功能

修复用户添加时未检查身份证号是否已存在的问题,改为根据身份证号判断是新增还是更新用户
为MQTT服务添加定时连接功能,每5分钟尝试连接一次,并避免重复连接相同配置
This commit is contained in:
dzq 2025-09-18 17:24:52 +08:00
parent c6b2c98bd8
commit 0b07c719a3
2 changed files with 38 additions and 6 deletions

View File

@ -198,12 +198,23 @@ public class Ab98UserApplicationService {
throw new ApiException(ErrorCode.Internal.INTERNAL_ERROR, "姓名不匹配"); throw new ApiException(ErrorCode.Internal.INTERNAL_ERROR, "姓名不匹配");
} }
AddAb98UserCommand addAb98UserCommand = getAddAb98UserCommand(ab98UserDto); AddAb98UserCommand addAb98UserCommand = getAddAb98UserCommand(ab98UserDto);
Ab98UserModel model = userModelFactory.create();
model.loadAddCommand(addAb98UserCommand); ab98UserEntity = userService.getByIdnum(addAb98UserCommand.getIdnum());
model.insert(); if (ab98UserEntity != null) {
ab98UserEntity = model.selectById(); Ab98UserModel model = userModelFactory.loadById(ab98UserEntity.getAb98UserId());
saveQyUserInfoByAb98(qyUser, ab98UserEntity); addAb98UserCommand.setAb98UserId(ab98UserEntity.getAb98UserId());
return ab98UserEntity; model.loadUpdateCommand(addAb98UserCommand);
model.updateById();
saveQyUserInfoByAb98(qyUser, ab98UserEntity);
return ab98UserEntity;
} else {
Ab98UserModel model = userModelFactory.create();
model.loadAddCommand(addAb98UserCommand);
model.insert();
ab98UserEntity = model.selectById();
saveQyUserInfoByAb98(qyUser, ab98UserEntity);
return ab98UserEntity;
}
} }
@NotNull @NotNull

View File

@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.*;
@ -31,6 +32,13 @@ public class MqttService implements MqttCallback {
&& config.getUsername().equals(other.getUsername()) && config.getUsername().equals(other.getUsername())
&& config.getPassword().equals(other.getPassword()); && config.getPassword().equals(other.getPassword());
} }
public boolean isSameConfig(MqttServerEntity other) {
if (other == null) {
return false;
}
return config.getMqttServerId().equals(other.getMqttServerId());
}
} }
@ -55,8 +63,21 @@ public class MqttService implements MqttCallback {
}); });
} }
@Scheduled(fixedRate = 5 * 60 * 1000) // 每5分钟执行一次 (300000毫秒)
public void scheduledConnect() {
try {
connect();
} catch (MqttException e) {
log.error("定时MQTT连接失败", e);
}
}
public void connect() throws MqttException { public void connect() throws MqttException {
for (MqttServerEntity config : mqttServerService.selectAll()) { for (MqttServerEntity config : mqttServerService.selectAll()) {
if (clientConfigs.stream().anyMatch(cc -> cc.isSameConfig(config))) {
continue;
}
// 检查是否已有相同连接的client实例 // 检查是否已有相同连接的client实例
ClientConfig existingConfig = clientConfigs.stream() ClientConfig existingConfig = clientConfigs.stream()
.filter(cc -> cc.isSameConnection(config)) .filter(cc -> cc.isSameConnection(config))