data_pb.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package data
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_task/models/data_manage"
  6. "hongze/hongze_task/utils"
  7. "net/url"
  8. "github.com/rdlucklib/rdluck_tools/http"
  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. //万得
  19. func SyncEdbDataByPb(edbInfoId int64, edbCode, startDate, endDate string) (err error) {
  20. defer func() {
  21. if err != nil {
  22. fmt.Println("SyncEdbDataByPb Err:" + err.Error())
  23. }
  24. }()
  25. addEdbCode := edbCode
  26. edbCode = url.QueryEscape(edbCode)
  27. pbUrl := utils.Hz_Data_PB_Url + `edbInfo/pb?EdbCode=%s&StartDate=%s&EndDate=%s`
  28. pbUrl = fmt.Sprintf(pbUrl, edbCode, startDate, endDate)
  29. utils.FileLog.Info("pbUrl:" + pbUrl)
  30. body, err := http.Get(pbUrl)
  31. utils.FileLog.Info("pb result:" + string(body))
  32. if strings.Contains(string(body), "html") {
  33. return nil
  34. }
  35. if err != nil {
  36. return
  37. }
  38. item := new(EdbDataFromPb)
  39. err = json.Unmarshal(body, &item)
  40. if err != nil {
  41. return
  42. }
  43. pbList := make([]*data_manage.EdbDataPb, 0)
  44. if len(item.Date) > 0 {
  45. dateMap := item.Date
  46. for k, v := range dateMap {
  47. dataTimestamp := v
  48. v = v / 1000
  49. t := time.Unix(v, 0)
  50. dateTime := t.Format(utils.FormatDate)
  51. val := item.Value[k]
  52. field := item.Field[k]
  53. ticker := item.Ticker[k]
  54. if field == "PX_LAST" {
  55. count, err := data_manage.GetEdbDataPbCount(addEdbCode, dateTime)
  56. if err != nil {
  57. return err
  58. }
  59. if count > 0 {
  60. err = data_manage.ModifyEdbDataPb(edbInfoId, addEdbCode, dateTime, val)
  61. if err != nil {
  62. return err
  63. }
  64. } else {
  65. item := new(data_manage.EdbDataPb)
  66. item.EdbCode = addEdbCode
  67. item.EdbInfoId = int(edbInfoId)
  68. item.CreateTime = time.Now()
  69. item.ModifyTime = time.Now()
  70. item.DataTime = dateTime
  71. item.Value = val
  72. item.Status = 1
  73. item.Field = field
  74. item.Ticker = ticker
  75. item.DataTimestamp = dataTimestamp
  76. pbList = append(pbList, item)
  77. }
  78. }
  79. }
  80. }
  81. if len(pbList) > 0 {
  82. err = data_manage.AddEdbDataPb(pbList)
  83. if err != nil {
  84. return
  85. }
  86. }
  87. return
  88. }