package jtznyj.engineer.service.impl;

import com.alibaba.fastjson.JSONObject;
import jtznyj.common.api.ICommonApi;
import jtznyj.engineer.entity.BWellOwnEngineer;
import jtznyj.engineer.service.IAFoucusWarnSectionService;
import jtznyj.engineer.service.IWellDataDiagnosticService;
import jtznyj.engineer.service.dataDiagnosticService.DataDiagnosticEngineer;
import jtznyj.engineer.service.dataDiagnosticService.data.MonitorAlarmRanges;
import jtznyj.engineer.service.winform.CheckEngineerWarning;
import jtznyj.engineer.service.winform.data.MonitorAlarmRecords;
import jtznyj.engineer.service.winform.data.RecordHashTable;
import jtznyj.engineer.vo.LtdRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WellDataDiagnosticServiceImpl implements IWellDataDiagnosticService {
    @Autowired
    ICommonApi iCommonApi;
    @Autowired
    IAFoucusWarnSectionService iaFoucusWarnSectionService;

    /**
     * 检测井规定时间区间内是否报警
     *
     * @param jh               井号
     * @param startTime        开始时间
     * @param durationInMinute 检测数据样本持续时长（分钟）
     * @param testAllData      是否检测所有样本数据
     * @return
     */
    @Override
    public Map<String, MonitorAlarmRanges> test(String jh, Date startTime, int durationInMinute, boolean testAllData) {
        log.info(String.format("%s 开始调用 jh:%s time:%s duration:%s", this.getClass().getName(), jh, startTime, durationInMinute));
        Date endTime = startTime;
        if (null == startTime) {
            endTime = new Date();
        }

        List<Map<String, Object>> list = iCommonApi.queryWellEngineerLogic(jh);
        //预警配置参数
        BWellOwnEngineer template = new BWellOwnEngineer();
        if (list.size() == 0) {
            log.info(String.format("%s没有定制模板,使用通用模板", jh));
            //如果不存在该井的配置参数就去默认参数配置
            list = iCommonApi.queryDefaultEngineerLogic();
        }
        if (list.size() == 0) {
            return null;
        }
        //从NClob获得配置参数信息
        String clob = IWellDataDiagnosticService.getNClob(list.get(0).get("LARGETEXT"));
        template.setLargeText(clob);

        //初始化预警引擎需要的参数
        DataDiagnosticEngineer engineer = new DataDiagnosticEngineer(jh);
        engineer.parseTemplate(template);
        log.info(String.format("已获取到算法模板  "));
        //监控预警记录--检查出的异常记录集
        Map<String, MonitorAlarmRanges> monitorAlarmRangeMap = new HashMap<>();

        //提取需要诊断的数据--按照时间顺序排列提取
        List<LtdRecord> ltdList = iCommonApi.queryLtdListInPeriodDesc(jh, endTime, -durationInMinute);
        List<RecordHashTable> dataList = ltdList.stream().map(x -> CheckEngineerWarning.LtdToRecordHashTable(x)).collect(Collectors.toList());
        ltdList.clear();
        //诊断分析
        engineer.analyseDataList(dataList, monitorAlarmRangeMap, testAllData);

        log.info(String.format("已完成计算调用  "));
        return monitorAlarmRangeMap;
    }

    @Override
    public void testAndSave(String jh, Date startTime, int durationInMinute, boolean testAllData) {
        Map<String, MonitorAlarmRanges> result = test(jh, startTime, durationInMinute, testAllData);
        iaFoucusWarnSectionService.savaWarnMessage(jh, result);
    }

    @Deprecated
    @Override
    public MonitorAlarmRecords get30MinutesWarnResults(String jh, Date endTime) {
//        List<Map<String, Object>> list = iCommonApi.queryWellEngineerLogic(jh);
//        BWellOwnEngineer template = new BWellOwnEngineer();
//        if (list.size() == 0) {
//            //如果不存在该井的配置参数就去默认参数配置
//            list = iCommonApi.queryDefaultEngineerLogic();
//        }
//        if (list.size() == 0) {
//            return null;
//        }
//        template.setLargeText( IWellDataDiagnosticService.GetNClobWithColorChange(list.get(0).get("LARGETEXT")));
//
//        Map<String, MonitorAlarmRanges> monitorAlarmRangeMap = new HashMap<>();
//
//
//        CheckEngineerWarning checkLogic = new CheckEngineerWarning(template);
        MonitorAlarmRecords monitorAlarmRecords = new MonitorAlarmRecords();
//        DrillState ds = DrillState.None;
//        List<LtdRecord> ltdList = iCommonApi.queryLtdListInPeriod(jh, endTime, -30);
//        List<RecordHashTable> dataList = ltdList.stream()
//                .map(x -> CheckEngineerWarning.LtdToRecordHashTable(x))
//                .collect(Collectors.toList());
//        ltdList.clear();
//        //诊断分析
//        checkLogic.AnalyseDataList(dataList, monitorAlarmRecords);
//        for (int i = 0; i < monitorAlarmRecords.size(); i++) {
//            MonitorAlarmRecord r = monitorAlarmRecords.get(i);
//            r.SetAlarmMsg(i + 1, jh);
//        }

        return monitorAlarmRecords;
    }

}
