package initialize

import (
	"fmt"
	"log-server/service/log"

	"log-server/config"
	"log-server/global"
	"log-server/utils"
)

var serviceStatisticsLog = new(log.ServiceStatisticsLog)
var ServiceLogList = new(log.ServiceLogList)

func Timer() {
	if global.GVA_CONFIG.Timer.Start {
		for i := range global.GVA_CONFIG.Timer.Detail {
			go func(detail config.Detail) {
				_, err := global.GVA_Timer.AddTaskByFunc("ClearDB", global.GVA_CONFIG.Timer.Spec, func() {
					err := utils.ClearTable(global.GVA_DB, detail.TableName, detail.CompareField, detail.Interval)
					if err != nil {
						fmt.Println("timer error:", err)
					}
				})
				if err != nil {
					fmt.Println("add timer error:", err)
				}
			}(global.GVA_CONFIG.Timer.Detail[i])
		}
	}

	//_, err := global.GVA_Timer.AddTaskByFunc("everyDayResetStatisticsCache","2 8 3 * * * ", serviceStatisticsLog.EveryDayResetStatisticsCache)
	//if err != nil {
	//	fmt.Println("everyDayResetStatisticsCache timer error:", err)
	//}
	// 定时更新前一天的统计数据
	_, err := global.GVA_Timer.AddTaskByFunc("StatisticsLog", "2 8 5 * * * ", serviceStatisticsLog.CreateStatisticsLog)
	if err != nil {
		fmt.Println("add taskCode timer error:", err)
	}
	// 定时同步电脑相关的统计数据
	//_, err = global.GVA_Timer.AddTaskByFunc("CreateComputerStatisticsData", "40 0/9 7-23 * * * ", serviceStatisticsLog.CreateComputerStatisticsData)
	//if err != nil {
	//	fmt.Println("add CreateComputerStatisticsData timer error:", err)
	//}
	//// 同步游戏相关的统计数据
	//_, err = global.GVA_Timer.AddTaskByFunc("TodayCreateStatisticsGameInfoLog", "2 0/8 6-23 * * * ", serviceStatisticsLog.TodayCreateStatisticsGameInfoLog)
	//if err != nil {
	//	fmt.Println("add TodayCreateStatisticsGameInfoLog timer error:", err)
	//}
	//// 同步任务统计数据
	//_, err = global.GVA_Timer.AddTaskByFunc("TaskStatisticsDataCache", "2 0/3 6-23 * * * ", serviceStatisticsLog.TaskStatisticsDataCache)
	//if err != nil {
	//	fmt.Println("add TaskStatisticsDataCache timer error:", err)
	//}
	//// 重试失败数据
	//_, err = global.GVA_Timer.AddTaskByFunc("createFailLog", "2 0/5 6-23 * * * ", ServiceLogList.CreateFailLog)
	//if err != nil {
	//	fmt.Println("everyDayResetStatisticsCache timer error:", err)
	//}
	//// 定时检查电脑上报
	//_, err = global.GVA_Timer.AddTaskByFunc("RegularCheckPc", "2 0/57 8-23 * * * ", serviceStatisticsLog.RegularCheckPc)
	//if err != nil {
	//	fmt.Println("RegularCheckPc timer error:", err)
	//}
	//// 新建日志表
	//_, err = global.GVA_Timer.AddTaskByFunc("RegularCreateLogingTable", "2 1 19 20,29 * * ", ServiceLogList.RegularCreateLogingTable)
	//if err != nil {
	//	fmt.Println("add RegularCreateLogingTable timer error:", err)
	//}
	//// 定时删缓存
	//_, err = global.GVA_Timer.AddTaskByFunc("RegularDelCheckData", "2 33 11 * * *", serviceStatisticsLog.RegularDelCheckData)
	//if err != nil {
	//	fmt.Println("add RegularDelCheckData timer error:", err)
	//}
}
