package com.iailab.module.model.mdk.sample; import com.iailab.module.data.api.plan.PlanItemApi; import com.iailab.module.data.api.plan.dto.ApiPlanItemDTO; import com.iailab.module.data.api.point.DataPointApi; import com.iailab.module.data.api.point.dto.ApiPointDTO; import com.iailab.module.data.enums.DataPointFreqEnum; import com.iailab.module.data.enums.TimeGranularitySecEnum; import com.iailab.module.model.mcs.pre.enums.PredGranularityEnum; import com.iailab.module.model.mdk.common.enums.ModelParamType; import com.iailab.module.model.mdk.sample.dto.SampleData; import com.iailab.module.model.mdk.sample.dto.SampleInfo; import com.iailab.module.model.mdk.vo.DataValueVO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import java.util.*; abstract class SampleDataConstructor { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private DataPointApi dataPointApi; @Autowired private PlanItemApi planItemApi; /** * prepareSampleData * * @param sampleInfo * @return */ public abstract List prepareSampleData(SampleInfo sampleInfo) throws Exception; /** * 补全数据 * * @param length * @param dataEntityList * @param startTime * @param endTime * @param planMap * @return */ public List completionData(int length, List dataEntityList, Date startTime, Date endTime, String paramId, String paramType, Map pointMap, Map planMap) { if (CollectionUtils.isEmpty(dataEntityList) || length == dataEntityList.size()) { return dataEntityList; } else if (length < dataEntityList.size()) { return dataEntityList.subList(dataEntityList.size() - length, dataEntityList.size()); } List completionDataEntityList = new ArrayList<>(); long oneMin = 0L; long start = startTime.getTime(); long end = endTime.getTime(); long mins = 0L; switch (ModelParamType.getEumByCode(paramType)) { case NORMALITEM: case MERGEITEM: // 预测值 oneMin = PredGranularityEnum.MIN1.getCode() * 1000L; start = start - (start % oneMin) + oneMin; end = end - (end % oneMin) + oneMin; mins = ((end - start) / oneMin); break; case DATAPOINT: // 测点值 ApiPointDTO dataPoint = pointMap.get(paramId); oneMin = 1000L * DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue(); // 设置时间偏移量 start = start - (start % oneMin) + oneMin; end = end - (end % oneMin) + oneMin; mins = ((end - start) / oneMin); break; case IND: // 指标数据 oneMin = 24 * 60 * 60 * 1000; Calendar calendar2 = Calendar.getInstance(); calendar2.setTime(startTime); calendar2.set(Calendar.HOUR_OF_DAY, 0); calendar2.set(Calendar.MINUTE, 0); calendar2.set(Calendar.SECOND, 0); start = calendar2.getTime().getTime(); calendar2.setTime(endTime); calendar2.set(Calendar.HOUR_OF_DAY, 0); calendar2.set(Calendar.MINUTE, 0); calendar2.set(Calendar.SECOND, 0); end = calendar2.getTime().getTime(); mins = ((end - start) / oneMin); break; case PLAN: // 计划数据 ApiPlanItemDTO planItem = planMap.get(paramId); oneMin = 1000L * TimeGranularitySecEnum.getEumByCode(planItem.getTimeGranularity()).getValue(); // 设置时间偏移量 start = start - (start % oneMin) + oneMin; end = end - (end % oneMin) + oneMin; mins = ((end - start) / oneMin); break; default: break; } Map sourceDataMap = new HashMap<>(dataEntityList.size()); for (DataValueVO dataEntity : dataEntityList) { sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue()); } //找出缺少项 Map dataMap = new LinkedHashMap<>(); for (int i = 0; i < mins; i++) { Long key = start + oneMin * i; Double value = sourceDataMap.get(key); dataMap.put(key, value); } //补充缺少项 int k = 0; Map.Entry lastItem = null; for (Map.Entry item : dataMap.entrySet()) { if (k == 0 && item.getValue() == null) { item.setValue(getFirstValue(dataMap)); } else if (item.getValue() == null) { item.setValue(lastItem.getValue()); } k++; lastItem = item; DataValueVO dataEntity = new DataValueVO(); dataEntity.setDataTime(new Date(item.getKey())); dataEntity.setDataValue(item.getValue()); completionDataEntityList.add(dataEntity); } return completionDataEntityList; } /** * getFirstValue * * @param dataMap * @return */ private Double getFirstValue(Map dataMap) { for (Map.Entry item : dataMap.entrySet()) { if (item.getValue() != null) { return item.getValue(); } } return null; } }