package services import ( aiPredictModel "eta/eta_api/models/ai_predict_model" "eta/eta_api/models/data_manage" "eta/eta_api/services/data" "eta/eta_api/utils" "fmt" "time" ) func ImportAiPredictModelIndexAndData(imports []*aiPredictModel.AiPredictModelImportData) (err error) { if len(imports) == 0 { return } // 查询已存在的标的 indexOb := new(aiPredictModel.AiPredictModelIndex) indexNameItem := make(map[string]*aiPredictModel.AiPredictModelIndex) { list, e := indexOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "") if e != nil { err = fmt.Errorf("获取标的失败, %v", e) return } for _, v := range list { indexNameItem[v.IndexName] = v } } updateCols := []string{indexOb.Cols().ClassifyId, indexOb.Cols().ModelFramework, indexOb.Cols().PredictDate, indexOb.Cols().PredictValue, indexOb.Cols().DirectionAccuracy, indexOb.Cols().AbsoluteDeviation, indexOb.Cols().SysUserId, indexOb.Cols().SysUserRealName, indexOb.Cols().ModifyTime} updateIndexes := make([]*aiPredictModel.AiPredictModelImportData, 0) createIndexes := make([]*aiPredictModel.AiPredictModelImportData, 0) for _, v := range imports { exist := indexNameItem[v.Index.IndexName] // 编辑 if exist != nil { v.Index.AiPredictModelIndexId = exist.AiPredictModelIndexId v.Index.IndexCode = exist.IndexCode updateIndexes = append(updateIndexes, v) continue } // 新增 indexCode, e := utils.GenerateEdbCode(1, "IPM") if e != nil { err = fmt.Errorf("生成标的编码失败, %v", e) return } v.Index.IndexCode = indexCode createIndexes = append(createIndexes, v) } // 新增/更新指标 if e := indexOb.ImportIndexAndData(createIndexes, updateIndexes, updateCols); e != nil { err = fmt.Errorf("导入指标失败, %v", e) return } return } func GetAiPredictChartDetailByData(indexItem *aiPredictModel.AiPredictModelIndex, indexData []*aiPredictModel.AiPredictModelData) (resp *data_manage.ChartInfoDetailResp, err error) { resp = new(data_manage.ChartInfoDetailResp) // 获取曲线图主题样式 chartView := new(data_manage.ChartInfoView) chartView.ChartType = utils.CHART_SOURCE_DEFAULT chartTheme, e := data.GetChartThemeConfig(0, chartView.ChartType, utils.CHART_TYPE_CURVE) if e != nil { err = fmt.Errorf("获取图表主题样式失败, %v", e) return } chartView.ChartThemeStyle = chartTheme.Config chartView.ChartThemeId = chartTheme.ChartThemeId chartView.ChartName = indexItem.IndexName chartView.ChartNameEn = indexItem.IndexName chartView.DateType = 3 chartView.Calendar = "公历" chartView.ChartSource = "AI预测模型" chartView.ChartSourceEn = "AI预测模型" // EdbList-固定一条为标的实际值、一条为预测值 edbList := make([]*data_manage.ChartEdbInfoMapping, 0) edbActual, edbPredict := new(data_manage.ChartEdbInfoMapping), new(data_manage.ChartEdbInfoMapping) edbActual.EdbName = indexItem.IndexName edbActual.EdbNameEn = indexItem.IndexName edbActual.IsAxis = 1 edbPredict.EdbName = "预测值" edbPredict.EdbNameEn = "预测值" edbPredict.IsAxis = 1 actualData, predictData := make([]*data_manage.EdbDataList, 0), make([]*data_manage.EdbDataList, 0) var startDate, endDate time.Time var actualValues, predictValues []float64 var actualNewest, predictNewest bool for k, v := range indexData { // 如果实际值和预测值都是null那么该日期无效直接忽略 if !v.Value.Valid && !v.PredictValue.Valid { continue } // 将有效值加入[]float64,最后取极值 if v.Value.Valid { actualValues = append(actualValues, v.Value.Float64) } if v.PredictValue.Valid { predictValues = append(predictValues, v.PredictValue.Float64) } // 开始结束时间 if k == 0 { startDate = v.DataTime endDate = v.CreateTime } if v.DataTime.Before(startDate) { startDate = v.DataTime } if v.DataTime.After(endDate) { endDate = v.DataTime } // 指标数据 if v.Value.Valid { if !actualNewest { edbActual.LatestDate = v.DataTime.Format(utils.FormatDate) edbActual.LatestValue = v.Value.Float64 actualNewest = true } actualData = append(actualData, &data_manage.EdbDataList{ DataTime: v.DataTime.Format(utils.FormatDate), Value: v.Value.Float64, DataTimestamp: v.DataTimestamp, }) } if v.PredictValue.Valid { if !predictNewest { edbPredict.LatestDate = v.DataTime.Format(utils.FormatDate) edbPredict.LatestValue = v.Value.Float64 predictNewest = true } predictData = append(predictData, &data_manage.EdbDataList{ DataTime: v.DataTime.Format(utils.FormatDate), Value: v.PredictValue.Float64, DataTimestamp: v.DataTimestamp, }) } } // 极值 actualMin, actualMax := utils.FindMinMax(actualValues) predictMin, predictMax := utils.FindMinMax(predictValues) edbActual.MinData = actualMin edbActual.MaxData = actualMax edbPredict.MinData = predictMin edbPredict.MaxData = predictMax edbActual.DataList = actualData edbPredict.DataList = predictData edbList = append(edbList, edbActual, edbPredict) // 上下限 if indexItem.LeftMin != "" { chartView.LeftMin = indexItem.LeftMin } else { leftMin := actualMin if leftMin > predictMin { leftMin = predictMin } chartView.LeftMin = fmt.Sprint(leftMin) } if indexItem.LeftMax != "" { chartView.LeftMax = indexItem.LeftMax } else { leftMax := actualMax if leftMax < predictMax { leftMax = predictMax } chartView.LeftMax = fmt.Sprint(leftMax) } chartView.StartDate = startDate.Format(utils.FormatDate) chartView.EndDate = endDate.Format(utils.FormatDate) resp.ChartInfo = chartView resp.EdbInfoList = edbList return }