package services import ( "fmt" "hongze/hongqi_watch/cache" "hongze/hongqi_watch/global" "os" "path/filepath" "strings" "time" ) // RefreshExcel 刷新excel文件 func RefreshExcel() { global.LOG.Info("开始刷新了") weekDay := time.Now().Weekday() // 周末不刷新数据 if weekDay == 0 || weekDay == 6 { return } // 如果没有配置就不监听了 if global.CONFIG.Serve.ListenExcelPath == `` { return } fileList, err := GetExcelFilePath(global.CONFIG.Serve.ListenExcelPath) if err != nil { fmt.Println("获取excel文件失败,ERR:", err) global.LOG.Info("开始刷新;获取excel文件失败,ERR:", err) return } for _, v := range fileList { fmt.Println(v) cache.IndexAutoRefresh(v) } } // GetExcelFilePath 获取需要刷新的excel路径 func GetExcelFilePath(dirPath string) (fileList []string, err error) { fileList = make([]string, 0) list, err := os.ReadDir(dirPath) if err != nil { return } for _, v := range list { tmpFilePath := dirPath + `\\` + v.Name() // 如果是目录的话,那么就查询子目录,然后返回 if v.IsDir() { tmpFileList, tmpErr := GetExcelFilePath(tmpFilePath) if tmpErr != nil { return } fileList = append(fileList, tmpFileList...) continue } fileName := v.Name() ////临时文件过滤 if strings.Contains(fileName, "tmp") || strings.Contains(fileName, ".TMP") || strings.Contains(fileName, "~") { continue } fileExt := filepath.Ext(tmpFilePath) if fileExt == ".xlsx" || fileExt == ".xls" { fileList = append(fileList, tmpFilePath) } } return }