package com.dcs.hnyz.cache.builder;

import com.dcs.hnyz.cache.CacheCenter;
import com.dcs.hnyz.domain.Equipment;
import com.dcs.hnyz.enums.DeviceTypeEnum;
import com.dcs.hnyz.service.IEquipmentParamService;
import com.dcs.hnyz.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * DeviceToRegistersMapBuilder
 * 设备code-->寄存器codeList映射关系缓存构造器
 * @author: hmy
 * @date: 2025/5/30 13:43
 */
@Component
public class DeviceToRegistersMapBuilder {
    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private IEquipmentParamService equipmentParamService;

    /**
     * 构建并返回缓存
     */
    public Map<String, List<String>> build() {
        CacheCenter.deviceToRegistersMap.clear();
        List<Equipment> equipmentList = equipmentService.selectEquipmentList(null);
        for (Equipment equipment : equipmentList) {
            if (!DeviceTypeEnum.TANK.getCode().equals(equipment.getEquipmentType())) {
                //设备标识
                String equipmentCode =equipment.getCode();
                //设备下所有寄存器code集合
                List<String> codes = equipmentParamService.getCodesByParentId(equipment.getEquipmentId());
                CacheCenter.deviceToRegistersMap.put(equipmentCode, codes);
            }
        }
//        log.info("设备code-->寄存器codeList映射关系缓存{}",CacheCenter.deviceToRegistersMap);
        return CacheCenter.deviceToRegistersMap;
    }
}
