Browse Source

新增指标时间过长

xyxie 2 months ago
parent
commit
effd32cf15
2 changed files with 23 additions and 5 deletions
  1. 15 5
      models/base_from_gpr_risk.go
  2. 8 0
      models/edb_data_base.go

+ 15 - 5
models/base_from_gpr_risk.go

@@ -1,6 +1,7 @@
 package models
 
 import (
+	"bytes"
 	"eta/eta_index_lib/utils"
 	"fmt"
 	"strconv"
@@ -44,14 +45,20 @@ func (obj BaseFromGprRisk) Add(edbCode string) (err error) {
 		condition += " AND index_code=? "
 		pars = append(pars, edbCode)
 	}
-	GprRiskBaseDataAll, err := GetBaseFromGprRiskDataByCondition(condition, pars)
+	gprRiskBaseDataAll, err := GetBaseFromGprRiskDataByCondition(condition, pars)
 	if err != nil && err.Error() != utils.ErrNoRow() {
 		return
 	}
 	var isAdd bool
+	//fmt.Println("开始时间", time.Now())
 	addSql := ` INSERT INTO edb_data_gpr_risk(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
-	existMap := make(map[string]string)
-	for _, sv := range GprRiskBaseDataAll {
+	length := len(gprRiskBaseDataAll)
+	existMap := make(map[string]struct{}, length)
+	nowStr := time.Now().Format(utils.FormatDateTime)
+	var buffer bytes.Buffer
+	buffer.WriteString(addSql)
+	for i := 0; i < length; i++ {
+		sv := gprRiskBaseDataAll[i]
 		eDate := sv.DataTime
 		dataTime, err := time.Parse(utils.FormatDate, eDate)
 		if err != nil {
@@ -60,12 +67,15 @@ func (obj BaseFromGprRisk) Add(edbCode string) (err error) {
 		timestamp := dataTime.UnixNano() / 1e6
 		timeStr := fmt.Sprintf("%d", timestamp)
 		if _, ok := existMap[eDate]; !ok {
-			addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
+			sqlTmp := GetAddSqlV2("0", edbCode, eDate, timeStr, sv.Value, nowStr)
+			buffer.WriteString(sqlTmp)
 			isAdd = true
 		}
-		existMap[eDate] = sv.Value
+		existMap[eDate] = struct{}{}
 	}
+	//fmt.Println("结束时间", time.Now())
 	if isAdd {
+		addSql = buffer.String()
 		addSql = strings.TrimRight(addSql, ",")
 		utils.FileLog.Info("addSql:" + addSql)
 		_, err = o.Raw(addSql).Exec()

+ 8 - 0
models/edb_data_base.go

@@ -40,6 +40,14 @@ func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string)
 	addSql += "),"
 	return
 }
+func GetAddSqlV2(edbInfoId, edbCode, dataTime, timestampStr string, value, nowStr string) (addSql string) {
+	addSql += "("
+	addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" +
+		"," + "'" + nowStr + "'"
+	addSql += "," + "'" + timestampStr + "'"
+	addSql += "),"
+	return
+}
 
 type AddEdbInfoReq struct {
 	EdbCode   string `description:"指标编码"`