| | |
| | | package com.mes.device.controller; |
| | | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.mes.device.entity.DeviceGroupConfig; |
| | | import com.mes.device.request.DeviceGroupRequest; |
| | | import com.mes.device.service.DeviceGroupConfigService; |
| | |
| | | |
| | | @Autowired |
| | | private DeviceGroupRelationService deviceGroupRelationService; |
| | | |
| | | @Resource |
| | | private ObjectMapper objectMapper; |
| | | |
| | | /** |
| | | * 创建设备组 |
| | |
| | | public Result<DeviceGroupConfig> createGroup( |
| | | @Valid @RequestBody DeviceGroupRequest request) { |
| | | try { |
| | | DeviceGroupConfig groupConfig = (DeviceGroupConfig) request.getGroupConfig(); |
| | | DeviceGroupConfig groupConfig = convertToDeviceGroupConfig(request.getGroupConfig()); |
| | | if (groupConfig == null) { |
| | | return Result.error("设备组配置信息不能为空"); |
| | | } |
| | | boolean success = deviceGroupConfigService.createDeviceGroup(groupConfig); |
| | | if (success) { |
| | | // 创建成功后,重新获取设备组对象 |
| | | DeviceGroupConfig created = deviceGroupConfigService.getDeviceGroupByCode(groupConfig.getGroupCode()); |
| | | return Result.success(created); |
| | | } else { |
| | | return Result.error(); |
| | | return Result.error("创建设备组失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("创建设备组失败", e); |
| | | return Result.error(); |
| | | return Result.error("创建设备组失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | public Result<DeviceGroupConfig> updateGroup( |
| | | @Valid @RequestBody DeviceGroupRequest request) { |
| | | try { |
| | | DeviceGroupConfig groupConfig = (DeviceGroupConfig) request.getGroupConfig(); |
| | | if (request.getGroupId() == null) { |
| | | return Result.error("设备组ID不能为空"); |
| | | } |
| | | DeviceGroupConfig groupConfig = convertToDeviceGroupConfig(request.getGroupConfig()); |
| | | if (groupConfig == null) { |
| | | return Result.error("设备组配置信息不能为空"); |
| | | } |
| | | 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(); |
| | | return Result.error("更新设备组配置失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("更新设备组配置失败", e); |
| | | return Result.error(); |
| | | return Result.error("更新设备组配置失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | return Result.error(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将Object转换为DeviceGroupConfig |
| | | * |
| | | * @param obj 待转换的对象 |
| | | * @return DeviceGroupConfig对象,如果转换失败返回null |
| | | */ |
| | | private DeviceGroupConfig convertToDeviceGroupConfig(Object obj) { |
| | | if (obj == null) { |
| | | return null; |
| | | } |
| | | |
| | | // 如果已经是DeviceGroupConfig类型,直接返回 |
| | | if (obj instanceof DeviceGroupConfig) { |
| | | return (DeviceGroupConfig) obj; |
| | | } |
| | | |
| | | // 如果是Map类型,使用ObjectMapper转换 |
| | | if (obj instanceof Map) { |
| | | try { |
| | | return objectMapper.convertValue(obj, DeviceGroupConfig.class); |
| | | } catch (Exception e) { |
| | | log.error("转换Map到DeviceGroupConfig失败", e); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | // 其他类型,尝试使用ObjectMapper转换 |
| | | try { |
| | | return objectMapper.convertValue(obj, DeviceGroupConfig.class); |
| | | } catch (Exception e) { |
| | | log.error("转换Object到DeviceGroupConfig失败: obj={}", obj, e); |
| | | return null; |
| | | } |
| | | } |
| | | } |