package jtznyj.engineer.service.dataDiagnosticService;

import cn.hutool.core.date.DateUtil;
import jtznyj.engineer.service.dataDiagnosticService.data.AlarmPoint;
import jtznyj.engineer.service.dataDiagnosticService.data.DrillState;
import lombok.extern.slf4j.Slf4j;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.Hashtable;

/**
 * 单井趋势--波动趋势诊断
 */
@Slf4j
public class WellFluctuantTrendDataDiagnostic extends WellTrendDataDiagnostic {
    /**
     * 单一模型诊断分析
     * @param record
     * @param currentPumpState
     * @param drillState
     * @param lastPumpRunTime
     * @param lastPumpStopTime
     * @return
     */
    @Override
    protected DataDiagnosticAnalyzeResult internalAnalyse(Hashtable record, boolean currentPumpState, DrillState drillState, Date lastPumpRunTime, Date lastPumpStopTime) {
        DataDiagnosticAnalyzeResult analyseResult = this.beforeAnalyse(record,currentPumpState,drillState,lastPumpRunTime,lastPumpStopTime);
        if(!analyseResult.result){
            //提前退出--不报警
            analyseResult.result = false;
            return analyseResult;
        }

        analyseResult.result = false;
        int iCount = this.trendDataList.size();
        if(iCount<2){
            return analyseResult;
        }

        boolean bInRange = true;
        double cmpValue = 0;
        TimeValuePair<Hashtable> tvFirst = this.trendDataList.get(0);
        for (int j = 0; j < iCount; j++) {
            cmpValue = this.trendDataList.get(j).getValue() - tvFirst.getValue();
            if (Math.abs(cmpValue) > this.param._bdLtValue) {
                bInRange = false;
                break;
            }
        }
        TimeValuePair<Hashtable> tvStep;
        if(bInRange){
            tvStep = this.trendDataList.get(iCount-1);
            this._secondState.BdRange(tvFirst.getDateTime(), tvStep.getDateTime(),this.param._bdLtValue);
            AlarmPoint description = new AlarmPoint();
            description.name = this.param._name;
            description.field = this.param._realField;
            description.alarmType = this.param._analyseType.toString();
            //description._dtStart = tvFirst.getDateTime();
            description.alarmDate = new Date(tvStep.getDateTime().getTime());
            //description._startValue = tvFirst.getValue();
            description.value = tvStep.getValue();
            //description._startDepth = getWellDepth(tvStep.getData());
            //description._startZtDepth = getDrillDepth(tvStep.getData());
            description.depth = getWellDepth(tvStep.getData());
            description.ztDepth = getDrillDepth(tvStep.getData());
            description.des = this.setDes("",description,cmpValue);
            analyseResult.description = description;
            analyseResult.result = true;
            description.WarningCause = setWarningCause(description,cmpValue);
            this._secondState.SetAlarm(true);
            //description._secondState = (SecondState) this._secondState.Clone();
            //analyseResult.percent = percent;
            log.debug(" {}, 数据趋势fluctuantTrend,位置{},波动起始值{}",
                    DataDiagnosticEngineer.packStackStr(Thread.currentThread().getStackTrace()),
                    trendDataList.size(), cmpValue);
        }
        if (!bInRange) {
            this.reset();
        }

        analyseResult.result = bInRange;
        //analyseResult.percent = percent;
        return analyseResult;
    }


    private String setDes(String jh,AlarmPoint description,double cmpValue){

        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
        DecimalFormat valueDf = new DecimalFormat("0.00");

        String des = "";
        des += "["+this.jh+" " +description.name+"]";
        des += "\n";
        des += "时间: " + DateUtil.format(description.alarmDate,"yyyy-MM-dd HH:mm:ss");
        des += "\n";
        des += "深度段: " + description.depth;
        des += "\n";
        des += "异常原因: "+ DataDiagnosticEngineer.getRealFieldName(description.field)
                + " 超出波动范围(小于"+ decimalFormat.format(this.param._bdLtValue)
                + this.param._unit + "),当前波动"
                + valueDf.format(cmpValue) + this.param._unit ;
        return des;
    }

    private String setWarningCause(AlarmPoint description,double cmpValue){
        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
        DecimalFormat valueDf = new DecimalFormat("0.00");

        return "异常原因: "+ DataDiagnosticEngineer.getRealFieldName(description.field) + " 超出波动范围(小于"
                + decimalFormat.format(this.param._bdLtValue) + this.param._unit +"),当前值："
                + decimalFormat.format(description.value)+ this.param._unit ;
    }


}
