package initialize

import (
	"fmt"
	"log-server/service/dataStatistics"
	"log-server/service/fileManager"
	"log-server/service/log"
	"log-server/service/rentComputer"
	"log-server/service/system"
	"log-server/service/task"

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

var serviceStatisticsLog = new(log.ServiceStatisticsLog)
var ServiceLogList = new(log.ServiceLogList)
var syncData = new(task.SyncData)
var serviceRentComputer = new(rentComputer.ServiceRentComputer)
var serviceWeChatScannerApi = new(dataStatistics.ServiceWeChatScannerApi)
var serviceDataAbnormalRate = new(dataStatistics.ServiceDataAbnormalRate)
var serviceWeChatScannerDetailed = new(dataStatistics.ServiceWeChatScannerDetailed)
var serviceDownLoadUrl = new(fileManager.ServiceDownLoadUrl)
var robotService = new(system.RobotService)
var ServiceRegularTask = new(task.ServiceRegularTask)
var serviceFileQiniu = new(fileManager.ServiceFileQiniu)

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("StatisticsLog", "2 8 5 * * * ", serviceStatisticsLog.CreateStatisticsLog)
	//if err != nil {
	//	fmt.Println("add taskCode timer error:", err)
	//}
	//
	////定时同步电脑相关的统计数据
	//_, err = global.GVA_Timer.AddTaskByFunc("CreateComputerStatisticsData", "40 9,19,29,39,49,59 7-23 * * * ", serviceStatisticsLog.CreateComputerStatisticsData)
	//if err != nil {
	//	fmt.Println("add CreateComputerStatisticsData timer error:", err)
	//}
	//// 同步游戏相关的统计数据
	//_, err = global.GVA_Timer.AddTaskByFunc("TodayCreateStatisticsGameInfoLog", "2 8,18,28,38,48,58 6-23 * * * ", serviceStatisticsLog.TodayCreateStatisticsGameInfoLog)
	//if err != nil {
	//	fmt.Println("add TodayCreateStatisticsGameInfoLog timer error:", err)
	//}
	//// 同步任务统计数据
	//_, err = global.GVA_Timer.AddTaskByFunc("TaskStatisticsDataCache", "20 0/3 6-23 * * * ", serviceStatisticsLog.TaskStatisticsDataCache)
	//if err != nil {
	//	fmt.Println("add TaskStatisticsDataCache timer error:", err)
	//}
	//// 重试失败数据
	//_, err = global.GVA_Timer.AddTaskByFunc("createFailLog", "2 5,15,25,35,45,55 6-23 * * * ", ServiceLogList.CreateFailLog)
	//if err != nil {
	//	fmt.Println("everyDayResetStatisticsCache timer error:", err)
	//}
	//// 定时检查电脑上报
	//_, err = global.GVA_Timer.AddTaskByFunc("RegularCheckPc", "2 57 8-23 * * * ", serviceStatisticsLog.RegularCheckPc)
	//if err != nil {
	//	fmt.Println("RegularCheckPc timer error:", err)
	//}
	//// 新建日志表
	//_, err = global.GVA_Timer.AddTaskByFunc("RegularCreateLogingTable", "2 1 19 20 * * ", 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)
	//}
	////定时添加任务
	//_, err = global.GVA_Timer.AddTaskByFunc("EveryDaySyncTaskData", "30 22 21 * * *", syncData.EveryDaySyncTaskData)
	//if err != nil {
	//	fmt.Println("add EveryDaySyncTaskData timer error:", err)
	//}
	////同步任务数据
	//_, err = global.GVA_Timer.AddTaskByFunc("SyncTaskData", "2 0/3 1-23 * * *", syncData.SyncTaskData)
	//if err != nil {
	//	fmt.Println("add SyncTaskData timer error:", err)
	//}
	//// 数优任务统计
	//_, err = global.GVA_Timer.AddTaskByFunc("DayTargetDataStatistics", "40 2 5 * * *", syncData.DayTargetDataStatistics)
	//if err != nil {
	//	fmt.Println("add DayTargetDataStatistics timer error:", err)
	//}
	//
	//// 定时检查是否有电脑到期，修改租机状态@every 1s
	//_, err = global.GVA_Timer.AddTaskByFunc("CheckIsExpire", "0 0/5 * * * *", serviceRentComputer.CheckIsExpire)
	//if err != nil {
	//	fmt.Println("add CheckIsExpire timer error:", err)
	//}
	//// 定时生成租机每日台账@every 1s
	//_, err = global.GVA_Timer.AddTaskByFunc("CreateRentComputerLedger", "0 30 23 * * *", serviceRentComputer.CreateRentComputerLedger)
	//if err != nil {
	//	fmt.Println("add CreateRentComputerLedger timer error:", err)
	//}
	//
	////定时获取扫码平台余额,每小时执行一次
	////_, err = global.GVA_Timer.AddTaskByFunc("SyncGetWeChatScannerBalance", "0 58 * * * *", serviceWeChatScannerApi.SyncGetWeChatScannerBalance)
	//_, err = global.GVA_Timer.AddTaskByFunc("SyncGetWeChatScannerBalance", "0 58 * * * *", serviceWeChatScannerApi.SyncGetWeChatScannerBalance)
	//if err != nil {
	//	fmt.Println("add UpdateGameVersion timer error:", err)
	//}
	////定时更新异常率
	//_, err = global.GVA_Timer.AddTaskByFunc("SyncAbnormalRateByBatch", "0 1,31 * * * *", serviceDataAbnormalRate.SyncAbnormalRateByBatch)
	//if err != nil {
	//	fmt.Println("add SyncAbnormalRateByBatch timer error:", err)
	//}
	//
	////定时更新当日微信扫码订单列表
	//_, err = global.GVA_Timer.AddTaskByFunc("SyncTodayWeChatScannerDetailed", "0 3,33 * * * *", serviceWeChatScannerDetailed.SyncTodayWeChatScannerDetailed)
	//if err != nil {
	//	fmt.Println("add SyncTodayWeChatScannerDetailed timer error:", err)
	//}
	//
	////0点更新昨日微信扫码订单列表
	//_, err = global.GVA_Timer.AddTaskByFunc("SyncYesterdayWeChatScannerDetailed", "1 1 0 * * *", serviceWeChatScannerDetailed.SyncYesterdayWeChatScannerDetailed)
	//if err != nil {
	//	fmt.Println("add SyncYesterdayWeChatScannerDetailed timer error:", err)
	//}
	//
	////半小时同步一次机房设备信息，播报是否有异常
	//_, err = global.GVA_Timer.AddTaskByFunc("SyncJfDevicesMessage", "0 0,30 * * * *", robotService.SyncJfDevicesMessage)
	//if err != nil {
	//	fmt.Println("add SyncJfDevicesMessage timer error:", err)
	//}
	//定时获取机房小绵羊新号剩余
	//_, err := global.GVA_Timer.AddTaskByFunc("SyncJfXmyNewAccount", "0 0,30 * * * *", robotService.SyncJfXmyNewAccount)
	//if err != nil {
	//	fmt.Println("add SyncJfXmyNewAccount timer error:", err)
	//}
	//
	//// 活跃定时推送消息
	//_, err = global.GVA_Timer.AddTaskByFunc("TaskMsgSend", "20 2/32 8-21 * * *", syncData.TaskMsgSend)
	//if err != nil {
	//	fmt.Println("add TaskMsgSend timer error:", err)
	//}
	//
	//// 活跃定时推送消息
	//_, err = global.GVA_Timer.AddTaskByFunc("TaskMsgSend", "30 1/10 22-23 * * *", syncData.TaskMsgSend)
	//if err != nil {
	//	fmt.Println("add TaskMsgSend timer error:", err)
	//}
	//
	//// 付费定时推送消息
	//_, err = global.GVA_Timer.AddTaskByFunc("TaskMsgSend", "45 2/15 8-21 * * *", syncData.TaskFreeMsgSend)
	//if err != nil {
	//	fmt.Println("add TaskFreeMsgSend timer error:", err)
	//}
	//
	//// 付费定时推送消息
	//_, err = global.GVA_Timer.AddTaskByFunc("TaskMsgSend", "50 1/5 22-23 * * *", syncData.TaskFreeMsgSend)
	//if err != nil {
	//	fmt.Println("add TaskFreeMsgSend timer error:", err)
	//}
	//
	//// 23起检测任务完成情况
	//_, err = global.GVA_Timer.AddTaskByFunc("CheckTaskCompletedInfo", "57 5/4 23 * * *", syncData.CheckTaskCompletedInfo)
	//if err != nil {
	//	fmt.Println("add CheckTaskCompletedInfo timer error:", err)
	//}
	//
	////定时更新游戏版本号和链接
	//_, err = global.GVA_Timer.AddTaskByFunc("UpdateGameVersion", "0 3/15 * * * *", serviceDownLoadUrl.UpdateGameVersion)
	//if err != nil {
	//	fmt.Println("add UpdateGameVersion timer error:", err)
	//}
	//
	////定时更新在线电脑缓存
	//_, err = global.GVA_Timer.AddTaskByFunc("OnlinePcCodeUpdateCache", "14 7 8-20 * * *", ServiceRegularTask.OnlinePcCodeUpdateCache)
	//if err != nil {
	//	fmt.Println("add OnlinePcCodeUpdateCache timer error:", err)
	//}
	//
	//// 定时同步arpu值
	//_, err = global.GVA_Timer.AddTaskByFunc("SyncArpu", "50 36 7,12,23 * * *", syncData.SyncArpu)
	//if err != nil {
	//	fmt.Println("add SyncArpu timer error:", err)
	//}
	//
	//// 提醒修改腾讯目标
	//_, err = global.GVA_Timer.AddTaskByFunc("RemindSendOne", "11 2 18,22 * * *", syncData.RemindSendOne)
	//if err != nil {
	//	fmt.Println("add RemindSendOne timer error:", err)
	//}

	//半小时同步一次IP信息，播报是否有异常
	_, err := global.GVA_Timer.AddTaskByFunc("SyncIPMessage", "0 */30 * * * ? ", robotService.SyncIPMessage)
	if err != nil {
		fmt.Println("add SyncIPMessage timer error:", err)
	}
	//定时查看延迟更新的镜像是否到期
	_, err = global.GVA_Timer.AddTaskByFunc("CheckPushTime", "35 0/2 * * * *", serviceFileQiniu.CheckPushTime)
	if err != nil {
		fmt.Println("add CheckPushTime timer error:", err)
	}
}
