有效-日数据逻辑开发,增加cors跨域过滤器

This commit is contained in:
gaoyutao
2025-08-28 09:40:11 +08:00
parent daa0a8779f
commit 966bd5cbec
19 changed files with 493 additions and 183 deletions
@@ -30,6 +30,8 @@ public class TableScheduleConfig {
public void calculate95BandwidthDaily() {
EpsInitialTrafficData queryParam = new EpsInitialTrafficData();
epsInitialTrafficDataService.calculate95BandwidthDaily(queryParam);
// 有效-95带宽值/日
epsInitialTrafficDataService.calculateBusiness95BandwidthDaily(queryParam);
InitialSwitchInfoDetails initialSwitchInfoDetails = new InitialSwitchInfoDetails();
epsInitialTrafficDataService.calculateSwitch95BandwidthDaily(initialSwitchInfoDetails);
}
@@ -1,9 +1,10 @@
package com.ruoyi.system.domain;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.web.domain.BaseEntity;
import lombok.Data;
import java.math.BigDecimal;
/**
* 收益方式修改记录对象 eps_method_change_record
@@ -11,6 +12,7 @@ import com.ruoyi.common.core.web.domain.BaseEntity;
* @author gyt
* @date 2025-08-15
*/
@Data
public class EpsMethodChangeRecord extends BaseEntity
{
private static final long serialVersionUID = 1L;
@@ -33,68 +35,23 @@ public class EpsMethodChangeRecord extends BaseEntity
/** 创建人 */
@Excel(name = "修改人")
private String creatBy;
/** 收益方式(1.流量,2包端) */
private String revenueMethod;
public void setId(Long id)
{
this.id = id;
}
/** 流量网口 */
private String trafficPort;
public Long getId()
{
return id;
}
/** 包端带宽值 */
private BigDecimal packageBandwidth;
public void setNodeName(String nodeName)
{
this.nodeName = nodeName;
}
/** 业务名称 */
private String businessName;
public String getNodeName()
{
return nodeName;
}
/** 业务代码(12位) */
private String businessCode;
/** 开始时间 */
private String startTime;
/** 结束时间 */
private String endTime;
public void setChangeContent(String changeContent)
{
this.changeContent = changeContent;
}
public String getChangeContent()
{
return changeContent;
}
public void setHardwareSn(String hardwareSn)
{
this.hardwareSn = hardwareSn;
}
public String getHardwareSn()
{
return hardwareSn;
}
public void setCreatBy(String creatBy)
{
this.creatBy = creatBy;
}
public String getCreatBy()
{
return creatBy;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("nodeName", getNodeName())
.append("changeContent", getChangeContent())
.append("hardwareSn", getHardwareSn())
.append("createTime", getCreateTime())
.append("creatBy", getCreatBy())
.append("updateTime", getUpdateTime())
.append("updateBy", getUpdateBy())
.toString();
}
}
@@ -60,4 +60,6 @@ public class EpsServerRevenueConfig extends BaseEntity
private String startTime;
/** 结束时间 */
private String endTime;
/** 业务是否有变化 */
private String changed;
}
@@ -87,4 +87,10 @@ public interface AllInterfaceNameMapper
* @return
*/
List<AllInterfaceName> getAllSwitchSn(AllInterfaceName interfaceName);
/**
* 批量编辑
* @param records
*/
void batchUpdate(List<AllInterfaceName> records);
}
@@ -40,6 +40,12 @@ public interface EpsInitialTrafficDataService {
* @return 初始流量数据列表
*/
List<EpsInitialTrafficData> getAllTraficMsg(EpsInitialTrafficData queryParam);
/**
* 计算95带宽值-有效/日
* @param queryParam 查询参数实体
* @return 流量数据列表
*/
void calculateBusiness95BandwidthDaily(EpsInitialTrafficData queryParam);
/**
* 计算95带宽值
* @param queryParam 查询参数实体
@@ -1,14 +1,8 @@
package com.ruoyi.system.service.impl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.AllInterfaceName;
import com.ruoyi.system.domain.EpsInitialTrafficData;
import com.ruoyi.system.domain.EpsNodeBandwidth;
import com.ruoyi.system.domain.InitialSwitchInfoDetails;
import com.ruoyi.system.mapper.AllInterfaceNameMapper;
import com.ruoyi.system.mapper.EpsInitialTrafficDataMapper;
import com.ruoyi.system.mapper.EpsNodeBandwidthMapper;
import com.ruoyi.system.mapper.InitialSwitchInfoDetailsMapper;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.EpsInitialTrafficDataService;
import com.ruoyi.system.util.TableRouterUtil;
import lombok.extern.slf4j.Slf4j;
@@ -22,6 +16,8 @@ import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Matcher;
@@ -34,6 +30,8 @@ public class EpsInitialTrafficDataServiceImpl implements EpsInitialTrafficDataSe
private static final Pattern SPEED_PATTERN = Pattern.compile("(\\d+\\.?\\d*)\\s*(\\S*)");
private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Autowired
private EpsInitialTrafficDataMapper epsInitialTrafficDataMapper;
@Autowired
@@ -42,6 +40,10 @@ public class EpsInitialTrafficDataServiceImpl implements EpsInitialTrafficDataSe
private AllInterfaceNameMapper allInterfaceNameMapper;
@Autowired
private InitialSwitchInfoDetailsMapper initialSwitchInfoDetailsMapper;
@Autowired
private EpsServerRevenueConfigMapper epsServerRevenueConfigMapper;
@Autowired
private EpsMethodChangeRecordMapper epsMethodChangeRecordMapper;
@Override
public void createNextMonthTables() {
LocalDate nextMonth = LocalDate.now().plusMonths(1);
@@ -197,60 +199,148 @@ public class EpsInitialTrafficDataServiceImpl implements EpsInitialTrafficDataSe
.collect(Collectors.toList());
}
/**
* 有业务变动的 计算有效95带宽值/日
* @param queryParam
*/
@Override
public void calculate95BandwidthDaily(EpsInitialTrafficData queryParam) {
Date now = DateUtils.getNowDate();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -24);
Date startDate = calendar.getTime();
// 格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startTime = sdf.format(startDate);
String endTime = sdf.format(now);
public void calculateBusiness95BandwidthDaily(EpsInitialTrafficData queryParam) {
// 1. 获取所有设备SN列表
List<AllInterfaceName> snList = allInterfaceNameMapper.getAllDeviceSn(new AllInterfaceName());
// 2. 获取昨天的日期范围(北京时间)
LocalDate yesterday = LocalDate.now(ZoneId.of("Asia/Shanghai")).minusDays(1);
String dailyStartTime = yesterday.atStartOfDay().format(TIME_FORMAT); // 00:00:00
String dailyEndTime = yesterday.atTime(23, 59, 59).format(TIME_FORMAT); // 23:59:59
// 3. 遍历每个设备
for (AllInterfaceName interfaceName : snList) {
queryParam.setServiceSn(interfaceName.getDeviceSn());
// 4. 检查设备是否有业务变更
EpsServerRevenueConfig revenueConfig = new EpsServerRevenueConfig();
revenueConfig.setHardwareSn(interfaceName.getDeviceSn());
List<EpsServerRevenueConfig> changedList = epsServerRevenueConfigMapper.selectEpsServerRevenueConfigList(revenueConfig);
if (!changedList.isEmpty() &&
"1".equals(changedList.get(0).getChanged()) &&
"1".equals(changedList.get(0).getRevenueMethod())) {
// 5. 获取业务变更记录(按时间降序)
EpsMethodChangeRecord changeQuery = new EpsMethodChangeRecord();
changeQuery.setHardwareSn(interfaceName.getDeviceSn());
changeQuery.setStartTime(dailyStartTime);
changeQuery.setEndTime(dailyEndTime);
List<EpsMethodChangeRecord> records = epsMethodChangeRecordMapper
.selectEpsMethodChangeRecordList(changeQuery);
if (!records.isEmpty()) {
// 确保按时间降序排列
records.sort((r1, r2) -> r2.getCreateTime().compareTo(r1.getCreateTime()));
// 6. 计算各时间段95值
calculateTimeSegments95(queryParam, records, dailyStartTime, dailyEndTime);
}
}
}
}
/**
* 计算设备各时间段的95带宽值
*/
private void calculateTimeSegments95(EpsInitialTrafficData queryParam,
List<EpsMethodChangeRecord> records,
String dailyStartTime,
String dailyEndTime) {
// 1. 处理变更时间段(从最新到最早)
String endTime = records.get(0).getCreateTime() + "";
for (int i = 1; i < records.size(); i++) {
String startTime = records.get(i).getCreateTime() + "";
calculateSegment95(queryParam, startTime, endTime);
endTime = startTime; // 移动时间窗口
}
// 2. 处理最早变更前的时间段(昨天00:00:00 ~ 最早变更时间)
String earliestChangeTime = records.get(records.size()-1).getCreateTime() + "";
calculateSegment95(queryParam, dailyStartTime, earliestChangeTime);
// 3. 处理最后变更后的时间段(最新变更时间 ~ 昨天23:59:59)
calculateSegment95(queryParam, endTime, dailyEndTime);
}
/**
* 计算单个时间段的95带宽值
*/
private void calculateSegment95(EpsInitialTrafficData queryParam, String startTime, String endTime) {
queryParam.setStartTime(startTime);
queryParam.setEndTime(endTime);
List<EpsInitialTrafficData> dataList = query(queryParam);
if (!dataList.isEmpty()) {
calculate95BandwidthDaily(dataList);
}
}
@Override
public void calculate95BandwidthDaily(EpsInitialTrafficData queryParam) {
// 1. 获取昨天的日期(北京时间)
LocalDate yesterday = LocalDate.now(ZoneId.of("Asia/Shanghai")).minusDays(1);
// 2. 定义时间范围字符串
String startTimeStr = yesterday.atStartOfDay().format(TIME_FORMAT); // 00:00:00
String endTimeStr = yesterday.atTime(23, 59, 59).format(TIME_FORMAT); // 23:59:59
queryParam.setStartTime(startTimeStr);
queryParam.setEndTime(endTimeStr);
queryParam.setRevenueMethod("1");
// 查询所有的服务器sn
AllInterfaceName allInterfaceName = new AllInterfaceName();
List<AllInterfaceName> snList = allInterfaceNameMapper.getAllDeviceSn(allInterfaceName);
for (AllInterfaceName interfaceName : snList) {
queryParam.setServiceSn(interfaceName.getDeviceSn());
// 查询原始数据
List<EpsInitialTrafficData> dataList = query(queryParam);
if(!dataList.isEmpty()){
// 1. 提取 outSpeed 并转换为 Mbps
List<BigDecimal> speedsInMbps = dataList.stream()
.map(data -> parseSpeedToMbps(data.getOutSpeed()))
.sorted()
.collect(Collectors.toList());
// 4. 检查设备是否有业务变更
EpsServerRevenueConfig revenueConfig = new EpsServerRevenueConfig();
revenueConfig.setHardwareSn(interfaceName.getDeviceSn());
List<EpsServerRevenueConfig> changedList = epsServerRevenueConfigMapper.selectEpsServerRevenueConfigList(revenueConfig);
// 2. 计算 95% 位置(向上取整)
int total = speedsInMbps.size();
int position = (int) Math.ceil(0.95 * total) - 1; // 转换为 0-based 索引
if (position < 0){
position = 0;
}
if (position >= total){
position = total - 1;
}
if (!changedList.isEmpty() &&
"1".equals(changedList.get(0).getChanged()) &&
"1".equals(changedList.get(0).getRevenueMethod())) {
// 3. 获取 95 值并四舍五入到两位小数
BigDecimal percentile95 = speedsInMbps.get(position);
BigDecimal dailyResult = percentile95.setScale(2, RoundingMode.HALF_UP);
// 服务器信息
EpsInitialTrafficData epsInitialTrafficData = dataList.get(0);
// 落入带宽计算结果表
EpsNodeBandwidth bandwidth = new EpsNodeBandwidth();
bandwidth.setHardwareSn(epsInitialTrafficData.getServiceSn());
bandwidth.setNodeName(epsInitialTrafficData.getNodeName());
bandwidth.setBusinessId(epsInitialTrafficData.getBusinessId());
bandwidth.setBusinessName(epsInitialTrafficData.getBusinessName());
bandwidth.setResourceType("1");
bandwidth.setBandwidthType("1");
bandwidth.setBandwidthResult(dailyResult);
bandwidth.setBandwidth95Daily(dailyResult);
bandwidth.setCreateTime(startDate);
epsNodeBandwidthMapper.insertEpsNodeBandwidth(bandwidth);
}else{
// 查询原始数据
List<EpsInitialTrafficData> dataList = query(queryParam);
if(!dataList.isEmpty()){
// 1. 提取 outSpeed 并转换为 Mbps
List<BigDecimal> speedsInMbps = dataList.stream()
.map(data -> parseSpeedToMbps(data.getOutSpeed()))
.sorted()
.collect(Collectors.toList());
// 2. 计算 95% 位置(向上取整)
int total = speedsInMbps.size();
int position = (int) Math.ceil(0.95 * total) - 1; // 转换为 0-based 索引
if (position < 0){
position = 0;
}
if (position >= total){
position = total - 1;
}
// 3. 获取 95 值并四舍五入到两位小数
BigDecimal percentile95 = speedsInMbps.get(position);
BigDecimal dailyResult = percentile95.setScale(2, RoundingMode.HALF_UP);
// 服务器信息
EpsInitialTrafficData epsInitialTrafficData = dataList.get(0);
// 落入带宽计算结果表
EpsNodeBandwidth bandwidth = new EpsNodeBandwidth();
bandwidth.setHardwareSn(epsInitialTrafficData.getServiceSn());
bandwidth.setNodeName(epsInitialTrafficData.getNodeName());
bandwidth.setBusinessId(epsInitialTrafficData.getBusinessId());
bandwidth.setBusinessName(epsInitialTrafficData.getBusinessName());
bandwidth.setResourceType("1");
bandwidth.setBandwidthType("1");
bandwidth.setBandwidthResult(dailyResult);
bandwidth.setBandwidth95Daily(dailyResult);
bandwidth.setCreateTime(DateUtils.parseDate(startTimeStr));
epsNodeBandwidthMapper.insertEpsNodeBandwidth(bandwidth);
}
}
}
}
@@ -318,6 +408,45 @@ public class EpsInitialTrafficDataServiceImpl implements EpsInitialTrafficDataSe
}
/**
* 计算有效-日95带宽值
* @param dataList
*/
private void calculate95BandwidthDaily(List<EpsInitialTrafficData> dataList){
// 1. 提取 outSpeed 并转换为 Mbps
List<BigDecimal> speedsInMbps = dataList.stream()
.map(data -> parseSpeedToMbps(data.getOutSpeed()))
.sorted()
.collect(Collectors.toList());
// 2. 计算 95% 位置(向上取整)
int total = speedsInMbps.size();
int position = (int) Math.ceil(0.95 * total) - 1; // 转换为 0-based 索引
if (position < 0){
position = 0;
}
if (position >= total){
position = total - 1;
}
// 3. 获取 95 值并四舍五入到两位小数
BigDecimal percentile95 = speedsInMbps.get(position);
BigDecimal dailyResult = percentile95.setScale(2, RoundingMode.HALF_UP);
// 服务器信息
EpsInitialTrafficData epsInitialTrafficData = dataList.get(0);
// 落入带宽计算结果表
EpsNodeBandwidth bandwidth = new EpsNodeBandwidth();
bandwidth.setHardwareSn(epsInitialTrafficData.getServiceSn());
bandwidth.setNodeName(epsInitialTrafficData.getNodeName());
bandwidth.setBusinessId(epsInitialTrafficData.getBusinessId());
bandwidth.setBusinessName(epsInitialTrafficData.getBusinessName());
bandwidth.setResourceType("1");
bandwidth.setBandwidthType("5");
bandwidth.setBandwidthResult(dailyResult);
bandwidth.setEffectiveBandwidth95Daily(dailyResult);
bandwidth.setCreateTime(DateUtils.parseDate(epsInitialTrafficData.getStartTime()));
epsNodeBandwidthMapper.insertEpsNodeBandwidth(bandwidth);
}
/**
* 单位换算
* @param speedWithUnit
@@ -17,10 +17,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -94,6 +92,11 @@ public class EpsServerRevenueConfigServiceImpl implements IEpsServerRevenueConfi
String revenueMethod = getMethodName(serverRevenueConfig.getRevenueMethod());
//现在的收益方式
String nowRevenueMethod = getMethodName(epsServerRevenueConfig.getRevenueMethod());
if(serverRevenueConfig.getBusinessCode() != null){
if(!serverRevenueConfig.getBusinessCode().equals(epsServerRevenueConfig.getBusinessCode())){
epsServerRevenueConfig.setChanged("1");
}
}
// 修改条数
int rows = epsServerRevenueConfigMapper.updateEpsServerRevenueConfig(epsServerRevenueConfig);
if(rows>=0){
@@ -128,6 +131,11 @@ public class EpsServerRevenueConfigServiceImpl implements IEpsServerRevenueConfi
",带宽值设置为"+epsServerRevenueConfig.getPackageBandwidth()+"Mbps,"+"业务为"+epsServerRevenueConfig.getBusinessName();
}
changeRecord.setChangeContent(content);
changeRecord.setBusinessCode(epsServerRevenueConfig.getBusinessCode());
changeRecord.setBusinessName(epsServerRevenueConfig.getBusinessName());
changeRecord.setRevenueMethod(epsServerRevenueConfig.getRevenueMethod());
changeRecord.setPackageBandwidth(epsServerRevenueConfig.getPackageBandwidth());
changeRecord.setTrafficPort(epsServerRevenueConfig.getTrafficPort());
// 添加操作记录
epsMethodChangeRecordMapper.insertEpsMethodChangeRecord(changeRecord);
}
@@ -210,31 +218,60 @@ public class EpsServerRevenueConfigServiceImpl implements IEpsServerRevenueConfi
// 批量查询已存在的接口名称
List<AllInterfaceName> existingNames = allInterfaceNameMapper.selectByNames(interfaceNames);
Set<String> existingNameSet = existingNames.stream()
.map(AllInterfaceName::getInterfaceName)
.collect(Collectors.toSet());
// 转为 Map<接口名称, 数据库记录> 便于快速查找
Map<String, AllInterfaceName> existingNameMap = existingNames.stream()
.collect(Collectors.toMap(AllInterfaceName::getInterfaceName, Function.identity()));
// 筛选出需要新增的接口名称
List<AllInterfaceName> newNames = trafficDataList.stream()
.filter(data -> !existingNameSet.contains(data.getName()))
.map(data -> {
AllInterfaceName name = new AllInterfaceName();
name.setInterfaceName(data.getName());
name.setClientId(data.getClientId());
name.setResourceType("1");
name.setDeviceSn(data.getServiceSn());
name.setNodeName(data.getNodeName());
name.setBusinessCode(data.getBusinessId());
name.setBusinessName(data.getBusinessName());
return name;
})
.collect(Collectors.toList());
// 分类处理:新增列表 vs 更新列表
List<AllInterfaceName> namesToInsert = new ArrayList<>();
List<AllInterfaceName> namesToUpdate = new ArrayList<>();
// 批量插入新接口名称
if (!newNames.isEmpty()) {
allInterfaceNameMapper.batchInsert(newNames);
log.info("接口名称表-新增接口名称数量:{}", newNames.size());
trafficDataList.forEach(data -> {
String name = data.getName();
if (StringUtils.isBlank(name)) {
return;
}
// 构造新记录(需更新的字段)
AllInterfaceName newRecord = new AllInterfaceName();
newRecord.setInterfaceName(name);
newRecord.setClientId(data.getClientId());
newRecord.setResourceType("1"); // 固定值
newRecord.setDeviceSn(data.getServiceSn());
newRecord.setNodeName(data.getNodeName());
newRecord.setBusinessCode(data.getBusinessId());
newRecord.setBusinessName(data.getBusinessName());
// 判断是否需要更新
if (existingNameMap.containsKey(name)) {
AllInterfaceName oldRecord = existingNameMap.get(name);
if (isRecordChanged(oldRecord, newRecord)) {
newRecord.setId(oldRecord.getId()); // 保留原ID
namesToUpdate.add(newRecord);
}
} else {
namesToInsert.add(newRecord);
}
});
// 批量操作数据库
if (!namesToInsert.isEmpty()) {
allInterfaceNameMapper.batchInsert(namesToInsert);
log.info("新增接口名称数量:{}", namesToInsert.size());
}
if (!namesToUpdate.isEmpty()) {
allInterfaceNameMapper.batchUpdate(namesToUpdate);
log.info("更新接口名称数量:{}", namesToUpdate.size());
}
}
// 辅助方法:检查记录是否有变化
private boolean isRecordChanged(AllInterfaceName oldRecord, AllInterfaceName newRecord) {
return !Objects.equals(oldRecord.getClientId(), newRecord.getClientId()) ||
!Objects.equals(oldRecord.getDeviceSn(), newRecord.getDeviceSn()) ||
!Objects.equals(oldRecord.getNodeName(), newRecord.getNodeName()) ||
!Objects.equals(oldRecord.getBusinessCode(), newRecord.getBusinessCode()) ||
!Objects.equals(oldRecord.getBusinessName(), newRecord.getBusinessName());
}
/**
* 批量删除服务器收益方式配置
@@ -14,9 +14,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -172,32 +171,62 @@ public class InitialSwitchInfoDetailsServiceImpl implements IInitialSwitchInfoDe
// 批量查询已存在的接口名称
List<AllInterfaceName> existingNames = allInterfaceNameMapper.selectByNames(interfaceNames);
Set<String> existingNameSet = existingNames.stream()
.map(AllInterfaceName::getInterfaceName)
.collect(Collectors.toSet());
Map<String, AllInterfaceName> existingNameMap = existingNames.stream()
.collect(Collectors.toMap(AllInterfaceName::getInterfaceName, Function.identity()));
// 筛选出需要新增的接口名称
List<AllInterfaceName> newNames = initialSwitchInfoDetails.stream()
.filter(data -> !existingNameSet.contains(data.getName()))
.map(data -> {
AllInterfaceName name = new AllInterfaceName();
name.setInterfaceName(data.getName());
name.setClientId(data.getClientId());
name.setResourceType("2");
name.setDeviceSn(data.getServerSn());
name.setNodeName(data.getServerName());
name.setServerPort(data.getServerPort());
name.setInterfaceDeviceType(data.getInterfaceDeviceType());
name.setSwitchName(data.getSwitchName());
name.setSwitchSn(data.getSwitchSn());
return name;
})
.collect(Collectors.toList());
// 分类处理:新增列表 vs 更新列表
List<AllInterfaceName> namesToInsert = new ArrayList<>();
List<AllInterfaceName> namesToUpdate = new ArrayList<>();
// 批量插入新接口名称
if (!newNames.isEmpty()) {
allInterfaceNameMapper.batchInsert(newNames);
log.info("接口名称表-新增交换机接口名称数量:{}", newNames.size());
initialSwitchInfoDetails.forEach(data -> {
String name = data.getName();
if (StringUtils.isBlank(name)) {
return;
}
AllInterfaceName record = new AllInterfaceName();
record.setInterfaceName(name);
record.setClientId(data.getClientId());
record.setResourceType("2");
record.setDeviceSn(data.getServerSn());
record.setNodeName(data.getServerName());
record.setServerPort(data.getServerPort());
record.setInterfaceDeviceType(data.getInterfaceDeviceType());
record.setSwitchName(data.getSwitchName());
record.setSwitchSn(data.getSwitchSn());
// 判断是否需要更新
if (existingNameMap.containsKey(name)) {
AllInterfaceName existingRecord = existingNameMap.get(name);
if (isRecordChanged(existingRecord, record)) {
record.setId(existingRecord.getId()); // 保留原ID
namesToUpdate.add(record);
}
} else {
namesToInsert.add(record);
}
});
// 批量操作数据库
if (!namesToInsert.isEmpty()) {
allInterfaceNameMapper.batchInsert(namesToInsert);
log.info("新增接口名称数量:{}", namesToInsert.size());
}
if (!namesToUpdate.isEmpty()) {
allInterfaceNameMapper.batchUpdate(namesToUpdate);
log.info("更新接口名称数量:{}", namesToUpdate.size());
}
}
// 辅助方法:检查记录是否有变化
private boolean isRecordChanged(AllInterfaceName oldRecord, AllInterfaceName newRecord) {
return !Objects.equals(oldRecord.getClientId(), newRecord.getClientId()) ||
!Objects.equals(oldRecord.getDeviceSn(), newRecord.getDeviceSn()) ||
!Objects.equals(oldRecord.getNodeName(), newRecord.getNodeName()) ||
!Objects.equals(oldRecord.getServerPort(), newRecord.getServerPort()) ||
!Objects.equals(oldRecord.getInterfaceDeviceType(), newRecord.getInterfaceDeviceType()) ||
!Objects.equals(oldRecord.getSwitchName(), newRecord.getSwitchName()) ||
!Objects.equals(oldRecord.getSwitchSn(), newRecord.getSwitchSn());
}
}