package com.iailab.module.model.mdk.sample;
|
|
import com.iailab.module.data.api.point.DataPointApi;
|
import com.iailab.module.data.api.point.dto.ApiPointDTO;
|
import com.iailab.module.data.enums.DataPointFreq;
|
import com.iailab.module.model.mdk.factory.ItemEntityFactory;
|
import com.iailab.module.model.mdk.sample.dto.ColumnItem;
|
import com.iailab.module.model.mdk.sample.dto.ColumnItemPort;
|
import com.iailab.module.model.mdk.sample.dto.SampleInfo;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.sql.Timestamp;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* @author PanZhibao
|
* @Description
|
* @createTime 2024年09月03日
|
*/
|
abstract class SampleInfoConstructor {
|
|
@Autowired
|
private DataPointApi dataPointApi;
|
|
@Autowired
|
private ItemEntityFactory itemEntityFactory;
|
|
/**
|
* prepareSampleInfo
|
*
|
* @param modelId
|
* @param predictTime
|
* @return
|
*/
|
public SampleInfo prepareSampleInfo(String modelId, Date predictTime) {
|
SampleInfo sampleInfo = new SampleInfo();
|
//调用样本列数的方法
|
sampleInfo.setSampleColumn(getSampleColumn(modelId));
|
//样本的列信息
|
sampleInfo.setColumnInfo(getColumnInfo(modelId, predictTime));
|
//样本的采样周期
|
sampleInfo.setSampleCycle(getSampleCycle(modelId));
|
return sampleInfo;
|
}
|
|
/**
|
* 返回样本矩阵的列数
|
*
|
* @param modelId
|
* @return
|
*/
|
protected abstract Integer getSampleColumn(String modelId);
|
|
/**
|
* 获取开始时间
|
*
|
* @param columnItem
|
* @param originalTime
|
* @return
|
* @throws Exception
|
*/
|
protected Date getStartTime(ColumnItem columnItem, Date originalTime) {
|
Date dateTime = new Date();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(originalTime);
|
switch (columnItem.getParamType()) {
|
case "DATAPOINT":
|
ApiPointDTO dataPoint = dataPointApi.getPointById(columnItem.getParamId());
|
if (dataPoint == null) {
|
return null;
|
}
|
dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), DataPointFreq.getEumByCode(dataPoint.getMinfreqid()).getValue());
|
break;
|
case "PREDICTITEM":
|
dateTime = calendar.getTime();
|
break;
|
case "IND-HIS":
|
dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), 60);
|
break;
|
case "IND-PLAN":
|
dateTime = calendar.getTime();
|
break;
|
default:
|
break;
|
}
|
return dateTime;
|
}
|
|
/**
|
* 获取结束时间
|
*
|
* @param columnItem
|
* @param originalTime
|
* @return
|
* @throws Exception
|
*/
|
protected Date getEndTime(ColumnItem columnItem, Date originalTime) {
|
Date dateTime = new Date();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(originalTime);
|
switch (columnItem.getParamType()) {
|
case "DATAPOINT":
|
ApiPointDTO dataPoint = dataPointApi.getPointById(columnItem.getParamId());
|
if (dataPoint == null) {
|
return null;
|
}
|
calendar.add(Calendar.SECOND, DataPointFreq.getEumByCode(dataPoint.getMinfreqid()).getValue() * -1);
|
break;
|
case "PREDICTITEM":
|
dateTime = calculateTime(originalTime, false, columnItem.getDataLength(), 60);
|
break;
|
case "IND-HIS":
|
dateTime = calendar.getTime();
|
break;
|
case "IND-PLAN":
|
dateTime = calculateTime(originalTime, false, columnItem.getDataLength(), 60);
|
break;
|
default:
|
break;
|
}
|
return dateTime;
|
}
|
|
/**
|
* 获取粒度,s
|
*
|
* @param columnItem
|
* @return
|
* @throws Exception
|
*/
|
protected Integer getGranularity(ColumnItem columnItem) {
|
Integer granularity = 60;
|
switch (columnItem.getParamType()) {
|
case "DATAPOINT":
|
ApiPointDTO dataPoint = dataPointApi.getPointById(columnItem.getParamId());
|
granularity = DataPointFreq.getEumByCode(dataPoint.getMinfreqid()).getValue();
|
break;
|
case "PREDICTITEM":
|
granularity = itemEntityFactory.getItemById(columnItem.getParamId()).getGranularity();
|
break;
|
case "IND-HIS":
|
granularity = 60;
|
break;
|
case "IND-PLAN":
|
granularity = 60;
|
break;
|
default:
|
break;
|
}
|
return granularity;
|
}
|
|
/**
|
* 样本的列信息
|
*
|
* @param modelId
|
* @param predictTime
|
* @return
|
*/
|
protected abstract List<ColumnItemPort> getColumnInfo(String modelId, Date predictTime);
|
|
/**
|
* 样本的采样周期
|
*
|
* @param modelId
|
* @return
|
*/
|
protected abstract Integer getSampleCycle(String modelId);
|
|
/**
|
* 计算取值的时间
|
*
|
* @param originalTime
|
* @param backward
|
* @param dataLength
|
* @param granularity
|
* @return
|
*/
|
public Date calculateTime(Date originalTime, Boolean backward, int dataLength, int granularity) {
|
int timeLength;
|
if (backward) {
|
timeLength = (-1) * dataLength;
|
} else {
|
timeLength = dataLength - 1;
|
}
|
Date desTime = originalTime;
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(desTime);
|
calendar.set(Calendar.MILLISECOND, 0);
|
// 数据长度 * 粒度
|
calendar.add(Calendar.SECOND, timeLength * granularity);
|
return calendar.getTime();
|
}
|
}
|