package com.iailab.module.model.sample.constructor; import com.iailab.module.data.api.IFeignDataApi; import com.iailab.module.data.dto.ApiDataPointDTO; import com.iailab.module.data.api.IFeignDataApi; import com.iailab.module.mcs.dto.StModelParamDTO; import com.iailab.module.mcs.service.StModelParamService; import com.iailab.module.model.sample.dto.ColumnItem; import com.iailab.module.model.sample.dto.ColumnItemPort; import com.iailab.module.model.sample.dto.SampleInfo; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.*; /** * @author PanZhibao * @Description * @createTime 2023年05月16日 16:25:00 */ @Component public class SampleInfoConstructor { @Resource private StModelParamService stModelParamService; @Resource private IFeignDataApi feignDataApi; public static final Map minFreqMap = new HashMap<>(); private int DAY_SECOND = 1000 * 60 * 60 * 24; SampleInfoConstructor() { minFreqMap.put("NET10", 10); minFreqMap.put("NET30", 30); minFreqMap.put("NET60", 60); minFreqMap.put("NET_KIO10", 10); minFreqMap.put("NET_KIO60", 60); } public SampleInfo prepareSampleInfo(String modelId, Date predictTime) throws Exception { SampleInfo sampleInfo = new SampleInfo(); sampleInfo.setSampleColumn(getSampleColumn(modelId)); sampleInfo.setColumnInfo(getColumnInfo(modelId, predictTime)); return sampleInfo; } private Integer getSampleColumn(String modelId) { return stModelParamService.getByModelId(modelId).size(); } private List getColumnInfo(String modelId, Date dateTime) throws Exception { List resultList = new ArrayList<>(); List columnItemList = new ArrayList<>(); ColumnItem columnInfo = new ColumnItem(); ColumnItemPort curPort = new ColumnItemPort(); //当前端口 List modelParamList = stModelParamService.getByModelId(modelId); if (CollectionUtils.isEmpty(modelParamList)) { return null; } int curPortOrder = modelParamList.get(0).getParamPortOrder(); int curDataLength = modelParamList.get(0).getDataLength(); for (StModelParamDTO entry : modelParamList) { columnInfo.setParamType(entry.getParamType()); columnInfo.setParamId(entry.getParamId()); columnInfo.setDataLength(entry.getDataLength()); columnInfo.setModelParamOrder(entry.getParamOrder()); columnInfo.setModelParamPortOrder(entry.getParamPortOrder()); columnInfo.setStartTime(this.getStartTime(columnInfo, dateTime)); columnInfo.setEndTime(this.getEndTime(columnInfo, dateTime)); if (curPortOrder != entry.getParamPortOrder()) { //当数据项端口号不为当前端口号时,封装上一个端口类,操作下一个端口类 curPort.setColumnItemList(columnItemList); curPort.setDataLength(curDataLength); curPort.setPortOrder(curPortOrder); resultList.add(curPort); curPort = new ColumnItemPort(); //封装上一个端口类后更新当前的各个参数 columnItemList = new ArrayList<>(); curDataLength = entry.getDataLength(); curPortOrder = entry.getParamPortOrder(); } columnItemList.add(columnInfo); columnInfo = new ColumnItem(); } //当迭代到最后一个项的时候,封装最后一个端口的信息 curPort.setColumnItemList(columnItemList); curPort.setDataLength(curDataLength); curPort.setPortOrder(curPortOrder); resultList.add(curPort); return resultList; } protected Date getStartTime(ColumnItem columnItem, Date originalTime) throws Exception { Date dateTime = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(originalTime); switch (columnItem.getParamType()) { case "point": ApiDataPointDTO dataPoint = feignDataApi.getPoint(columnItem.paramId); if (dataPoint == null) { throw new Exception("apiDataPointDTO is null"); } dateTime = calculateTime(dateTime, true, columnItem.getDataLength(), minFreqMap.get(dataPoint.getMinfreqid())); break; case "predict": dateTime = calendar.getTime(); break; case "ind": dateTime = calculateTime(dateTime, true, columnItem.getDataLength(), DAY_SECOND); break; case "device-main": dateTime = calculateTime(dateTime, true, columnItem.getDataLength(), DAY_SECOND); break; case "event": dateTime = calculateTime(dateTime, true, columnItem.getDataLength(), DAY_SECOND); break; default: break; } return dateTime; } protected Date getEndTime(ColumnItem columnItem, Date originalTime) throws Exception { Date dateTime = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(originalTime); switch (columnItem.getParamType()) { case "point": ApiDataPointDTO dataPoint = feignDataApi.getPoint(columnItem.paramId); if (dataPoint == null) { throw new Exception("apiDataPointDTO is null"); } calendar.add(Calendar.SECOND, minFreqMap.get(dataPoint.getMinfreqid()) * -1); break; case "predict": dateTime = calculateTime(dateTime, false, columnItem.getDataLength(), 60); break; case "ind": dateTime = calendar.getTime(); break; case "device-main": dateTime = calendar.getTime(); break; case "event": dateTime = calendar.getTime(); break; default: break; } return dateTime; } 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(); } }