package com.gyee.wisdom.alarm.sharding.cache;

import com.gyee.wisdom.alarm.sharding.entity.Windturbine;
import com.gyee.wisdom.alarm.sharding.mapper.WindturbineMapper;
import com.gyee.wisdom.alarm.sharding.service.WindturbineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @description:
 * @auther: Wanghs
 * @date: 2022-03-17
 */
@Component
public class WindturbineCache {

    @Autowired
    private WindturbineService windturbineService;

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


    private void initWindturbineList(){
        List<Windturbine> windturbines = windturbineService.windturbineList();

        windturbineList=windturbines;
    }

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

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

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

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