소스 검색

Merge branch 'hotfix/bug7031_predict_edb' into debug

xyxie 3 달 전
부모
커밋
3080a028a3
5개의 변경된 파일76개의 추가작업 그리고 3개의 파일을 삭제
  1. 14 0
      controllers/base_from_rzd.go
  2. 2 2
      logic/predict_edb.go
  3. 51 0
      models/base_from_rzd_data.go
  4. 1 0
      models/edb_info.go
  5. 8 1
      models/predict_edb_info_rule.go

+ 14 - 0
controllers/base_from_rzd.go

@@ -262,6 +262,20 @@ func (this *BaseFromRzdController) AddBatchRzdData() {
 		br.ErrMsg = "新增指标数据失败,Err:" + err.Error()
 		return
 	}
+
+	// 维护start_date,end_date,latest_value字段
+	var indexCodes []string
+	for _, data := range req {
+		indexCodes = append(indexCodes, data.IndexCode)
+	}
+	mapInfo, err := models.GetRzdDataMaxAndMinDateByIndexCodes(indexCodes)
+	if err != nil {
+		return
+	}
+	for indexCode, info := range mapInfo {
+		_ = models.UpdateRzdEdbDataByIndexCodes(indexCode, info)
+	}
+
 	// 同步新增指标库数据
 	// 判断是否存在于指标库
 	// 获取指标详情

+ 2 - 2
logic/predict_edb.go

@@ -172,7 +172,7 @@ func AddPredictEdbInfo(sourceEdbInfoId, classifyId int, edbName, dataDateType st
 			return
 		}
 
-		if v.RuleType == 16 && endDateType != 1 {
+		if v.RuleType == 16 && endDateType == 1 {
 			errMsg = "年度值倒推不支持截止期数"
 			err = errors.New(errMsg)
 			return
@@ -577,7 +577,7 @@ func EditPredictEdbInfo(edbInfoId, classifyId int, edbName, dataDateType string,
 			err = errors.New(errMsg)
 			return
 		}
-		if v.RuleType == 16 && endDateType != 1 {
+		if v.RuleType == 16 && endDateType == 1 {
 			errMsg = "年度值倒推不支持截止期数"
 			err = errors.New(errMsg)
 			return

+ 51 - 0
models/base_from_rzd_data.go

@@ -3,6 +3,7 @@ package models
 
 import (
 	"errors"
+	"eta/eta_index_lib/utils"
 	"github.com/beego/beego/v2/client/orm"
 )
 
@@ -69,3 +70,53 @@ func UpdateRzdData(item *BaseFromRzdData) (err error) {
 	_, err = o.Update(item)
 	return
 }
+
+// GetRzdDataMaxAndMinDateByIndexCodes 查询数据时间最新值,数据时间最小值和最新值对应的value值
+func GetRzdDataMaxAndMinDateByIndexCodes(indexCodes []string) (resultMap map[string]*EdbInfoMaxAndMinInfo, err error) {
+	resultMap = make(map[string]*EdbInfoMaxAndMinInfo)
+
+	o := orm.NewOrm()
+	sql := ` SELECT index_code, MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_rzd_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code`
+	var items []EdbInfoMaxAndMinInfo
+	_, err = o.Raw(sql, indexCodes).QueryRows(&items)
+	if err != nil {
+		return nil, err
+	}
+
+	for _, row := range items {
+		resultMap[row.IndexCode] = &EdbInfoMaxAndMinInfo{
+			MinDate:  row.MinDate,
+			MaxDate:  row.MaxDate,
+			MinValue: row.MinValue,
+			MaxValue: row.MaxValue,
+		}
+	}
+
+	// 查询最新的值(最新数据时间对应的 value)
+	sql = `SELECT a.index_code, a.value AS latest_value
+        FROM base_from_rzd_data a
+        JOIN (
+            SELECT index_code, MAX(data_time) AS latest_time
+            FROM base_from_rzd_data
+            WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `)
+            GROUP BY index_code
+        ) b ON a.index_code = b.index_code AND a.data_time = b.latest_time`
+	_, err = o.Raw(sql, indexCodes).QueryRows(&items)
+	if err != nil {
+		return nil, err
+	}
+	for _, row := range items {
+		if _, ok := resultMap[row.IndexCode]; ok {
+			resultMap[row.IndexCode].LatestValue = row.LatestValue
+		}
+	}
+
+	return resultMap, nil
+}
+
+func UpdateRzdEdbDataByIndexCodes(indexCode string, info *EdbInfoMaxAndMinInfo) error {
+	o := orm.NewOrm()
+	sql := `UPDATE base_from_rzd_index SET start_date=?, end_date=?, latest_value=? WHERE index_code=?`
+	_, err := o.Raw(sql, info.MinDate, info.MaxDate, info.LatestValue, indexCode).Exec()
+	return err
+}

+ 1 - 0
models/edb_info.go

@@ -410,6 +410,7 @@ type EdbInfoMaxAndMinInfo struct {
 	LatestValue float64 `description:"最新值" bson:"latest_value"`
 	LatestDate  string  `description:"实际数据最新日期" bson:"latest_date"`
 	EndValue    float64 `description:"最新值" bson:"end_value"`
+	IndexCode   string  `description:"指标编码" bson:"index_code"`
 }
 
 // GetEdbInfoMaxAndMinInfo 获取指标的最新数据记录信息

+ 8 - 1
models/predict_edb_info_rule.go

@@ -1656,6 +1656,10 @@ func GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(edbInfoId int, con
 		//兼容历史数据
 		yearList = append(yearList, annualValueInversionConf.Year)
 	}
+	if len(yearList) == 0 {
+		err = errors.New("同比年份不能为空")
+		return
+	}
 	// 每年截止到当前日期的累计值
 	dateTotalMap := make(map[time.Time]float64)
 
@@ -1757,7 +1761,10 @@ func GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(edbInfoId int, con
 	avg = sum / float64(len(yearList))
 	//fmt.Printf("同比年份的余额%.4f\n", avg)
 	// 同比增速=当年余额/同比年份上一期日期的余额
-	tbVal := decimal.NewFromFloat(currYearBalance).Div(decimal.NewFromFloat(avg))
+	tbVal := decimal.NewFromFloat(0)
+	if avg != 0 {
+		tbVal = decimal.NewFromFloat(currYearBalance).Div(decimal.NewFromFloat(avg))
+	}
 	/*tbVal11, _ := tbVal.Round(4).Float64()
 	fmt.Printf("同比增速%.4f\n", tbVal11)*/
 	//(同比增速=余额/同比年份相应日期的余额的平均值,预测值等于同比年份同期值*同比增速);