huang
2025-11-20 366ba040d2447bacd3455299425e3166f1f992bb
mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceGroupConfigServiceImpl.java
@@ -7,7 +7,9 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mes.device.entity.DeviceGroupConfig;
import com.mes.device.entity.DeviceGroupRelation;
import com.mes.device.mapper.DeviceGroupConfigMapper;
import com.mes.device.mapper.DeviceGroupRelationMapper;
import com.mes.device.service.DeviceGroupConfigService;
import com.mes.device.vo.DeviceGroupConfigVO;
import com.mes.device.vo.DeviceGroupVO;
@@ -27,6 +29,12 @@
public class DeviceGroupConfigServiceImpl extends ServiceImpl<DeviceGroupConfigMapper, DeviceGroupConfig> implements DeviceGroupConfigService {
    private final ObjectMapper objectMapper = new ObjectMapper();
    private final DeviceGroupRelationMapper deviceGroupRelationMapper;
    private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {};
    public DeviceGroupConfigServiceImpl(DeviceGroupRelationMapper deviceGroupRelationMapper) {
        this.deviceGroupRelationMapper = deviceGroupRelationMapper;
    }
    @Override
    public boolean createDeviceGroup(DeviceGroupConfig groupConfig) {
@@ -194,6 +202,7 @@
                vo.setGroupType(getGroupTypeName(group.getGroupType()));
                vo.setStatus(getStatusName(group.getStatus()));
                vo.setDeviceCount(getDeviceCountByGroupId(group.getId()));
                vo.setOnlineDeviceCount(getOnlineDeviceCountByGroupId(group.getId()));
                vo.setCreateTime(group.getCreatedTime());
                vo.setProjectId(group.getProjectId());
                return vo;
@@ -212,7 +221,6 @@
    @Override
    public List<DeviceGroupConfigVO.GroupInfo> getDeviceGroupVOList(Long projectId, Integer groupType, Integer status) {
        // TODO: 这里需要实现VO转换逻辑,包括设备数量统计
        List<DeviceGroupConfig> groupList = getDeviceGroupList(projectId, groupType, status);
        
        return groupList.stream().map(group -> {
@@ -225,8 +233,8 @@
            vo.setStatus(getStatusName(group.getStatus()));
            vo.setDeviceCount(getDeviceCountByGroupId(group.getId()));
            vo.setIsEnabled(group.getStatus() != null && group.getStatus() == DeviceGroupConfig.Status.ENABLED);
            vo.setLocation("默认位置"); // TODO: 从扩展配置或关联表中获取
            vo.setSupervisor("默认管理员"); // TODO: 从扩展配置或关联表中获取
            vo.setLocation(extractLocationFromExtraConfig(group));
            vo.setSupervisor(extractSupervisorFromExtraConfig(group));
            vo.setCreatedTime(new Date());
            vo.setUpdatedTime(new Date());
            vo.setProjectId(group.getProjectId());
@@ -425,9 +433,37 @@
    @Override
    public int getDeviceCountByGroupId(Long groupId) {
        // 这里需要查询device_group_relation表来获取设备数量
        // 简化实现,实际需要注入DeviceGroupRelationMapper
        return 0; // TODO: 实现真实逻辑
        if (groupId == null) {
            return 0;
        }
        try {
            LambdaQueryWrapper<DeviceGroupRelation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DeviceGroupRelation::getGroupId, groupId)
                   .eq(DeviceGroupRelation::getIsDeleted, 0);
            return (int) deviceGroupRelationMapper.selectCount(wrapper);
        } catch (Exception e) {
            log.error("获取设备组设备数量失败, groupId={}", groupId, e);
            return 0;
        }
    }
    /**
     * 获取设备组下的在线设备数量
     *
     * @param groupId 设备组ID
     * @return 在线设备数量
     */
    private int getOnlineDeviceCountByGroupId(Long groupId) {
        if (groupId == null) {
            return 0;
        }
        try {
            Integer count = deviceGroupRelationMapper.getOnlineDeviceCountByGroupId(groupId);
            return count != null ? count : 0;
        } catch (Exception e) {
            log.error("获取设备组在线设备数量失败, groupId={}", groupId, e);
            return 0;
        }
    }
    /**
@@ -577,8 +613,8 @@
            // 获取设备组下的设备数量
            int totalDevices = getDeviceCountByGroupId(groupId);
            
            // 获取在线设备数量
            int activeDevices = 0; // TODO: 需要根据实际业务逻辑实现
            // 获取在线设备数量(状态为正常的设备数)
            int activeDevices = getActiveDeviceCountByGroupId(groupId);
            
            // 计算平均性能指标(模拟数据)
            double averageCpuUsage = 45.5; // CPU使用率百分比
@@ -628,7 +664,7 @@
            // 获取设备组下的设备信息
            int totalDevices = getDeviceCountByGroupId(groupId);
            int onlineDevices = 0; // TODO: 需要根据实际业务逻辑实现
            int onlineDevices = getActiveDeviceCountByGroupId(groupId);
            int offlineDevices = totalDevices - onlineDevices;
            
            // 计算健康状态
@@ -792,6 +828,59 @@
        }
    }
    /**
     * 从扩展配置中提取位置信息
     */
    private String extractLocationFromExtraConfig(DeviceGroupConfig group) {
        if (group == null || group.getExtraConfig() == null) {
            return "默认位置";
        }
        try {
            Map<String, Object> extraConfig = objectMapper.readValue(group.getExtraConfig(), MAP_TYPE);
            Object location = extraConfig.get("location");
            return location != null ? String.valueOf(location) : "默认位置";
        } catch (Exception e) {
            log.warn("解析设备组位置信息失败, groupId={}", group.getId(), e);
            return "默认位置";
        }
    }
    /**
     * 从扩展配置中提取管理员信息
     */
    private String extractSupervisorFromExtraConfig(DeviceGroupConfig group) {
        if (group == null || group.getExtraConfig() == null) {
            return "默认管理员";
        }
        try {
            Map<String, Object> extraConfig = objectMapper.readValue(group.getExtraConfig(), MAP_TYPE);
            Object supervisor = extraConfig.get("supervisor");
            return supervisor != null ? String.valueOf(supervisor) : "默认管理员";
        } catch (Exception e) {
            log.warn("解析设备组管理员信息失败, groupId={}", group.getId(), e);
            return "默认管理员";
        }
    }
    /**
     * 获取设备组中活跃设备数量(状态为正常的设备)
     */
    private int getActiveDeviceCountByGroupId(Long groupId) {
        if (groupId == null) {
            return 0;
        }
        try {
            LambdaQueryWrapper<DeviceGroupRelation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DeviceGroupRelation::getGroupId, groupId)
                   .eq(DeviceGroupRelation::getStatus, DeviceGroupRelation.Status.NORMAL)
                   .eq(DeviceGroupRelation::getIsDeleted, 0);
            return (int) deviceGroupRelationMapper.selectCount(wrapper);
        } catch (Exception e) {
            log.error("获取设备组活跃设备数量失败, groupId={}", groupId, e);
            return 0;
        }
    }
    @Override
    public java.util.List<String> getAllGroupStatuses() {
        try {