package services

import (
	"fmt"
	"hongze/hongtao3_watch/global"
	"hongze/hongtao3_watch/utils"
	"path/filepath"
	"strings"
)

// HandleFileUpdate 处理文件变化
func HandleFileUpdate() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("[AutoHandle]", err)
		}
	}()
	for {
		global.Rc.Brpop(utils.HANDLE_HONGTAO_EXCEL, func(b []byte) {
			updateFilePath := string(b)
			// 移除多余的双引号
			updateFilePath = strings.Replace(updateFilePath, `"`, "", -1)
			fileExt := filepath.Ext(updateFilePath)
			//fmt.Println("fileExt:", fileExt)

			if fileExt != ".xlsx" && fileExt != ".xls" {
				//fmt.Println("不是excel文件")
				return
			}
			// 读取excel内容并操作入库
			ReadExcel(updateFilePath)
		})
	}

}

// HandleRefreshExcel 处理excel刷新(实际去调用刷新)
func HandleRefreshExcel() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("[AutoRefresh]", err)
		}
	}()
	for {
		global.Rc.Brpop(utils.REFRESH_HONGTAO_EXCEL, func(b []byte) {
			updateFilePath := string(b)
			// 移除多余的双引号
			updateFilePath = strings.Replace(updateFilePath, `"`, "", -1)
			fileExt := filepath.Ext(updateFilePath)
			//fmt.Println("fileExt:", fileExt)

			if fileExt != ".xlsx" && fileExt != ".xls" {
				//fmt.Println("不是excel文件")
				return
			}
			// 读取excel内容并操作入库
			InvokeRefreshServer(updateFilePath)
		})
	}

}