| | |
| | | package cn.iocoder.yudao.module.bi.service.decision; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.iocoder.yudao.module.bi.controller.admin.decision.vo.DecisionForecastHistorySaveReqVO; |
| | | import cn.iocoder.yudao.module.bi.controller.admin.decision.vo.DecisionForecastWorkOrderCreateReqVO; |
| | | import cn.iocoder.yudao.module.bi.dal.dataobject.decision.BiForecastDataDO; |
| | | import cn.iocoder.yudao.module.bi.dal.mysql.decision.BiForecastDataMapper; |
| | | import cn.iocoder.yudao.module.mes.api.workorder.MesProWorkOrderApi; |
| | | import cn.iocoder.yudao.module.mes.api.workorder.dto.MesProWorkOrderCreateReqDTO; |
| | | import jakarta.annotation.Resource; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.time.LocalDateTime; |
| | | import java.util.List; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.bi.enums.ErrorCodeConstants.BI_FORECAST_NO_HISTORY; |
| | | import static cn.iocoder.yudao.module.bi.enums.ErrorCodeConstants.BI_FORECAST_NOT_EXISTS; |
| | | |
| | | /** |
| | | * 负荷预测 Service 实现 |
| | |
| | | @Service |
| | | public class BiForecastServiceImpl implements BiForecastService { |
| | | |
| | | /** |
| | | * 工单来源类型:负荷预测(与字典 mes_pro_work_order_source_type 的 value=4 一致) |
| | | */ |
| | | private static final Integer WORK_ORDER_SOURCE_TYPE_FORECAST = 4; |
| | | |
| | | @Resource |
| | | private BiForecastDataMapper forecastDataMapper; |
| | | @Resource |
| | | private MesProWorkOrderApi mesProWorkOrderApi; |
| | | |
| | | @Override |
| | | public List<BiForecastDataDO> getForecastList(String forecastCode, LocalDateTime beginTime, LocalDateTime endTime) { |
| | | return forecastDataMapper.selectListByCodeAndTimeRange(forecastCode, beginTime, endTime); |
| | | } |
| | | |
| | | /** |
| | | * 移动平均窗口大小 |
| | | */ |
| | | private static final int SMA_WINDOW = 12; |
| | | |
| | | @Override |
| | | public Long createForecastHistory(DecisionForecastHistorySaveReqVO reqVO) { |
| | | BiForecastDataDO data = BiForecastDataDO.builder() |
| | | .forecastCode(reqVO.getForecastCode()) |
| | | .forecastName(reqVO.getForecastName()) |
| | | .pointTime(reqVO.getPointTime()) |
| | | .forecastValue(reqVO.getForecastValue()) |
| | | .actualValue(reqVO.getActualValue()) |
| | | .dimension(reqVO.getDimension()) |
| | | .dimensionValue(reqVO.getDimensionValue()) |
| | | .remark(reqVO.getRemark()) |
| | | .modelVersion("manual-history") |
| | | .build(); |
| | | forecastDataMapper.insert(data); |
| | | return data.getId(); |
| | | } |
| | | |
| | | @Override |
| | | public void generateForecast(String forecastCode, LocalDateTime pointTime) { |
| | | // 本期占位:简单移动平均预测 |
| | | // 实际项目应集成 ML 模型(ARIMA/Prophet/LSTM) |
| | | log.info("[generateForecast][生成预测] forecastCode={}, pointTime={}, 当前为占位实现", forecastCode, pointTime); |
| | | // 基于同预测编码的历史预测数据做简单移动平均;无历史数据时不伪造零值 |
| | | List<BiForecastDataDO> history = forecastDataMapper.selectRecentByCode(forecastCode, SMA_WINDOW); |
| | | if (CollUtil.isEmpty(history)) { |
| | | throw exception(BI_FORECAST_NO_HISTORY); |
| | | } |
| | | BigDecimal sum = history.stream() |
| | | .map(BiForecastDataDO::getForecastValue) |
| | | .filter(value -> value != null) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | BigDecimal avg = sum.divide(BigDecimal.valueOf(history.size()), 4, RoundingMode.HALF_UP); |
| | | BigDecimal bound = avg.multiply(new BigDecimal("0.10")); |
| | | |
| | | BiForecastDataDO forecast = BiForecastDataDO.builder() |
| | | .forecastCode(forecastCode) |
| | | .forecastName("负荷预测(占位)") |
| | | .forecastName(history.get(0).getForecastName()) |
| | | .pointTime(pointTime) |
| | | .forecastValue(BigDecimal.ZERO) |
| | | .lowerBound(BigDecimal.ZERO) |
| | | .upperBound(BigDecimal.ZERO) |
| | | .forecastValue(avg) |
| | | .lowerBound(avg.subtract(bound)) |
| | | .upperBound(avg.add(bound)) |
| | | .confidenceLevel(new BigDecimal("95.00")) |
| | | .modelVersion("v1.0-placeholder") |
| | | .modelVersion("v1.0-sma") |
| | | .build(); |
| | | forecastDataMapper.insert(forecast); |
| | | } |
| | | |
| | | } |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createWorkOrderFromForecast(DecisionForecastWorkOrderCreateReqVO reqVO) { |
| | | // 1. 校验预测数据存在 |
| | | BiForecastDataDO forecast = forecastDataMapper.selectById(reqVO.getForecastId()); |
| | | if (forecast == null) { |
| | | throw exception(BI_FORECAST_NOT_EXISTS); |
| | | } |
| | | // 2. 组装工单 DTO 并调用 MES API 创建(无产品工单,跳 BOM) |
| | | MesProWorkOrderCreateReqDTO dto = new MesProWorkOrderCreateReqDTO(); |
| | | dto.setCode(generateWorkOrderCode()); |
| | | dto.setName(reqVO.getWorkOrderName() != null |
| | | ? reqVO.getWorkOrderName() |
| | | : ("负荷预测-" + forecast.getForecastName())); |
| | | dto.setType(reqVO.getWorkOrderType()); |
| | | dto.setOrderSourceType(WORK_ORDER_SOURCE_TYPE_FORECAST); |
| | | dto.setOrderSourceCode(forecast.getForecastCode()); |
| | | dto.setRequestDate(reqVO.getRequestDate()); |
| | | dto.setQuantity(reqVO.getQuantity()); |
| | | dto.setRemark(reqVO.getRemark()); |
| | | return mesProWorkOrderApi.createWorkOrder(dto); |
| | | } |
| | | |
| | | private String generateWorkOrderCode() { |
| | | return "GZ" + DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmss") |
| | | + String.format("%03d", (int) (Math.random() * 1000)); |
| | | } |
| | | |
| | | } |