package main

import (
	"time"

	"mongo.games.com/goserver/core"
	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/schedule"

	"mongo.games.com/game/model"
)

func init() {
	core.RegisteHook(core.HOOK_BEFORE_START, func() error {
		//每日凌晨4点执行清理任务
		//0 0 4 * * *
		task := schedule.NewTask("定期清理cardlog", "0 0 4 * * *", func() error {
			logger.Logger.Info("@@@执行定期清理任务@@@")
			tNow := time.Now()
			tStart := tNow.AddDate(0, -3, 0)
			changeInfo, err := model.RemoveCoinLog(tStart.Unix())
			if err != nil {
				logger.Logger.Warnf("定期清理coinlog失败: %v", err)
			} else {
				logger.Logger.Warnf("定期清理coinlog成功: updated:%v removed:%v", changeInfo.Updated, changeInfo.Removed)
			}
			//APIlog
			changeInfo, err = model.RemoveAPILog(tStart.Unix())
			if err != nil {
				logger.Logger.Warnf("定期清理APIlog失败: %v", err)
			} else {
				logger.Logger.Warnf("定期清理APIlog成功: updated:%v removed:%v", changeInfo.Updated, changeInfo.Removed)
			}
			return nil
		})
		if task != nil {
			logger.Logger.Info("@@@执行定期清理任务@@@加入调度")
			schedule.AddTask(task.Taskname, task)
		}

		//每日凌晨执行汇总任务
		//0 0 0 * * *
		//taskDay := schedule.NewTask("定期汇总玩家金币总额", "0 0 0 * * *", func() error {
		//	logger.Logger.Info("@@@执行定期汇总任务@@@")
		//	return model.AggregatePlayerCoin()
		//})
		//if taskDay != nil {
		//	logger.Logger.Info("@@@执行定期汇总任务@@@加入调度")
		//	schedule.AddTask(taskDay.Taskname, taskDay)
		//}
		return nil
	})
}
