初次提交代码

This commit is contained in:
2026-01-11 15:33:22 +08:00
commit 6603c6f4a1
455 changed files with 32175 additions and 0 deletions
@@ -0,0 +1,136 @@
package com.common.service;
import com.common.entity.DeviceCollectTask;
import com.common.mapper.DeviceCollectTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@CacheConfig(cacheNames = "collectTask")
public class DeviceCollectTaskService {
@Autowired
private DeviceCollectTaskMapper deviceCollectTaskMapper;
/**
* 根据ID查询采集任务 - 缓存
*/
@Cacheable(key = "'id:' + #id")
public DeviceCollectTask getById(Integer id) {
return deviceCollectTaskMapper.selectById(id);
}
/**
* 查询所有采集任务 - 缓存
*/
@Cacheable(key = "'all'")
public List<DeviceCollectTask> getAll() {
return deviceCollectTaskMapper.selectAll();
}
/**
* 根据设备ID查询采集任务 - 缓存
*/
@Cacheable(key = "'device:' + #deviceId")
public List<DeviceCollectTask> getByDeviceId(Integer deviceId) {
return deviceCollectTaskMapper.selectByDeviceId(deviceId);
}
/**
* 根据方法类型查询采集任务
*/
public List<DeviceCollectTask> getByMethod(Integer method) {
return deviceCollectTaskMapper.selectByMethod(method);
}
/**
* 多条件组合查询
*/
public List<DeviceCollectTask> getByCondition(DeviceCollectTask condition) {
return deviceCollectTaskMapper.selectByCondition(condition);
}
/**
* 新增采集任务 - 清除相关缓存
*/
@CacheEvict(allEntries = true)
public int create(DeviceCollectTask task) {
return deviceCollectTaskMapper.insert(task);
}
/**
* 更新采集任务 - 更新缓存
*/
@CachePut(key = "'id:' + #task.id")
@CacheEvict(key = "'device:' + #task.deviceId")
public DeviceCollectTask update(DeviceCollectTask task) {
deviceCollectTaskMapper.update(task);
return deviceCollectTaskMapper.selectById(task.getId());
}
/**
* 删除采集任务 - 清除缓存
*/
@CacheEvict(key = "'id:' + #id")
public int delete(Integer id) {
return deviceCollectTaskMapper.deleteById(id);
}
/**
* 标记任务成功 - 更新缓存
*/
@CacheEvict(key = "'id:' + #id")
public int markSuccess(Integer id) {
return deviceCollectTaskMapper.updateSuccessStatus(id);
}
/**
* 标记任务失败 - 更新缓存
*/
@CacheEvict(key = "'id:' + #id")
public int markFailed(Integer id) {
return deviceCollectTaskMapper.updateFailedStatus(id);
}
/**
* 更新EPM指标 - 更新缓存
*/
@CacheEvict(key = "'id:' + #id")
public int updateEpm(Integer id, Integer epm) {
return deviceCollectTaskMapper.updateEpm(id, epm);
}
/**
* 查询成功的任务
*/
public List<DeviceCollectTask> getSuccessTasks() {
return deviceCollectTaskMapper.selectSuccessTasks();
}
/**
* 查询失败的任务
*/
public List<DeviceCollectTask> getFailedTasks() {
return deviceCollectTaskMapper.selectFailedTasks();
}
/**
* 获取设备的最新采集任务
*/
@Cacheable(key = "'latest:device:' + #deviceId")
public DeviceCollectTask getLatestByDeviceId(Integer deviceId) {
return deviceCollectTaskMapper.selectLatestByDeviceId(deviceId);
}
/**
* 统计设备任务数量
*/
public int countByDeviceId(Integer deviceId) {
return deviceCollectTaskMapper.countByDeviceId(deviceId);
}
}
@@ -0,0 +1,31 @@
package com.common.service;
import com.common.entity.DeviceDevice;
import java.util.List;
import java.util.Map;
public interface DeviceDeviceService {
DeviceDevice getByIdSafely(Integer id);
List<DeviceDevice> getByIpSafely(String ip);
DeviceDevice getById(Integer id);
List<DeviceDevice> getAll();
List<DeviceDevice> getByIp(String ip);
List<DeviceDevice> getByNameLike(String name);
List<DeviceDevice> getByCondition(DeviceDevice condition);
List<DeviceDevice> getByMap(Map<String, Object> params);
List<DeviceDevice> getByPage(int pageNum, int pageSize);
Long getCount();
List<DeviceDevice> getMonitoringDevices();
List<DeviceDevice> getActiveDevices();
}
@@ -0,0 +1,35 @@
package com.common.service;
import com.common.entity.DeviceReceiveLog;
import com.github.pagehelper.PageInfo;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
public interface DeviceReceiveLogService {
// 插入操作
Long insertLog(DeviceReceiveLog log);
int batchInsertLogs(List<DeviceReceiveLog> logs);
// 查询操作
DeviceReceiveLog getById(Long id);
List<DeviceReceiveLog> getByDeviceId(Integer deviceId);
List<DeviceReceiveLog> getByCollectId(Integer collectId);
List<DeviceReceiveLog> getByTimeRange(LocalDateTime startTime, LocalDateTime endTime);
List<DeviceReceiveLog> getByCondition(DeviceReceiveLog condition);
PageInfo<DeviceReceiveLog> getByConditionPage(DeviceReceiveLog condition, Integer pageNum, Integer pageSize);
// 统计操作
Long countByCondition(DeviceReceiveLog condition);
List<Map<String, Object>> getDeviceLogStatistics();
// 删除操作
int deleteOldLogs(LocalDateTime beforeTime);
// 获取最近日志
List<DeviceReceiveLog> getRecentLogs(Integer limit);
}
@@ -0,0 +1,108 @@
package com.common.service;
import com.common.entity.DeviceUnknown;
import java.util.List;
import java.time.LocalDateTime;
public interface DeviceUnknownService {
/**
* 创建设备记录
* @param device 设备信息
* @return 创建的设备ID
*/
Long createDevice(DeviceUnknown device);
/**
* 批量创建设备记录
* @param devices 设备列表
* @return 成功创建的记录数
*/
int batchCreateDevices(List<DeviceUnknown> devices);
/**
* 根据ID获取设备信息
* @param id 设备ID
* @return 设备信息
*/
DeviceUnknown getDeviceById(Long id);
/**
* 根据IP获取设备列表
* @param deviceIp 设备IP
* @return 设备列表
*/
List<DeviceUnknown> getDevicesByIp(String deviceIp);
/**
* 根据组织ID获取设备列表
* @param organizationId 组织ID
* @return 设备列表
*/
List<DeviceUnknown> getDevicesByOrganizationId(Integer organizationId);
/**
* 获取所有设备列表
* @return 设备列表
*/
List<DeviceUnknown> getAllDevices();
/**
* 分页查询设备列表
* @param pageNum 页码
* @param pageSize 每页大小
* @return 设备列表
*/
List<DeviceUnknown> getDevicesByPage(int pageNum, int pageSize);
/**
* 根据条件查询设备列表
* @param device 查询条件
* @return 设备列表
*/
List<DeviceUnknown> searchDevices(DeviceUnknown device);
/**
* 更新设备信息
* @param device 设备信息
* @return 是否成功
*/
boolean updateDevice(DeviceUnknown device);
/**
* 更新设备的最后发现时间
* @param id 设备ID
* @param lastTime 最后发现时间
* @return 是否成功
*/
boolean updateLastTime(Long id, LocalDateTime lastTime);
/**
* 删除设备
* @param id 设备ID
* @return 是否成功
*/
boolean deleteDevice(Long id);
/**
* 根据组织ID删除设备
* @param organizationId 组织ID
* @return 删除的记录数
*/
int deleteDevicesByOrganizationId(Integer organizationId);
/**
* 获取设备总数
* @return 设备总数
*/
long getTotalCount();
/**
* 根据条件获取设备数量
* @param device 查询条件
* @return 设备数量
*/
long getCountByCondition(DeviceUnknown device);
}
@@ -0,0 +1,103 @@
package com.common.service.impl;
import com.common.entity.DeviceDevice;
import com.common.mapper.DeviceDeviceMapper;
import com.common.service.DeviceDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import com.haobang.util.SafeCacheUtil;
@CacheConfig(cacheNames = "device")
@Service
public class DeviceDeviceServiceImpl implements DeviceDeviceService {
@Autowired
private DeviceDeviceMapper deviceDeviceMapper;
@Autowired
private SafeCacheUtil safeCacheUtil;
/**
* 使用安全的缓存方法
*/
@Cacheable( key = "'device:id:' +#id")
@Override
public DeviceDevice getByIdSafely(Integer id) {
String cacheKey = "device:id:" + id;
return safeCacheUtil.getSafe(cacheKey, DeviceDevice.class,
() -> deviceDeviceMapper.selectById(id));
}
/**
* 使用安全的列表缓存方法
*/
@Cacheable( key = "'device:ip:' + #ip")
@Override
public List<DeviceDevice> getByIpSafely(String ip) {
String cacheKey = "device:ip:" + ip;
return safeCacheUtil.getSafeList(cacheKey, DeviceDevice.class,
() -> deviceDeviceMapper.selectByIp(ip));
}
@Cacheable( key = "'device:id:' +#id")
@Override
public DeviceDevice getById(Integer id) {
System.out.println("exec deviceDeviceMapper.selectById :" + id.toString());
return deviceDeviceMapper.selectById(id);
}
@Override
public List<DeviceDevice> getAll() {
return deviceDeviceMapper.selectAll();
}
@Cacheable( key = "'device:ip:' + #ip")
@Override
public List<DeviceDevice> getByIp(String ip) {
return deviceDeviceMapper.selectByIp(ip);
}
@Override
public List<DeviceDevice> getByNameLike(String name) {
return deviceDeviceMapper.selectByNameLike(name);
}
@Override
public List<DeviceDevice> getByCondition(DeviceDevice condition) {
return deviceDeviceMapper.selectByCondition(condition);
}
@Override
public List<DeviceDevice> getByMap(Map<String, Object> params) {
return deviceDeviceMapper.selectByMap(params);
}
@Override
public List<DeviceDevice> getByPage(int pageNum, int pageSize) {
int offset = (pageNum - 1) * pageSize;
return deviceDeviceMapper.selectByPage(offset, pageSize);
}
@Override
public Long getCount() {
return deviceDeviceMapper.count();
}
@Override
public List<DeviceDevice> getMonitoringDevices() {
return deviceDeviceMapper.selectMonitoringDevices();
}
@Override
public List<DeviceDevice> getActiveDevices() {
return deviceDeviceMapper.selectActiveDevices();
}
}
@@ -0,0 +1,159 @@
package com.common.service.impl;
import com.common.entity.DeviceReceiveLog;
import com.common.mapper.DeviceReceiveLogMapper;
import com.common.service.DeviceReceiveLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceReceiveLogServiceImpl implements DeviceReceiveLogService {
private final DeviceReceiveLogMapper deviceReceiveLogMapper;
@Override
@Transactional(rollbackFor = Exception.class)
public Long insertLog(DeviceReceiveLog log) {
// 设置默认时间
if (log.getCreatedAt() == null) {
log.setCreatedAt(LocalDateTime.now());
}
if (log.getReceiveTimeStr() == null && log.getReceiveTime() != null) {
log.setReceiveTimeStr(log.getReceiveTime().toString());
}
deviceReceiveLogMapper.insert(log);
// log.info("插入设备接收日志成功,ID: {}", log.getId());
return log.getId();
}
@Override
@Transactional(rollbackFor = Exception.class)
public int batchInsertLogs(List<DeviceReceiveLog> logs) {
if (logs == null || logs.isEmpty()) {
return 0;
}
// 批量处理,每1000条提交一次
int batchSize = 1000;
int totalInserted = 0;
for (int i = 0; i < logs.size(); i += batchSize) {
int end = Math.min(i + batchSize, logs.size());
List<DeviceReceiveLog> batchList = logs.subList(i, end);
// 设置默认值
batchList.forEach(log -> {
if (log.getCreatedAt() == null) {
log.setCreatedAt(LocalDateTime.now());
}
if (log.getReceiveTimeStr() == null && log.getReceiveTime() != null) {
log.setReceiveTimeStr(log.getReceiveTime().toString());
}
});
int inserted = deviceReceiveLogMapper.batchInsert(batchList);
totalInserted += inserted;
log.info("批量插入进度: {}/{}", end, logs.size());
}
return totalInserted;
}
@Override
public DeviceReceiveLog getById(Long id) {
return deviceReceiveLogMapper.selectById(id);
}
@Override
public List<DeviceReceiveLog> getByDeviceId(Integer deviceId) {
return deviceReceiveLogMapper.selectByDeviceId(deviceId);
}
@Override
public List<DeviceReceiveLog> getByCollectId(Integer collectId) {
return deviceReceiveLogMapper.selectByCollectId(collectId);
}
@Override
public List<DeviceReceiveLog> getByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
if (startTime == null || endTime == null) {
throw new IllegalArgumentException("时间范围不能为空");
}
if (startTime.isAfter(endTime)) {
throw new IllegalArgumentException("开始时间不能晚于结束时间");
}
return deviceReceiveLogMapper.selectByTimeRange(startTime, endTime);
}
@Override
public List<DeviceReceiveLog> getByCondition(DeviceReceiveLog condition) {
return deviceReceiveLogMapper.selectByCondition(condition);
}
@Override
public PageInfo<DeviceReceiveLog> getByConditionPage(DeviceReceiveLog condition, Integer pageNum, Integer pageSize) {
if (pageNum == null || pageNum < 1) {
pageNum = 1;
}
if (pageSize == null || pageSize < 1) {
pageSize = 10;
}
PageHelper.startPage(pageNum, pageSize);
List<DeviceReceiveLog> list = deviceReceiveLogMapper.selectByCondition(condition);
return new PageInfo<>(list);
}
@Override
public Long countByCondition(DeviceReceiveLog condition) {
return deviceReceiveLogMapper.countByCondition(condition);
}
@Override
public List<Map<String, Object>> getDeviceLogStatistics() {
return deviceReceiveLogMapper.countByDeviceGroup();
}
@Override
@Transactional(rollbackFor = Exception.class)
public int deleteOldLogs(LocalDateTime beforeTime) {
if (beforeTime == null) {
throw new IllegalArgumentException("删除时间点不能为空");
}
LocalDateTime endTime = LocalDateTime.now().minusDays(30); // 默认保留30天
if (beforeTime.isAfter(endTime)) {
log.warn("删除时间点{}晚于默认保留时间{},使用默认时间", beforeTime, endTime);
beforeTime = endTime;
}
int deleted = deviceReceiveLogMapper.deleteByTimeRange(
LocalDateTime.of(2000, 1, 1, 0, 0), // 很早的时间
beforeTime
);
log.info("删除{}天前的日志,共删除{}条", beforeTime, deleted);
return deleted;
}
@Override
public List<DeviceReceiveLog> getRecentLogs(Integer limit) {
if (limit == null || limit < 1) {
limit = 50; // 默认返回50条
}
return deviceReceiveLogMapper.selectRecent(limit);
}
}
@@ -0,0 +1,286 @@
package com.common.service.impl;
import com.common.entity.DeviceUnknown;
import com.common.mapper.DeviceUnknownMapper;
import com.common.service.DeviceUnknownService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.cache.CacheManager;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.Cacheable;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
@Slf4j
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "deviceunknown")
public class DeviceUnknownServiceImpl implements DeviceUnknownService {
private final DeviceUnknownMapper deviceUnknownMapper;
@Autowired
private CacheManager cacheManager;
@Override
@Transactional
public Long createDevice(DeviceUnknown device) {
// 设置默认值
if (device.getCreatedAt() == null) {
device.setCreatedAt(LocalDateTime.now());
}
if (device.getNetworkProtocol() == null) {
device.setNetworkProtocol("TCP");
}
if (device.getSourceMethod() == null) {
device.setSourceMethod("SYSTEM");
}
try {
int result = deviceUnknownMapper.insert(device);
if (result > 0) {
log.info("设备记录创建成功,ID: {}", device.getId());
//清除缓存IP:XXXX
clearDeviceCache(device.getDeviceIp());
List<DeviceUnknown> devlist=this.getDevicesByIp(device.getDeviceIp());
return device.getId();
}
return null;
} catch (Exception e) {
log.error("创建设备记录失败: {}", e.getMessage(), e);
throw new RuntimeException("创建设备记录失败", e);
}
}
/**
* 清除缓存
*/
public void clearDeviceCache(String deviceIp) {
Cache cache = cacheManager.getCache("deviceunknown");
if (cache != null) {
System.out.println("清除缓存"+"ip:" + deviceIp);
cache.evict("ip:" + deviceIp);
}
}
@Override
@Transactional
public int batchCreateDevices(List<DeviceUnknown> devices) {
if (CollectionUtils.isEmpty(devices)) {
return 0;
}
// 设置默认值
devices.forEach(device -> {
if (device.getCreatedAt() == null) {
device.setCreatedAt(LocalDateTime.now());
}
if (device.getNetworkProtocol() == null) {
device.setNetworkProtocol("TCP");
}
if (device.getSourceMethod() == null) {
device.setSourceMethod("SYSTEM");
}
});
try {
int result = deviceUnknownMapper.batchInsert(devices);
log.info("批量插入设备记录成功,数量: {}", result);
return result;
} catch (Exception e) {
log.error("批量插入设备记录失败: {}", e.getMessage(), e);
throw new RuntimeException("批量插入设备记录失败", e);
}
}
@Override
public DeviceUnknown getDeviceById(Long id) {
if (id == null || id <= 0) {
throw new IllegalArgumentException("设备ID不能为空");
}
try {
return deviceUnknownMapper.selectById(id);
} catch (Exception e) {
log.error("查询设备记录失败,ID: {}, 错误: {}", id, e.getMessage(), e);
throw new RuntimeException("查询设备记录失败", e);
}
}
@Cacheable( key = "'ip:' +#deviceIp")
@Override
public List<DeviceUnknown> getDevicesByIp(String deviceIp) {
if (deviceIp == null || deviceIp.trim().isEmpty()) {
throw new IllegalArgumentException("设备IP不能为空");
}
try {
return deviceUnknownMapper.selectByIp(deviceIp);
} catch (Exception e) {
log.error("根据IP查询设备记录失败,IP: {}, 错误: {}", deviceIp, e.getMessage(), e);
throw new RuntimeException("根据IP查询设备记录失败", e);
}
}
@Override
public List<DeviceUnknown> getDevicesByOrganizationId(Integer organizationId) {
if (organizationId == null || organizationId <= 0) {
throw new IllegalArgumentException("组织ID不能为空");
}
try {
return deviceUnknownMapper.selectByOrganizationId(organizationId);
} catch (Exception e) {
log.error("根据组织ID查询设备记录失败,组织ID: {}, 错误: {}", organizationId, e.getMessage(), e);
throw new RuntimeException("根据组织ID查询设备记录失败", e);
}
}
@Override
public List<DeviceUnknown> getAllDevices() {
try {
return deviceUnknownMapper.selectAll();
} catch (Exception e) {
log.error("查询所有设备记录失败: {}", e.getMessage(), e);
throw new RuntimeException("查询所有设备记录失败", e);
}
}
@Override
public List<DeviceUnknown> getDevicesByPage(int pageNum, int pageSize) {
if (pageNum <= 0) {
pageNum = 1;
}
if (pageSize <= 0) {
pageSize = 10;
}
int offset = (pageNum - 1) * pageSize;
try {
return deviceUnknownMapper.selectPage(offset, pageSize);
} catch (Exception e) {
log.error("分页查询设备记录失败,页码: {}, 页大小: {}, 错误: {}",
pageNum, pageSize, e.getMessage(), e);
throw new RuntimeException("分页查询设备记录失败", e);
}
}
@Override
public List<DeviceUnknown> searchDevices(DeviceUnknown device) {
try {
return deviceUnknownMapper.selectByCondition(device);
} catch (Exception e) {
log.error("条件查询设备记录失败: {}", e.getMessage(), e);
throw new RuntimeException("条件查询设备记录失败", e);
}
}
@Override
@Transactional
public boolean updateDevice(DeviceUnknown device) {
if (device == null || device.getId() == null) {
throw new IllegalArgumentException("设备信息或ID不能为空");
}
try {
int result = deviceUnknownMapper.updateById(device);
if (result > 0) {
log.info("更新设备记录成功,ID: {}", device.getId());
return true;
}
log.warn("未找到要更新的设备记录,ID: {}", device.getId());
return false;
} catch (Exception e) {
log.error("更新设备记录失败,ID: {}, 错误: {}", device.getId(), e.getMessage(), e);
throw new RuntimeException("更新设备记录失败", e);
}
}
@Override
@Transactional
public boolean updateLastTime(Long id, LocalDateTime lastTime) {
if (id == null || lastTime == null) {
throw new IllegalArgumentException("设备ID和最后发现时间不能为空");
}
try {
int result = deviceUnknownMapper.updateLastTime(id, lastTime);
if (result > 0) {
log.info("更新设备最后发现时间成功,ID: {}", id);
return true;
}
log.warn("未找到要更新最后发现时间的设备记录,ID: {}", id);
return false;
} catch (Exception e) {
log.error("更新设备最后发现时间失败,ID: {}, 错误: {}", id, e.getMessage(), e);
throw new RuntimeException("更新设备最后发现时间失败", e);
}
}
@Override
@Transactional
public boolean deleteDevice(Long id) {
if (id == null || id <= 0) {
throw new IllegalArgumentException("设备ID不能为空");
}
try {
int result = deviceUnknownMapper.deleteById(id);
if (result > 0) {
log.info("删除设备记录成功,ID: {}", id);
return true;
}
log.warn("未找到要删除的设备记录,ID: {}", id);
return false;
} catch (Exception e) {
log.error("删除设备记录失败,ID: {}, 错误: {}", id, e.getMessage(), e);
throw new RuntimeException("删除设备记录失败", e);
}
}
@Override
@Transactional
public int deleteDevicesByOrganizationId(Integer organizationId) {
if (organizationId == null || organizationId <= 0) {
throw new IllegalArgumentException("组织ID不能为空");
}
try {
int result = deviceUnknownMapper.deleteByOrganizationId(organizationId);
log.info("根据组织ID删除设备记录成功,组织ID: {}, 删除数量: {}", organizationId, result);
return result;
} catch (Exception e) {
log.error("根据组织ID删除设备记录失败,组织ID: {}, 错误: {}",
organizationId, e.getMessage(), e);
throw new RuntimeException("根据组织ID删除设备记录失败", e);
}
}
@Override
public long getTotalCount() {
try {
Long count = deviceUnknownMapper.count();
return count != null ? count : 0L;
} catch (Exception e) {
log.error("统计设备总数失败: {}", e.getMessage(), e);
throw new RuntimeException("统计设备总数失败", e);
}
}
@Override
public long getCountByCondition(DeviceUnknown device) {
try {
Long count = deviceUnknownMapper.countByCondition(device);
return count != null ? count : 0L;
} catch (Exception e) {
log.error("条件统计设备数量失败: {}", e.getMessage(), e);
throw new RuntimeException("条件统计设备数量失败", e);
}
}
}