package com.iailab.module.model.mcs.sche.service.impl; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.util.date.DateUtils; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; import com.iailab.module.model.mcs.pre.service.MmItemOutputService; import com.iailab.module.model.mcs.sche.dao.StAdjustResultDao; import com.iailab.module.model.mcs.sche.entity.StAdjustResultEntity; import com.iailab.module.model.mcs.sche.service.StAdjustResultService; import com.iailab.module.model.mcs.sche.vo.StAdjustResultPageReqVO; import com.iailab.module.model.mcs.sche.vo.StAdjustResultRespVO; import com.iailab.module.model.mdk.vo.DataValueVO; import lombok.extern.slf4j.Slf4j; 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.util.*; import java.util.stream.Collectors; /** * @author PanZhibao * @Description * @createTime 2025年02月23日 */ @Slf4j @Service public class StAdjustResultServiceImpl extends BaseServiceImpl implements StAdjustResultService { @Autowired private MmItemOutputService mmItemOutputService; @Override public void saveResult(Map> resultMap, Date predictTime, String adjustValue, String scheduleModelId, String configId) { List list = new ArrayList<>(resultMap.size()); for (Map.Entry> entry : resultMap.entrySet()) { StAdjustResultEntity entity = new StAdjustResultEntity(); entity.setId(UUID.randomUUID().toString()); entity.setConfigId(configId); entity.setOutputId(entry.getKey()); entity.setScheduleModelId(scheduleModelId); entity.setAdjustTime(predictTime); entity.setAdjustValue(adjustValue); List jsonValueList = entry.getValue().stream().map(DataValueVO::getDataValue).collect(Collectors.toList()); entity.setJsonValue(JSONArray.toJSONString(jsonValueList)); list.add(entity); } baseDao.insert(list); } @Override public double[] getSimpleData(String outputId, Date predictTime, int predictLength) { double[] result = new double[predictLength]; QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("output_id", outputId) .eq("adjust_time", DateUtils.format(predictTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); StAdjustResultEntity data = baseDao.selectOne(wrapper); if (data == null || StringUtils.isBlank(data.getJsonValue())) { return null; } List valueList = JSONArray.parseArray(data.getJsonValue(), Double.class); if (CollectionUtils.isEmpty(valueList)) { return result; } for (int i = 0; i < predictLength; i++) { result[i] = valueList.get(i); } return result; } @Override public List getPredictValue(String outputId, Date predictTime) { List result = new ArrayList<>(); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("output_id", outputId) .eq("adjust_time", DateUtils.format(predictTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); StAdjustResultEntity data = baseDao.selectOne(wrapper); if (data == null || StringUtils.isBlank(data.getJsonValue())) { return result; } List valueList = JSONArray.parseArray(data.getJsonValue(), Double.class); if (CollectionUtils.isEmpty(valueList)) { return result; } Calendar calendar = Calendar.getInstance(); calendar.setTime(predictTime); for (int i = 1; i < valueList.size() + 1; i++) { calendar.add(Calendar.MINUTE, i); DataValueVO vo = new DataValueVO(); vo.setDataValue(valueList.get(i - 1)); vo.setDataTime(calendar.getTime()); result.add(vo); } return result; } @Override public List getData(String outputId, Date predictTime, String timeFormat) { List result = new ArrayList<>(); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("output_id", outputId) .eq("adjust_time", DateUtils.format(predictTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); StAdjustResultEntity data = baseDao.selectOne(wrapper); if (data == null || StringUtils.isBlank(data.getJsonValue())) { return result; } List valueList = JSONArray.parseArray(data.getJsonValue(), Double.class); if (CollectionUtils.isEmpty(valueList)) { return result; } Calendar calendar = Calendar.getInstance(); calendar.setTime(predictTime); valueList.forEach(value -> { Object[] dv = {DateUtils.format(calendar.getTime(), timeFormat), value}; calendar.add(Calendar.MINUTE, 1); result.add(dv); }); return result; } @Override public StAdjustResultRespVO getInfo(String id) { StAdjustResultEntity entity = baseDao.selectById(id); StAdjustResultRespVO result = ConvertUtils.sourceToTarget(entity, StAdjustResultRespVO.class); MmItemOutputEntity mmItemOutputEntity = mmItemOutputService.getOutPutById(entity.getOutputId()); result.setOutputName(mmItemOutputEntity.getResultName()); return result; } @Override public PageResult page(StAdjustResultPageReqVO pageVO) { IPage page = baseDao.selectPage(pageVO); return new PageResult<>(page.getRecords(), page.getTotal()); } }