package com.mes.device.controller;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.mes.device.entity.DeviceGroupConfig;
|
import com.mes.device.request.DeviceGroupRequest;
|
import com.mes.device.service.DeviceGroupConfigService;
|
import com.mes.device.service.DeviceGroupRelationService;
|
import com.mes.device.vo.DeviceGroupVO;
|
import com.mes.device.vo.StatisticsVO;
|
import com.mes.vo.Result;
|
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import javax.validation.Valid;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 设备组管理控制器
|
*
|
* @author mes
|
* @since 2024-10-30
|
*/
|
@Slf4j
|
@RestController
|
@RequestMapping("device/group")
|
@Tag(name = "设备组管理", description = "设备组管理相关接口")
|
public class DeviceGroupController {
|
|
@Resource
|
private DeviceGroupConfigService deviceGroupConfigService;
|
|
@Autowired
|
private DeviceGroupRelationService deviceGroupRelationService;
|
|
/**
|
* 创建设备组
|
*/
|
@PostMapping("/create")
|
@Operation(summary = "创建设备组", description = "创建设备组信息")
|
public Result<DeviceGroupConfig> createGroup(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
DeviceGroupConfig groupConfig = (DeviceGroupConfig) request.getGroupConfig();
|
boolean success = deviceGroupConfigService.createDeviceGroup(groupConfig);
|
if (success) {
|
// 创建成功后,重新获取设备组对象
|
DeviceGroupConfig created = deviceGroupConfigService.getDeviceGroupByCode(groupConfig.getGroupCode());
|
return Result.success(created);
|
} else {
|
return Result.error();
|
}
|
} catch (Exception e) {
|
log.error("创建设备组失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 更新设备组配置
|
*/
|
@PostMapping("/update")
|
@Operation(summary = "更新设备组配置", description = "更新指定ID的设备组配置")
|
public Result<DeviceGroupConfig> updateGroup(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
DeviceGroupConfig groupConfig = (DeviceGroupConfig) request.getGroupConfig();
|
groupConfig.setId(request.getGroupId());
|
boolean success = deviceGroupConfigService.updateDeviceGroup(groupConfig);
|
if (success) {
|
// 更新成功后,重新获取设备组对象
|
DeviceGroupConfig updated = deviceGroupConfigService.getDeviceGroupByCode(groupConfig.getGroupCode());
|
return Result.success(updated);
|
} else {
|
return Result.error();
|
}
|
} catch (Exception e) {
|
log.error("更新设备组配置失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 删除设备组配置
|
*/
|
@PostMapping("/delete")
|
@Operation(summary = "删除设备组配置", description = "删除指定ID的设备组配置")
|
public Result<Void> deleteGroup(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupConfigService.deleteDeviceGroup(request.getGroupId());
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("删除设备组配置失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 根据ID获取设备组配置
|
*/
|
@PostMapping("/detail")
|
@Operation(summary = "获取设备组配置详情", description = "根据ID获取设备组配置的详细信息")
|
public Result<DeviceGroupConfig> getGroupById(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
DeviceGroupConfig group = deviceGroupConfigService.getDeviceGroupById(request.getGroupId());
|
return Result.success(group);
|
} catch (Exception e) {
|
log.error("获取设备组配置失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 分页查询设备组列表
|
*/
|
@PostMapping("/list")
|
@Operation(summary = "分页查询设备组列表", description = "分页查询设备组列表")
|
public Result<Page<DeviceGroupVO.GroupInfo>> getGroupList(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
@SuppressWarnings("unchecked")
|
Map<String, Object> configMap = (Map<String, Object>) (request.getGroupConfig() != null ? request.getGroupConfig() : new HashMap<>());
|
Page pageResult = deviceGroupConfigService.getDeviceGroupList(
|
(Long) configMap.get("projectId"),
|
(String) configMap.get("groupType"),
|
(String) configMap.get("groupStatus"),
|
(String) configMap.get("keyword"),
|
(Integer) configMap.get("page"),
|
(Integer) configMap.get("size")
|
);
|
return Result.success(pageResult);
|
} catch (Exception e) {
|
log.error("查询设备组列表失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 启用设备组
|
*/
|
@PostMapping("/enable")
|
@Operation(summary = "启用设备组", description = "启用指定设备组")
|
public Result<Void> enableGroup(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupConfigService.enableDeviceGroup(request.getGroupId());
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("启用设备组失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 禁用设备组
|
*/
|
@PostMapping("/disable")
|
@Operation(summary = "禁用设备组", description = "禁用指定设备组")
|
public Result<Void> disableGroup(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupConfigService.disableDeviceGroup(request.getGroupId());
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("禁用设备组失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 批量启用设备组
|
*/
|
@PostMapping("/batch-enable")
|
@Operation(summary = "批量启用设备组", description = "批量启用指定ID列表的设备组")
|
public Result<Void> batchEnableGroups(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupConfigService.batchEnableDeviceGroups(request.getGroupIds());
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("批量启用设备组失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 批量禁用设备组
|
*/
|
@PostMapping("/batch-disable")
|
@Operation(summary = "批量禁用设备组", description = "批量禁用指定ID列表的设备组")
|
public Result<Void> batchDisableGroups(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupConfigService.batchDisableDeviceGroups(request.getGroupIds());
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("批量禁用设备组失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 获取设备组统计信息
|
*/
|
@PostMapping("/statistics/groups")
|
@Operation(summary = "获取设备组统计信息", description = "获取设备组相关的统计信息")
|
public Result<StatisticsVO.GroupStatistics> getGroupStatistics(
|
@RequestBody(required = false) Map<String, Object> request) {
|
try {
|
Long projectId = request != null ? (Long) request.get("projectId") : null;
|
StatisticsVO.GroupStatistics statistics = deviceGroupConfigService.getDeviceGroupStatistics(projectId);
|
return Result.success(statistics);
|
} catch (Exception e) {
|
log.error("获取设备组统计信息失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 检查设备组编码是否已存在
|
*/
|
@PostMapping("/check-code")
|
@Operation(summary = "检查设备组编码", description = "检查设备组编码是否已存在")
|
public Result<Boolean> checkGroupCodeExists(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
@SuppressWarnings("unchecked")
|
Map<String, Object> configMap = (Map<String, Object>) (request.getGroupConfig() != null ? request.getGroupConfig() : new HashMap<>());
|
boolean exists = deviceGroupConfigService.isGroupCodeExists(
|
(String) configMap.get("groupCode"),
|
request.getGroupId());
|
return Result.success(exists);
|
} catch (Exception e) {
|
log.error("检查设备组编码失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 获取设备组类型列表
|
*/
|
@PostMapping("/types")
|
@Operation(summary = "获取设备组类型列表", description = "获取所有可用的设备组类型")
|
public Result<List<String>> getGroupTypes() {
|
try {
|
List<String> groupTypes = deviceGroupConfigService.getAllGroupTypes();
|
return Result.success(groupTypes);
|
} catch (Exception e) {
|
log.error("获取设备组类型列表失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 获取设备组状态列表
|
*/
|
@PostMapping("/statuses")
|
@Operation(summary = "获取设备组状态列表", description = "获取所有可用的设备组状态")
|
public Result<List<String>> getGroupStatuses() {
|
try {
|
List<String> groupStatuses = deviceGroupConfigService.getAllGroupStatuses();
|
return Result.success(groupStatuses);
|
} catch (Exception e) {
|
log.error("获取设备组状态列表失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 添加设备到设备组
|
*/
|
@PostMapping("/devices")
|
@Operation(summary = "添加设备到设备组", description = "将指定设备添加到设备组中")
|
public Result<Void> addDeviceToGroup(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupRelationService.addDeviceToGroup(request.getGroupId(), request.getDeviceId(),
|
request.getDeviceRole() != null ? request.getDeviceRole() : "MEMBER");
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("添加设备到设备组失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 从设备组移除设备
|
*/
|
@PostMapping("/devices/remove")
|
@Operation(summary = "从设备组移除设备", description = "从设备组中移除指定设备")
|
public Result<Void> removeDeviceFromGroup(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupRelationService.removeDeviceFromGroup(request.getGroupId(), request.getDeviceId());
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("从设备组移除设备失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 更新设备角色
|
*/
|
@PostMapping("/devices/role")
|
@Operation(summary = "更新设备角色", description = "更新设备在设备组中的角色")
|
public Result<Void> updateDeviceRole(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupRelationService.updateDeviceRole(request.getGroupId(), request.getDeviceId(),
|
request.getDeviceRole());
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("更新设备角色失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 获取设备组设备列表
|
*/
|
@PostMapping("/devices/list")
|
@Operation(summary = "获取设备组设备列表", description = "获取指定设备组下的所有设备")
|
public Result<List<DeviceGroupVO.DeviceInfo>> getGroupDevices(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
List<DeviceGroupVO.DeviceInfo> devices = deviceGroupRelationService.getGroupDevices(request.getGroupId());
|
return Result.success(devices);
|
} catch (Exception e) {
|
log.error("获取设备组设备列表失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 获取设备设备组列表
|
*/
|
@PostMapping("/devices/groups")
|
@Operation(summary = "获取设备设备组列表", description = "获取指定设备所属的所有设备组")
|
public Result<List<DeviceGroupVO.GroupInfo>> getDeviceGroups(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
List<DeviceGroupVO.GroupInfo> groups = deviceGroupRelationService.getDeviceGroups(request.getDeviceId());
|
return Result.success(groups);
|
} catch (Exception e) {
|
log.error("获取设备设备组列表失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 批量添加设备到设备组
|
*/
|
@PostMapping("/batch-add-devices")
|
@Operation(summary = "批量添加设备到设备组", description = "批量将指定设备列表添加到设备组中")
|
public Result<Void> batchAddDevicesToGroup(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupRelationService.batchAddDevicesToGroup(request.getGroupId(), request.getDeviceIds());
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("批量添加设备到设备组失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 批量从设备组移除设备
|
*/
|
@PostMapping("/devices/batch-remove")
|
@Operation(summary = "批量从设备组移除设备", description = "批量从设备组中移除指定设备列表")
|
public Result<Void> batchRemoveDevicesFromGroup(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
deviceGroupRelationService.batchRemoveDevicesFromGroup(request.getGroupId(), request.getDeviceIds());
|
return Result.success(null);
|
} catch (Exception e) {
|
log.error("批量从设备组移除设备失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 设备组健康检查
|
*/
|
@PostMapping("/health-check")
|
@Operation(summary = "设备组健康检查", description = "对指定设备组进行健康检查")
|
public Result<DeviceGroupVO.HealthCheckResult> performGroupHealthCheck(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
DeviceGroupVO.HealthCheckResult result = deviceGroupConfigService.performGroupHealthCheck(request.getGroupId());
|
return Result.success(result);
|
} catch (Exception e) {
|
log.error("设备组健康检查失败", e);
|
return Result.error();
|
}
|
}
|
|
/**
|
* 获取设备组性能统计
|
*/
|
@PostMapping("/performance")
|
@Operation(summary = "获取设备组性能统计", description = "获取指定设备组的性能统计信息")
|
public Result<DeviceGroupVO.PerformanceStats> getGroupPerformance(
|
@Valid @RequestBody DeviceGroupRequest request) {
|
try {
|
DeviceGroupVO.PerformanceStats stats = deviceGroupConfigService.getGroupPerformance(request.getGroupId());
|
return Result.success(stats);
|
} catch (Exception e) {
|
log.error("获取设备组性能统计失败", e);
|
return Result.error();
|
}
|
}
|
}
|