package com.sunwin.metro.drools;



import com.alibaba.fastjson.JSONObject;
import com.sunwin.metro.bean.*;
import com.sunwin.metro.bean.RuleBase;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author kane
 * @date 2020/01/29
 */
public class MetroProcessorWithRules implements MetroProcessor {
    private static final Logger logger = LoggerFactory.getLogger(MetroProcessorWithRules.class);

    private KieBase kieBase = null;
    private boolean isEmptyKie = true;
    private final String rulePackage;

    public MetroProcessorWithRules(String pkg) {
        rulePackage = pkg;
    }



    @Override
    public List<JSONObject> execute(String msg) {
        return null;
    }


    @Override
    public List<JSONObject> execute(JSONObject entry) {
        List<JSONObject> result = new LinkedList<>();
        KieSession kSession = kieBase.newKieSession();
        try {
            //新增日志对象log
            if (!isEmptyKie) {
                logger.info("kie对象不为空");
            }
            if(entry.toString().contains("ups巡检仪")){
                UpsXjyMes upsXjyMes = JSONObject.parseObject(entry.toString(), UpsXjyMes.class);
                kSession.insert(upsXjyMes);
            }
            if(entry.toString().contains("雨水泵")){
                BasAtMes basAtMes = JSONObject.parseObject(entry.toString(), BasAtMes.class);
                kSession.insert(basAtMes);
            }
            if(entry.toString().contains("不间断电源")){
                UpsMes upsMes = JSONObject.parseObject(entry.toString(), UpsMes.class);
                kSession.insert(upsMes);
            }
            if(entry.toString().contains("排热风机")){
                BasTvfMes basTvfMes = JSONObject.parseObject(entry.toString(), BasTvfMes.class);
                kSession.insert(basTvfMes);
            }
            if(entry.toString().contains("隧道风机")){
                BasTefMes basTefMes = JSONObject.parseObject(entry.toString(), BasTefMes.class);
                kSession.insert(basTefMes);
            }
            System.out.println("命中数目" + kSession.fireAllRules());
            for (Object obj : kSession.getObjects()) {
                result.add(JSONObject.parseObject(JSONObject.toJSONString(obj)));
            }
        } catch (Exception ex) {
            logger.error("处理规则失败, 异常原因:{}", ex);
        } finally {
            kSession.dispose();
        }
        return result;
    }

    /**
     * 加载规则
     */
    @Override
    public boolean loadRules(RuleBase rb) {
        KieSessionHelper ksHelper = new KieSessionHelper(rulePackage);
        //解析采集到规则体 compileRules  判断返回结果  不为空则是解析规则失败
        if (!ksHelper.compileRules(rb).isEmpty()) {
            logger.error("加载规则失败, hash值:{}", rb.getHash());
            return false;
        }
        //规则对象中是否为空
        isEmptyKie = rb.getDroolsRules().isEmpty();
        kieBase = ksHelper.createKieBase();
        return true;
    }
}
