|
@@ -37,12 +37,6 @@ type PredictCalculateItems struct {
|
|
|
|
|
|
// EditPredictCalculate 新增预测计算(运算)指标
|
|
|
func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalculateMapping) (err error) {
|
|
|
- //o := orm.NewOrm()
|
|
|
- //to, err := o.Begin()
|
|
|
- //if err != nil {
|
|
|
- // return
|
|
|
- //}
|
|
|
-
|
|
|
to := global.DEFAULT_DmSQL.Begin()
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
@@ -63,7 +57,6 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
}()
|
|
|
|
|
|
// 修改指标信息
|
|
|
- //_, err = to.Update(edbInfo, "EdbName", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "EmptyType", "MaxEmptyType", "Extra", "EdbNameEn", "UnitEn")
|
|
|
err = edbInfo.Update(
|
|
|
[]string{"EdbName", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "EmptyType", "MaxEmptyType", "Extra", "EdbNameEn", "UnitEn"},
|
|
|
)
|
|
@@ -73,7 +66,6 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
|
|
|
//删除,计算指标关联的,基础指标的关联关系
|
|
|
sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
|
|
|
- //_, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
err = to.Exec(sql, edbInfo.EdbInfoId).Error
|
|
|
if err != nil {
|
|
|
err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
|
|
@@ -82,7 +74,6 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
|
|
|
// 插入新的关系表
|
|
|
if len(calculateMappingList) > 0 {
|
|
|
- //_, err = to.InsertMulti(len(calculateMappingList), calculateMappingList)
|
|
|
err = to.CreateInBatches(calculateMappingList, len(calculateMappingList)).Error
|
|
|
}
|
|
|
|
|
@@ -90,7 +81,6 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
|
|
|
//清空原有数据
|
|
|
sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
|
|
|
- //_, err = o.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
err = to.Exec(sql, edbInfo.EdbInfoId).Error
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -99,146 +89,7 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// AddPredictCalculateData 新增预测计算(运算)指标的数据
|
|
|
-//func AddPredictCalculateDataBak(edbInfoIdList []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (latestDateStr string, latestValue float64, err error) {
|
|
|
-// o := orm.NewOrm()
|
|
|
-// defer func() {
|
|
|
-// if err != nil {
|
|
|
-// utils.FileLog.Info(fmt.Sprintf("Calculate Err:%s" + err.Error()))
|
|
|
-// }
|
|
|
-// }()
|
|
|
-//
|
|
|
-// var minLatestDate time.Time
|
|
|
-// oldSaveDataMap := make(map[string]map[int]float64)
|
|
|
-// saveDataMap := make(map[string]map[int]float64)
|
|
|
-// dateList := make([]string, 0)
|
|
|
-//
|
|
|
-// for edbInfoIndex, v := range edbInfoIdList {
|
|
|
-// tmpLatestDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
|
|
|
-// if tmpErr != nil {
|
|
|
-// err = errors.New(`最近的日期格式化失败;日期:` + v.LatestDate + `;err:` + tmpErr.Error())
|
|
|
-// return
|
|
|
-// }
|
|
|
-// // 如果不存在最小的真实数据日期,那么将当前指标的真实数据日期给赋值;
|
|
|
-// // 如果存在最小的真实数据日期,同时当前指标的真实数据日期 早于 最小的真实数据日期, 那么将当前指标的真实数据日期给赋值;
|
|
|
-// if minLatestDate.IsZero() || tmpLatestDate.Before(minLatestDate) {
|
|
|
-// minLatestDate = tmpLatestDate
|
|
|
-// }
|
|
|
-//
|
|
|
-// dataList, tmpErr := GetPredictEdbDataListAll(v, 1)
|
|
|
-// if tmpErr != nil {
|
|
|
-// err = tmpErr
|
|
|
-// return
|
|
|
-// }
|
|
|
-// dataMap := make(map[string]float64)
|
|
|
-// for _, dv := range dataList {
|
|
|
-// // 现有实际数据
|
|
|
-// if val, ok := oldSaveDataMap[dv.DataTime]; ok {
|
|
|
-// if _, ok := val[v.EdbInfoId]; !ok {
|
|
|
-// val[v.EdbInfoId] = dv.Value
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// temp := make(map[int]float64)
|
|
|
-// temp[v.EdbInfoId] = dv.Value
|
|
|
-// oldSaveDataMap[dv.DataTime] = temp
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 待处理的数据
|
|
|
-// if val, ok := saveDataMap[dv.DataTime]; ok {
|
|
|
-// if _, ok := val[v.EdbInfoId]; !ok {
|
|
|
-// val[v.EdbInfoId] = dv.Value
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// temp := make(map[int]float64)
|
|
|
-// temp[v.EdbInfoId] = dv.Value
|
|
|
-// saveDataMap[dv.DataTime] = temp
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 以第一个指标的日期作为基准日期
|
|
|
-// if edbInfoIndex == 0 {
|
|
|
-// dateList = append(dateList, dv.DataTime)
|
|
|
-// }
|
|
|
-// }
|
|
|
-// item := new(PredictCalculateItems)
|
|
|
-// item.EdbInfoId = v.EdbInfoId
|
|
|
-// item.DataMap = dataMap
|
|
|
-// }
|
|
|
-//
|
|
|
-// //数据处理,将日期内不全的数据做补全
|
|
|
-// handleDateSaveDataMap(dateList, oldSaveDataMap, saveDataMap, edbInfoIdList)
|
|
|
-//
|
|
|
-// formulaMap := utils.CheckFormula(formulaStr)
|
|
|
-// addSql := ` INSERT INTO edb_data_predict_calculate (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
|
|
|
-// nowStr := time.Now().Format(utils.FormatDateTime)
|
|
|
-// var isAdd bool
|
|
|
-//
|
|
|
-// if !minLatestDate.IsZero() {
|
|
|
-// latestDateStr = minLatestDate.Format(utils.FormatDate)
|
|
|
-// }
|
|
|
-// for sk, sv := range saveDataMap {
|
|
|
-// formulaStr = strings.ToUpper(formulaStr)
|
|
|
-// formulaFormStr := ReplaceFormula(edbInfoIdList, sv, formulaMap, formulaStr, edbInfoIdBytes)
|
|
|
-// if formulaStr == "" {
|
|
|
-// return
|
|
|
-// }
|
|
|
-// if formulaFormStr != "" {
|
|
|
-// expression := formula.NewExpression(formulaFormStr)
|
|
|
-// calResult, tmpErr := expression.Evaluate()
|
|
|
-// if tmpErr != nil {
|
|
|
-// err = errors.New("计算失败:Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
-// fmt.Println(err)
|
|
|
-// return
|
|
|
-// }
|
|
|
-// calVal, tmpErr := calResult.Float64()
|
|
|
-// if tmpErr != nil {
|
|
|
-// err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
-// fmt.Println(err)
|
|
|
-// return
|
|
|
-// }
|
|
|
-//
|
|
|
-// //需要存入的数据
|
|
|
-// {
|
|
|
-// if latestDateStr == sk {
|
|
|
-// nanCheck := fmt.Sprintf("%0.f", calVal)
|
|
|
-// // 分母为0.0的报错
|
|
|
-// if nanCheck == "NaN" || nanCheck == "+Inf" || nanCheck == "-Inf" {
|
|
|
-// err = errors.New("计算失败 分母为0:Err: formulaStr:" + formulaFormStr)
|
|
|
-// fmt.Println(err)
|
|
|
-// return
|
|
|
-// }
|
|
|
-// latestValue, _ = decimal.NewFromFloat(calVal).Truncate(4).Float64() //保留4位小数
|
|
|
-// }
|
|
|
-// dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
|
|
|
-// timestamp := dataTime.UnixNano() / 1e6
|
|
|
-// timeStr := fmt.Sprintf("%d", timestamp)
|
|
|
-// addSql += "("
|
|
|
-// addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
|
|
|
-// "," + "'" + nowStr + "'" + "," + "'" + timeStr + "'"
|
|
|
-// addSql += "),"
|
|
|
-// isAdd = true
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// fmt.Println("formulaFormStr is empty")
|
|
|
-// }
|
|
|
-// }
|
|
|
-// if isAdd {
|
|
|
-// addSql = strings.TrimRight(addSql, ",")
|
|
|
-// _, err = o.Raw(addSql).Exec()
|
|
|
-// if err != nil {
|
|
|
-// fmt.Println("AddPredictCalculate Err:" + err.Error())
|
|
|
-// //errMsg = " tx.Exec Err :" + err.Error()
|
|
|
-// return
|
|
|
-// }
|
|
|
-// }
|
|
|
-// return
|
|
|
-//}
|
|
|
-
|
|
|
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()
|
|
|
- //if err != nil {
|
|
|
- // return
|
|
|
- //}
|
|
|
to := global.DEFAULT_DmSQL.Begin()
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
@@ -255,11 +106,6 @@ 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()
|
|
|
- //if err != nil {
|
|
|
- // return
|
|
|
- //}
|
|
|
to := global.DEFAULT_DmSQL.Begin()
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
@@ -307,14 +153,6 @@ func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTa
|
|
|
if newLatestDate.IsZero() || tmpNewLatestDate.Before(newLatestDate) {
|
|
|
newLatestDate = tmpNewLatestDate
|
|
|
}
|
|
|
- /*tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
|
|
|
- if tmpErr != nil {
|
|
|
- err = errors.New(`最近的日期格式化失败;日期:` + v.StartDate + `;err:` + tmpErr.Error())
|
|
|
- return
|
|
|
- }
|
|
|
- if maxStartDate.IsZero() || maxStartDate.Before(tmpStartDate) {
|
|
|
- maxStartDate = tmpStartDate
|
|
|
- }*/
|
|
|
|
|
|
// 获取关联指标数据
|
|
|
dataList, tmpErr := GetPredictEdbDataListAllByStartDate(v, 1, startDate)
|
|
@@ -367,17 +205,6 @@ func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTa
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- /*if edbInfoIndex == 0 {
|
|
|
- dateList = append(dateList, dv.DataTime)
|
|
|
- tmpDate, _ := time.ParseInLocation(utils.FormatDate, dv.DataTime, time.Local)
|
|
|
- if minLatestDate.IsZero() || tmpDate.After(minLatestDate) {
|
|
|
- minLatestDate = tmpDate
|
|
|
- }
|
|
|
- if maxStartDate.IsZero() || tmpDate.Before(maxStartDate) {
|
|
|
- maxStartDate = tmpDate
|
|
|
- }
|
|
|
- }*/
|
|
|
}
|
|
|
}
|
|
|
// 处理最大日期和最小日期
|
|
@@ -431,7 +258,6 @@ func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTa
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
- //fmt.Println(sk, sv)
|
|
|
// 根据时间范围,选择对应的公式
|
|
|
formulaMap := make(map[string]string)
|
|
|
formulaStr = ""
|
|
@@ -461,13 +287,9 @@ func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTa
|
|
|
formulaStr = strings.ToUpper(formulaStr)
|
|
|
formulaFormStr := ReplaceFormula(edbInfoIdList, sv, svMax, formulaMap, formulaStr, edbInfoIdBytes, maxDealFlag)
|
|
|
if formulaFormStr != "" {
|
|
|
- //utils.FileLog.Info(fmt.Sprintf("formulaFormStr:%s", formulaFormStr))
|
|
|
fmt.Println(fmt.Sprintf("date %s:formulaFormStr:%s", sk, formulaFormStr))
|
|
|
- //expression := formula.NewExpression(formulaFormStr)
|
|
|
- //calResult, tmpErr := expression.Evaluate()
|
|
|
|
|
|
calVal, err := engine.ParseAndExec(formulaFormStr)
|
|
|
- //calVal, err := calResult.Float64()
|
|
|
if err != nil {
|
|
|
// 分母为0的报错,忽略该循环
|
|
|
if utils.IsDivideZero(err) {
|
|
@@ -477,24 +299,6 @@ func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTa
|
|
|
err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
return "", 0, err
|
|
|
}
|
|
|
- //nanCheck := fmt.Sprintf("%0.f", calVal)
|
|
|
-
|
|
|
- //if tmpErr != nil {
|
|
|
- // // 分母为0的报错
|
|
|
- // if strings.Contains(err.Error(), "divide by zero") {
|
|
|
- // removeDateList = append(removeDateList, sk)
|
|
|
- // continue
|
|
|
- // }
|
|
|
- // err = errors.New("计算失败:Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
- // fmt.Println(err)
|
|
|
- // return
|
|
|
- //}
|
|
|
- //calVal, tmpErr := calResult.Float64()
|
|
|
- //if tmpErr != nil {
|
|
|
- // err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
- // fmt.Println(err)
|
|
|
- // return
|
|
|
- //}
|
|
|
nanCheck := fmt.Sprintf("%0.f", calVal)
|
|
|
// 分母为0.0的报错
|
|
|
if nanCheck == "NaN" || nanCheck == "+Inf" || nanCheck == "-Inf" {
|
|
@@ -522,7 +326,6 @@ func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTa
|
|
|
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.Exec(sql, saveValue, edbInfoId, sk).Error
|
|
|
if err != nil {
|
|
|
return "", 0, err
|
|
@@ -538,7 +341,6 @@ func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTa
|
|
|
|
|
|
if isAdd {
|
|
|
addSql = strings.TrimRight(addSql, ",")
|
|
|
- //_, err = to.Raw(addSql).Exec()
|
|
|
err = global.DEFAULT_DmSQL.Exec(addSql).Error
|
|
|
if err != nil {
|
|
|
fmt.Println("RefreshAllCalculate add Err", err.Error())
|
|
@@ -553,7 +355,6 @@ func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTa
|
|
|
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.Exec(sql, edbInfoId).Error
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
|
|
@@ -566,13 +367,11 @@ func refreshAllPredictCalculate(to *gorm.DB, edbInfoIdList []*EdbInfo, edbInfoTa
|
|
|
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).Find(&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).Find(&latestDate).Error
|
|
|
if e != nil {
|
|
|
err = fmt.Errorf("查询最新日期失败,Err:" + e.Error())
|