From 4ef99d83ca8b5a97357ace4053dc0d06868b62f5 Mon Sep 17 00:00:00 2001 From: cvokii Date: Tue, 9 Apr 2024 16:12:52 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E6=8F=92=E5=85=A5=E6=97=B6?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E6=8D=AE=E5=BA=93=E6=97=B6=E6=A6=82=E7=8E=87?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/tsd/internal/tdengine/device.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/tsd/internal/tdengine/device.go b/pkg/tsd/internal/tdengine/device.go index 5bf5c03..9aea862 100644 --- a/pkg/tsd/internal/tdengine/device.go +++ b/pkg/tsd/internal/tdengine/device.go @@ -10,6 +10,7 @@ import ( "github.com/gogf/gf/v2/os/gtime" "sagooiot/pkg/iotModel" "sagooiot/pkg/tsd/comm" + "sort" "time" "strings" @@ -45,7 +46,7 @@ func (m *TdEngine) InsertDeviceData(deviceKey string, data iotModel.ReportProper // getDeviceField 获取设备数据字段 func getDeviceField(data iotModel.ReportPropertyData) []string { - var field = []string{"ts"} + var field []string for k := range data { k = comm.TsdColumnName(k) @@ -53,6 +54,7 @@ func getDeviceField(data iotModel.ReportPropertyData) []string { // 属性上报时间 field = append(field, k+"_time") } + sort.Strings(field) return field } @@ -61,7 +63,17 @@ func getDeviceValue(data iotModel.ReportPropertyData) []string { //ts := time.Now().Format("Y-m-d H:i:s") //var value = []string{"'" + ts + "'"} var value []string - for _, v := range data { + + var keys []string + // 提取map中的键 + for k := range data { + keys = append(keys, k) + } + sort.Strings(keys) + + // 给key排序后,从map取值 + for _, k := range keys { + v := data[k] value = append(value, "'"+gvar.New(v.Value).String()+"'") value = append(value, "'"+gtime.New(v.CreateTime).Format("Y-m-d H:i:s")+"'") } @@ -145,7 +157,8 @@ func (m *TdEngine) BatchInsertMultiDeviceData(multiDeviceDataList map[string][]i for deviceKey, deviceData := range multiDeviceDataList { table := comm.DeviceTableName(deviceKey) - field := getDeviceField(deviceData[0]) + var field = []string{"ts"} + field = append(field, getDeviceField(deviceData[0])...) ts++