refresh_excel.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hongqi_watch/cache"
  5. "hongze/hongqi_watch/global"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. )
  11. // RefreshExcel 刷新excel文件
  12. func RefreshExcel() {
  13. global.LOG.Info("开始刷新了")
  14. weekDay := time.Now().Weekday()
  15. // 周末不刷新数据
  16. if weekDay == 0 || weekDay == 6 {
  17. return
  18. }
  19. // 如果没有配置就不监听了
  20. if global.CONFIG.Serve.ListenExcelPath == `` {
  21. return
  22. }
  23. fileList, err := GetExcelFilePath(global.CONFIG.Serve.ListenExcelPath)
  24. if err != nil {
  25. fmt.Println("获取excel文件失败,ERR:", err)
  26. global.LOG.Info("开始刷新;获取excel文件失败,ERR:", err)
  27. return
  28. }
  29. for _, v := range fileList {
  30. fmt.Println(v)
  31. cache.IndexAutoRefresh(v)
  32. }
  33. }
  34. // GetExcelFilePath 获取需要刷新的excel路径
  35. func GetExcelFilePath(dirPath string) (fileList []string, err error) {
  36. fileList = make([]string, 0)
  37. list, err := os.ReadDir(dirPath)
  38. if err != nil {
  39. return
  40. }
  41. for _, v := range list {
  42. tmpFilePath := dirPath + `\\` + v.Name()
  43. // 如果是目录的话,那么就查询子目录,然后返回
  44. if v.IsDir() {
  45. tmpFileList, tmpErr := GetExcelFilePath(tmpFilePath)
  46. if tmpErr != nil {
  47. return
  48. }
  49. fileList = append(fileList, tmpFileList...)
  50. continue
  51. }
  52. fileName := v.Name()
  53. ////临时文件过滤
  54. if strings.Contains(fileName, "tmp") ||
  55. strings.Contains(fileName, ".TMP") ||
  56. strings.Contains(fileName, "~") {
  57. continue
  58. }
  59. fileExt := filepath.Ext(tmpFilePath)
  60. if fileExt == ".xlsx" || fileExt == ".xls" {
  61. fileList = append(fileList, tmpFilePath)
  62. }
  63. }
  64. return
  65. }