hq_index.go 3.8 KB

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