You've already forked FrameTour-BE
Compare commits
3 Commits
e8c645a3c0
...
5f4f89112b
Author | SHA1 | Date | |
---|---|---|---|
5f4f89112b | |||
d68b062951 | |||
99857db006 |
@@ -10,7 +10,6 @@ import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.CreateScenicRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2WithConfigDTO;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.UpdateScenicRequest;
|
||||
import com.ycwl.basic.integration.scenic.service.ScenicConfigIntegrationService;
|
||||
import com.ycwl.basic.integration.scenic.service.ScenicIntegrationService;
|
||||
|
@@ -1,275 +0,0 @@
|
||||
package com.ycwl.basic.integration.device.example;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.device.dto.defaults.*;
|
||||
import com.ycwl.basic.integration.device.service.DeviceDefaultConfigIntegrationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 默认配置集成服务使用示例
|
||||
*
|
||||
* 通过在 application.yml 中设置 integration.device.example.default-config.enabled=true 来启用
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(name = "integration.device.example.default-config.enabled", havingValue = "true")
|
||||
public class DefaultConfigIntegrationExample implements CommandLineRunner {
|
||||
|
||||
private final DeviceDefaultConfigIntegrationService defaultConfigService;
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
log.info("=== 默认配置集成服务使用示例 ===");
|
||||
|
||||
try {
|
||||
// 1. 基础查询操作示例(支持自动 Fallback)
|
||||
basicQueryExamples();
|
||||
|
||||
// 2. 配置管理操作示例(直接操作)
|
||||
configManagementExamples();
|
||||
|
||||
// 3. 批量操作示例
|
||||
batchOperationExamples();
|
||||
|
||||
// 4. 高级使用模式示例
|
||||
advancedUsageExamples();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("默认配置集成示例执行失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 基础查询操作示例(支持自动 Fallback)
|
||||
*/
|
||||
private void basicQueryExamples() {
|
||||
log.info("--- 基础查询操作示例(支持自动 Fallback)---");
|
||||
|
||||
try {
|
||||
// 获取默认配置列表(自动缓存,服务不可用时返回缓存数据)
|
||||
PageResponse<DefaultConfigResponse> configList = defaultConfigService.listDefaultConfigs(1, 10);
|
||||
log.info("默认配置列表: 总数={}, 当前页配置数={}",
|
||||
configList.getTotal(), configList.getList().size());
|
||||
|
||||
// 显示配置详情
|
||||
for (DefaultConfigResponse config : configList.getList()) {
|
||||
log.info("配置详情: key={}, value={}, type={}, description={}",
|
||||
config.getConfigKey(), config.getConfigValue(),
|
||||
config.getConfigType(), config.getDescription());
|
||||
|
||||
// 获取单个配置详情(自动缓存,服务不可用时返回缓存数据)
|
||||
DefaultConfigResponse detailConfig = defaultConfigService.getDefaultConfig(config.getConfigKey());
|
||||
if (detailConfig != null) {
|
||||
log.info("配置详情获取成功: usageCount={}, isActive={}",
|
||||
detailConfig.getUsageCount(), detailConfig.getIsActive());
|
||||
}
|
||||
break; // 只展示第一个
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("基础查询操作失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置管理操作示例(直接操作)
|
||||
*/
|
||||
private void configManagementExamples() {
|
||||
log.info("--- 配置管理操作示例(直接操作)---");
|
||||
|
||||
String testConfigKey = "example_test_config";
|
||||
|
||||
try {
|
||||
// 1. 创建默认配置(直接操作,失败时立即报错)
|
||||
DefaultConfigRequest createRequest = new DefaultConfigRequest();
|
||||
createRequest.setConfigKey(testConfigKey);
|
||||
createRequest.setConfigValue("1920x1080");
|
||||
createRequest.setConfigType("string");
|
||||
createRequest.setDescription("示例测试配置 - 默认分辨率");
|
||||
|
||||
boolean createResult = defaultConfigService.createDefaultConfig(createRequest);
|
||||
log.info("创建默认配置结果: {}", createResult ? "成功" : "失败");
|
||||
|
||||
// 2. 更新默认配置(直接操作,可能返回冲突信息)
|
||||
Map<String, Object> updates = new HashMap<>();
|
||||
updates.put("configValue", "3840x2160");
|
||||
updates.put("description", "更新后的默认分辨率 - 4K");
|
||||
|
||||
DefaultConfigConflict conflict = defaultConfigService.updateDefaultConfig(testConfigKey, updates);
|
||||
if (conflict != null) {
|
||||
log.warn("更新配置存在冲突: configKey={}, conflictType={}, deviceCount={}",
|
||||
conflict.getConfigKey(), conflict.getConflictType(), conflict.getDeviceCount());
|
||||
} else {
|
||||
log.info("配置更新成功,无冲突");
|
||||
}
|
||||
|
||||
// 3. 验证更新结果
|
||||
DefaultConfigResponse updatedConfig = defaultConfigService.getDefaultConfig(testConfigKey);
|
||||
if (updatedConfig != null) {
|
||||
log.info("更新后配置值: {}", updatedConfig.getConfigValue());
|
||||
}
|
||||
|
||||
// 4. 删除测试配置(直接操作)
|
||||
boolean deleteResult = defaultConfigService.deleteDefaultConfig(testConfigKey);
|
||||
log.info("删除默认配置结果: {}", deleteResult ? "成功" : "失败");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("配置管理操作失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量操作示例
|
||||
*/
|
||||
private void batchOperationExamples() {
|
||||
log.info("--- 批量操作示例 ---");
|
||||
|
||||
try {
|
||||
// 1. 使用构建器模式创建批量配置
|
||||
BatchDefaultConfigRequest batchRequest = defaultConfigService.createBatchConfigBuilder()
|
||||
.addVideoConfig("1920x1080", 30, "H264") // 添加视频配置组
|
||||
.addNetworkConfig("192.168.1.100", 554, "RTSP") // 添加网络配置组
|
||||
.addConfig("recording_enabled", "true", "bool", "是否启用录制")
|
||||
.addConfig("storage_path", "/data/recordings", "string", "录制存储路径")
|
||||
.addConfig("max_file_size", "1024", "int", "最大文件大小(MB)")
|
||||
.build();
|
||||
|
||||
log.info("准备批量创建 {} 个默认配置", batchRequest.getConfigs().size());
|
||||
|
||||
// 2. 执行批量更新(直接操作)
|
||||
BatchDefaultConfigResponse batchResult = defaultConfigService.batchUpdateDefaultConfigs(batchRequest);
|
||||
|
||||
// 3. 处理批量结果
|
||||
log.info("批量操作结果: 成功={}, 失败={}", batchResult.getSuccess(), batchResult.getFailed());
|
||||
|
||||
if (batchResult.getConflicts() != null && !batchResult.getConflicts().isEmpty()) {
|
||||
log.warn("发现 {} 个配置冲突:", batchResult.getConflicts().size());
|
||||
for (DefaultConfigConflict conflict : batchResult.getConflicts()) {
|
||||
log.warn("冲突配置: key={}, type={}, deviceCount={}",
|
||||
conflict.getConfigKey(), conflict.getConflictType(), conflict.getDeviceCount());
|
||||
}
|
||||
}
|
||||
|
||||
if (batchResult.getProcessedItems() != null) {
|
||||
log.info("处理详情:");
|
||||
batchResult.getProcessedItems().forEach(item ->
|
||||
log.info(" 配置 {}: status={}, action={}, finalType={}",
|
||||
item.getConfigKey(), item.getStatus(), item.getAction(), item.getFinalType())
|
||||
);
|
||||
}
|
||||
|
||||
if (batchResult.getErrors() != null && !batchResult.getErrors().isEmpty()) {
|
||||
log.error("批量操作错误:");
|
||||
batchResult.getErrors().forEach(error -> log.error(" {}", error));
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("批量操作失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 高级使用模式示例
|
||||
*/
|
||||
private void advancedUsageExamples() {
|
||||
log.info("--- 高级使用模式示例 ---");
|
||||
|
||||
try {
|
||||
// 1. 设备类型特定的默认配置模式
|
||||
createDeviceTypeSpecificConfigs();
|
||||
|
||||
// 2. 配置验证和完整性检查模式
|
||||
validateConfigCompleteness();
|
||||
|
||||
// 3. 配置迁移和批量更新模式
|
||||
configMigrationPattern();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("高级使用模式示例失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建设备类型特定的默认配置
|
||||
*/
|
||||
private void createDeviceTypeSpecificConfigs() {
|
||||
log.info("创建设备类型特定的默认配置...");
|
||||
|
||||
// IPC摄像头默认配置
|
||||
BatchDefaultConfigRequest ipcDefaults = defaultConfigService.createBatchConfigBuilder()
|
||||
.addVideoConfig("1920x1080", 25, "H264")
|
||||
.addConfig("night_vision", "true", "bool", "夜视功能")
|
||||
.addConfig("motion_detection", "true", "bool", "移动检测")
|
||||
.addConfig("stream_profile", "main", "string", "码流类型")
|
||||
.build();
|
||||
|
||||
// NVR设备默认配置
|
||||
BatchDefaultConfigRequest nvrDefaults = defaultConfigService.createBatchConfigBuilder()
|
||||
.addConfig("max_channels", "16", "int", "最大通道数")
|
||||
.addConfig("storage_mode", "continuous", "string", "存储模式")
|
||||
.addConfig("backup_enabled", "true", "bool", "备份启用")
|
||||
.build();
|
||||
|
||||
log.info("IPC默认配置项数: {}, NVR默认配置项数: {}",
|
||||
ipcDefaults.getConfigs().size(), nvrDefaults.getConfigs().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置验证和完整性检查
|
||||
*/
|
||||
private void validateConfigCompleteness() {
|
||||
log.info("验证配置完整性...");
|
||||
|
||||
// 获取所有默认配置
|
||||
PageResponse<DefaultConfigResponse> allConfigs = defaultConfigService.listDefaultConfigs(1, 100);
|
||||
|
||||
// 检查必需的基础配置是否存在
|
||||
String[] requiredConfigs = {"resolution", "frameRate", "codec", "protocol"};
|
||||
for (String requiredConfig : requiredConfigs) {
|
||||
boolean exists = allConfigs.getList().stream()
|
||||
.anyMatch(config -> requiredConfig.equals(config.getConfigKey()));
|
||||
log.info("必需配置 {} 存在: {}", requiredConfig, exists ? "✓" : "✗");
|
||||
}
|
||||
|
||||
// 统计配置类型分布
|
||||
Map<String, Long> typeDistribution = new HashMap<>();
|
||||
allConfigs.getList().forEach(config ->
|
||||
typeDistribution.merge(config.getConfigType(), 1L, Long::sum)
|
||||
);
|
||||
|
||||
log.info("配置类型分布: {}", typeDistribution);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置迁移和批量更新模式
|
||||
*/
|
||||
private void configMigrationPattern() {
|
||||
log.info("配置迁移模式示例...");
|
||||
|
||||
try {
|
||||
// 1. 获取需要升级的配置
|
||||
PageResponse<DefaultConfigResponse> oldConfigs = defaultConfigService.listDefaultConfigs(1, 50);
|
||||
|
||||
// 2. 创建升级配置批次
|
||||
BatchDefaultConfigRequest upgradeRequest = defaultConfigService.createBatchConfigBuilder()
|
||||
.addConfig("api_version", "v2", "string", "API版本")
|
||||
.addConfig("security_mode", "enhanced", "string", "安全模式")
|
||||
.build();
|
||||
|
||||
// 3. 执行批量升级
|
||||
BatchDefaultConfigResponse upgradeResult = defaultConfigService.batchUpdateDefaultConfigs(upgradeRequest);
|
||||
log.info("配置升级结果: 成功={}, 失败={}", upgradeResult.getSuccess(), upgradeResult.getFailed());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("配置迁移示例执行失败(这是正常的,因为是示例代码)", e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,190 +0,0 @@
|
||||
package com.ycwl.basic.integration.device.example;
|
||||
|
||||
import com.ycwl.basic.integration.device.dto.device.*;
|
||||
import com.ycwl.basic.integration.device.service.DeviceIntegrationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 设备配置筛选功能使用示例
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DeviceFilterExample {
|
||||
|
||||
private final DeviceIntegrationService deviceIntegrationService;
|
||||
|
||||
/**
|
||||
* 示例1: 查找高质量户外IPC设备
|
||||
* 条件: 分辨率1920x1080,帧率>=30,位置包含"outdoor"
|
||||
*/
|
||||
public void findHighQualityOutdoorDevices() {
|
||||
log.info("=== 查找高质量户外IPC设备 ===");
|
||||
|
||||
List<ConfigFilter> configFilters = Arrays.asList(
|
||||
new ConfigFilter("resolution", "1920x1080", "eq"),
|
||||
new ConfigFilter("framerate", 30, "gte"),
|
||||
new ConfigFilter("location", "outdoor", "like")
|
||||
);
|
||||
|
||||
Map<String, Object> deviceFilters = new HashMap<>();
|
||||
deviceFilters.put("type", "IPC");
|
||||
deviceFilters.put("isActive", 1);
|
||||
|
||||
FilterDevicesByConfigsRequest request = new FilterDevicesByConfigsRequest();
|
||||
request.setPage(1);
|
||||
request.setPageSize(20);
|
||||
request.setConfigFilters(configFilters);
|
||||
request.setFilterLogic("AND");
|
||||
request.setDeviceFilters(deviceFilters);
|
||||
|
||||
try {
|
||||
FilterDevicesByConfigsResponse response = deviceIntegrationService.filterDevicesByConfigs(request);
|
||||
log.info("找到 {} 个高质量户外IPC设备", response.getTotal());
|
||||
|
||||
response.getList().forEach(device -> {
|
||||
log.info("设备: {} ({}), 配置: {}", device.getName(), device.getNo(), device.getConfig());
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("查找高质量户外IPC设备失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例2: 查找缺少关键配置的设备
|
||||
* 条件: 缺少备份服务器或夜视功能配置
|
||||
*/
|
||||
public void findDevicesWithMissingConfigs() {
|
||||
log.info("=== 查找缺少关键配置的设备 ===");
|
||||
|
||||
List<ConfigFilter> configFilters = Arrays.asList(
|
||||
new ConfigFilter("backup_server", null, "is_null"),
|
||||
new ConfigFilter("night_vision", null, "is_null")
|
||||
);
|
||||
|
||||
FilterDevicesByConfigsRequest request = new FilterDevicesByConfigsRequest();
|
||||
request.setConfigFilters(configFilters);
|
||||
request.setFilterLogic("OR"); // 缺少任一配置即返回
|
||||
request.setPageSize(50);
|
||||
|
||||
try {
|
||||
FilterDevicesByConfigsResponse response = deviceIntegrationService.filterDevicesByConfigs(request);
|
||||
log.info("找到 {} 个缺少关键配置的设备", response.getTotal());
|
||||
|
||||
response.getList().forEach(device -> {
|
||||
Map<String, Object> config = device.getConfig();
|
||||
boolean missingBackup = !config.containsKey("backup_server") || config.get("backup_server") == null;
|
||||
boolean missingNightVision = !config.containsKey("night_vision") || config.get("night_vision") == null;
|
||||
|
||||
log.info("设备: {} ({}), 缺少配置: {}{}",
|
||||
device.getName(), device.getNo(),
|
||||
missingBackup ? "备份服务器 " : "",
|
||||
missingNightVision ? "夜视功能 " : "");
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("查找缺少关键配置的设备失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例3: 根据多个位置查找设备
|
||||
* 条件: 位置在指定列表中
|
||||
*/
|
||||
public void findDevicesByMultipleLocations() {
|
||||
log.info("=== 根据多个位置查找设备 ===");
|
||||
|
||||
List<String> locations = Arrays.asList("outdoor", "indoor", "parking");
|
||||
List<ConfigFilter> configFilters = Arrays.asList(
|
||||
new ConfigFilter("location", locations, "in")
|
||||
);
|
||||
|
||||
FilterDevicesByConfigsRequest request = new FilterDevicesByConfigsRequest();
|
||||
request.setConfigFilters(configFilters);
|
||||
request.setPageSize(100);
|
||||
|
||||
try {
|
||||
FilterDevicesByConfigsResponse response = deviceIntegrationService.filterDevicesByConfigs(request);
|
||||
log.info("找到 {} 个设备在指定位置", response.getTotal());
|
||||
|
||||
response.getList().forEach(device -> {
|
||||
Object location = device.getConfig().get("location");
|
||||
log.info("设备: {} ({}), 位置: {}", device.getName(), device.getNo(), location);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("根据多个位置查找设备失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例4: 使用便捷方法查找设备
|
||||
*/
|
||||
public void useConvenienceMethods() {
|
||||
log.info("=== 使用便捷方法查找设备 ===");
|
||||
|
||||
try {
|
||||
// 查找缺少配置的设备
|
||||
FilterDevicesByConfigsResponse response4 = deviceIntegrationService
|
||||
.findDevicesWithMissingConfig("firmware_version", 1, 10);
|
||||
log.info("缺少固件版本配置的设备数: {}", response4.getTotal());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("使用便捷方法查找设备失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例5: 性能监控查询
|
||||
* 条件: CPU使用率>80% 或 内存使用率>85%
|
||||
*/
|
||||
public void findHighLoadDevices() {
|
||||
log.info("=== 查找高负载设备 ===");
|
||||
|
||||
List<ConfigFilter> configFilters = Arrays.asList(
|
||||
new ConfigFilter("cpu_usage", 80, "gt"),
|
||||
new ConfigFilter("memory_usage", 85, "gt")
|
||||
);
|
||||
|
||||
FilterDevicesByConfigsRequest request = new FilterDevicesByConfigsRequest();
|
||||
request.setConfigFilters(configFilters);
|
||||
request.setFilterLogic("OR");
|
||||
request.setPageSize(50);
|
||||
|
||||
try {
|
||||
FilterDevicesByConfigsResponse response = deviceIntegrationService.filterDevicesByConfigs(request);
|
||||
log.info("找到 {} 个高负载设备", response.getTotal());
|
||||
|
||||
response.getList().forEach(device -> {
|
||||
Map<String, Object> config = device.getConfig();
|
||||
Object cpuUsage = config.get("cpu_usage");
|
||||
Object memoryUsage = config.get("memory_usage");
|
||||
|
||||
log.info("高负载设备: {} ({}), CPU: {}%, 内存: {}%",
|
||||
device.getName(), device.getNo(), cpuUsage, memoryUsage);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("查找高负载设备失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行所有示例
|
||||
*/
|
||||
public void runAllExamples() {
|
||||
log.info("开始运行设备配置筛选示例...");
|
||||
|
||||
findHighQualityOutdoorDevices();
|
||||
findDevicesWithMissingConfigs();
|
||||
findDevicesByMultipleLocations();
|
||||
useConvenienceMethods();
|
||||
findHighLoadDevices();
|
||||
|
||||
log.info("所有示例运行完成");
|
||||
}
|
||||
}
|
@@ -1,177 +0,0 @@
|
||||
package com.ycwl.basic.integration.device.example;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.device.dto.device.*;
|
||||
import com.ycwl.basic.integration.device.dto.config.*;
|
||||
import com.ycwl.basic.integration.device.service.DeviceConfigIntegrationService;
|
||||
import com.ycwl.basic.integration.device.service.DeviceIntegrationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Device Integration 使用示例
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DeviceIntegrationExample {
|
||||
|
||||
private final DeviceIntegrationService deviceService;
|
||||
private final DeviceConfigIntegrationService deviceConfigService;
|
||||
|
||||
/**
|
||||
* 基本设备操作
|
||||
*/
|
||||
public void basicDeviceOperations() {
|
||||
log.info("=== 基本设备操作 ===");
|
||||
|
||||
// 创建IPC摄像头设备(默认排序)
|
||||
DeviceV2DTO ipcDevice = deviceService.createIpcDevice(
|
||||
"前门摄像头", "CAM001", 1001L);
|
||||
log.info("创建IPC设备: {}, 排序值: {}", ipcDevice.getName(), ipcDevice.getSort());
|
||||
|
||||
// 根据ID获取设备信息
|
||||
DeviceV2DTO device = deviceService.getDevice(ipcDevice.getId());
|
||||
log.info("获取设备信息: {}, 排序值: {}", device.getName(), device.getSort());
|
||||
|
||||
// 根据设备编号获取设备信息
|
||||
DeviceV2DTO deviceByNo = deviceService.getDeviceByNo("CAM001");
|
||||
log.info("根据编号获取设备: {}", deviceByNo.getName());
|
||||
|
||||
// 分页查询景区设备列表
|
||||
PageResponse<DeviceV2DTO> deviceList = deviceService.getScenicIpcDevices(1001L, 1, 10);
|
||||
log.info("景区设备列表: 总数={}", deviceList.getTotal());
|
||||
|
||||
// 启用设备
|
||||
deviceService.enableDevice(ipcDevice.getId());
|
||||
log.info("设备已启用");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备排序功能演示
|
||||
*/
|
||||
public void deviceSortingOperations() {
|
||||
log.info("=== 设备排序功能演示 ===");
|
||||
|
||||
Long scenicId = 1001L;
|
||||
|
||||
// 创建带排序的设备
|
||||
DeviceV2DTO camera1 = deviceService.createIpcDeviceWithSort(
|
||||
"大门摄像头", "CAM_GATE", scenicId, 10);
|
||||
log.info("创建摄像头1: {}, 排序: {}", camera1.getName(), camera1.getSort());
|
||||
|
||||
DeviceV2DTO camera2 = deviceService.createIpcDeviceWithSort(
|
||||
"后门摄像头", "CAM_BACK", scenicId, 20);
|
||||
log.info("创建摄像头2: {}, 排序: {}", camera2.getName(), camera2.getSort());
|
||||
|
||||
DeviceV2DTO sensor1 = deviceService.createCustomDeviceWithSort(
|
||||
"温度传感器", "TEMP_01", scenicId, 5);
|
||||
log.info("创建传感器: {}, 排序: {}", sensor1.getName(), sensor1.getSort());
|
||||
|
||||
// 更新设备排序
|
||||
deviceService.updateDeviceSort(camera1.getId(), 1);
|
||||
log.info("更新摄像头1排序为1(置顶)");
|
||||
|
||||
// 获取排序后的设备列表
|
||||
PageResponse<DeviceV2DTO> sortedList = deviceService.listDevices(1, 10, null, null, null, 1, scenicId);
|
||||
log.info("排序后的设备列表:");
|
||||
for (DeviceV2DTO device : sortedList.getList()) {
|
||||
log.info(" - {}: 排序={}, 类型={}", device.getName(), device.getSort(), device.getType());
|
||||
}
|
||||
|
||||
// 批量调整排序演示
|
||||
log.info("--- 批量调整排序演示 ---");
|
||||
deviceService.updateDeviceSort(sensor1.getId(), 15); // 传感器排到中间
|
||||
deviceService.updateDeviceSort(camera2.getId(), 30); // 后门摄像头排到最后
|
||||
log.info("批量排序调整完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备配置管理
|
||||
*/
|
||||
public void deviceConfigurationOperations() {
|
||||
log.info("=== 设备配置管理 ===");
|
||||
|
||||
Long deviceId = 1L;
|
||||
|
||||
// 获取设备所有配置
|
||||
List<DeviceConfigV2DTO> configs = deviceConfigService.getDeviceConfigs(deviceId);
|
||||
log.info("设备配置数量: {}", configs.size());
|
||||
|
||||
// 使用批量配置API
|
||||
BatchDeviceConfigRequest builderRequest = deviceConfigService.createBatchConfigBuilder()
|
||||
.build();
|
||||
|
||||
BatchUpdateResponse result = deviceConfigService.batchUpdateDeviceConfig(deviceId, builderRequest);
|
||||
log.info("批量配置更新结果: 成功={}, 失败={}", result.getSuccess(), result.getFailed());
|
||||
}
|
||||
|
||||
/**
|
||||
* 排序最佳实践演示
|
||||
*/
|
||||
public void sortingBestPractices() {
|
||||
log.info("=== 排序最佳实践演示 ===");
|
||||
|
||||
Long scenicId = 1001L;
|
||||
|
||||
// 推荐使用10的倍数作为排序值
|
||||
DeviceV2DTO device1 = deviceService.createIpcDeviceWithSort(
|
||||
"重要摄像头", "CAM_IMPORTANT", scenicId, 10);
|
||||
|
||||
DeviceV2DTO device2 = deviceService.createIpcDeviceWithSort(
|
||||
"普通摄像头", "CAM_NORMAL", scenicId, 20);
|
||||
|
||||
DeviceV2DTO device3 = deviceService.createIpcDeviceWithSort(
|
||||
"备用摄像头", "CAM_BACKUP", scenicId, 30);
|
||||
|
||||
log.info("使用10的倍数创建设备排序: 10, 20, 30");
|
||||
|
||||
// 在中间插入新设备
|
||||
DeviceV2DTO insertDevice = deviceService.createIpcDeviceWithSort(
|
||||
"中间摄像头", "CAM_MIDDLE", scenicId, 25);
|
||||
log.info("在20和30之间插入设备,排序值: 25");
|
||||
|
||||
// 置顶操作
|
||||
deviceService.updateDeviceSort(device2.getId(), 1);
|
||||
log.info("将普通摄像头置顶(排序值: 1)");
|
||||
|
||||
// 查看最终排序结果
|
||||
PageResponse<DeviceV2DTO> finalList = deviceService.listDevices(1, 10, null, null, null, 1, scenicId);
|
||||
log.info("最终排序结果:");
|
||||
for (DeviceV2DTO device : finalList.getList()) {
|
||||
log.info(" - {}: 排序={}", device.getName(), device.getSort());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行所有示例
|
||||
*/
|
||||
public void runAllExamples() {
|
||||
try {
|
||||
basicDeviceOperations();
|
||||
deviceSortingOperations();
|
||||
sortingBestPractices();
|
||||
deviceConfigurationOperations();
|
||||
log.info("=== 所有示例执行完成 ===");
|
||||
} catch (Exception e) {
|
||||
log.error("示例执行过程中发生错误", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行基础示例(简化版)
|
||||
*/
|
||||
public void runBasicExamples() {
|
||||
try {
|
||||
basicDeviceOperations();
|
||||
deviceConfigurationOperations();
|
||||
log.info("=== 基础示例执行完成 ===");
|
||||
} catch (Exception e) {
|
||||
log.error("示例执行过程中发生错误", e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,117 +0,0 @@
|
||||
package com.ycwl.basic.integration.device.example;
|
||||
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.device.service.DeviceIntegrationService;
|
||||
import com.ycwl.basic.integration.device.service.DeviceConfigIntegrationService;
|
||||
import com.ycwl.basic.integration.device.dto.device.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 设备集成示例(包含降级机制)
|
||||
* 演示设备集成和失败降级策略的使用
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DeviceIntegrationFallbackExample {
|
||||
|
||||
private final DeviceIntegrationService deviceService;
|
||||
private final DeviceConfigIntegrationService configService;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-device";
|
||||
|
||||
/**
|
||||
* 演示设备信息获取的降级机制
|
||||
*/
|
||||
public void deviceInfoFallbackExample() {
|
||||
log.info("=== 设备信息获取降级示例 ===");
|
||||
|
||||
Long deviceId = 1001L;
|
||||
String deviceNo = "CAM001";
|
||||
|
||||
try {
|
||||
// 获取设备信息 - 自动降级
|
||||
DeviceV2DTO device = deviceService.getDevice(deviceId);
|
||||
log.info("获取设备成功: {}", device.getName());
|
||||
|
||||
// 根据设备号获取设备 - 自动降级
|
||||
DeviceV2DTO deviceByNo = deviceService.getDeviceByNo(deviceNo);
|
||||
log.info("根据设备号获取设备成功: {}", deviceByNo.getName());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("所有降级策略失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示设备操作(无降级机制)
|
||||
*/
|
||||
public void deviceOperationExample() {
|
||||
log.info("=== 设备操作示例 ===");
|
||||
|
||||
Long deviceId = 1001L;
|
||||
|
||||
try {
|
||||
// 设备更新操作 - 直接操作,失败时抛出异常
|
||||
UpdateDeviceRequest updateRequest = new UpdateDeviceRequest();
|
||||
updateRequest.setName("更新后的摄像头");
|
||||
deviceService.updateDevice(deviceId, updateRequest);
|
||||
log.info("设备更新操作完成");
|
||||
|
||||
// 设备排序更新 - 直接操作,失败时抛出异常
|
||||
deviceService.updateDeviceSort(deviceId, 5);
|
||||
log.info("设备排序更新完成");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("设备操作失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示降级缓存管理
|
||||
*/
|
||||
public void fallbackCacheManagementExample() {
|
||||
log.info("=== 降级缓存管理示例 ===");
|
||||
|
||||
String deviceCacheKey = "device:1001";
|
||||
|
||||
// 检查降级缓存状态
|
||||
boolean hasDeviceCache = fallbackService.hasFallbackCache(SERVICE_NAME, deviceCacheKey);
|
||||
|
||||
log.info("设备降级缓存存在: {}", hasDeviceCache);
|
||||
|
||||
// 清理特定的降级缓存
|
||||
if (hasDeviceCache) {
|
||||
fallbackService.clearFallbackCache(SERVICE_NAME, deviceCacheKey);
|
||||
log.info("已清理设备降级缓存");
|
||||
}
|
||||
|
||||
// 获取降级缓存统计信息
|
||||
IntegrationFallbackService.FallbackCacheStats stats = fallbackService.getFallbackCacheStats(SERVICE_NAME);
|
||||
log.info("设备服务降级缓存统计: {}", stats);
|
||||
|
||||
// 批量清理所有设备降级缓存
|
||||
if (stats.getTotalCacheCount() > 10) {
|
||||
fallbackService.clearAllFallbackCache(SERVICE_NAME);
|
||||
log.info("已批量清理所有设备降级缓存");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行所有示例
|
||||
*/
|
||||
public void runAllExamples() {
|
||||
log.info("开始运行设备集成示例...");
|
||||
|
||||
deviceInfoFallbackExample();
|
||||
deviceOperationExample();
|
||||
fallbackCacheManagementExample();
|
||||
|
||||
log.info("设备集成示例运行完成");
|
||||
}
|
||||
}
|
@@ -1,57 +0,0 @@
|
||||
package com.ycwl.basic.integration.kafka.example;
|
||||
|
||||
import com.ycwl.basic.integration.kafka.dto.KafkaMessage;
|
||||
import com.ycwl.basic.integration.kafka.service.KafkaIntegrationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Kafka集成使用示例(暂时注释,后续开发时启用)
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(name = "kafka.example.enabled", havingValue = "true", matchIfMissing = false)
|
||||
public class KafkaIntegrationExample {
|
||||
|
||||
private final KafkaIntegrationService kafkaService;
|
||||
|
||||
/**
|
||||
* 演示Kafka连接测试
|
||||
*/
|
||||
public void demonstrateConnectionTest() {
|
||||
log.info("=== Kafka Integration Example ===");
|
||||
|
||||
// 测试连接
|
||||
boolean connected = kafkaService.testConnection();
|
||||
log.info("Kafka connection status: {}", connected ? "SUCCESS" : "FAILED");
|
||||
|
||||
// 显示配置信息
|
||||
var properties = kafkaService.getKafkaProperties();
|
||||
log.info("Kafka Bootstrap Servers: {}", properties.getBootstrapServers());
|
||||
log.info("Consumer Group ID: {}", properties.getConsumer().getGroupId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示消息发送(预留示例)
|
||||
*/
|
||||
public void demonstrateMessageSending() {
|
||||
log.info("=== Message Sending Example (Not Implemented) ===");
|
||||
|
||||
// 创建示例消息
|
||||
KafkaMessage<String> message = KafkaMessage.of(
|
||||
"test-topic",
|
||||
"TEST_EVENT",
|
||||
"Hello Kafka from liuying-microservice!"
|
||||
);
|
||||
|
||||
// 发送消息(暂不实现)
|
||||
kafkaService.sendMessage("test-topic", "test-key", message);
|
||||
log.info("Message sending demonstration completed");
|
||||
}
|
||||
|
||||
// TODO: 后续添加消费者示例
|
||||
// public void demonstrateMessageConsuming() { ... }
|
||||
}
|
@@ -1,306 +0,0 @@
|
||||
package com.ycwl.basic.integration.questionnaire.example;
|
||||
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.questionnaire.dto.answer.AnswerRequest;
|
||||
import com.ycwl.basic.integration.questionnaire.dto.answer.ResponseDetailResponse;
|
||||
import com.ycwl.basic.integration.questionnaire.dto.answer.SubmitAnswerRequest;
|
||||
import com.ycwl.basic.integration.questionnaire.dto.question.CreateQuestionOptionRequest;
|
||||
import com.ycwl.basic.integration.questionnaire.dto.question.CreateQuestionRequest;
|
||||
import com.ycwl.basic.integration.questionnaire.dto.questionnaire.CreateQuestionnaireRequest;
|
||||
import com.ycwl.basic.integration.questionnaire.dto.questionnaire.QuestionnaireResponse;
|
||||
import com.ycwl.basic.integration.questionnaire.dto.statistics.QuestionnaireStatistics;
|
||||
import com.ycwl.basic.integration.questionnaire.service.QuestionnaireIntegrationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(prefix = "integration.questionnaire.example", name = "enabled", havingValue = "true")
|
||||
public class QuestionnaireIntegrationExample {
|
||||
|
||||
private final QuestionnaireIntegrationService questionnaireService;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runExamples() {
|
||||
try {
|
||||
log.info("=== 开始问卷集成服务示例 ===");
|
||||
|
||||
// 示例1:创建问卷
|
||||
createQuestionnaireExample();
|
||||
|
||||
// 示例2:查询问卷
|
||||
queryQuestionnaireExample();
|
||||
|
||||
// 示例3:提交答案
|
||||
submitAnswerExample();
|
||||
|
||||
// 示例4:统计查询
|
||||
statisticsExample();
|
||||
|
||||
// 示例5:Fallback 缓存管理
|
||||
fallbackCacheExample();
|
||||
|
||||
log.info("=== 问卷集成服务示例完成 ===");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("问卷集成服务示例执行失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例1:创建问卷
|
||||
*/
|
||||
private void createQuestionnaireExample() {
|
||||
log.info("--- 示例1:创建客户满意度问卷 ---");
|
||||
|
||||
try {
|
||||
CreateQuestionnaireRequest request = new CreateQuestionnaireRequest();
|
||||
request.setName("客户满意度调查");
|
||||
request.setDescription("用于了解客户对我们服务的满意度");
|
||||
request.setIsAnonymous(true);
|
||||
request.setMaxAnswers(1000);
|
||||
|
||||
// 添加单选题
|
||||
CreateQuestionRequest question1 = new CreateQuestionRequest();
|
||||
question1.setTitle("您对我们的服务满意吗?");
|
||||
question1.setType(1); // 单选题
|
||||
question1.setIsRequired(true);
|
||||
question1.setSort(1);
|
||||
|
||||
List<CreateQuestionOptionRequest> options1 = new ArrayList<>();
|
||||
options1.add(new CreateQuestionOptionRequest("非常满意", "5", 1));
|
||||
options1.add(new CreateQuestionOptionRequest("满意", "4", 2));
|
||||
options1.add(new CreateQuestionOptionRequest("一般", "3", 3));
|
||||
options1.add(new CreateQuestionOptionRequest("不满意", "2", 4));
|
||||
options1.add(new CreateQuestionOptionRequest("非常不满意", "1", 5));
|
||||
question1.setOptions(options1);
|
||||
|
||||
// 添加多选题
|
||||
CreateQuestionRequest question2 = new CreateQuestionRequest();
|
||||
question2.setTitle("您感兴趣的服务有哪些?");
|
||||
question2.setType(2); // 多选题
|
||||
question2.setIsRequired(false);
|
||||
question2.setSort(2);
|
||||
|
||||
List<CreateQuestionOptionRequest> options2 = new ArrayList<>();
|
||||
options2.add(new CreateQuestionOptionRequest("技术支持", "tech_support", 1));
|
||||
options2.add(new CreateQuestionOptionRequest("产品培训", "training", 2));
|
||||
options2.add(new CreateQuestionOptionRequest("定制开发", "custom_dev", 3));
|
||||
options2.add(new CreateQuestionOptionRequest("其他", "others", 4));
|
||||
question2.setOptions(options2);
|
||||
|
||||
// 添加文本域题
|
||||
CreateQuestionRequest question3 = new CreateQuestionRequest();
|
||||
question3.setTitle("您还有什么建议吗?");
|
||||
question3.setType(4); // 文本域题
|
||||
question3.setIsRequired(false);
|
||||
question3.setSort(3);
|
||||
question3.setOptions(null); // 文本域题不需要选项
|
||||
|
||||
request.setQuestions(Arrays.asList(question1, question2, question3));
|
||||
|
||||
QuestionnaireResponse response = questionnaireService.createQuestionnaire(request, "admin");
|
||||
log.info("✅ 问卷创建成功,ID: {}, 名称: {}", response.getId(), response.getName());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 创建问卷示例失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例2:查询问卷
|
||||
*/
|
||||
private void queryQuestionnaireExample() {
|
||||
log.info("--- 示例2:查询问卷示例 ---");
|
||||
|
||||
try {
|
||||
// 获取问卷列表(支持 fallback)
|
||||
PageResponse<QuestionnaireResponse> listResponse = questionnaireService.getQuestionnaireList(1, 10, null, null, null);
|
||||
log.info("✅ 问卷列表查询成功,总数: {}, 当前页数据: {}",
|
||||
listResponse.getTotal(), listResponse.getList().size());
|
||||
|
||||
if (listResponse.getList() != null && !listResponse.getList().isEmpty()) {
|
||||
Long questionnaireId = listResponse.getList().get(0).getId();
|
||||
|
||||
// 获取问卷详情(支持 fallback)
|
||||
QuestionnaireResponse detailResponse = questionnaireService.getQuestionnaire(questionnaireId);
|
||||
log.info("✅ 问卷详情查询成功,ID: {}, 名称: {}, 问题数: {}",
|
||||
detailResponse.getId(), detailResponse.getName(),
|
||||
detailResponse.getQuestions() != null ? detailResponse.getQuestions().size() : 0);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 查询问卷示例失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例3:提交答案
|
||||
*/
|
||||
private void submitAnswerExample() {
|
||||
log.info("--- 示例3:提交问卷答案示例 ---");
|
||||
|
||||
try {
|
||||
SubmitAnswerRequest request = new SubmitAnswerRequest();
|
||||
request.setQuestionnaireId(1001L);
|
||||
request.setUserId("user123");
|
||||
|
||||
List<AnswerRequest> answers = new ArrayList<>();
|
||||
// 单选题答案
|
||||
answers.add(new AnswerRequest(123L, "4")); // 满意
|
||||
// 多选题答案
|
||||
answers.add(new AnswerRequest(124L, "tech_support,training")); // 技术支持和产品培训
|
||||
// 文本域题答案
|
||||
answers.add(new AnswerRequest(125L, "服务很好,希望能增加更多实用功能"));
|
||||
|
||||
request.setAnswers(answers);
|
||||
|
||||
ResponseDetailResponse response = questionnaireService.submitAnswer(request);
|
||||
log.info("✅ 问卷答案提交成功,回答ID: {}, 提交时间: {}",
|
||||
response.getId(), response.getSubmittedAt());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 提交答案示例失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例4:统计查询
|
||||
*/
|
||||
private void statisticsExample() {
|
||||
log.info("--- 示例4:问卷统计查询示例 ---");
|
||||
|
||||
try {
|
||||
Long questionnaireId = 1001L;
|
||||
|
||||
// 获取问卷统计(支持 fallback)
|
||||
QuestionnaireStatistics stats = questionnaireService.getStatistics(questionnaireId);
|
||||
log.info("✅ 统计查询成功,总回答数: {}, 完成率: {}%, 平均用时: {}秒",
|
||||
stats.getTotalResponses(),
|
||||
stats.getCompletionRate() != null ? stats.getCompletionRate() * 100 : 0,
|
||||
stats.getAverageTime());
|
||||
|
||||
// 获取回答记录列表(支持 fallback)
|
||||
questionnaireService.getResponseList(1, 10, questionnaireId, null, null, null);
|
||||
log.info("✅ 回答记录列表查询成功");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 统计查询示例失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例5:Fallback 缓存管理
|
||||
*/
|
||||
private void fallbackCacheExample() {
|
||||
log.info("--- 示例5:Fallback 缓存管理示例 ---");
|
||||
|
||||
try {
|
||||
String serviceName = "zt-questionnaire";
|
||||
|
||||
// 检查缓存状态
|
||||
boolean hasQuestionnaireCache = fallbackService.hasFallbackCache(serviceName, "questionnaire:1001");
|
||||
boolean hasListCache = fallbackService.hasFallbackCache(serviceName, "questionnaire:list:1:10:null:null:null");
|
||||
log.info("✅ 缓存状态检查 - 问卷缓存: {}, 列表缓存: {}", hasQuestionnaireCache, hasListCache);
|
||||
|
||||
// 获取缓存统计
|
||||
IntegrationFallbackService.FallbackCacheStats stats = fallbackService.getFallbackCacheStats(serviceName);
|
||||
log.info("✅ 缓存统计 - 缓存项目数: {}, TTL: {} 天",
|
||||
stats.getTotalCacheCount(), stats.getFallbackTtlDays());
|
||||
|
||||
// 清理特定缓存示例(仅演示,实际使用时谨慎操作)
|
||||
// fallbackService.clearFallbackCache(serviceName, "questionnaire:1001");
|
||||
// log.info("✅ 已清理问卷缓存");
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ Fallback 缓存管理示例失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 问卷管理工作流示例
|
||||
*/
|
||||
public void questionnaireWorkflowExample(String userId) {
|
||||
log.info("--- 问卷管理工作流示例 ---");
|
||||
|
||||
try {
|
||||
// 1. 创建问卷
|
||||
CreateQuestionnaireRequest createRequest = createSampleQuestionnaire();
|
||||
QuestionnaireResponse questionnaire = questionnaireService.createQuestionnaire(createRequest, userId);
|
||||
log.info("✅ 步骤1 - 问卷创建成功: {}", questionnaire.getName());
|
||||
|
||||
Long questionnaireId = questionnaire.getId();
|
||||
|
||||
// 2. 发布问卷
|
||||
QuestionnaireResponse published = questionnaireService.publishQuestionnaire(questionnaireId, userId);
|
||||
log.info("✅ 步骤2 - 问卷发布成功,状态: {}", published.getStatusText());
|
||||
|
||||
// 3. 模拟用户提交答案
|
||||
SubmitAnswerRequest answerRequest = createSampleAnswers(questionnaireId);
|
||||
ResponseDetailResponse answerResponse = questionnaireService.submitAnswer(answerRequest);
|
||||
log.info("✅ 步骤3 - 答案提交成功: {}", answerResponse.getId());
|
||||
|
||||
// 4. 查看统计数据
|
||||
QuestionnaireStatistics statistics = questionnaireService.getStatistics(questionnaireId);
|
||||
log.info("✅ 步骤4 - 统计查询成功,回答数: {}", statistics.getTotalResponses());
|
||||
|
||||
// 5. 停止问卷
|
||||
QuestionnaireResponse stopped = questionnaireService.stopQuestionnaire(questionnaireId, userId);
|
||||
log.info("✅ 步骤5 - 问卷停止成功,状态: {}", stopped.getStatusText());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 问卷管理工作流示例失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
private CreateQuestionnaireRequest createSampleQuestionnaire() {
|
||||
CreateQuestionnaireRequest request = new CreateQuestionnaireRequest();
|
||||
request.setName("产品体验调查");
|
||||
request.setDescription("收集用户对产品的使用体验反馈");
|
||||
request.setIsAnonymous(false);
|
||||
request.setMaxAnswers(500);
|
||||
|
||||
// 评分题
|
||||
CreateQuestionRequest ratingQuestion = new CreateQuestionRequest();
|
||||
ratingQuestion.setTitle("请对我们的产品进行评分(1-10分)");
|
||||
ratingQuestion.setType(5); // 评分题
|
||||
ratingQuestion.setIsRequired(true);
|
||||
ratingQuestion.setSort(1);
|
||||
ratingQuestion.setOptions(null); // 评分题不需要选项
|
||||
|
||||
// 填空题
|
||||
CreateQuestionRequest textQuestion = new CreateQuestionRequest();
|
||||
textQuestion.setTitle("请输入您的姓名");
|
||||
textQuestion.setType(3); // 填空题
|
||||
textQuestion.setIsRequired(true);
|
||||
textQuestion.setSort(2);
|
||||
textQuestion.setOptions(null); // 填空题不需要选项
|
||||
|
||||
request.setQuestions(Arrays.asList(ratingQuestion, textQuestion));
|
||||
return request;
|
||||
}
|
||||
|
||||
private SubmitAnswerRequest createSampleAnswers(Long questionnaireId) {
|
||||
SubmitAnswerRequest request = new SubmitAnswerRequest();
|
||||
request.setQuestionnaireId(questionnaireId);
|
||||
request.setUserId("test_user");
|
||||
|
||||
List<AnswerRequest> answers = new ArrayList<>();
|
||||
answers.add(new AnswerRequest(1L, "8")); // 评分题答案
|
||||
answers.add(new AnswerRequest(2L, "张三")); // 填空题答案
|
||||
|
||||
request.setAnswers(answers);
|
||||
return request;
|
||||
}
|
||||
}
|
@@ -5,7 +5,6 @@ import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterPageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.CreateScenicRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2WithConfigDTO;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.UpdateScenicRequest;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
|
@@ -1,14 +0,0 @@
|
||||
package com.ycwl.basic.integration.scenic.dto.scenic;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class ScenicV2WithConfigDTO extends ScenicV2DTO {
|
||||
@JsonProperty("config")
|
||||
private Map<String, Object> config;
|
||||
}
|
@@ -1,179 +0,0 @@
|
||||
package com.ycwl.basic.integration.scenic.example;
|
||||
|
||||
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
|
||||
import com.ycwl.basic.integration.scenic.dto.config.*;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.*;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.*;
|
||||
import com.ycwl.basic.integration.scenic.service.ScenicConfigIntegrationService;
|
||||
import com.ycwl.basic.integration.scenic.service.ScenicIntegrationService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 景区集成示例(包含降级机制)
|
||||
* 演示景区集成和失败降级策略的使用
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ScenicIntegrationExample {
|
||||
|
||||
private final ScenicIntegrationService scenicIntegrationService;
|
||||
private final ScenicConfigIntegrationService scenicConfigIntegrationService;
|
||||
private final IntegrationFallbackService fallbackService;
|
||||
|
||||
private static final String SERVICE_NAME = "zt-scenic";
|
||||
|
||||
/**
|
||||
* 示例:创建景区并设置配置
|
||||
*/
|
||||
public void createScenicWithConfig() {
|
||||
try {
|
||||
// 1. 创建景区
|
||||
CreateScenicRequest createRequest = new CreateScenicRequest();
|
||||
createRequest.setName("测试景区");
|
||||
createRequest.setMpId(1001);
|
||||
|
||||
var scenic = scenicIntegrationService.createScenic(createRequest);
|
||||
log.info("创建景区成功: {}", scenic.getName());
|
||||
|
||||
// 2. 为景区添加配置
|
||||
CreateConfigRequest configRequest = new CreateConfigRequest();
|
||||
configRequest.setConfigKey("tour_time");
|
||||
configRequest.setConfigValue("120");
|
||||
configRequest.setConfigType("int");
|
||||
configRequest.setDescription("游览时长");
|
||||
|
||||
var config = scenicConfigIntegrationService.createConfig(
|
||||
Long.valueOf(scenic.getId()), configRequest);
|
||||
log.info("创建配置成功: {} = {}", config.getConfigKey(), config.getConfigValue());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("创建景区和配置失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例:筛选景区
|
||||
*/
|
||||
public void filterScenics() {
|
||||
try {
|
||||
FilterCondition condition = new FilterCondition();
|
||||
condition.setConfigKey("tour_time");
|
||||
condition.setConfigValue("120");
|
||||
condition.setOperator("gte");
|
||||
|
||||
ScenicFilterRequest filterRequest = new ScenicFilterRequest();
|
||||
filterRequest.setFilters(Collections.singletonList(condition));
|
||||
filterRequest.setPage(1);
|
||||
filterRequest.setPageSize(10);
|
||||
|
||||
var result = scenicIntegrationService.filterScenics(filterRequest);
|
||||
log.info("筛选到 {} 个景区", result.getTotal());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("筛选景区失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示基础景区操作的降级机制
|
||||
*/
|
||||
public void basicScenicOperationsExample() {
|
||||
log.info("=== 基础景区操作示例(含降级机制) ===");
|
||||
|
||||
Long scenicId = 2001L;
|
||||
|
||||
try {
|
||||
// 获取景区信息 - 自动降级
|
||||
ScenicV2DTO scenic = scenicIntegrationService.getScenic(scenicId);
|
||||
log.info("获取景区成功: {}", scenic.getName());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("景区操作降级失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示景区配置管理的降级机制
|
||||
*/
|
||||
public void scenicConfigManagementFallbackExample() {
|
||||
log.info("=== 景区配置管理示例(含降级机制) ===");
|
||||
|
||||
Long scenicId = 2001L;
|
||||
|
||||
try {
|
||||
// 获取配置列表 - 自动降级
|
||||
var configs = scenicConfigIntegrationService.listConfigs(scenicId);
|
||||
log.info("获取配置列表成功,配置项数量: {}", configs.size());
|
||||
|
||||
// 批量更新配置 - 直接操作,失败时抛出异常
|
||||
BatchConfigRequest batchRequest = new BatchConfigRequest();
|
||||
// 添加配置项示例
|
||||
BatchConfigRequest.BatchConfigItem item1 = new BatchConfigRequest.BatchConfigItem();
|
||||
item1.setConfigKey("max_visitors");
|
||||
item1.setConfigValue("5000");
|
||||
BatchConfigRequest.BatchConfigItem item2 = new BatchConfigRequest.BatchConfigItem();
|
||||
item2.setConfigKey("opening_hours");
|
||||
item2.setConfigValue("08:00-18:00");
|
||||
batchRequest.setConfigs(java.util.Arrays.asList(item1, item2));
|
||||
|
||||
BatchUpdateResponse result = scenicConfigIntegrationService.batchUpdateConfigs(scenicId, batchRequest);
|
||||
log.info("批量更新配置完成: 成功 {}, 失败 {}", result.getSuccess(), result.getFailed());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("景区配置管理操作失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示降级缓存管理
|
||||
*/
|
||||
public void fallbackCacheManagementExample() {
|
||||
log.info("=== 景区降级缓存管理示例 ===");
|
||||
|
||||
String scenicCacheKey = "scenic:2001";
|
||||
|
||||
// 检查降级缓存状态
|
||||
boolean hasScenicCache = fallbackService.hasFallbackCache(SERVICE_NAME, scenicCacheKey);
|
||||
|
||||
log.info("景区降级缓存存在: {}", hasScenicCache);
|
||||
|
||||
// 获取降级缓存统计信息
|
||||
IntegrationFallbackService.FallbackCacheStats stats = fallbackService.getFallbackCacheStats(SERVICE_NAME);
|
||||
log.info("景区服务降级缓存统计: 缓存数量={}, TTL={}天",
|
||||
stats.getTotalCacheCount(), stats.getFallbackTtlDays());
|
||||
|
||||
// 清理特定的降级缓存
|
||||
if (hasScenicCache) {
|
||||
fallbackService.clearFallbackCache(SERVICE_NAME, scenicCacheKey);
|
||||
log.info("已清理景区降级缓存");
|
||||
}
|
||||
|
||||
// 如果缓存过多,批量清理
|
||||
if (stats.getTotalCacheCount() > 50) {
|
||||
fallbackService.clearAllFallbackCache(SERVICE_NAME);
|
||||
log.info("已批量清理所有景区降级缓存");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行所有示例
|
||||
*/
|
||||
public void runAllExamples() {
|
||||
log.info("开始运行景区集成示例(包含降级机制)...");
|
||||
|
||||
createScenicWithConfig();
|
||||
filterScenics();
|
||||
basicScenicOperationsExample();
|
||||
scenicConfigManagementFallbackExample();
|
||||
fallbackCacheManagementExample();
|
||||
|
||||
log.info("景区集成示例运行完成");
|
||||
}
|
||||
}
|
@@ -9,7 +9,6 @@ import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterPageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.CreateScenicRequest;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2WithConfigDTO;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.UpdateScenicRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
@@ -4,7 +4,6 @@ import com.ycwl.basic.facebody.enums.FaceBodyAdapterType;
|
||||
import com.ycwl.basic.integration.common.util.ConfigValueUtil;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
|
||||
import com.ycwl.basic.integration.common.response.PageResponse;
|
||||
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2WithConfigDTO;
|
||||
import com.ycwl.basic.integration.scenic.service.ScenicIntegrationService;
|
||||
import com.ycwl.basic.integration.scenic.service.ScenicConfigIntegrationService;
|
||||
import com.ycwl.basic.integration.scenic.dto.config.ScenicConfigV2DTO;
|
||||
@@ -54,25 +53,9 @@ public class ScenicRepository {
|
||||
public ScenicEntity getScenic(Long id) {
|
||||
// 分别获取景区基础信息和配置信息
|
||||
ScenicV2DTO scenicBasic = scenicIntegrationService.getScenic(id);
|
||||
List<ScenicConfigV2DTO> configList = scenicConfigIntegrationService.listConfigs(id);
|
||||
ScenicConfigManager configManager = getScenicConfigManager(id);
|
||||
|
||||
// 将配置列表转换为Map
|
||||
Map<String, Object> configMap = new HashMap<>();
|
||||
if (configList != null) {
|
||||
for (ScenicConfigV2DTO config : configList) {
|
||||
configMap.put(config.getConfigKey(), config.getConfigValue());
|
||||
}
|
||||
}
|
||||
|
||||
// 手动组合成WithConfig对象用于转换
|
||||
ScenicV2WithConfigDTO scenicWithConfig = new ScenicV2WithConfigDTO();
|
||||
scenicWithConfig.setId(scenicBasic.getId());
|
||||
scenicWithConfig.setName(scenicBasic.getName());
|
||||
scenicWithConfig.setMpId(scenicBasic.getMpId());
|
||||
scenicWithConfig.setStatus(scenicBasic.getStatus());
|
||||
scenicWithConfig.setConfig(configMap);
|
||||
|
||||
ScenicEntity scenicEntity = convertToScenicEntity(scenicWithConfig);
|
||||
ScenicEntity scenicEntity = convertToScenicEntity(scenicBasic, configManager);
|
||||
return scenicEntity;
|
||||
}
|
||||
|
||||
@@ -239,7 +222,7 @@ public class ScenicRepository {
|
||||
}
|
||||
}
|
||||
|
||||
private ScenicEntity convertToScenicEntity(ScenicV2WithConfigDTO dto) {
|
||||
private ScenicEntity convertToScenicEntity(ScenicV2DTO dto, ScenicConfigManager configManager) {
|
||||
if (dto == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -248,19 +231,17 @@ public class ScenicRepository {
|
||||
entity.setName(dto.getName());
|
||||
entity.setMpId(dto.getMpId());
|
||||
entity.setStatus(dto.getStatus().toString());
|
||||
if (dto.getConfig() != null) {
|
||||
entity.setAddress(ConfigValueUtil.getStringValue(dto.getConfig(), "address"));
|
||||
entity.setArea(ConfigValueUtil.getStringValue(dto.getConfig(), "area"));
|
||||
entity.setCity(ConfigValueUtil.getStringValue(dto.getConfig(), "city"));
|
||||
entity.setProvince(ConfigValueUtil.getStringValue(dto.getConfig(), "province"));
|
||||
entity.setLatitude(ConfigValueUtil.getBigDecimalValue(dto.getConfig(), "latitude"));
|
||||
entity.setLongitude(ConfigValueUtil.getBigDecimalValue(dto.getConfig(), "longitude"));
|
||||
entity.setRadius(ConfigValueUtil.getBigDecimalValue(dto.getConfig(), "radius"));
|
||||
entity.setPhone(ConfigValueUtil.getStringValue(dto.getConfig(), "phone"));
|
||||
entity.setLogoUrl(ConfigValueUtil.getStringValue(dto.getConfig(), "logoUrl"));
|
||||
entity.setCoverUrl(ConfigValueUtil.getStringValue(dto.getConfig(), "coverUrl"));
|
||||
entity.setKfCodeUrl(ConfigValueUtil.getStringValue(dto.getConfig(), "kfCodeUrl"));
|
||||
}
|
||||
entity.setAddress(configManager.getString("address"));
|
||||
entity.setArea(configManager.getString("area"));
|
||||
entity.setCity(configManager.getString("city"));
|
||||
entity.setProvince(configManager.getString("province"));
|
||||
entity.setLatitude(configManager.getBigDecimal("latitude"));
|
||||
entity.setLongitude(configManager.getBigDecimal("longitude"));
|
||||
entity.setRadius(configManager.getBigDecimal("radius"));
|
||||
entity.setPhone(configManager.getString("phone"));
|
||||
entity.setLogoUrl(configManager.getString("logo_url"));
|
||||
entity.setCoverUrl(configManager.getString("cover_url"));
|
||||
entity.setKfCodeUrl(configManager.getString("kf_code_url"));
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user