zwxi 1 year ago
parent
commit
c93aee3a6b
4 changed files with 75 additions and 2 deletions
  1. 1 2
      services/commodity_coal.go
  2. 57 0
      services/mtjh_watch.go
  3. 11 0
      services/task.go
  4. 6 0
      utils/config.go

+ 1 - 2
services/commodity_coal.go

@@ -491,8 +491,7 @@ func Mtjh(path string) (err error) {
 			utils.FileLog.Info(fmt.Sprintf("RefreshDataFromCoalCoastal, Err: %s", err))
 		}
 	}()
-	path = "/Users/xi/Desktop/煤炭江湖数据定制化服务——中国主流港口煤炭库存20231129.xlsx"
-	//path := "D:\\瑞茂通-中国煤炭市场网数据\\442家晋陕蒙、沿海8省、内陆17省历史数据\\CⅢ-8-16 25省市库存和日耗情况(CCTD).xlsx"
+	//path = "/Users/xi/Desktop/煤炭江湖数据定制化服务——中国主流港口煤炭库存20231129.xlsx"
 
 	var xlFile *xlsx.File
 	exist, err := PathExists(path)

+ 57 - 0
services/mtjh_watch.go

@@ -0,0 +1,57 @@
+package services
+
+import (
+	"eta/eta_data_analysis/utils"
+	"fmt"
+	"github.com/patrickmn/go-cache"
+	"io/fs"
+	"os"
+	"path/filepath"
+	"strings"
+	"time"
+)
+
+func mtjhWatch() {
+	fmt.Println("mtjhWatch start")
+	var err error
+	defer func() {
+		if err != nil {
+			fmt.Println("mtjhWatch Err:" + err.Error())
+		}
+	}()
+	var cacheClient *cache.Cache
+	if cacheClient == nil {
+		cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
+	}
+	err = filepath.Walk(utils.CoalMineFilePath, func(path string, info fs.FileInfo, err error) error {
+		if err != nil {
+			return err
+		}
+		if !info.IsDir() {
+			fileInfo, err := os.Stat(path)
+			if err != nil {
+				fmt.Println("os.Stat:", err.Error())
+			}
+			winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
+			modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
+
+			existModifyTime, ok := cacheClient.Get(path)
+			if ok {
+				existModifyTimeStr := existModifyTime.(string)
+				if existModifyTimeStr != modifyTimeStr {
+					if strings.Contains(path, "煤炭江湖") {
+						err = Mtjh(path)
+					}
+				}
+			} else {
+				if strings.Contains(path, "煤炭江湖") {
+					err = Mtjh(path)
+				}
+			}
+			cacheClient.Delete(path)
+			cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
+		}
+		return nil
+	})
+}
+

+ 11 - 0
services/task.go

@@ -50,6 +50,17 @@ func Task() {
 		task.AddTask("汾渭数据指标文件检测", fenWeiReadWatchIndexFile)
 	}
 
+	if utils.MtjhOpen == "1" {
+		c := cron.New(cron.WithSeconds())
+		//每2分钟检测一次指标文件是否更新
+		_, err := c.AddFunc("0 */2 * * * *", mtjhWatch)
+		if err != nil {
+			fmt.Println("watch.mtjhWatch err" + err.Error())
+			utils.FileLog.Info("watch.mtjhWatch err" + err.Error())
+		}
+		c.Start()
+	}
+
 	task.StartTask()
 
 	fmt.Println("task end")

+ 6 - 0
utils/config.go

@@ -55,6 +55,12 @@ var (
 	FenweiOpen       string // 是否配置汾渭数据源
 )
 
+// 煤炭江湖
+var (
+	MtjhPath string //excel文件地址
+	MtjhOpen string //是否配置煤炭江湖数据源,1已配置
+)
+
 var TerminalCode string
 
 func init() {