Ver Fonte

Merge branch 'fix/2022-08-10' into debug

Roc há 2 anos atrás
pai
commit
ea8eb5fc2f
2 ficheiros alterados com 75 adições e 10 exclusões
  1. 8 6
      models/base_from_google_travel.go
  2. 67 4
      services/base_from_ths.go

+ 8 - 6
models/base_from_google_travel.go

@@ -10,7 +10,7 @@ import (
 )
 )
 
 
 type BaseFromChangesVisitorsCovid struct {
 type BaseFromChangesVisitorsCovid struct {
-	Id                  int `orm:"column(id);pk"`
+	Id                  int    `orm:"column(id);pk"`
 	Entity              string `description:"国家"`
 	Entity              string `description:"国家"`
 	Code                string `description:"国家编码"`
 	Code                string `description:"国家编码"`
 	EdbCode             string `description:"指标ID"`
 	EdbCode             string `description:"指标ID"`
@@ -34,12 +34,12 @@ func GetBaseFromChangesVisitorsCovidDataAllByIndexCode(indexCode string) (list [
 }
 }
 
 
 func GetBaseFromChangesVisitorsCovidDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromChangesVisitorsCovid, err error) {
 func GetBaseFromChangesVisitorsCovidDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromChangesVisitorsCovid, err error) {
-	sql := ` SELECT * FROM base_from_trade_dalian_index WHERE 1=1 `
+	sql := ` SELECT * FROM base_from_changes_visitors_covid WHERE 1=1 `
 	o := orm.NewOrm()
 	o := orm.NewOrm()
 	if condition != "" {
 	if condition != "" {
 		sql += condition
 		sql += condition
 	}
 	}
-	sql += ` ORDER BY data_time DESC `
+	sql += ` ORDER BY day DESC `
 	_, err = o.Raw(sql, pars).QueryRows(&item)
 	_, err = o.Raw(sql, pars).QueryRows(&item)
 	return
 	return
 }
 }
@@ -61,7 +61,7 @@ func AddEdbDataGoogleTravel(edbCode string) (err error) {
 		dataTime, err := time.Parse(utils.FormatDate, eDate)
 		dataTime, err := time.Parse(utils.FormatDate, eDate)
 		if err != nil {
 		if err != nil {
 			fmt.Println("time.Parse Err:" + eDate)
 			fmt.Println("time.Parse Err:" + eDate)
-			return  err
+			return err
 		}
 		}
 		timestamp := dataTime.UnixNano() / 1e6
 		timestamp := dataTime.UnixNano() / 1e6
 		timeStr := fmt.Sprintf("%d", timestamp)
 		timeStr := fmt.Sprintf("%d", timestamp)
@@ -102,7 +102,9 @@ func RefreshEdbDataGoogleTravel(edbInfoId int, edbCode, startDate string) (err e
 	}
 	}
 
 
 	dataList, err := GetBaseFromChangesVisitorsCovidDataByTradeCode(condition, pars)
 	dataList, err := GetBaseFromChangesVisitorsCovidDataByTradeCode(condition, pars)
-
+	if err != nil {
+		return err
+	}
 	//获取指标所有数据
 	//获取指标所有数据
 	var existCondition string
 	var existCondition string
 	var existPars []interface{}
 	var existPars []interface{}
@@ -163,4 +165,4 @@ func RefreshEdbDataGoogleTravel(edbInfoId int, edbCode, startDate string) (err e
 		}
 		}
 	}
 	}
 	return
 	return
-}
+}

+ 67 - 4
services/base_from_ths.go

