eia_steo.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_task/models/data_manage"
  5. "eta/eta_task/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // SyncEiaSteoIndex EIA STEO报告 指标
  10. func SyncEiaSteoIndex() (err error) {
  11. var startDate string
  12. maxDate, err := data_manage.GetBaseFromEiaSteoIndexMaxDate()
  13. if err != nil || maxDate.IsZero() {
  14. startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
  15. } else {
  16. startDate = maxDate.AddDate(0, 0, 1).Format(utils.FormatDate) + " 00:00:00"
  17. }
  18. method := `index/list`
  19. data := make(map[string]interface{})
  20. data["Source"] = utils.DATA_SOURCE_EIA_STEO
  21. data["StartDate"] = startDate
  22. //data["EndDate"] = endDate
  23. result, err := HttpPost("SyncEiaSteoIndex", method, data)
  24. utils.FileLog.Info(result)
  25. fmt.Println(result)
  26. respObj := new(data_manage.EiaSteoIndexResp)
  27. err = json.Unmarshal([]byte(result), &respObj)
  28. if err != nil {
  29. fmt.Println("json.Unmarshal err:" + err.Error())
  30. return err
  31. }
  32. //获取所有指标信息 某一天的
  33. /*allIndex, err := data_manage.GetBaseFromEiaSteoIndexAll(startDate)
  34. if err != nil {
  35. fmt.Println("get GetBaseFromEiaSteoIndexAll err:" + err.Error())
  36. return
  37. }
  38. existIndexMap := make(map[string]*data_manage.BaseFromEiaSteoIndex)
  39. for _, v := range allIndex {
  40. existIndexMap[v.IndexCode] = v
  41. }*/
  42. for _, zv := range respObj.Data {
  43. //if _, ok := existIndexMap[zv.IndexCode]; !ok {
  44. newID, err := data_manage.InsertOrUpdateBaseFromEiaSteoIndex(zv)
  45. if err != nil {
  46. fmt.Println("InsertOrUpdateBaseFromEiaSteoIndex error:", err)
  47. }
  48. fmt.Println("InsertOrUpdateBaseFromEiaSteoIndex new indexID:", newID)
  49. //}
  50. }
  51. return err
  52. }
  53. // SyncEiaSteoIndexData EIA STEO报告 指标
  54. func SyncEiaSteoIndexData() (err error) {
  55. startDate := time.Now().Format(utils.FormatDate) + " 00:00:00"
  56. method := `index/data/list`
  57. //获取所有指标信息 某一天的
  58. allIndex, err := data_manage.GetBaseFromEiaSteoIndexAll(startDate)
  59. if err != nil {
  60. fmt.Println("get GetBaseFromEiaSteoIndexAll err:" + err.Error())
  61. return
  62. }
  63. for _, zv := range allIndex {
  64. data := make(map[string]interface{})
  65. data["Source"] = utils.DATA_SOURCE_EIA_STEO
  66. data["StartDate"] = startDate
  67. data["IndexCode"] = zv.IndexCode
  68. var result string
  69. result, err = HttpPost("SyncEiaSteoIndexData", method, data)
  70. utils.FileLog.Info(result)
  71. fmt.Println(result)
  72. respObj := new(data_manage.EiaSteoIndexDataResp)
  73. err = json.Unmarshal([]byte(result), &respObj)
  74. if err != nil {
  75. fmt.Println("json.Unmarshal err:" + err.Error())
  76. return err
  77. }
  78. existDataMap := make(map[int]*data_manage.BaseFromEiaSteoData)
  79. allData, err1 := data_manage.GetBaseFromEiaSteoIndexDataAll(startDate)
  80. if err1 != nil {
  81. fmt.Println("get GetBaseFromEiaSteoIndexDataAll err:" + err1.Error())
  82. return
  83. }
  84. for _, dv := range allData {
  85. existDataMap[dv.BaseFromEiaSteoDataId] = dv
  86. }
  87. for _, dv := range respObj.Data {
  88. if _, ok := existDataMap[dv.BaseFromEiaSteoDataId]; !ok {
  89. newID, err := data_manage.AddBaseFromEiaSteoData(dv)
  90. if err != nil {
  91. fmt.Println("insert error:", err)
  92. }
  93. fmt.Println("insert new indexID:", newID)
  94. }
  95. }
  96. }
  97. return err
  98. }