data_ths.go 2.5 KB

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