package data import ( "encoding/json" "errors" "fmt" "hongze/hongze_task/models/data_manage" "hongze/hongze_task/utils" "github.com/rdlucklib/rdluck_tools/http" "strings" "time" ) type EdbDataFromThs struct { DataVol int64 `json:"dataVol"` Errmsg string `json:"errmsg"` Errorcode int64 `json:"errorcode"` Perf interface{} `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_Server_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s` thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate) utils.FileLog.Info("thsUrl:" + thsUrl) body, err := http.Get(thsUrl) if strings.Contains(string(body), "html") { err = errors.New("SyncEdbDataByThs Err:" + string(body)) return err } utils.FileLog.Info("ths result:" + string(body)) if err != nil { return errors.New("SyncEdbDataByThs:" + err.Error() + "; result:" + string(body)) } item := new(EdbDataFromThs) err = json.Unmarshal(body, &item) if err != nil { return errors.New("EdbDataFromThs Unmarshal:" + err.Error()) } 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(edbCode, eDate) if err != nil { return errors.New("GetEdbDataThsCount Err:" + err.Error()) } if count > 0 { err = data_manage.ModifyEdbDataThs(edbInfoId, edbCode, eDate, sValue) if err != nil { return errors.New("ModifyEdbDataThs Err:" + err.Error()) } } else { dataTime, err := time.Parse(utils.FormatDate, eDate) if err != nil { } timestamp := dataTime.UnixNano() / 1e6 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() dataItem.DataTimestamp = timestamp dataList = append(dataList, dataItem) } } } if len(dataList) > 0 { err = data_manage.AddEdbDataThs(dataList) if err != nil { return errors.New("AddEdbDataThs Err:" + err.Error()) } } return }