com_trade.go 4.5 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. // SyncComTradeIndex 同步UN指标
  11. func SyncComTradeIndex() (err error) {
  12. var startDate string
  13. maxDate, err := data_manage.GetBaseFromComTradeMaxDate()
  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.AddDate(0, 0, 1).Format(utils.FormatDate) + " 00:00:00"
  18. }
  19. method := `index/list`
  20. data := make(map[string]interface{})
  21. data["Source"] = utils.DATA_SOURCE_COM_TRADE
  22. data["StartDate"] = startDate
  23. //data["EndDate"] = endDate
  24. result, err := HttpPost("SyncComTradeIndex", method, data)
  25. utils.FileLog.Info(result)
  26. fmt.Println(result)
  27. respObj := new(data_manage.ComTradeIndexMappingResp)
  28. err = json.Unmarshal([]byte(result), &respObj)
  29. if err != nil {
  30. fmt.Println("json.Unmarshal err:" + err.Error())
  31. return err
  32. }
  33. // 指标处理
  34. {
  35. //获取所有指标信息 某一天的
  36. allComTradeIndex, tmpErr := data_manage.GetAllComTradeIndexList(startDate)
  37. if tmpErr != nil {
  38. fmt.Println("get GetAllComTradeCodeMappingList err:" + tmpErr.Error())
  39. return
  40. }
  41. existCodeMappingMap := make(map[string]*data_manage.ComTradeIndex)
  42. for _, v := range allComTradeIndex {
  43. existCodeMappingMap[v.IndexCode] = v
  44. }
  45. for _, zv := range respObj.Data.IndexList {
  46. if _, ok := existCodeMappingMap[zv.IndexCode]; !ok {
  47. newID, tmpErr := data_manage.AddBaseFromComTradeIndex(&zv)
  48. if tmpErr != nil {
  49. fmt.Println("insert error:", tmpErr)
  50. }
  51. fmt.Println("insert new indexID:", newID)
  52. }
  53. }
  54. }
  55. // mapping处理
  56. {
  57. //获取所有指标信息 某一天的
  58. allCodeMappingIndex, tmpErr := data_manage.GetAllComTradeCodeMappingList(startDate)
  59. if tmpErr != nil {
  60. fmt.Println("get GetAllComTradeCodeMappingList err:" + tmpErr.Error())
  61. return
  62. }
  63. existCodeMappingMap := make(map[string]*data_manage.ComTradeCodeMapping)
  64. for _, v := range allCodeMappingIndex {
  65. existCodeMappingMap[v.Code] = v
  66. }
  67. for _, zv := range respObj.Data.MappingList {
  68. if _, ok := existCodeMappingMap[zv.Code]; !ok {
  69. _, err = data_manage.AddBaseFromComTradeCodeMapping(&zv)
  70. if err != nil {
  71. fmt.Println("AddBaseFromComTradeCodeMapping error:", err)
  72. }
  73. fmt.Println("AddBaseFromComTradeCodeMapping new Code:", zv.Code)
  74. }
  75. }
  76. }
  77. return err
  78. }
  79. // SyncComTradeData 同步UN指标数据
  80. func SyncComTradeData() (err error) {
  81. startDate := time.Now().Format(utils.FormatDate) + " 00:00:00"
  82. //var startDate string
  83. maxDate, err := data_manage.GetBaseFromComTradeMaxDate()
  84. if err != nil || maxDate.IsZero() {
  85. startDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate) + " 00:00:00"
  86. } else {
  87. startDate = maxDate.AddDate(0, 0, 1).Format(utils.FormatDate) + " 00:00:00"
  88. }
  89. method := `index/data/list_page`
  90. existDataMap := make(map[string]*data_manage.ComTradeData)
  91. allData, err1 := data_manage.GetAllComTradeDataList(startDate)
  92. if err1 != nil {
  93. fmt.Println("get GetBaseFromEiaSteoIndexDataAll err:" + err1.Error())
  94. return
  95. }
  96. for _, dv := range allData {
  97. tmpKey := dv.IndexTradeCode + "_" + dv.DataTime
  98. existDataMap[tmpKey] = dv
  99. }
  100. //获取所有指标信息 某一天的
  101. maxPage := 1
  102. for currPage := 0; currPage < maxPage; currPage++ {
  103. data := make(map[string]interface{})
  104. data["Source"] = utils.DATA_SOURCE_COM_TRADE
  105. data["StartDate"] = startDate
  106. data["CurrPage"] = currPage
  107. data["PageSize"] = 500 //
  108. var result string
  109. result, err = HttpPost("SyncComTradeData", method, data)
  110. utils.FileLog.Info(result)
  111. fmt.Println(result)
  112. respObj := new(data_manage.ComTradeIndexDataResp)
  113. err = json.Unmarshal([]byte(result), &respObj)
  114. if err != nil {
  115. fmt.Println("json.Unmarshal err:" + err.Error())
  116. return err
  117. }
  118. if respObj.Ret != 200 {
  119. err = errors.New(respObj.ErrMsg)
  120. return
  121. }
  122. // 总页码数
  123. maxPage = respObj.Data.Paging.Pages
  124. addDataList := make([]*data_manage.ComTradeData, 0)
  125. if respObj.Data.List != nil && len(respObj.Data.List) > 0 {
  126. for _, dv := range respObj.Data.List {
  127. tmpKey := dv.IndexTradeCode + "_" + dv.DataTime
  128. if _, ok := existDataMap[tmpKey]; !ok {
  129. addDataList = append(addDataList, dv)
  130. existDataMap[tmpKey] = dv
  131. }
  132. }
  133. }
  134. // 最后如果还有数据未插入,那么继续插入吧
  135. if len(addDataList) > 0 {
  136. _, err = data_manage.MultiAddBaseFromComTradeData(addDataList)
  137. if err != nil {
  138. fmt.Println("AddBaseFromComTradeData error:", err)
  139. }
  140. }
  141. }
  142. return err
  143. }