package com.dcs.hnyz.service.impl;

import com.dcs.hnyz.cache.CacheCenter;
import com.dcs.hnyz.domain.vo.EquipmentParamFormVO;
import com.dcs.hnyz.domain.vo.PageDeviceRealtimeVO;
import com.dcs.hnyz.service.IDeviceDataService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * DeviceDataServiceImpl
 *
 * @author: hmy
 * @date: 2025/5/30 16:16
 */
@Service
public class DeviceDataServiceImpl implements IDeviceDataService {

    @Override
    public Map<String, List<PageDeviceRealtimeVO>> buildRealtimeData(Map<String, List<String>> deviceGroupMap) {
        Map<String, List<PageDeviceRealtimeVO>> result = new HashMap<>();

        for (Map.Entry<String, List<String>> entry : deviceGroupMap.entrySet()) {
            String group = entry.getKey();
            List<PageDeviceRealtimeVO> voList = new ArrayList<>();
            for (String deviceCode : entry.getValue()) {
                List<String> registerCodes = CacheCenter.deviceToRegistersMap.get(deviceCode);
                List<Object> values = Optional.ofNullable(registerCodes).orElse(Collections.emptyList()).stream()
                        .map(code -> Optional.ofNullable(CacheCenter.registerNowDataMap.get(code)).map(EquipmentParamFormVO::getValue).orElse(null))
                        .collect(Collectors.toList());
                voList.add(new PageDeviceRealtimeVO(deviceCode, values));
            }
            result.put(group, voList);
        }

        return result;
    }
}
