gpr_risk.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. // UsdaFasGPR地缘风险指数指标数据同步
  11. func SyncBaseFromGprRisk() (err error) {
  12. var startDate string
  13. maxDate, err := data_manage.GetBaseFromGprRiskIndexMaxDate()
  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("SyncRankingFromGprRisk", method, data)
  25. utils.FileLog.Info(result)
  26. fmt.Println(result)
  27. respObj := new(data_manage.GprRiskIndexResp)
  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. GprRiskObj := new(data_manage.BaseFromGprRiskIndex)
  35. for _, zv := range respObj.Data {
  36. newID, err := GprRiskObj.InsertOrUpdateBaseFromGprRiskIndex(zv)
  37. if err != nil {
  38. fmt.Println("InsertOrUpdateBaseFromGprRiskIndex error:", err)
  39. }
  40. fmt.Println("InsertOrUpdateBaseFromGprRiskIndex new indexID:", newID)
  41. }
  42. return err
  43. }
  44. // GprRiskGPR地缘风险指数-分类
  45. func SyncBaseFromGprRiskClassify() (err error) {
  46. data := make(map[string]interface{})
  47. data["Source"] = utils.DATA_SOURCE_USDA_FAS
  48. method := `classify/list`
  49. result, err := HttpPost("SyncBaseFromGprRiskClassify", method, data)
  50. if err != nil {
  51. fmt.Println("SyncBaseFromGprRiskClassify HttpPost Err:", err.Error())
  52. }
  53. utils.FileLog.Info(result)
  54. fmt.Println("SyncBaseFromGprRiskClassify result:", result)
  55. respObj := new(data_manage.GprRiskClassifyResp)
  56. err = json.Unmarshal([]byte(result), &respObj)
  57. if err != nil {
  58. return err
  59. }
  60. //获取所有分类
  61. /* allClassify, err := data_manage.GetBaseFromGprRiskClassifyAll()
  62. if err != nil {
  63. return
  64. }
  65. existIndexMap := make(map[int]*data_manage.BaseFromGprRiskClassify)
  66. for _, v := range allClassify {
  67. existIndexMap[v.BaseFromGprRiskClassifyId] = v
  68. }*/
  69. for _, item := range respObj.Data {
  70. //if _, ok := existIndexMap[item.BaseFromGprRiskClassifyId]; !ok {
  71. newID, err := data_manage.InsertOrUpdateBaseFromGprRiskClassify(item)
  72. if err != nil {
  73. fmt.Println("InsertOrUpdateBaseFromGprRiskClassify error:", err)
  74. }
  75. fmt.Println("InsertOrUpdateBaseFromGprRiskClassify new indexID:", newID)
  76. //}
  77. }
  78. return err
  79. }
  80. // GprRiskGPR地缘风险指数-数据
  81. func SyncBaseFromGprRiskData() (err error) {
  82. startDate := time.Now().Format(utils.FormatDate) + " 00:00:00"
  83. //var startDate string
  84. maxDate, err := data_manage.GetBaseFromGprRiskMaxDate()
  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.BaseFromGprRiskData)
  92. allData, err1 := data_manage.GetAllBaseFromGprRiskDataList(startDate)
  93. if err1 != nil {
  94. fmt.Println("get GetAllBaseFromGprRiskDataList 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("SyncBaseFromGprRiskData", method, data)
  111. utils.FileLog.Info(result)
  112. fmt.Println(result)
  113. respObj := new(data_manage.BaseFromGprRiskDataResp)
  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.BaseFromGprRiskData, 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.MultiAddBaseFromGprRiskDataIndex(addDataList)
  138. if err != nil {
  139. fmt.Println("MultiAddBaseFromGprRiskDataIndex error:", err)
  140. }
  141. }
  142. }
  143. return err
  144. }