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

import com.gyee.wisdom.alarm.sharding.entity.AlertKnowledge;
import com.gyee.wisdom.alarm.sharding.entity.Alertrule2;
import com.gyee.wisdom.alarm.sharding.entity.WindturbineParts;
import com.gyee.wisdom.alarm.sharding.mapper.AlertKnowledgeMapper;
import com.gyee.wisdom.alarm.sharding.mapper.Alertrule2Mapper;
import com.gyee.wisdom.alarm.sharding.mapper.WindturbinePartsMapper;
import com.gyee.wisdom.alarm.sharding.service.Alertrule2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @descrition: 报警信息类缓存
 * @author:Wanghs
 * @date:2020-06-22
 */
@Component
public class AlarmInfoCache {

    @Autowired
    private Alertrule2Mapper alertrule2Mapper;
    @Autowired
    private WindturbinePartsMapper windturbinePartsMapper;
    @Autowired
    private AlertKnowledgeMapper alertKnowledgeMapper;


    //key:自定义报警内容(AlertText) ,value:所属部件(relatedParts)
    private HashMap<String, String> alertTextToPartMap = new HashMap<>();

    private HashMap<String, String> relatePartsMap = new HashMap<>();

    private HashMap<String,AlertKnowledge> knowledgeHashMap=new HashMap<>();


    public HashMap<String, String> getAlertTextToPartMap() {
        if (alertTextToPartMap.size() <= 0) {
            List<Alertrule2> lst = alertrule2Mapper.getAllByStationIdAndModelId("");
            this.alertTextToPartMap.clear();
            for (Alertrule2 data : lst) {
                if (!alertTextToPartMap.containsKey(data.getName())) {
                    alertTextToPartMap.put(data.getName(), data.getRelatedParts());
                }
            }
        }
        return alertTextToPartMap;

    }

    public HashMap<String, String> getRelatePartsMap() {
        if (relatePartsMap.size() <= 0) {

            List<WindturbineParts> lst = windturbinePartsMapper.selectAllWindturbineParts();
            for (WindturbineParts parts : lst) {
                if (!relatePartsMap.containsKey(parts.getPartCode())) {
                    relatePartsMap.put(parts.getPartCode(), parts.getName());
                }
            }
        }
        return relatePartsMap;
    }

    public HashMap<String,AlertKnowledge> getKnowledgeHashMap(){
        if(knowledgeHashMap.size()<=0){
            List<AlertKnowledge> lst=alertKnowledgeMapper.searchAll();
            for (AlertKnowledge data:lst) {

                if(!knowledgeHashMap.containsKey(data.getAlertName().trim())){
                    knowledgeHashMap.put(data.getAlertName().trim(),data);
                }
            }
        }

        return  knowledgeHashMap;
    }
}
