package org.dromara.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.dromara.domain.TblBreakdown;
import org.dromara.domain.bo.TblBreakdownBo;
import org.dromara.domain.bo.TblWarnBo;
import org.dromara.service.ITblBreakdownService;
import org.dromara.service.ITblSensorService;
import org.dromara.service.ITblWarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RuleProcess {
    @Autowired
    private ITblSensorService sensorService;

    @Autowired
    private ITblWarnService tblWarnService;
    @Autowired
    private ITblBreakdownService tblBreakdownService;

    public void insertAlarm(String content, Exchange exchange){
//        System.out.println(exchange.getMessage().getBody().toString());
//        System.out.println("------alarm------");
        if(exchange.getMessage().getBody()!=null) {


            String pointDataJSon = exchange.getMessage().getBody().toString();
            if (!StrUtil.isEmptyIfStr(pointDataJSon)) {
                try {
                    JSONObject object = JSONUtil.parseObj(pointDataJSon);
                    JSONObject objectp = object.getJSONObject("params");
                    String sensorName = objectp.getStr("sensorName");
                    TblWarnBo bo = new TblWarnBo();
                    bo.setSensorId(object.getLong("sensorId"));
                    bo.setSensorName(sensorName);
                    bo.setContent(content);
                    bo.setAlarmTime(DateUtil.date(object.getLong("createTime")));
                    bo.setVal(object.getStr("pointValue"));
                    bo.setName(objectp.getStr("pointLabel"));
                    tblWarnService.insertByBo(bo);
                } catch (Exception e) {
                }
            }
        }
    }

    public void insertFattle(String content, Exchange exchange){
        if(exchange.getMessage().getBody()!=null) {


            String pointDataJSon = exchange.getMessage().getBody().toString();
            if (!StrUtil.isEmptyIfStr(pointDataJSon)) {
                try {
                    JSONObject object = JSONUtil.parseObj(pointDataJSon);
                    JSONObject objectp = object.getJSONObject("params");
                    String sensorName = objectp.getStr("sensorName");
                    TblBreakdownBo bo = new TblBreakdownBo();
                    bo.setSensorId(object.getLong("sensorId"));
                    bo.setSensorName(sensorName);
                    bo.setContent(content);
                    bo.setFaultTime(DateUtil.date(object.getLong("createTime")));
                    bo.setVal(object.getStr("pointValue"));
                    bo.setName(objectp.getStr("pointLabel"));
                    tblBreakdownService.insertByBo(bo);
                } catch (Exception e) {
                }
            }
        }
    }

    public Object yz(String pointName,String fh,String value, Exchange exchange){

        try{
            String  inputdata = exchange.getMessage().getBody().toString();
            JSONObject datapoint = JSONUtil.parseObj(inputdata);
            if(datapoint.getObj("pointName").equals(pointName)) {

                String pointvalue = datapoint.getObj("pointValue").toString();
                switch (fh) {
                    case "大于": {
                        if (Double.parseDouble(pointvalue) > Double.parseDouble(value)) {
                            return inputdata;
                        }
                        break;
                    }
                    case "小于": {
                        if (Double.parseDouble(pointvalue) < Double.parseDouble(value)) {
                            return inputdata;
                        }
                        break;
                    }
                    case "等于": {
                        if (Double.parseDouble(pointvalue) == Double.parseDouble(value)) {
                            return inputdata;
                        }
                        break;
                    }
                    case "大于等于": {
                        if (Double.parseDouble(pointvalue) >= Double.parseDouble(value)) {
                            return inputdata;
                        }
                        break;
                    }
                    case "小于等于": {
                        if (Double.parseDouble(pointvalue) <= Double.parseDouble(value)) {
                            return inputdata;
                        }
                        break;
                    }
                    case "不等于": {
                        if (Double.parseDouble(pointvalue) != Double.parseDouble(value)) {
                            return inputdata;
                        }
                        break;
                    }
                }
            }
        }catch (Exception e){

        }
        exchange.getMessage().setBody("");
        return "";
    }


    public Object control(String eventName, Exchange exchange){

        try{
            String  inputdata = exchange.getMessage().getBody().toString();
            JSONObject datapoint = JSONUtil.parseObj(inputdata);
            //TODO 发送指令
            System.out.println(eventName);
            String[] c= eventName.split("__");

            System.out.println(c[0]);
            System.out.println(c[1]);

        }catch (Exception e){

        }
        exchange.getMessage().setBody("");
        return "";
    }
}
