package services import ( "encoding/json" "errors" "eta/eta_index_lib/models" "eta/eta_index_lib/services/alarm_msg" "eta/eta_index_lib/utils" "fmt" "time" ) // FactorEdbStepCalculate 因子指标-多公式计算 func FactorEdbStepCalculate(seriesId, edbInfoId int, edbCode string, edbData []*models.EdbInfoSearchData, calculates []models.CalculatesReq) (err error) { if len(edbData) == 0 || len(calculates) == 0 { return } defer func() { if err != nil { tips := fmt.Sprintf("FactorEdbStepCalculate计算失败, EdbCode: %s, ErrMsg: %v", edbCode, err) utils.FileLog.Info(tips) go alarm_msg.SendAlarmMsg(tips, 2) } }() // 重新计算-先清除原数据 calculateDataOb := new(models.FactorEdbSeriesCalculateData) { cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId) pars := make([]interface{}, 0) pars = append(pars, seriesId) if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil { err = fmt.Errorf("清除原数据失败, err: %v", e) return } } // 多公式嵌套计算 dataMap, dateList, _, e := StepCalculate(edbData, calculates) if e != nil { err = fmt.Errorf("嵌套计算失败, err: %v", e) return } // 计算成功的保存结果 dataArr := make([]*models.FactorEdbSeriesCalculateData, 0) for _, d := range dateList { val, ok := dataMap[d] if !ok { continue } dataTime, e := time.ParseInLocation(utils.FormatDate, d, time.Local) if e != nil { err = fmt.Errorf("解析计算结果日期失败, err: %v", e) return } dataArr = append(dataArr, &models.FactorEdbSeriesCalculateData{ FactorEdbSeriesId: seriesId, EdbInfoId: edbInfoId, EdbCode: edbCode, DataTime: dataTime, Value: val, CreateTime: time.Now().Local(), ModifyTime: time.Now().Local(), DataTimestamp: dataTime.UnixNano() / 1e6, }) } if len(dataArr) == 0 { //err = fmt.Errorf("计算结果无数据") return } if e = calculateDataOb.CreateMulti(dataArr); e != nil { err = fmt.Errorf("保存计算结果失败, err: %v", e) return } return } func RangeAnalysisChartCalculate(seriesId, chartInfoId int, seriesMappingItem *models.FactorEdbSeriesChartMapping) (err error, errMsg string) { defer func() { if err != nil { tips := fmt.Sprintf("RangeAnalysisChartCalculate 区间分析图表计算失败, 图表ID:%d, ErrMsg: %v", chartInfoId, err) fmt.Println(tips) utils.FileLog.Info(tips) go alarm_msg.SendAlarmMsg(tips, 3) } }() chartInfo, err := models.GetChartInfoById(chartInfoId) if err != nil { if err.Error() == utils.ErrNoRow() { errMsg = "图表已被删除,请刷新页面" err = errors.New(errMsg) return } errMsg = "获取图表信息失败" err = errors.New("获取图表信息失败,Err:" + err.Error()) return } // 区间计算图表配置校验 var extraConfig models.RangeAnalysisCalculateFormula err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig) if err != nil { errMsg = "配置信息错误" err = errors.New(errMsg + ", Err: " + err.Error()) return } if seriesMappingItem.FactorEdbSeriesChartMappingId > 0 { ob := new(models.FactorEdbSeriesMapping) seriesEdbMappingList, e := ob.GetItemBySeriesId(seriesMappingItem.FactorEdbSeriesId) if e != nil { // 没有数据,则不计算 return } edbInfoIds := make([]int, 0) for _, item := range seriesEdbMappingList { edbInfoIds = append(edbInfoIds, item.EdbInfoId) } edbInfoList, e := models.GetEdbInfoByIdList(edbInfoIds) if e != nil { errMsg = "获取指标信息失败" err = errors.New(errMsg + ", Err: " + e.Error()) return } // 重新计算-先清除原数据 calculateDataOb := new(models.FactorEdbSeriesCalculateDataQjjs) cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId) pars := make([]interface{}, 0) pars = append(pars, seriesId) if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil { err = fmt.Errorf("清除原数据失败, err: %v", e) return } // 计算成功的保存结果 dataArr := make([]*models.FactorEdbSeriesCalculateDataQjjs, 0) for _, fromEdbInfo := range edbInfoList { dataList, e := models.GetRangeAnalysisChartDataByEdbInfo(fromEdbInfo, extraConfig) if e != nil { errMsg = "获取区间分析图表数据失败" err = errors.New(errMsg + ", Err: " + e.Error()) return } for _, dataItem := range dataList { dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local) dataArr = append(dataArr, &models.FactorEdbSeriesCalculateDataQjjs{ FactorEdbSeriesId: seriesId, EdbInfoId: fromEdbInfo.EdbInfoId, EdbCode: fromEdbInfo.EdbCode, DataTime: dataTime, Value: dataItem.Value, CreateTime: time.Now().Local(), ModifyTime: time.Now().Local(), DataTimestamp: dataItem.DataTimestamp, }) } } if len(dataArr) == 0 { err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId) return } if e := calculateDataOb.CreateMulti(dataArr); e != nil { err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e) return } } return }