edb_data_pb.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package data
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/http"
  6. "hongze/hz_crm_api/models/data_manage"
  7. "hongze/hz_crm_api/utils"
  8. "net/url"
  9. "strings"
  10. "time"
  11. )
  12. type EdbDataFromPb struct {
  13. Date map[string]int64 `json:"date"`
  14. Ticker map[string]string `json:"ticker"`
  15. Field map[string]string `json:"field"`
  16. Value map[string]float64 `json:"value"`
  17. }
  18. func GetEdbDataByPb(edbCode, startDate, endDate string) (searchItem *data_manage.EdbInfoSearch, err error) {
  19. defer func() {
  20. if err != nil {
  21. utils.FileLog.Info("GetEdbDataByPb Err:%s", err.Error())
  22. }
  23. }()
  24. edbCode = url.QueryEscape(edbCode)
  25. bpUrl := utils.Hz_Data_PB_Url + `edbInfo/pb?EdbCode=%s&StartDate=%s&EndDate=%s`
  26. bpUrl = fmt.Sprintf(bpUrl, edbCode, startDate, endDate)
  27. utils.FileLog.Info("bpUrl:%s", bpUrl)
  28. body, err := http.Get(bpUrl)
  29. if err != nil {
  30. utils.FileLog.Info("GetEdbDataByPb Err:%s", err.Error())
  31. return
  32. }
  33. utils.FileLog.Info("GetEdbDataByPb result:%s", string(body))
  34. item := new(EdbDataFromPb)
  35. err = json.Unmarshal(body, &item)
  36. if err != nil {
  37. utils.FileLog.Info("GetEdbDataByPb Unmarshal Err:%s", err.Error())
  38. return
  39. }
  40. edbCode, _ = url.QueryUnescape(edbCode)
  41. searchItem = new(data_manage.EdbInfoSearch)
  42. searchItem.EdbCode = edbCode
  43. if len(item.Date) > 0 {
  44. var isAdd bool
  45. dateMap := item.Date
  46. addSql := ` INSERT INTO edb_data_pb(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,ticker,field,data_timestamp) values `
  47. nowStr := time.Now().Format(utils.FormatDateTime)
  48. for k, v := range dateMap {
  49. timeStr := fmt.Sprintf("%d", v)
  50. v = v / 1000
  51. t := time.Unix(v, 0)
  52. dateTime := t.Format(utils.FormatDate)
  53. val := item.Value[k]
  54. field := item.Field[k]
  55. ticker := item.Ticker[k]
  56. if field == "PX_LAST" {
  57. addSql += "("
  58. addSql += "0," + "'" + edbCode + "'" + "," + "'" + dateTime + "'" + "," + utils.SubFloatToString(val, 20) + "," + "'" + nowStr + "'" +
  59. "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + ticker + "'" + "," + "'" + field + "'" + "," + "'" + timeStr + "'"
  60. addSql += "),"
  61. isAdd = true
  62. }
  63. }
  64. if isAdd {
  65. addSql = strings.TrimRight(addSql, ",")
  66. err = data_manage.AddEdbDataPbBySql(addSql)
  67. if err != nil {
  68. utils.FileLogData.Info("GetEdbDataByPb AddEdbDataPbBySql Err:%s", err.Error())
  69. return searchItem, err
  70. }
  71. }
  72. size := utils.EDB_DATA_LIMIT
  73. dataList, err := data_manage.GetEdbDataPbByCode(edbCode, size)
  74. if err != nil {
  75. fmt.Println("GetEdbDataWindByCode Err", err.Error())
  76. return searchItem, err
  77. }
  78. minDate, maxDate, err := data_manage.GetEdbDataPbMaxOrMinDate(edbCode)
  79. if err != nil {
  80. return searchItem, err
  81. }
  82. searchItem.DataList = dataList
  83. searchItem.StartDate = minDate
  84. searchItem.EndDate = maxDate
  85. }
  86. if searchItem.DataList == nil {
  87. searchItem.DataList = make([]*data_manage.EdbInfoSearchData, 0)
  88. }
  89. return
  90. }
  91. func RefreshEdbDataByPb(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  92. refreshEdbCode := edbCode
  93. edbCode = url.QueryEscape(edbCode)
  94. bpUrl := utils.Hz_Data_PB_Url + `edbInfo/pb?EdbCode=%s&StartDate=%s&EndDate=%s`
  95. bpUrl = fmt.Sprintf(bpUrl, edbCode, startDate, endDate)
  96. utils.FileLog.Info("bpUrl+" + bpUrl)
  97. body, err := http.Get(bpUrl)
  98. fmt.Println("RefreshEdbDataByPb body:")
  99. fmt.Println(string(body))
  100. utils.FileLog.Info("RefreshEdbDataByPb:" + string(body))
  101. if err != nil {
  102. return
  103. }
  104. item := new(EdbDataFromPb)
  105. err = json.Unmarshal(body, &item)
  106. if err != nil {
  107. return
  108. }
  109. if len(item.Date) > 0 {
  110. dateMap := item.Date
  111. pbList := make([]*data_manage.EdbDataPb, 0)
  112. for k, v := range dateMap {
  113. timestamp := v
  114. v = v / 1000
  115. t := time.Unix(v, 0)
  116. dateTime := t.Format(utils.FormatDate)
  117. val := item.Value[k]
  118. field := item.Field[k]
  119. ticker := item.Ticker[k]
  120. if field == "PX_LAST" {
  121. //判断数据是否已经存在
  122. count, err := data_manage.GetEdbDataPbByCodeAndDate(refreshEdbCode, dateTime)
  123. if err != nil && err.Error() != utils.ErrNoRow() {
  124. return err
  125. }
  126. fmt.Println("field:", field)
  127. if count <= 0 {
  128. item := new(data_manage.EdbDataPb)
  129. item.EdbCode, _ = url.QueryUnescape(edbCode)
  130. item.EdbInfoId = edbInfoId
  131. item.CreateTime = time.Now()
  132. item.ModifyTime = time.Now()
  133. item.DataTime = dateTime
  134. item.Value = val
  135. item.Status = 1
  136. item.Field = field
  137. item.Ticker = ticker
  138. item.DataTimestamp = timestamp
  139. pbList = append(pbList, item)
  140. } else {
  141. err = data_manage.ModifyEdbDataPb(edbInfoId, dateTime, val)
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. }
  147. }
  148. if len(pbList) > 0 {
  149. err = data_manage.AddEdbDataPb(pbList)
  150. if err != nil {
  151. fmt.Println("AddEdbDataPb Err", err.Error())
  152. return
  153. }
  154. }
  155. }
  156. return
  157. }