Browse Source

Merge branch 'bugfix_3745' into debug

zwxi 1 year ago
parent
commit
82b78652fc
2 changed files with 31 additions and 3 deletions
  1. 30 2
      models/data_manage/edb_data_insert_config.go
  2. 1 1
      models/data_manage/edb_info.go

+ 30 - 2
models/data_manage/edb_data_insert_config.go

@@ -5,6 +5,7 @@ import (
 	"eta/eta_api/utils"
 	"fmt"
 	"github.com/beego/beego/v2/client/orm"
+	"strconv"
 	"time"
 )
 
@@ -40,7 +41,7 @@ func GetEdbDataInsertConfigByEdbIdList(edbInfoIdList []int) (items []*EdbDataIns
 }
 
 // CreateEdbDataInsertConfigAndData 创建数据插入配置规则,及插入数据
-func CreateEdbDataInsertConfigAndData(edbInfo *EdbInfo, date time.Time, value float64) (err error, errMsg string, isSendEmail bool) {
+func CreateEdbDataInsertConfigAndData(edbInfo *EdbInfo, date time.Time, value string) (err error, errMsg string, isSendEmail bool) {
 	isSendEmail = true
 
 	tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
@@ -52,7 +53,17 @@ func CreateEdbDataInsertConfigAndData(edbInfo *EdbInfo, date time.Time, value fl
 	dateStr := date.Format(utils.FormatDate)
 	timestamp := date.UnixNano() / 1e6
 	timeStr := fmt.Sprintf("%d", timestamp)
-	saveValue := utils.SubFloatToString(value, 30)
+
+	var saveValue string
+	if value != "" {
+		floatValue, err := strconv.ParseFloat(value, 64)
+		if err != nil {
+			fmt.Println("转换失败:", err)
+			return
+		}
+		saveValue = utils.SubFloatToString(floatValue, 30)
+	}
+
 
 	to, err := orm.NewOrmUsingDB("data").Begin()
 	if err != nil {
@@ -120,6 +131,7 @@ func CreateEdbDataInsertConfigAndData(edbInfo *EdbInfo, date time.Time, value fl
 		}
 	}
 
+	var offsetDate string
 	// 更改数据
 	{
 		var edbDateData *EdbDataList
@@ -131,6 +143,14 @@ func CreateEdbDataInsertConfigAndData(edbInfo *EdbInfo, date time.Time, value fl
 				return
 			}
 			err = nil
+
+			sql = `SELECT data_time FROM %s WHERE edb_info_id=? ORDER BY data_time DESC LIMIT 1 OFFSET 1`
+			sql = fmt.Sprintf(sql, tableName)
+			err = to.Raw(sql, edbInfo.EdbInfoId).QueryRow(&offsetDate)
+			if err != nil && err.Error() != utils.ErrNoRow() {
+				return
+			}
+			err = nil
 		}
 
 		// 如果是没有历史数据,那么就需要增加数据
@@ -138,6 +158,11 @@ func CreateEdbDataInsertConfigAndData(edbInfo *EdbInfo, date time.Time, value fl
 			addSql := ` INSERT INTO %s (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) VALUES ( %d, "%s", "%s", "%s", now(), now(), %s) `
 			addSql = fmt.Sprintf(addSql, tableName, edbInfo.EdbInfoId, edbInfo.EdbCode, dateStr, saveValue, timeStr)
 			_, err = to.Raw(addSql).Exec()
+		} else if value == "" {
+			// 传空值时删除
+			deleteSql := `DELETE FROM %s WHERE edb_data_id = %d;`
+			deleteSql = fmt.Sprintf(deleteSql, tableName, edbDateData.EdbDataId)
+			_, err = to.Raw(deleteSql).Exec()
 		} else {
 			updateSql := `UPDATE %s SET data_time = "%s", value = "%s", modify_time= now(), data_timestamp= %s WHERE edb_data_id = %d;`
 			updateSql = fmt.Sprintf(updateSql, tableName, dateStr, saveValue, timeStr, edbDateData.EdbDataId)
@@ -149,6 +174,9 @@ func CreateEdbDataInsertConfigAndData(edbInfo *EdbInfo, date time.Time, value fl
 	}
 
 	// 更改指标的数据结束日期
+	if value == "" {
+		dateStr = offsetDate
+	}
 	edbInfo.EndDate = dateStr
 	_, err = to.Update(edbInfo, "EndDate")
 

+ 1 - 1
models/data_manage/edb_info.go

@@ -1589,7 +1589,7 @@ type EdbInfoDataSeasonalResp struct {
 type SetEdbDataInsertConfigReq struct {
 	EdbInfoId int     `description:"指标ID"`
 	Date      string  `description:"日期"`
-	Value     float64 `description:"值"`
+	Value     string `description:"值"`
 }
 
 // GetEdbInfoByClassifyId 用于分类展示