@@ -5,18 +5,36 @@ import (
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
 	"github.com/rdlucklib/rdluck_tools/http"
 	"github.com/rdlucklib/rdluck_tools/http"
+	"github.com/shopspring/decimal"
 	"hongze/hongze_edb_lib/utils"
 	"hongze/hongze_edb_lib/utils"
+	"reflect"
 )
 )
 
 
 type EdbDataFromThs struct {
 type EdbDataFromThs struct {
+	DataVol   int64       `json:"dataVol"`
+	Errmsg    string      `json:"errmsg"`
+	Errorcode int64       `json:"errorcode"`
+	Perf      interface{} `json:"perf"`
+	Tables    []Tables    `json:"tables"`
+}
+
+// Tables 表格数据
+type Tables struct {
+	ID    []string  `json:"id"`
+	Time  []string  `json:"time"`
+	Value []float64 `json:"value"`
+}
+
+// EdbDataFromThsInterface 数据类型转为interface
+type EdbDataFromThsInterface struct {
 	DataVol   int64       `json:"dataVol"`
 	DataVol   int64       `json:"dataVol"`
 	Errmsg    string      `json:"errmsg"`
 	Errmsg    string      `json:"errmsg"`
 	Errorcode int64       `json:"errorcode"`
 	Errorcode int64       `json:"errorcode"`
 	Perf      interface{} `json:"perf"`
 	Perf      interface{} `json:"perf"`
 	Tables    []struct {
 	Tables    []struct {
-		ID    []string  `json:"id"`
-		Time  []string  `json:"time"`
-		Value []float64 `json:"value"`
+		ID    []string      `json:"id"`
+		Time  []string      `json:"time"`
+		Value []interface{} `json:"value"`
 	} `json:"tables"`
 	} `json:"tables"`
 }
 }
 
 
@@ -35,7 +53,8 @@ func getEdbDataFromThs(edbCode, startDate, endDate string, num int) (item *EdbDa
 		err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
 		err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
 		return
 		return
 	}
 	}
-	item = new(EdbDataFromThs)
+	items := new(EdbDataFromThsInterface)
+
 	err = json.Unmarshal(body, &item)
 	err = json.Unmarshal(body, &item)
 	if err != nil {
 	if err != nil {
 		err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
 		err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
@@ -52,5 +71,49 @@ func getEdbDataFromThs(edbCode, startDate, endDate string, num int) (item *EdbDa
 		err = errors.New(string(body))
 		err = errors.New(string(body))
 		return
 		return
 	}
 	}
+
+	// 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
+	tablesList := make([]Tables, 0)
+	for _, table := range items.Tables {
+		tableIdList := make([]string, 0)
+		tableTimeList := make([]string, 0)
+		tableValueList := make([]float64, 0)
+		for _, tableId := range table.ID {
+			tableTimeList = append(tableTimeList, tableId)
+		}
+		for _, tableTime := range table.Time {
+			tableIdList = append(tableIdList, tableTime)
+		}
+		for _, tmpValue := range table.Time {
+			var tableValue float64
+			if reflect.TypeOf(tmpValue).Kind() == reflect.Float64 {
+				tableValue = reflect.ValueOf(tmpValue).Float()
+			} else if reflect.TypeOf(tmpValue).Kind() == reflect.String {
+				tmpTableValue, tmpErr := decimal.NewFromString(reflect.ValueOf(tmpValue).String())
+				if tmpErr != nil {
+					err = tmpErr
+					return
+				}
+				tableValue, _ = tmpTableValue.Float64()
+			} else {
+				err = errors.New("错误的数据类型" + reflect.TypeOf(tmpValue).String())
+				return
+			}
+			tableValueList = append(tableValueList, tableValue)
+		}
+		tmpTable := Tables{
+			ID:    tableIdList,
+			Time:  tableTimeList,
+			Value: tableValueList,
+		}
+		tablesList = append(tablesList, tmpTable)
+	}
+	item = &EdbDataFromThs{
+		DataVol:   items.DataVol,
+		Errmsg:    items.Errmsg,
+		Errorcode: items.Errorcode,
+		Perf:      items.Perf,
+		Tables:    tablesList,
+	}
 	return item, nil
 	return item, nil
 }
 }