package com.dcs.hnyz.cache.builder;

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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * RegisterCodeMapBuilder
 * 设备id-->主数据项对应寄存器唯一code缓存
 * @author: hmy
 * @date: 2025/5/27 14:16
 */
@Slf4j
@Component
public class RegisterCodeMapBuilder {

    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private IEquipmentParamService equipmentParamService;
    // 缓存存储
    private final Map<Integer, String> registerCodeMap = new HashMap<>();


    /**
     * 构建并返回缓存
     */
    public Map<Integer, String> build() {
        registerCodeMap.clear();
        List<Equipment> equipmentList = equipmentService.selectEquipmentList(null);
        for (Equipment equipment : equipmentList) {
            if (!DeviceTypeEnum.TANK.getCode().equals(equipment.getEquipmentType())) {
                String code = equipmentParamService.getCodeByParentId(equipment.getEquipmentId());
                if (code != null) {
                    registerCodeMap.put(Math.toIntExact(equipment.getEquipmentId()), code);
                }
            }
        }
//        log.info("registerCodeMap 构建完成，缓存数量：{}", registerCodeMap.size());
        return registerCodeMap;
    }

    public Map<Integer, String> getRegisterCodeMap() {
        return registerCodeMap;
    }

    //TODO 提供缓存更新方法
}
