package cmd

import (
	"gadmin/internal/cron"
	"gadmin/internal/cron/jobs"
	"time"

	"github.com/jinzhu/now"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cast"
	"github.com/spf13/cobra"
)

var stop = make(chan struct{})

type cmdFunc func()

var cronCmd = &cobra.Command{
	Use:   "cron",
	Short: "定时任务",
	Long: `定时任务
exec:
1. 	直接执行(./console cron ，不加参数，执行cron任务
2.  加参数执行
	go run cmd.go last 					// 执行上次的定时任务
	go run cmd.go cron redis 			// 执行redis历史key删除，并记录数据日志
	go run cmd.go cron downloadBackup 	// 执行下载备份数据到obs
	go run cmd.go cron order			// 同步订单列表
	go run cmd.go cron rolesCount 		// 统计各个角色等级
	go run cmd.go cron haveRoles        // 统计拥有角色数据
	go run cmd.go cron chapter			// 同步章节进度
	go run cmd.go cron email 			// 备份email.db
	go run cmd.go cron notice 			// 备份notices.json
	go run cmd.go cron changed		    // 统计玩家消费
	go run cmd.go cron still		    // 返还所有玩家装备材料
	go run cmd.go cron roleStill        // 转换角色结构体
	go run cmd.go cron channel		    // 同步渠道玩家
	go run cmd.go cron oldGuide		    // 更新老玩家新手引导屏蔽
	go run cmd.go cron filterInitDps	// 过滤战斗数据中的初始dps值
	go run cmd.go cron joinChannel		// 导入渠道ID
	go run cmd.go cron exchangePray		// 新年礼包祈福道具返还金币
	go run cmd.go cron logins		    // 过滤最近连续登录的玩家
	go run cmd.go cron convPvpKey		// 转换pvp玩家key  hash -> string.
	go run cmd.go cron repay38		    // 38活动补偿
	go run cmd.go cron maxTalents		// 最大天赋统计
	go run cmd.go cron levelOutput		// 同步等级产出
	go run cmd.go cron gameAlarm		// 游戏进程监控报警
	go run cmd.go cron suppleOrderAt	// 追加安卓订单创建时间
	go run cmd.go cron migrateZoneBonus	// 迁移地下城活动难度加成
	go run cmd.go cron chatLog 			//同步客服聊天记录
	go run cmd.go cron chatLogDiscover 	//处理异常聊天记录
	go run cmd.go cron downloadJson v1.0.0 //下载shop.json数据

	> 迁移玩家ID数据 
	go run cmd.go cron migrate changed_logs						// 消费记录
	go run cmd.go cron migrate changed_player					// 玩家消费统计
	go run cmd.go cron migrate changed_statistics				// 玩家消费统计
	go run cmd.go cron migrate chapter_logs_user_details	    // 玩家战斗数据
	go run cmd.go cron migrate gem_player					    // 玩家宝石统计
	go run cmd.go cron migrate orders							// 玩家订单
	go run cmd.go cron migrate player_channel					// 玩家渠道
	go run cmd.go cron migrate redeem_received					// 兑换码领取记录
	go run cmd.go cron migrate chapter					        // 玩家章节通关统计
	go run cmd.go cron migrate chapter_logs					    // 玩家章节日志
	go run cmd.go cron migrate disconnect_logs					// 断线重连


	cd /opt/gadmin/ && ./console cron migrate changed_logs						// 消费记录
	cd /opt/gadmin/ && ./console cron migrate changed_player					// 玩家消费统计
	cd /opt/gadmin/ && ./console cron migrate changed_statistics				// 消费类型统计
	cd /opt/gadmin/ && ./console cron migrate chapter_logs_user_details	    	// 玩家战斗数据
	cd /opt/gadmin/ && ./console cron migrate gem_player					    // 玩家宝石统计
	cd /opt/gadmin/ && ./console cron migrate orders							// 玩家订单
	cd /opt/gadmin/ && ./console cron migrate player_channel					// 玩家渠道
	cd /opt/gadmin/ && ./console cron migrate redeem_received					// 兑换码领取记录
	cd /opt/gadmin/ && ./console cron migrate login_logs						// 登录记录
	cd /opt/gadmin/ && ./console cron migrate chapter							// 玩家章节通关统计
	cd /opt/gadmin/ && ./console cron migrate chapter_logs						// 玩家章节日志
	cd /opt/gadmin/ && ./console cron migrate advertisement_logs				// 玩家章节日志
	cd /opt/gadmin/ && ./console cron migrate disconnect_logs					// 断线重连


	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_1						// 更新章节渠道数据
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_2						// 更新章节渠道数据
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_3						// 更新章节渠道数据 ok
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_4						// 更新章节渠道数据 ok
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_5						// 更新章节渠道数据 ok
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_6						// 更新章节渠道数据 ok
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_7						// 更新章节渠道数据 ok
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_8						// 更新章节渠道数据 ok
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_9						// 更新章节渠道数据 ok
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_10						// 更新章节渠道数据 ok
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_11						// 更新章节渠道数据 ok
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_12						// 更新章节渠道数据 ok 
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_13						// 更新章节渠道数据 ok

	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_100						// 更新章节渠道数据
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_101						// 更新章节渠道数据
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_102						// 更新章节渠道数据
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_103						// 更新章节渠道数据
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_104						// 更新章节渠道数据
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_105						// 更新章节渠道数据
	cd /opt/gadmin/ && ./console cron joinChannel chapter_logs_106						// 更新章节渠道数据

	cd /opt/gadmin/ && ./console cron downloadJson v1.0.0								// 更新shop.json数据
	cd /opt/gadmin/ && ./console cron haveRoles 7										// 统计拥有角色数据 7天
	`,
	Run: func(cmd *cobra.Command, args []string) {
		logrus.Info("cron called")
		if len(args) == 0 {
			cron.Run()
			<-stop
		}

		if len(args) == 1 && args[0] != "gadminDevConsole" {
			logrus.Info("args:", args[0])
			argMapping := map[string]cmdFunc{
				"redis":          cron.BackupRedisJob,
				"downloadBackup": cron.DownloadBackupJob,
				"rolesCount":     cron.RolesCount,
				"haveRoles":      cron.HaveRoles,
				"order":          cron.SyncOrders,
				"chapter":        cron.SyncChapter,
				"email":          cron.BackupEmail,
				"notice":         cron.BackupNotice,
				"changed":        cron.ChangedStatistics,
				"dps":            cron.StatisticsDPS,
				//"still":            cron.EquipmentStill,
				"roleStill":     cron.RoleStill,
				"channel":       cron.SyncChannel,
				"oldGuide":      cron.OldGuide,
				"filterInitDps": cron.FilterInitDps,
				"joinChannel":   cron.JoinChannel,
				"exchangePray":  cron.ExchangePray,
				"logins":        cron.Logins,
				"convPvpKey":    cron.ConvPvpKey,
				"repay38":       cron.Repay38,
				"maxTalents":    cron.MaxTalents,
				"levelOutput":   cron.LevelOutput,
				// "gameAlarm":        cron.GameAlarm,
				"suppleOrderAt":    cron.SuppleOrderAt,
				"migrateZoneBonus": cron.MigrateZoneBonus,
				"chatLog":          cron.SyncChatLog,
				"chatLogDiscover":  cron.SyncChatLogDiscover,
			}

			if f, ok := argMapping[args[0]]; ok {
				f()
				return
			}

			logrus.Info("cron RunLast...")
			cron.RunLast()

		} else if len(args) >= 2 { // 如果有参数，那么单独执行
			switch args[0] {
			case "migrate":
				cron.Migrate(args[1])
				return
			case "joinChannel":
				cron.JoinChannelByTable(args[1])
				return
			case "haveRoles":
				yesterday := time.Now()
				for i := 0; i < cast.ToInt(args[1]); i++ {
					jobs.HaveRoles.RunWithDate(now.With(yesterday.AddDate(0, 0, -1-i)))
					time.Sleep(3 * time.Second)
				}
				return
			case "downloadJson":
				if args[1] == "" {
					logrus.Warn("请输入json版本号")
					return
				}
				cron.DownloadJson(args[1])
				return
			default:
				return
			}
		}
	},
}

func init() {

	rootCmd.AddCommand(cronCmd)
}
