package jtznyj.engineer.service;

import jtznyj.engineer.api.IEngineerApi;
import jtznyj.engineer.dto.WarnItemDto;
import jtznyj.engineer.service.dataDiagnosticService.data.MonitorAlarmRange;
import jtznyj.engineer.service.dataDiagnosticService.data.MonitorAlarmRanges;
import jtznyj.engineer.service.winform.data.MonitorAlarmRecords;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EngineerExternalApi implements IEngineerApi {
    @Autowired
    IWellDataDiagnosticService iWellDataDiagnosticService;

    @Override
    public void testAndSaveWarnResult(String jh, Date startTime, int durationInMinute, boolean testAllData) {
        iWellDataDiagnosticService.testAndSave(jh, startTime, durationInMinute, testAllData);
    }

    @Override
    public List<WarnItemDto> getHalfHourWarnResults(String jh, Date endTime) {
        List<WarnItemDto> gcList = new ArrayList<>();
        Map<String, MonitorAlarmRanges> warn = iWellDataDiagnosticService.test(jh, endTime, 30, true);
        for (String category : warn.keySet()) {
            //获取预警区间
            ArrayList<MonitorAlarmRange> rangeArrayList = warn.get(category).children;
            if (rangeArrayList == null || rangeArrayList.size() == 0) {
                continue;
            }
            for (MonitorAlarmRange range : rangeArrayList) {
                WarnItemDto record = new WarnItemDto();
                record.source = "工程预警";
                record.jh = jh;
                record.catogary = category;
                record.warnTime = range.endDate;
                record.jcrq = null;
                record.bjnr = StringUtils.isEmpty(range.description) ? "" : range.description.replace("<br/>", "\r\n");
                record.clfs = "";
                record.dsjs = Double.toString(range.endDepth);
                record.key = range.endZtDepth + category + range.endDepth;
                gcList.add(record);
            }

        }
        return gcList;
    }
}
