hq_index.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongtao3_watch/global"
  6. "hongze/hongtao3_watch/utils"
  7. "strings"
  8. "time"
  9. "github.com/xuri/excelize/v2"
  10. )
  11. type SciHqExcel struct {
  12. IndexName string
  13. ExcelIndexCode string
  14. Frequency string
  15. Unit string
  16. TerminalCode string
  17. FilePath string
  18. Data map[string]string
  19. }
  20. var dateFormats = []string{"01-02-06", utils.FormatDate}
  21. func ReadHqExcel(filePath string) {
  22. // time.Sleep(time.Second)
  23. excelInfo, err := excelize.OpenFile(filePath)
  24. if err != nil {
  25. fmt.Println("OpenFile excel err:" + err.Error())
  26. return
  27. }
  28. fmt.Println("开始读取EXCEL了")
  29. defer func() {
  30. excelInfo.Close()
  31. }()
  32. sheetList := excelInfo.GetSheetList()
  33. excelInfoList := make([]*SciHqExcel, 0)
  34. for _, sheet := range sheetList {
  35. rows, err := excelInfo.GetRows(sheet)
  36. if err != nil {
  37. fmt.Println("err:", err)
  38. continue
  39. }
  40. if len(rows) < 6 {
  41. fmt.Println("错误的excel表")
  42. continue
  43. }
  44. indexLen := len(rows[0])
  45. // 没有指标
  46. if indexLen <= 1 {
  47. fmt.Println("没有指标")
  48. continue
  49. }
  50. for col := 1; col < indexLen; col++ {
  51. indexInfo := new(SciHqExcel)
  52. dataLen := len(rows)
  53. if rows[0][0] != "数据名称" || rows[1][0] != "数据项ID" || rows[2][0] != "发布周期" || rows[3][0] != "单位" || rows[4][0] != "频率" {
  54. fmt.Println("sheet错误,ErrSheetName:", sheet)
  55. break
  56. } else {
  57. indexInfo.IndexName = rows[0][col]
  58. indexInfo.ExcelIndexCode = rows[1][col]
  59. indexInfo.Unit = rows[3][col]
  60. if strings.Contains(rows[4][col], "日度") {
  61. indexInfo.Frequency = "日度"
  62. } else if strings.Contains(rows[4][col], "周度") {
  63. indexInfo.Frequency = "周度"
  64. } else if strings.Contains(rows[4][col], "旬度") {
  65. indexInfo.Frequency = "旬度"
  66. } else if strings.Contains(rows[4][col], "月度") {
  67. indexInfo.Frequency = "月度"
  68. } else if strings.Contains(rows[4][col], "季度") {
  69. indexInfo.Frequency = "季度"
  70. } else if strings.Contains(rows[4][col], "年度") {
  71. indexInfo.Frequency = "年度"
  72. } else {
  73. indexInfo.Frequency = "周度"
  74. }
  75. indexInfo.Frequency = rows[4][col]
  76. indexInfo.Data = make(map[string]string)
  77. indexInfo.FilePath = filePath
  78. indexInfo.TerminalCode = global.CONFIG.Serve.TerminalCode
  79. }
  80. for row := 5; row < dataLen; row++ {
  81. var newTime time.Time
  82. var tmpErr error
  83. for _, v := range dateFormats {
  84. newTime, tmpErr = time.Parse(v, rows[row][0])
  85. if !newTime.IsZero() {
  86. break
  87. }
  88. if tmpErr != nil {
  89. continue
  90. }
  91. }
  92. if newTime.IsZero() {
  93. fmt.Println("日期类型错误,ErrDate:", rows[row][0])
  94. continue
  95. }
  96. // 没有数据
  97. if len(rows[row]) <= col {
  98. continue
  99. }
  100. if rows[row][col] == "" {
  101. continue
  102. }
  103. indexInfo.Data[newTime.Format(utils.FormatDate)] = rows[row][col]
  104. }
  105. excelInfoList = append(excelInfoList, indexInfo)
  106. }
  107. }
  108. resp, err := HandleHqExcelDataByEdbLib(excelInfoList)
  109. if err != nil {
  110. global.LOG.Info("调用指标库公共服务处理数据失败,err:" + err.Error())
  111. return
  112. }
  113. if resp.Ret != 200 {
  114. b, err := json.Marshal(resp)
  115. if err != nil {
  116. fmt.Println("json.Marshal err:" + err.Error())
  117. return
  118. }
  119. fmt.Println("调用指标库公共服务处理数据失败, return:" + string(b))
  120. global.LOG.Info("调用指标库公共服务处理数据失败,return:" + string(b))
  121. }
  122. }