data_wind.go 2.0 KB

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