| | |
| | | package com.iailab.module.model.mcs.sche.service.impl; |
| | | |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.date.format.DateParser; |
| | | import com.baomidou.dynamic.datasource.annotation.DSTransactional; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.iailab.framework.common.service.impl.BaseServiceImpl; |
| | | import com.iailab.framework.common.util.object.BeanUtils; |
| | | import com.iailab.framework.common.util.object.ConvertUtils; |
| | | import com.iailab.module.data.api.ind.IndItemApi; |
| | | import com.iailab.module.data.api.ind.dto.ApiIndItemQueryDTO; |
| | | import com.iailab.module.data.api.ind.dto.ApiIndItemValueDTO; |
| | | import com.iailab.module.data.api.plan.PlanItemApi; |
| | | import com.iailab.module.data.api.point.DataPointApi; |
| | | import com.iailab.module.data.api.point.dto.ApiPointValueDTO; |
| | | import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; |
| | | import com.iailab.module.data.common.ApiDataQueryDTO; |
| | | import com.iailab.module.data.common.ApiDataValueDTO; |
| | | import com.iailab.module.model.api.mcs.dto.PreDataBarLineReqVO; |
| | | import com.iailab.module.model.api.mcs.dto.PreDataBarLineRespVO; |
| | | import com.iailab.module.model.api.mcs.dto.StSuggestSnapshotRecordRespVO; |
| | | import com.iailab.module.model.common.enums.DataCategoryEnum; |
| | | import com.iailab.module.model.common.utils.DateUtils; |
| | | import com.iailab.module.model.mcs.pre.service.MmItemResultService; |
| | | import com.iailab.module.model.mcs.sche.dao.StSuggestSnapshotRecordDao; |
| | | import com.iailab.module.model.mcs.sche.entity.StSuggestSnapshotRecordEntity; |
| | | import com.iailab.module.model.mcs.sche.service.StSuggestSnapshotConfDetService; |
| | | import com.iailab.module.model.mcs.sche.service.StSuggestSnapshotConfMainService; |
| | | import com.iailab.module.model.mcs.sche.service.StSuggestSnapshotRecordService; |
| | | import com.iailab.module.model.mcs.sche.vo.StSuggestSnapshotRecordRespVO; |
| | | import com.iailab.module.model.mcs.sche.vo.StSuggestSnapshotConfDetRespVO; |
| | | import com.iailab.module.model.mcs.sche.vo.StSuggestSnapshotConfMainRespVO; |
| | | import com.iailab.module.model.mcs.sche.vo.StSuggestSnapshotRecordSaveReqVO; |
| | | import org.apache.commons.lang.time.DateFormatUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneId; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | |
| | | import static com.iailab.module.model.common.utils.DateUtils.DATE_TIME_PATTERN; |
| | | |
| | | |
| | | /** |
| | |
| | | @Service |
| | | public class StSuggestSnapshotRecordServiceImpl extends BaseServiceImpl<StSuggestSnapshotRecordDao, StSuggestSnapshotRecordEntity> |
| | | implements StSuggestSnapshotRecordService { |
| | | @Autowired |
| | | private DataPointApi dataPointApi; |
| | | |
| | | @Autowired |
| | | private IndItemApi indItemApi; |
| | | |
| | | @Autowired |
| | | private PlanItemApi planItemApi; |
| | | |
| | | @Autowired |
| | | private MmItemResultService mmItemResultService; |
| | | |
| | | |
| | | |
| | | @Autowired |
| | | private StSuggestSnapshotRecordService StSuggestSnapshotRecordService; |
| | | private StSuggestSnapshotConfMainService stSuggestSnapshotConfMainService; |
| | | |
| | | @Autowired |
| | | private StSuggestSnapshotConfDetService stSuggestSnapshotConfDetService; |
| | | |
| | | @Override |
| | | @DSTransactional(rollbackFor = Exception.class) |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<StSuggestSnapshotRecordRespVO> getListByOperationId(String operationId) { |
| | | return ConvertUtils.sourceToTarget(baseDao.selectList("operation_id", operationId), StSuggestSnapshotRecordRespVO.class); |
| | | public List<StSuggestSnapshotRecordRespVO> getListBySuggestId(String suggestId) { |
| | | QueryWrapper<StSuggestSnapshotRecordEntity> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("suggest_id", suggestId); |
| | | queryWrapper.orderByAsc("sort"); |
| | | return ConvertUtils.sourceToTarget(baseDao.selectList(queryWrapper), StSuggestSnapshotRecordRespVO.class); |
| | | } |
| | | |
| | | @Override |
| | | public void createSnapshotRecord(String modelId, String scheduleObj,Date scheduleTime, String suggestId) { |
| | | if (StringUtils.isBlank(modelId) || StringUtils.isBlank(scheduleObj) || StringUtils.isBlank(suggestId)) { |
| | | log.error("生成快照失败,参数错误!modelId:" + modelId + ",scheduleObj:" + scheduleObj + ",suggestId:" + suggestId); |
| | | return; |
| | | } |
| | | //获取调度建议快照配置表信息 |
| | | StSuggestSnapshotConfMainRespVO confMain = stSuggestSnapshotConfMainService.getByModelIdAndScheduleObj(modelId, scheduleObj); |
| | | if (confMain == null) { |
| | | log.error("生成快照失败,confMain is null!modelId:" + modelId + ",scheduleObj:" + scheduleObj); |
| | | return; |
| | | } |
| | | List<StSuggestSnapshotConfDetRespVO> confDetList = stSuggestSnapshotConfDetService.getByConfId(confMain.getId()); |
| | | if (CollectionUtils.isEmpty(confDetList)) { |
| | | log.error("生成快照失败,confDetList is empty!modelId:" + modelId + ",scheduleObj:" + scheduleObj); |
| | | return; |
| | | } |
| | | confDetList.forEach(confDet -> { |
| | | StSuggestSnapshotRecordSaveReqVO snapshotRecordSaveReqVO = new StSuggestSnapshotRecordSaveReqVO(); |
| | | snapshotRecordSaveReqVO.setSuggestId(suggestId); |
| | | snapshotRecordSaveReqVO.setModelId(modelId); |
| | | snapshotRecordSaveReqVO.setDataNo(confDet.getDataNo()); |
| | | snapshotRecordSaveReqVO.setDataType(confDet.getDataType()); |
| | | snapshotRecordSaveReqVO.setDataName(confDet.getDataName()); |
| | | snapshotRecordSaveReqVO.setScheduleTime(scheduleTime); |
| | | snapshotRecordSaveReqVO.setSort(confDet.getSort()); |
| | | snapshotRecordSaveReqVO.setLimitH(confDet.getLimitH()); |
| | | snapshotRecordSaveReqVO.setLimitL(confDet.getLimitL()); |
| | | snapshotRecordSaveReqVO.setSort(confDet.getSort()); |
| | | //计算开始时间,开始时间为当前调度时间减去快照配置表配置的左侧时间长度 |
| | | LocalDateTime localStartDateTime = scheduleTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().minusMinutes(confDet.getLeftLength() == null ? 60 : confDet.getLeftLength()); |
| | | Date startTime = Date.from(localStartDateTime.atZone(ZoneId.systemDefault()).toInstant()); |
| | | snapshotRecordSaveReqVO.setStartTime(startTime); |
| | | //计算结束时间,结束时间为当前调度时间加上快照配置表配置的右侧时间长度 |
| | | LocalDateTime localEndDateTime = scheduleTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusMinutes(confDet.getRightLength() == null ? 60 : confDet.getLeftLength()); |
| | | Date endTime = Date.from(localEndDateTime.atZone(ZoneId.systemDefault()).toInstant()); |
| | | snapshotRecordSaveReqVO.setEndTime(endTime); |
| | | this.create(snapshotRecordSaveReqVO); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public List<StSuggestSnapshotRecordRespVO> getChartData(List<StSuggestSnapshotRecordRespVO> reqList) { |
| | | reqList.forEach(item -> { |
| | | List<Object[]> dataList = new ArrayList<>(); |
| | | if(DataCategoryEnum.DATAPOINT.getCode().equals(item.getDataType())){ |
| | | ApiPointValueQueryDTO queryDTO = new ApiPointValueQueryDTO(); |
| | | queryDTO.setPointNo(item.getDataNo()); |
| | | queryDTO.setStart(item.getStartTime()); |
| | | queryDTO.setEnd(item.getEndTime()); |
| | | List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDTO); |
| | | if (valueList != null && !valueList.isEmpty()){ |
| | | for (ApiPointValueDTO apiPointValueDTO : valueList) { |
| | | Object[] data = new Object[2]; |
| | | data[0] = DateUtils.format(apiPointValueDTO.getT(), DATE_TIME_PATTERN); |
| | | data[1] = apiPointValueDTO.getV(); |
| | | dataList.add(data); |
| | | } |
| | | } |
| | | } else if (DataCategoryEnum.IND.getCode().equals(item.getDataType())) { |
| | | ApiIndItemQueryDTO queryDTO = new ApiIndItemQueryDTO(); |
| | | queryDTO.setItemNo(item.getDataNo()); |
| | | queryDTO.setStart(item.getStartTime()); |
| | | queryDTO.setEnd(item.getEndTime()); |
| | | List<ApiIndItemValueDTO> valueList = indItemApi.queryIndItemHistoryValue(queryDTO); |
| | | if (valueList != null && !valueList.isEmpty()){ |
| | | for (ApiIndItemValueDTO apiIndItemValueDTO : valueList) { |
| | | Object[] data = new Object[2]; |
| | | data[0] = apiIndItemValueDTO.getDataTime(); |
| | | data[1] = apiIndItemValueDTO.getDataValue(); |
| | | dataList.add(data); |
| | | } |
| | | } |
| | | } else if (DataCategoryEnum.PLAN.getCode().equals(item.getDataType())) { |
| | | ApiDataQueryDTO queryDTO = new ApiDataQueryDTO(); |
| | | queryDTO.setItemNo(item.getDataNo()); |
| | | queryDTO.setStart(item.getStartTime()); |
| | | queryDTO.setEnd(item.getEndTime()); |
| | | List<ApiDataValueDTO> valueList = planItemApi.queryPlanItemHistoryValue(queryDTO); |
| | | if (valueList != null && !valueList.isEmpty()){ |
| | | for (ApiDataValueDTO apiDataValueDTO : valueList) { |
| | | Object[] data = new Object[2]; |
| | | data[0] = DateUtils.format(apiDataValueDTO.getDataTime(), DATE_TIME_PATTERN); |
| | | data[1] = apiDataValueDTO.getDataValue(); |
| | | dataList.add(data); |
| | | } |
| | | } |
| | | } else if (DataCategoryEnum.PREDICT_ITEM_N.getCode().equals(item.getDataType())) { |
| | | dataList = getPreDataList(item, "N"); |
| | | }else if (DataCategoryEnum.PREDICT_ITEM_L.getCode().equals(item.getDataType())) { |
| | | dataList = getPreDataList(item, "L"); |
| | | }else if (DataCategoryEnum.PREDICT_ITEM_C.getCode().equals(item.getDataType())) { |
| | | dataList = getPreDataList(item, "C"); |
| | | } |
| | | item.setDataList(dataList); |
| | | |
| | | if (!CollectionUtils.isEmpty(dataList)) { |
| | | List<String> overLimitTimes = new ArrayList<>(); |
| | | dataList.forEach(data -> { |
| | | BigDecimal value = new BigDecimal(data[1].toString()); |
| | | if (value.compareTo(item.getLimitH()) >= 0 || value.compareTo(item.getLimitL()) <= 0) { |
| | | overLimitTimes.add(data[0].toString()); |
| | | } |
| | | }); |
| | | item.setOverLimitTimes(overLimitTimes); |
| | | } |
| | | |
| | | }); |
| | | return reqList; |
| | | } |
| | | |
| | | @Override |
| | | public List<StSuggestSnapshotRecordRespVO> getAllChartData(String suggestId) { |
| | | List<StSuggestSnapshotRecordRespVO> records = this.getListBySuggestId(suggestId); |
| | | if (CollectionUtils.isEmpty(records)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | return this.getChartData(records); |
| | | } |
| | | |
| | | private List<Object[]> getPreDataList(StSuggestSnapshotRecordRespVO resVo, String type){ |
| | | List<Object[]> dataList = new ArrayList<>(); |
| | | PreDataBarLineReqVO reqVO = new PreDataBarLineReqVO(); |
| | | List<String> outIds = new ArrayList<>(); |
| | | outIds.add(resVo.getDataNo()); |
| | | reqVO.setOutIds(outIds); |
| | | reqVO.setStartTime(resVo.getStartTime()); |
| | | reqVO.setEndTime(resVo.getEndTime()); |
| | | PreDataBarLineRespVO result = mmItemResultService.getPreDataCharts(reqVO); |
| | | if (result == null || result.getDataViewList() == null || result.getDataViewList().isEmpty()){ |
| | | return dataList; |
| | | } |
| | | if ("N".equals(type) && result.getDataViewList().get(0).getPreDataN()!=null){ |
| | | dataList = result.getDataViewList().get(0).getPreDataN(); |
| | | }else if ("L".equals(type) && result.getDataViewList().get(0).getPreDataL()!=null){ |
| | | dataList = result.getDataViewList().get(0).getPreDataL(); |
| | | }else if ("C".equals(type) && result.getDataViewList().get(0).getCurData()!=null){ |
| | | dataList = result.getDataViewList().get(0).getCurData(); |
| | | } |
| | | return dataList; |
| | | } |
| | | } |