data_wind.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package data
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_task/models/data_manage"
  6. "hongze/hongze_task/utils"
  7. "rdluck_tools/http"
  8. "time"
  9. )
  10. type EdbDataFromWind struct {
  11. Close map[string]float64 `json:"CLOSE"`
  12. Dt map[string]int64 `json:"DT"`
  13. }
  14. //万得
  15. func SyncEdbDataByWind(edbInfoId int64, edbCode, startDate, endDate string) (err error) {
  16. defer func() {
  17. if err != nil {
  18. fmt.Println("GetEdbDataByWind Err:" + err.Error())
  19. }
  20. }()
  21. windUrl := utils.Hz_Data_Url + `edbInfo/wind?EdbCode=%s&StartDate=%s&EndDate=%s`
  22. windUrl = fmt.Sprintf(windUrl, edbCode, startDate, endDate)
  23. utils.FileLog.Info("windUrl:", windUrl)
  24. body, err := http.Get(windUrl)
  25. fmt.Println(windUrl)
  26. fmt.Println("SyncEdbDataByWind body:")
  27. fmt.Println(string(body))
  28. utils.FileLog.Info("wind result:", string(body))
  29. if err != nil {
  30. return
  31. }
  32. item := new(EdbDataFromWind)
  33. err = json.Unmarshal(body, &item)
  34. if err != nil {
  35. return
  36. }
  37. windList := make([]*data_manage.EdbDataWind, 0)
  38. for k, v := range item.Dt {
  39. dataTimestamp := v
  40. v = v / 1000
  41. fmt.Println(k, v)
  42. t := time.Unix(v, 0)
  43. dateTime := t.Format(utils.FormatDate)
  44. fmt.Println(dateTime)
  45. val := item.Close[k]
  46. count, err := data_manage.GetEdbDataWindCount(edbCode, dateTime)
  47. if err != nil {
  48. return err
  49. }
  50. if count > 0 {
  51. err = data_manage.ModifyEdbDataWind(edbInfoId, edbCode, dateTime, val)
  52. if err != nil {
  53. return err
  54. }
  55. } else {
  56. item := new(data_manage.EdbDataWind)
  57. item.EdbCode = edbCode
  58. item.EdbInfoId = int(edbInfoId)
  59. item.CreateTime = time.Now()
  60. item.ModifyTime = time.Now()
  61. item.DataTime = dateTime
  62. item.Value = val
  63. item.Status = 1
  64. item.DataTimestamp = dataTimestamp
  65. windList = append(windList, item)
  66. }
  67. }
  68. if len(windList) > 0 {
  69. err = data_manage.AddEdbDataWind(windList)
  70. if err != nil {
  71. return
  72. }
  73. }
  74. return
  75. }