package services

import (
	"eta/eta_data_analysis/utils"
	"fmt"
	"github.com/beego/beego/v2/task"
	"github.com/patrickmn/go-cache"
	"github.com/robfig/cron/v3"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
	"syscall"
	"time"
)

func Task() {
	fmt.Println("task start")
	if utils.YongyiOpen == "1" {
		handleYongyiExcelDaily := task.NewTask("handleYongyiExcelDaily", "0 35 13,23 * * *", HandleYongyiExcelDaily)
		task.AddTask("涌益咨询日度指标处理", handleYongyiExcelDaily)

		handleYongyiExcelWeekly := task.NewTask("handleYongyiExcelWeekly", "0 5 17,23 * * *", HandleYongyiExcelWeekly)
		task.AddTask("涌益咨询周度指标处理", handleYongyiExcelWeekly)
		//HandleYongyiExcelDaily("/Users/xiexiaoyuan/Downloads/2023年11月21日涌益咨询日度数据 (1).xlsx")
		//HandleYongyiExcelWeekly()

		yongyiDownloadDaily := task.NewTask("YongyiDownloadDaily", "0 30 13,23 * * *", YongyiDownloadDaily)
		task.AddTask("涌益咨询日度指标下载", yongyiDownloadDaily)

		yongyiDownloadWeekly := task.NewTask("YongyiDownloadWeekly", "0 0 17,23 * * *", YongyiDownloadWeeyly)
		task.AddTask("涌益咨询周度指标下载", yongyiDownloadWeekly)
		task.StartTask()
	}

	if utils.CoalMineOpen == "1" {
		c := cron.New(cron.WithSeconds())
		//每2分钟检测一次指标文件是否更新
		_, err := c.AddFunc("0 */2 * * * *", ReadWatchIndexFile)
		if err != nil {
			fmt.Println("watch.ReadWatchIndexFile err" + err.Error())
			utils.FileLog.Info("watch.ReadWatchIndexFile err" + err.Error())
		}
		c.Start()
	}

	fmt.Println("task end")
}


func ReadWatchIndexFile() {
	fmt.Println("ReadWatchIndexFile start")
	var err error
	defer func() {
		if err != nil {
			fmt.Println("ReadWatchIndexFile 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, "442家晋陕蒙煤矿周度产量数据") {
						err = Jsm(path)
					} else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
						err = Inland(path)
					} else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
						err = Coastal(path)
					} else if strings.Contains(path, "442家晋陕蒙历史数据") {
						err = JsmHistory(path)
					} else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
						err = CoastalHistory(path)
						time.Sleep(time.Second*10)
						err = InlandHistory(path)
					}
				}
			} else {
				if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
					err = Jsm(path)
				} else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
					err = Inland(path)
				} else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
					err = Coastal(path)
				} else if strings.Contains(path, "442家晋陕蒙历史数据") {
					err = JsmHistory(path)
				} else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
					err = CoastalHistory(path)
					time.Sleep(time.Second*10)
					err = InlandHistory(path)
				}
			}
			cacheClient.Delete(path)
			cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
		}
		return nil
	})
}
