|
@@ -3,11 +3,12 @@ package models
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
+ "eta/eta_index_lib/global"
|
|
|
"eta/eta_index_lib/utils"
|
|
|
"fmt"
|
|
|
- "github.com/beego/beego/v2/client/orm"
|
|
|
"github.com/dengsgo/math-engine/engine"
|
|
|
"github.com/shopspring/decimal"
|
|
|
+ "gorm.io/gorm"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -36,14 +37,15 @@ type PredictCalculateItems struct {
|
|
|
|
|
|
// EditPredictCalculate 新增预测计算(运算)指标
|
|
|
func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalculateMapping) (err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
- to, err := o.Begin()
|
|
|
+ //o := orm.NewOrm()
|
|
|
+ //to, err := o.Begin()
|
|
|
+ to := global.DEFAULT_DB.Begin()
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
|
- tmpErr := to.Rollback()
|
|
|
+ tmpErr := to.Rollback().Error
|
|
|
errMsg := fmt.Sprintf("AddPredictCalculate Err:%s" + err.Error())
|
|
|
if tmpErr != nil {
|
|
|
errMsg += `;rollback失败:` + tmpErr.Error()
|
|
@@ -52,7 +54,7 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- tmpErr := to.Commit()
|
|
|
+ tmpErr := to.Commit().Error
|
|
|
if tmpErr != nil {
|
|
|
errMsg := fmt.Sprintf("AddPredictCalculate commit Err:%s" + tmpErr.Error())
|
|
|
utils.FileLog.Info(errMsg)
|
|
@@ -60,14 +62,16 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
}()
|
|
|
|
|
|
// 修改指标信息
|
|
|
- _, err = to.Update(edbInfo, "EdbName", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "EmptyType", "MaxEmptyType", "Extra", "EdbNameEn", "UnitEn")
|
|
|
+ //_, err = to.Update(edbInfo, "EdbName", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "EmptyType", "MaxEmptyType", "Extra", "EdbNameEn", "UnitEn")
|
|
|
+ err = to.Model(&edbInfo).Select([]string{"EdbName", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "EmptyType", "MaxEmptyType", "Extra", "EdbNameEn", "UnitEn"}).Updates(&edbInfo).Error
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//删除,计算指标关联的,基础指标的关联关系
|
|
|
sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
|
|
|
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
+ //_, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
+ err = to.Exec(sql, edbInfo.EdbInfoId).Error
|
|
|
if err != nil {
|
|
|
err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
|
|
|
return
|
|
@@ -75,14 +79,16 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
|
|
|
// 插入新的关系表
|
|
|
if len(calculateMappingList) > 0 {
|
|
|
- _, err = to.InsertMulti(len(calculateMappingList), calculateMappingList)
|
|
|
+ //_, err = to.InsertMulti(len(calculateMappingList), calculateMappingList)
|
|
|
+ err = to.CreateInBatches(&calculateMappingList, len(calculateMappingList)).Error
|
|
|
}
|
|
|
|
|
|
//数据表
|
|
|
tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
|
|
|
//清空原有数据
|
|
|
sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
|
|
|
- _, err = o.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
+ //_, err = o.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
+ err = to.Exec(sql, edbInfo.EdbInfoId).Error
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -225,8 +231,9 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
//}
|
|
|
|
|
|
func AddPredictCalculateData(edbInfoIdList []*EdbInfo, edbInfoTag map[string]int, edbInfo *EdbInfo, edbCode, formulaStr string, edbInfoIdBytes []string) (latestDateStr string, latestValue float64, err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
- to, err := o.Begin()
|
|
|
+ //o := orm.NewOrm()
|
|
|
+ //to, err := o.Begin()
|
|
|
+ to := global.DEFAULT_DB.Begin()
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -245,8 +252,9 @@ func AddPredictCalculateData(edbInfoIdList []*EdbInfo, edbInfoTag map[string]int
|
|
|
|
|
|
// RefreshAllPredictCalculate 刷新预测计算指标的全部数据
|
|
|
func RefreshAllPredictCalculate(edbInfoIdList []*EdbInfo, edbInfoTag map[string]int, edbInfoId, source, subSource int, edbCode, formulaStr, startDate string, edbInfoIdBytes []string, emptyType, maxEmptyType int, extra string) (latestDateStr string, latestValue float64, err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
- to, err := o.Begin()
|
|
|
+ //o := orm.NewOrm()
|
|
|
+ //to, err := o.Begin()
|
|
|
+ to := global.DEFAULT_DB.Begin()
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -265,7 +273,7 @@ func RefreshAllPredictCalculate(edbInfoIdList []*EdbInfo, edbInfoTag map[string]
|
|
|
}
|
|
|
|
|
|
// refreshAllPredictCalculate 刷新预测计算指标的全部数据
|
|
|
-func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInfoTag map[string]int, edbInfoId, source, subSource int, edbCode, formulaStr, startDate string, edbInfoIdBytes []string, emptyType, maxEmptyType int, extra string) (latestDateStr string, latestValue float64, err error) {
|
|
|
+func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTag map[string]int, edbInfoId, source, subSource int, edbCode, formulaStr, startDate string, edbInfoIdBytes []string, emptyType, maxEmptyType int, extra string) (latestDateStr string, latestValue float64, err error) {
|
|
|
fmt.Println("startDate:", startDate)
|
|
|
// 最小的结束日期 , 最晚的数据开始日期
|
|
|
var newLatestDate, minLatestDate, maxStartDate time.Time
|
|
@@ -394,7 +402,8 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
|
|
|
dataTableName := GetEdbDataTableName(source, subSource)
|
|
|
sql := `SELECT * FROM %s WHERE edb_info_id=? `
|
|
|
sql = fmt.Sprintf(sql, dataTableName)
|
|
|
- _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
|
|
|
+ //_, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
|
|
|
+ err = to.Raw(sql, edbInfoId).Find(&dataList).Error
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -513,7 +522,8 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
|
|
|
if existStr != saveValue {
|
|
|
sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
|
|
|
sql = fmt.Sprintf(sql, dataTableName)
|
|
|
- _, err = to.Raw(sql, saveValue, edbInfoId, sk).Exec()
|
|
|
+ //_, err = to.Raw(sql, saveValue, edbInfoId, sk).Exec()
|
|
|
+ err = to.Exec(sql, saveValue, edbInfoId, sk).Error
|
|
|
if err != nil {
|
|
|
return "", 0, err
|
|
|
}
|
|
@@ -528,7 +538,8 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
|
|
|
|
|
|
if isAdd {
|
|
|
addSql = strings.TrimRight(addSql, ",")
|
|
|
- _, err = to.Raw(addSql).Exec()
|
|
|
+ //_, err = to.Raw(addSql).Exec()
|
|
|
+ err = to.Exec(addSql).Error
|
|
|
if err != nil {
|
|
|
fmt.Println("RefreshAllCalculate add Err", err.Error())
|
|
|
return
|
|
@@ -542,7 +553,8 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
|
|
|
tableName := GetEdbDataTableName(source, subSource)
|
|
|
sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
|
|
|
|
|
|
- _, err = to.Raw(sql, edbInfoId).Exec()
|
|
|
+ //_, err = to.Raw(sql, edbInfoId).Exec()
|
|
|
+ err = to.Exec(sql, edbInfoId).Error
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
|
|
|
return
|
|
@@ -554,12 +566,14 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
|
|
|
latestDateStr = newLatestDate.Format(utils.FormatDate)
|
|
|
sql = fmt.Sprintf(` SELECT * FROM %s WHERE edb_info_id = ? and data_time <= ? ORDER BY data_time desc limit 1`, dataTableName)
|
|
|
var latestDate EdbData
|
|
|
- e := to.Raw(sql, edbInfoId, latestDateStr).QueryRow(&latestDate)
|
|
|
+ //e := to.Raw(sql, edbInfoId, latestDateStr).QueryRow(&latestDate)
|
|
|
+ e := to.Raw(sql, edbInfoId, latestDateStr).First(&latestDate).Error
|
|
|
if e != nil {
|
|
|
if e.Error() == utils.ErrNoRow() {
|
|
|
//首个日期的值
|
|
|
sql = fmt.Sprintf(` SELECT * FROM %s WHERE edb_info_id = ? ORDER BY data_time ASC limit 1`, dataTableName)
|
|
|
- e = to.Raw(sql, edbInfoId).QueryRow(&latestDate)
|
|
|
+ //e = to.Raw(sql, edbInfoId).QueryRow(&latestDate)
|
|
|
+ e = to.Raw(sql, edbInfoId).First(&latestDate).Error
|
|
|
if e != nil {
|
|
|
err = fmt.Errorf("查询最新日期失败,Err:" + e.Error())
|
|
|
return
|