package com.gyee.alarm.init.Cache;

import com.gyee.alarm.model.auto.ProBasicEquipment;
import com.gyee.alarm.service.auto.impl.ProBasicEquipmentServiceImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Component
public class WindturbineCache {

    @Resource
    private ProBasicEquipmentServiceImpl proBasicEquipmentService;

    private List<ProBasicEquipment> windturbineList=new ArrayList<>();


    private void initWindturbineList(){
        List<ProBasicEquipment> windturbines = proBasicEquipmentService.windturbineList();

        windturbineList=windturbines;
    }

    public List<ProBasicEquipment> getWindturbineList(){
        if(windturbineList.size()<=0)
            initWindturbineList();
        return this.windturbineList;
    }

    public ProBasicEquipment getWindturbine(String windturbineId){
        if(windturbineList.size()<=0)
            initWindturbineList();

        Optional<ProBasicEquipment> first = windturbineList.stream().filter(s -> s.getId().equals(windturbineId)).findFirst();

        if(first.isPresent())
            return first.get();
        else
            return  null;
    }
}
