init_pcsg_bloomberg.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package services
  2. import (
  3. "eta/eta_data_init/utils"
  4. "fmt"
  5. "github.com/araddon/dateparse"
  6. "github.com/xuri/excelize/v2"
  7. "math"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // InitPCSGBloombergData 初始化中石油新加坡Bloomberg指标
  15. func InitPCSGBloombergData(dataPath string, taskKey string) {
  16. var err error
  17. defer func() {
  18. if err != nil {
  19. fmt.Println(err.Error())
  20. utils.FileLog.Info("InitPCSGBloombergData Err: " + err.Error())
  21. }
  22. }()
  23. // 读取excel
  24. path, e := filepath.Abs(os.Args[0])
  25. if e != nil {
  26. err = fmt.Errorf("file abs err: %s", e.Error())
  27. return
  28. }
  29. dir := filepath.Dir(path)
  30. dataPath = dir + dataPath
  31. //dataPath = "docs/dailyHistory-IDpcsgDailyRun7-20240904.xlsx"
  32. f, e := excelize.OpenFile(dataPath)
  33. if e != nil {
  34. err = fmt.Errorf("open file err: %s", e.Error())
  35. return
  36. }
  37. defer func() {
  38. if e = f.Close(); e != nil {
  39. err = fmt.Errorf("f close err: %s", e.Error())
  40. }
  41. }()
  42. type ExcelRows struct {
  43. IndexCode string `description:"指标编码"`
  44. DataMap map[time.Time]float64 `description:"数据日期/值"`
  45. }
  46. indexMap := make(map[string]*ExcelRows, 0)
  47. sheets := f.GetSheetList()
  48. for _, sheet := range sheets {
  49. fmt.Printf("sheet: %s\n", sheet)
  50. rows, e := f.GetRows(sheet)
  51. if e != nil {
  52. err = fmt.Errorf("f GetRows err: %s", e.Error())
  53. return
  54. }
  55. if len(rows) == 0 {
  56. continue
  57. }
  58. for rk, row := range rows {
  59. // 标题行
  60. if rk <= 0 {
  61. continue
  62. }
  63. // 数据行
  64. var rCode, rDate, rVal string
  65. for ck, cell := range row {
  66. cell = strings.TrimSpace(cell)
  67. switch ck {
  68. case 4:
  69. rCode = cell
  70. case 6:
  71. rDate = cell
  72. case 7:
  73. rVal = cell
  74. }
  75. }
  76. if rCode == "" {
  77. fmt.Printf("%d行指标编码为空\n", rk+1)
  78. continue
  79. }
  80. if indexMap[rCode] == nil {
  81. indexMap[rCode] = new(ExcelRows)
  82. indexMap[rCode].DataMap = make(map[time.Time]float64, 0)
  83. }
  84. indexMap[rCode].IndexCode = rCode
  85. // 日期/值
  86. if rDate == "" {
  87. fmt.Printf("%d行数据日期为空\n", rk+1)
  88. continue
  89. }
  90. // 格式dd-mm-yy
  91. rDate = strings.ReplaceAll(rDate, "-", ".")
  92. d, e := dateparse.ParseAny(rDate)
  93. if e != nil {
  94. fmt.Printf("%d行数据日期格式有误: %s, err: %v\n", rk+1, rDate, e)
  95. continue
  96. }
  97. if rVal == "" || rVal == "NULL" {
  98. fmt.Printf("%d行数据值为空\n", rk+1)
  99. continue
  100. }
  101. val, e := strconv.ParseFloat(rVal, 64)
  102. if e != nil {
  103. fmt.Printf("%d行数据值有误: %s\n", rk+1, rVal)
  104. continue
  105. }
  106. // 保留四位小数
  107. rounded := math.Round(val*10000) / 10000
  108. if rounded == math.Floor(rounded) {
  109. val = float64(int64(rounded))
  110. } else {
  111. val = rounded
  112. }
  113. indexMap[rCode].DataMap[d] = val
  114. }
  115. }
  116. // 请求接口导入数据
  117. method := "bloomberg/pcsg/import_history_data"
  118. for _, v := range indexMap {
  119. if v.IndexCode == "" {
  120. continue
  121. }
  122. requestMap := make(map[string]interface{})
  123. requestMap["IndexCode"] = v.IndexCode
  124. requestMap["DataMap"] = v.DataMap
  125. requestMap["TaskKey"] = taskKey
  126. indexRes, e := PostEdbLib(requestMap, method)
  127. if e != nil {
  128. fmt.Printf("post edb lib err: %s; result: %s", e.Error(), string(indexRes))
  129. continue
  130. }
  131. }
  132. }