data_ths.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_task/models/data_manage"
  7. "hongze/hongze_task/utils"
  8. "rdluck_tools/http"
  9. "time"
  10. )
  11. type EdbDataFromThs struct {
  12. DataVol int64 `json:"dataVol"`
  13. Errmsg string `json:"errmsg"`
  14. Errorcode int64 `json:"errorcode"`
  15. Perf int64 `json:"perf"`
  16. Tables []struct {
  17. ID []string `json:"id"`
  18. Time []string `json:"time"`
  19. Value []float64 `json:"value"`
  20. } `json:"tables"`
  21. }
  22. func SyncEdbDataByThs(edbInfoId int64, edbCode, startDate, endDate string) (err error) {
  23. thsUrl := utils.Hz_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
  24. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  25. utils.FileLog.Info("thsUrl:%s", thsUrl)
  26. body, err := http.Get(thsUrl)
  27. fmt.Println(thsUrl)
  28. fmt.Println("GetEdbDataByThs body:")
  29. fmt.Println(string(body))
  30. if err != nil {
  31. return errors.New("SyncEdbDataByThs:"+err.Error()+"; result:"+string(body))
  32. }
  33. item := new(EdbDataFromThs)
  34. err = json.Unmarshal(body, &item)
  35. if err != nil {
  36. return errors.New("EdbDataFromThs Unmarshal:"+err.Error())
  37. }
  38. if item.Errorcode != 0 {
  39. err = errors.New(string(body))
  40. return
  41. }
  42. dataList := make([]*data_manage.EdbDataThs, 0)
  43. if len(item.Tables) > 0 {
  44. table := item.Tables[0]
  45. dataLen := len(table.Time)
  46. for i := 0; i < dataLen; i++ {
  47. eDate := table.Time[i]
  48. sValue := table.Value[i]
  49. count, err := data_manage.GetEdbDataThsCount(edbCode, eDate)
  50. if err != nil {
  51. return errors.New("GetEdbDataThsCount Err:"+err.Error())
  52. }
  53. if count > 0 {
  54. err = data_manage.ModifyEdbDataThs(edbInfoId,edbCode, eDate, sValue)
  55. if err != nil {
  56. return errors.New("ModifyEdbDataThs Err:"+err.Error())
  57. }
  58. } else {
  59. dataItem := new(data_manage.EdbDataThs)
  60. dataItem.EdbInfoId = int(edbInfoId)
  61. dataItem.EdbCode = edbCode
  62. dataItem.DataTime = eDate
  63. dataItem.Value = sValue
  64. dataItem.Status = 1
  65. dataItem.CreateTime = time.Now()
  66. dataItem.ModifyTime = time.Now()
  67. dataList = append(dataList, dataItem)
  68. }
  69. }
  70. }
  71. if len(dataList) > 0 {
  72. err = data_manage.AddEdbDataThs(dataList)
  73. if err != nil {
  74. return errors.New("AddEdbDataThs Err:"+err.Error())
  75. }
  76. }
  77. return
  78. }