package data_manage import ( "encoding/json" "errors" "fmt" "hongze/hongze_task/models/data_manage" "hongze/hongze_task/utils" "rdluck_tools/http" "time" ) type EdbDataFromThs struct { DataVol int64 `json:"dataVol"` Errmsg string `json:"errmsg"` Errorcode int64 `json:"errorcode"` Perf int64 `json:"perf"` Tables []struct { ID []string `json:"id"` Time []string `json:"time"` Value []float64 `json:"value"` } `json:"tables"` } func SyncEdbDataByThs(edbInfoId int64, edbCode, startDate, endDate string) (err error) { thsUrl := utils.Hz_Data_Url + `edbInfo/ths/?EdbCode=%s&StartDate=%s&EndDate=%s` thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate) utils.FileLog.Info("thsUrl:%s", thsUrl) body, err := http.Get(thsUrl) fmt.Println("GetEdbDataByThs body:") fmt.Println(string(body)) if err != nil { return } item := new(EdbDataFromThs) err = json.Unmarshal(body, &item) if err != nil { return } if item.Errorcode != 0 { err = errors.New(string(body)) return } dataList := make([]*data_manage.EdbDataThs, 0) if len(item.Tables) > 0 { table := item.Tables[0] dataLen := len(table.Time) for i := 0; i < dataLen; i++ { eDate := table.Time[i] sValue := table.Value[i] count, err := data_manage.GetEdbDataThsCount(edbInfoId, eDate) if err != nil { return err } if count > 0 { err = data_manage.ModifyEdbDataThs(edbInfoId, eDate, sValue) if err != nil { return } } else { dataItem := new(data_manage.EdbDataThs) dataItem.EdbInfoId = int(edbInfoId) dataItem.EdbCode = edbCode dataItem.DataTime = eDate dataItem.Value = sValue dataItem.Status = 1 dataItem.CreateTime = time.Now() dataItem.ModifyTime = time.Now() dataList = append(dataList, dataItem) } } } if len(dataList) > 0 { err = data_manage.AddEdbDataThs(dataList) if err != nil { return } } return }