usda_fas.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_task/models/data_manage"
  6. "eta/eta_task/utils"
  7. "fmt"
  8. "time"
  9. )
  10. // UsdaFas美国农业部指标数据同步
  11. func SyncBaseFromUsdaFas() (err error) {
  12. var startDate string
  13. maxDate, err := data_manage.GetBaseFromUsdaFasIndexMaxDate()
  14. if err != nil || maxDate.IsZero() {
  15. startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
  16. } else {
  17. startDate = maxDate.Format(utils.FormatDateTime)
  18. }
  19. method := `index/list`
  20. data := make(map[string]interface{})
  21. data["Source"] = utils.DATA_SOURCE_USDA_FAS
  22. data["StartDate"] = startDate
  23. //data["EndDate"] = endDate
  24. result, err := HttpPost("SyncRankingFromUsdaFas", method, data)
  25. utils.FileLog.Info(result)
  26. fmt.Println(result)
  27. respObj := new(data_manage.UsdaFasIndexResp)
  28. err = json.Unmarshal([]byte(result), &respObj)
  29. if err != nil {
  30. utils.FileLog.Info("err:", err.Error())
  31. fmt.Println("err:", err.Error())
  32. return err
  33. }
  34. UsdaFasObj := new(data_manage.BaseFromUsdaFasIndex)
  35. for _, zv := range respObj.Data {
  36. newID, err := UsdaFasObj.InsertOrUpdateBaseFromUsdaFasIndex(zv)
  37. if err != nil {
  38. fmt.Println("InsertOrUpdateBaseFromUsdaFasIndex error:", err)
  39. }
  40. fmt.Println("InsertOrUpdateBaseFromUsdaFasIndex new indexID:", newID)
  41. }
  42. return err
  43. }
  44. // UsdaFas美国农业部-分类
  45. func SyncBaseFromUsdaFasClassify() (err error) {
  46. data := make(map[string]interface{})
  47. data["Source"] = utils.DATA_SOURCE_USDA_FAS
  48. method := `classify/list`
  49. result, err := HttpPost("SyncBaseFromUsdaFasClassify", method, data)
  50. if err != nil {
  51. fmt.Println("SyncBaseFromUsdaFasClassify HttpPost Err:", err.Error())
  52. }
  53. utils.FileLog.Info(result)
  54. fmt.Println("SyncBaseFromUsdaFasClassify result:", result)
  55. respObj := new(data_manage.UsdaFasClassifyResp)
  56. err = json.Unmarshal([]byte(result), &respObj)
  57. if err != nil {
  58. return err
  59. }
  60. //获取所有分类
  61. /* allClassify, err := data_manage.GetBaseFromUsdaFasClassifyAll()
  62. if err != nil {
  63. return
  64. }
  65. existIndexMap := make(map[int]*data_manage.BaseFromUsdaFasClassify)
  66. for _, v := range allClassify {
  67. existIndexMap[v.BaseFromUsdaFasClassifyId] = v
  68. }*/
  69. for _, item := range respObj.Data {
  70. //if _, ok := existIndexMap[item.BaseFromUsdaFasClassifyId]; !ok {
  71. newID, err := data_manage.InsertOrUpdateBaseFromUsdaFasClassify(item)
  72. if err != nil {
  73. fmt.Println("InsertOrUpdateBaseFromUsdaFasClassify error:", err)
  74. }
  75. fmt.Println("InsertOrUpdateBaseFromUsdaFasClassify new indexID:", newID)
  76. //}
  77. }
  78. return err
  79. }
  80. // UsdaFas美国农业部-数据
  81. func SyncBaseFromUsdaFasData() (err error) {
  82. startDate := time.Now().Format(utils.FormatDate) + " 00:00:00"
  83. //var startDate string
  84. maxDate, err := data_manage.GetBaseFromUsdaFasMaxDate()
  85. if err != nil || maxDate.IsZero() {
  86. startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
  87. } else {
  88. startDate = maxDate.Format(utils.FormatDateTime)
  89. }
  90. method := `index/data/list_page`
  91. existDataMap := make(map[string]*data_manage.BaseFromUsdaFasData)
  92. allData, err1 := data_manage.GetAllBaseFromUsdaFasDataList(startDate)
  93. if err1 != nil {
  94. fmt.Println("get GetAllBaseFromUsdaFasDataList err:" + err1.Error())
  95. return
  96. }
  97. for _, dv := range allData {
  98. tmpKey := dv.IndexCode + "_" + dv.DataTime
  99. existDataMap[tmpKey] = dv
  100. }
  101. //获取所有指标信息 某一天的
  102. maxPage := 1
  103. for currPage := 0; currPage < maxPage; currPage++ {
  104. data := make(map[string]interface{})
  105. data["Source"] = utils.DATA_SOURCE_USDA_FAS
  106. data["StartDate"] = startDate
  107. data["CurrPage"] = currPage
  108. data["PageSize"] = 500 //
  109. var result string
  110. result, err = HttpPost("SyncBaseFromUsdaFasData", method, data)
  111. utils.FileLog.Info(result)
  112. fmt.Println(result)
  113. respObj := new(data_manage.BaseFromUsdaFasDataResp)
  114. err = json.Unmarshal([]byte(result), &respObj)
  115. if err != nil {
  116. fmt.Println("json.Unmarshal err:" + err.Error())
  117. return err
  118. }
  119. if respObj.Ret != 200 {
  120. err = errors.New(respObj.ErrMsg)
  121. return
  122. }
  123. // 总页码数
  124. maxPage = respObj.Data.Paging.Pages
  125. addDataList := make([]*data_manage.BaseFromUsdaFasData, 0)
  126. if respObj.Data.List != nil && len(respObj.Data.List) > 0 {
  127. for _, dv := range respObj.Data.List {
  128. tmpKey := dv.IndexCode + "_" + dv.DataTime
  129. if _, ok := existDataMap[tmpKey]; !ok {
  130. addDataList = append(addDataList, dv)
  131. existDataMap[tmpKey] = dv
  132. }
  133. }
  134. }
  135. // 最后如果还有数据未插入,那么继续插入吧
  136. if len(addDataList) > 0 {
  137. _, err = data_manage.MultiAddBaseFromUsdaFasDataIndex(addDataList)
  138. if err != nil {
  139. fmt.Println("MultiAddBaseFromUsdaFasDataIndex error:", err)
  140. }
  141. }
  142. }
  143. return err
  144. }