edb_data_ths.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "hongze/hz_crm_api/models/data_manage"
  8. "hongze/hz_crm_api/utils"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type EdbDataFromThs struct {
  14. DataVol int64 `json:"dataVol"`
  15. Errmsg string `json:"errmsg"`
  16. Errorcode int64 `json:"errorcode"`
  17. Perf interface{} `json:"perf"`
  18. Tables []struct {
  19. ID []string `json:"id"`
  20. Time []string `json:"time"`
  21. Value []float64 `json:"value"`
  22. } `json:"tables"`
  23. }
  24. func GetEdbDataByThs(edbCode, startDate, endDate string) (searchItem *data_manage.EdbInfoSearch, err error) {
  25. thsUrl := utils.Hz_Server_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
  26. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  27. utils.FileLogData.Info("thsUrl:%s", thsUrl)
  28. body, err := http.Get(thsUrl)
  29. utils.FileLogData.Info("ths result:%s", string(body))
  30. if err != nil {
  31. return
  32. }
  33. item := new(EdbDataFromThs)
  34. err = json.Unmarshal(body, &item)
  35. if err != nil {
  36. return
  37. }
  38. if item.Errorcode != 0 {
  39. err = errors.New(string(body))
  40. return
  41. }
  42. searchItem = new(data_manage.EdbInfoSearch)
  43. searchItem.EdbCode = edbCode
  44. if len(item.Tables) > 0 {
  45. table := item.Tables[0]
  46. dataLen := len(table.Time)
  47. var isAdd bool
  48. addSql := ` INSERT INTO edb_data_ths(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  49. nowStr := time.Now().Format(utils.FormatDateTime)
  50. dataList := make([]*data_manage.EdbInfoSearchData, 0)
  51. for i := 0; i < dataLen; i++ {
  52. eDate := table.Time[i]
  53. sValue := table.Value[i]
  54. dataTime, err := time.Parse(utils.FormatDate, eDate)
  55. if err != nil {
  56. return nil, err
  57. }
  58. timestamp := dataTime.UnixNano() / 1e6
  59. timeStr := fmt.Sprintf("%d", timestamp)
  60. addSql += "("
  61. addSql += "0," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + utils.SubFloatToString(sValue, 20) + "," + "'" + nowStr + "'" +
  62. "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'"
  63. addSql += "),"
  64. isAdd = true
  65. }
  66. if isAdd {
  67. addSql = strings.TrimRight(addSql, ",")
  68. err = data_manage.AddEdbDataThsBySql(addSql)
  69. if err != nil {
  70. utils.FileLogData.Info("AddEdbDataThsBySql Err:%s", err.Error())
  71. return
  72. }
  73. }
  74. size := utils.EDB_DATA_LIMIT
  75. dataList, err := data_manage.GetEdbDataThsByCode(edbCode, size)
  76. if err != nil {
  77. utils.FileLogData.Info("GetEdbDataThsByCode Err:%s", err.Error())
  78. return searchItem, err
  79. }
  80. minDate, maxDate, err := data_manage.GetEdbDataThsMaxOrMinDate(edbCode)
  81. if err != nil {
  82. return searchItem, err
  83. }
  84. searchItem.DataList = dataList
  85. searchItem.StartDate = minDate
  86. searchItem.EndDate = maxDate
  87. }
  88. if searchItem.DataList == nil {
  89. searchItem.DataList = make([]*data_manage.EdbInfoSearchData, 0)
  90. }
  91. return
  92. }
  93. func RefreshEdbDataByThs(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  94. thsUrl := utils.Hz_Server_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
  95. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  96. utils.FileLog.Info("thsUrl:%s", thsUrl)
  97. body, err := http.Get(thsUrl)
  98. fmt.Println("GetEdbDataByThs body:")
  99. fmt.Println(string(body))
  100. if err != nil {
  101. return
  102. }
  103. item := new(EdbDataFromThs)
  104. err = json.Unmarshal(body, &item)
  105. if err != nil {
  106. return
  107. }
  108. if item.Errorcode != 0 {
  109. err = errors.New(string(body))
  110. return
  111. }
  112. if len(item.Tables) > 0 {
  113. table := item.Tables[0]
  114. dataLen := len(table.Time)
  115. addSql := ` INSERT INTO edb_data_ths(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  116. nowStr := time.Now().Format(utils.FormatDateTime)
  117. var isAdd bool
  118. for i := 0; i < dataLen; i++ {
  119. eDate := table.Time[i]
  120. sValue := table.Value[i]
  121. count, err := data_manage.GetEdbDataThsByCodeAndDate(edbCode, eDate)
  122. if err != nil && err.Error() != utils.ErrNoRow() {
  123. return err
  124. }
  125. if count <= 0 {
  126. dataTime, err := time.Parse(utils.FormatDate, eDate)
  127. if err != nil {
  128. }
  129. timestamp := dataTime.UnixNano() / 1e6
  130. timeStr := fmt.Sprintf("%d", timestamp)
  131. addSql += "("
  132. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + utils.SubFloatToString(sValue, 30) + "," + "'" + nowStr + "'" +
  133. "," + "'" + nowStr + "'" + "," + "1"
  134. addSql += "," + "'" + timeStr + "'"
  135. addSql += "),"
  136. isAdd = true
  137. } else {
  138. err = data_manage.ModifyEdbDataThs(int64(edbInfoId), eDate, sValue)
  139. if err != nil {
  140. return err
  141. }
  142. }
  143. }
  144. if isAdd {
  145. addSql = strings.TrimRight(addSql, ",")
  146. err = data_manage.AddEdbDataThsBySql(addSql)
  147. if err != nil {
  148. fmt.Println("AddEdbDataThs Err", err.Error())
  149. return
  150. }
  151. }
  152. }
  153. return
  154. }