优化交换机分表、新增丢包率clientId记录

This commit is contained in:
gaoyutao
2025-11-24 14:00:49 +08:00
parent c6e778c74e
commit c0f51ef007
17 changed files with 718 additions and 86 deletions
@@ -10,8 +10,9 @@ import com.ruoyi.system.service.EpsInitialTrafficDataService;
import com.ruoyi.system.service.IEpsServerRevenueConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@@ -237,7 +238,8 @@ public class EpsServerRevenueConfigServiceImpl implements IEpsServerRevenueConfi
/**
* 批量处理接口名称
*/
private void processInterfaceNames(List<EpsInitialTrafficData> trafficDataList) {
@Transactional(rollbackFor = Exception.class, isolation = Isolation.REPEATABLE_READ)
public void processInterfaceNames(List<EpsInitialTrafficData> trafficDataList) {
// 分类处理:新增列表 vs 更新列表
List<AllInterfaceName> namesToInsert = new ArrayList<>();
List<AllInterfaceName> namesToUpdate = new ArrayList<>();
@@ -308,30 +310,13 @@ public class EpsServerRevenueConfigServiceImpl implements IEpsServerRevenueConfi
try {
allInterfaceNameMapper.batchInsert(namesToInsert);
log.info("新增接口名称数量:{}", namesToInsert.size());
} catch (DuplicateKeyException e) {
// 如果批量插入出现重复,转为逐条插入(带异常处理)
log.warn("批量插入出现重复,转为逐条处理");
namesToInsert.forEach(record -> {
try {
allInterfaceNameMapper.insertAllInterfaceName(record);
} catch (DuplicateKeyException ex) {
// 重复记录转为更新
AllInterfaceName query = new AllInterfaceName();
query.setInterfaceName(record.getInterfaceName());
query.setClientId(record.getClientId());
query.setServerIp(record.getServerIp());
List<AllInterfaceName> existing = allInterfaceNameMapper.selectByNames(query);
if (!existing.isEmpty()) {
record.setId(existing.get(0).getId());
allInterfaceNameMapper.updateAllInterfaceName(record);
}
}
});
} catch (Exception e) {
log.error("新增接口名称失败:{}", e.getMessage());
}
}
if (!namesToUpdate.isEmpty()) {
namesToUpdate.sort(Comparator.comparing(AllInterfaceName::getId));
allInterfaceNameMapper.batchUpdate(namesToUpdate);
log.info("更新接口名称数量:{}", namesToUpdate.size());
}
@@ -13,7 +13,6 @@ import com.ruoyi.system.util.PaginationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
@@ -225,7 +224,7 @@ public class InitialSwitchInfoDetailsServiceImpl implements IInitialSwitchInfoDe
*/
public List<InitialSwitchInfoDetails> getSwitchTrafficMsgSharding(InitialSwitchInfoDetails queryParam) {
// 获取涉及的表名
Set<String> tableNames = TableSubUtil.getTableNamesBetween(queryParam.getStartTime(), queryParam.getEndTime(), "initial_switch_info");
Set<String> tableNames = TableSubUtil.getExistingTableNamesBetween(queryParam.getStartTime(), queryParam.getEndTime(), "initial_switch_info");
// 并行查询各表
return tableNames.parallelStream()
@@ -353,6 +352,7 @@ public class InitialSwitchInfoDetailsServiceImpl implements IInitialSwitchInfoDe
/**
* 批量处理接口名称
*/
@Transactional(rollbackFor = Exception.class, isolation = Isolation.REPEATABLE_READ)
private void processSwitchInterfaceNames(List<InitialSwitchInfoDetails> initialSwitchInfoDetails) {
// 分类处理:新增列表 vs 更新列表
List<AllInterfaceName> namesToInsert = new ArrayList<>();
@@ -421,33 +421,16 @@ public class InitialSwitchInfoDetailsServiceImpl implements IInitialSwitchInfoDe
if (!namesToInsert.isEmpty()) {
try {
allInterfaceNameMapper.batchInsert(namesToInsert);
log.info("新增接口名称数量:{}", namesToInsert.size());
} catch (DuplicateKeyException e) {
// 如果批量插入出现重复,转为逐条插入(带异常处理)
log.warn("批量插入出现重复,转为逐条处理");
namesToInsert.forEach(record -> {
try {
allInterfaceNameMapper.insertAllInterfaceName(record);
} catch (DuplicateKeyException ex) {
// 重复记录转为更新
AllInterfaceName query = new AllInterfaceName();
query.setInterfaceName(record.getInterfaceName());
query.setClientId(record.getClientId());
query.setServerIp(record.getServerIp());
List<AllInterfaceName> existing = allInterfaceNameMapper.selectByNames(query);
if (!existing.isEmpty()) {
record.setId(existing.get(0).getId());
allInterfaceNameMapper.updateAllInterfaceName(record);
}
}
});
log.info("交换机新增接口名称数量:{}", namesToInsert.size());
} catch (Exception e) {
log.error("交换机接口名称批量插入失败:{}", e.getMessage());
}
}
if (!namesToUpdate.isEmpty()) {
namesToUpdate.sort(Comparator.comparing(AllInterfaceName::getId));
allInterfaceNameMapper.batchUpdate(namesToUpdate);
log.info("更新接口名称数量:{}", namesToUpdate.size());
log.info("交换机更新接口名称数量:{}", namesToUpdate.size());
}
}
@@ -1141,7 +1124,7 @@ public class InitialSwitchInfoDetailsServiceImpl implements IInitialSwitchInfoDe
*/
public List<InitialSwitchInfoDetails> getSwitchTrafficDetailsMsgSharding(InitialSwitchInfoDetails queryParam) {
// 获取涉及的表名
Set<String> tableNames = TableSubUtil.getTableNamesBetween(queryParam.getStartTime(), queryParam.getEndTime(), TABLE_PREFIX);
Set<String> tableNames = TableSubUtil.getExistingTableNamesBetween(queryParam.getStartTime(), queryParam.getEndTime(), TABLE_PREFIX);
// 并行查询各表
return tableNames.parallelStream()
@@ -1164,7 +1147,7 @@ public class InitialSwitchInfoDetailsServiceImpl implements IInitialSwitchInfoDe
@Override
public List<InitialSwitchInfoDetails> sumSwitchTrafficDetailsSharding(InitialSwitchInfoDetails queryParam) {
// 获取涉及的表名
Set<String> tableNames = TableSubUtil.getTableNamesBetween(queryParam.getStartTime(), queryParam.getEndTime(), TABLE_PREFIX);
Set<String> tableNames = TableSubUtil.getExistingTableNamesBetween(queryParam.getStartTime(), queryParam.getEndTime(), TABLE_PREFIX);
// 并行查询各表
return tableNames.parallelStream()
@@ -1188,7 +1171,7 @@ public class InitialSwitchInfoDetailsServiceImpl implements IInitialSwitchInfoDe
*/
public List<InitialSwitchInfoDetails> getSwitchTrafficDetailsListSharding(InitialSwitchInfoDetails queryParam) {
// 获取涉及的表名
Set<String> tableNames = TableSubUtil.getTableNamesBetween(queryParam.getStartTime(), queryParam.getEndTime(), TABLE_PREFIX);
Set<String> tableNames = TableSubUtil.getExistingTableNamesBetween(queryParam.getStartTime(), queryParam.getEndTime(), TABLE_PREFIX);
// 并行查询各表
return tableNames.parallelStream()