123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- 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
- }
|