package task

import (
	"github.com/robfig/cron/v3"
	"time"

	"applet/app/cfg"
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/md"
	"applet/app/utils"
	"applet/app/utils/logx"
	"xorm.io/xorm"
)

var (
	timer                 *cron.Cron
	jobs                  = map[string]func(*xorm.Engine, string){}
	baseEntryId           cron.EntryID
	entryIds              []cron.EntryID
	taskCfgList           map[string]*[]model.SysCfg
	ch                    = make(chan int, 50)
	workerNum             = 50 // 智盟跟单并发数量
	orderStatWorkerNum    = 50 // 智盟跟单并发数量
	tbagoworkerNum        = 50 // 智盟跟单并发数量
	tbsettleworkerNum     = 50 // 智盟跟单并发数量
	pddOrderWorkerNum     = 50 // 拼多多跟单并发数量
	orderSuccessWorkerNum = 50 //
	tbOrderWorkerNum      = 50 // 淘宝跟单并发数量
	jdOrderWorkerNum      = 50 // 京东跟单并发数量
	wphOrderWorkerNum     = 50 // 唯品会跟单并发数量
	cardWorkerNum         = 20 // 权益卡并发数量
	tbRelationWorkerNum   = 50 // 淘宝并发数量
	hw365WorkerNum        = 50 // 海威并发数量
	hw365TourismWorkerNum = 50 // 海威并发数量
	tbpubWorkerNum        = 50 // 海威并发数量
	liveWorkerNum         = 50 // 海威并发数量
	tikTokOwnWorkerNum    = 50 // 海威并发数量
	cardUpdateWorkerNum   = 50 // 海威并发数量
	lifeWorkerNum         = 50 //生活服务跟单
	pddWorkerNum          = 50 //
	oilWorkerNum          = 50 //
	otherWorkerNum        = 50 // 淘宝, 苏宁, 考拉并发量
	jdWorkerNum           = 50 //
	tikTokWorkerNum       = 50 //
	teamGoodsWorkerNum    = 50
	jdCh                  = make(chan int, 50)
	jdWorkerNum1          = 50 //
	orderStatCh           = make(chan int, 50)
	jdCh1                 = make(chan int, 50)
	oilCh                 = make(chan int, 50)
	otherCh               = make(chan int, 50)
	otherTourismCh        = make(chan int, 50)
	liveOtherCh           = make(chan int, 50)
	teamGoodsCh           = make(chan int, 50)
	tikTokOwnOtherCh      = make(chan int, 50)
	cardUpdateCh          = make(chan int, 50)
	tbpubCh               = make(chan int, 50)
	cardCh                = make(chan int, 20)
	pddCh                 = make(chan int, 50)
	tikTokCh              = make(chan int, 50)
	tbRefundCh            = make(chan int, 50)
	tbagodCh              = make(chan int, 50)
	tbsettleCh            = make(chan int, 50)
	pddFailCh             = make(chan int, 50)
	orderSuccessCh        = make(chan int, 50)
	tbRelationCh          = make(chan int, 50)
)

func Init() {
	// 初始化任务列表
	initTasks()
	var err error
	timer = cron.New()
	if baseEntryId, err = timer.AddFunc("@every 20m", reload); err != nil {
		_ = logx.Fatal(err)
	}
}

func Run() {
	reload()
	timer.Start()
	_ = logx.Info("auto tasks running...")
}

func reload() {
	// 重新初始化数据库
	db.InitMapDbs(cfg.DB, cfg.Prd)

	if len(taskCfgList) == 0 {
		taskCfgList = map[string]*[]model.SysCfg{}
	}

	// 获取所有站长的配置信息
	for dbName, v := range db.DBs {
		if conf := db.MapCrontabCfg(v); conf != nil {
			if cfg.Debug {
				dbInfo := md.SplitDbInfo(v)
				// 去掉模版库
				if dbName == "000000" {
					continue
				}
				_ = logx.Debugf("【MasterId】%s, 【Host】%s, 【Name】%s, 【User】%s, 【prd】%v, 【Task】%v\n", dbName, dbInfo.Host, dbInfo.Name, dbInfo.User, cfg.Prd, utils.SerializeStr(*conf))
			}
			taskCfgList[dbName] = conf
		}
	}
	if len(taskCfgList) > 0 {
		// 删除原有所有任务
		if len(entryIds) > 0 {
			for _, v := range entryIds {
				if v != baseEntryId {
					timer.Remove(v)
				}
			}
			entryIds = nil
		}
		var (
			entryId cron.EntryID
			err     error
		)
		// 添加任务
		for dbName, v := range taskCfgList {
			for _, vv := range *v {
				if _, ok := jobs[vv.Key]; ok && vv.Val != "" {
					// fmt.Println(vv.Val)
					if entryId, err = timer.AddFunc(vv.Val, doTask(dbName, vv.Key)); err == nil {
						entryIds = append(entryIds, entryId)
					}
				}
			}
		}

	}
}

func doTask(dbName, fnName string) func() {
	return func() {
		begin := time.Now().Local()
		jobs[fnName](db.DBs[dbName], dbName)
		end := time.Now().Local()
		logx.Infof(
			"[%s] AutoTask <%s> started at <%s>, ended at <%s> duration <%s>",
			dbName,
			fnName,
			begin.Format("2006-01-02 15:04:05.000"),
			end.Format("2006-01-02 15:04:05.000"),
			time.Duration(end.UnixNano()-begin.UnixNano()).String(),
		)
	}
}

// 增加自动任务队列
func initTasks() {
	//v2
	//jobs[md.KEY_CFG_CRON_BUCKLE] = taskOrderBuckle                  //
	//jobs[md.KEY_CFG_CRON_CHECK_BUCKLE_ORDER] = taskCheckBuckleOrder //
	//jobs[md.KEY_CFG_CRON_USER_RELATE] = taskUserRelate              //

	//v3
	//jobs[md.KEY_CFG_CRON_TB12] = taskOrderTaobao12                                //淘宝抓单
	//jobs[md.KEY_CFG_CRON_TBBYAGOTIME] = taskAgoOrderTB                            //用于恢复个别时间丢单的
	//jobs[md.KEY_CFG_CRON_TB_PUNISH_REFUND] = taskTbPunishOrderRefund              //淘宝退款订单处理
	//jobs[md.KEY_CFG_CRON_TBREFUND] = taskTbOrderRefund                            //淘宝退款订单处理
	//jobs[md.KEY_CFG_CRON_PUBLISHER_RELATION] = taskTaobaoPublisherRelation        //获取渠道信息
	//jobs[md.KEY_CFG_CRON_PUBLISHER_RELATION_NEW] = taskTaobaoPublisherRelationNew //获取渠道信息
	//jobs[md.KEY_CFG_CRON_TBSETTLEORDER] = taskOrderTaobaoSettleOrder              //淘宝抓单结算订单

	//v4
	//jobs[md.KEY_CFG_CRON_PDD_SUCC] = taskOrderPddSucc
	//jobs[md.KEY_CFG_CRON_PDDBYSTATUS] = taskOrderPddStatus
	//jobs[md.KEY_CFG_CRON_PDDBYSTATUSSUCCESS] = taskOrderPddStatusSuccess
	//jobs[md.KEY_CFG_CRON_PDDBYSTATUSFAIL] = taskOrderPddStatusFail
	//jobs[md.KEY_CFG_CRON_PDDBYLOOPTIME] = taskLoopOrderPdd           //拼多多创建时间循环当天
	//jobs[md.KEY_CFG_CRON_PDDREFUND] = taskPddOrderRefund             //拼多多退款订单处理
	//jobs[md.KEY_CFG_CRON_PDDBYAGOTIME] = taskAgoOrderPdd             //用于恢复个别时间丢单的
	//jobs[md.KEY_CFG_CRON_PDDBYCREATETIME] = taskOrderPddByCreateTime //拼多多创建时间跟踪订单
	//jobs[md.KEY_CFG_CRON_PDD] = taskOrderPdd

	//v5-guide-settle
	//jobs[md.KEY_CFG_CRON_SETTLE] = taskOrderSettle // 结算
	//v5
	//jobs[md.KEY_CFG_CRON_FREE_SETTLE] = taskOrderFreeSettle                           // 结算
	//jobs[md.KEY_CFG_CRON_SECOND_FREE_SETTLE] = taskOrderSecondFreeSettle              // 结算
	//jobs[md.KEY_CFG_CRON_THIRD_FREE_SETTLE] = taskOrderMoreFreeSettle                 // 结算
	//jobs[md.KEY_CFG_CRON_AGGREGATION_RECHARGE_SETTLE] = taskAggregationRechargeSettle //
	//jobs[md.KEY_CFG_CRON_PLAYLET_SETTLE] = taskAggregationPlaylet                     //
	//jobs[md.KEY_CFG_CRON_DUOYOUORD_SETTLE] = taskDuoYouSettle                         //
	//jobs[md.KEY_CFG_CRON_LIANLIAN_SETTLE] = taskLianlianSettle                        //
	//jobs[md.KEY_CFG_CRON_SWIPE_SETTLE] = taskSwipeSettle                              //
	//jobs[md.KEY_CFG_CRON_USER_LV_UP_SETTLE] = taskUserLvUpOrderSettle                 // 会员费订单结算
	//jobs[md.KEY_CFG_CRON_PRIVILEGE_CARD_SETTLE] = taskPrivilegeCardOrderSettle        // 权益卡订单结算
	//jobs[md.KEY_CFG_CRON_ACQUISITION_CONDITION] = taskAcquisitionCondition
	//jobs[md.KEY_CFG_CRON_ACQUISITION_CONDITION_BY_LV] = taskAcquisitionConditionByLv
	//jobs[md.KEY_CFG_CRON_ACQUISITION_REWARD] = taskAcquisitionReward
	//jobs[md.KEY_CFG_CRON_NEW_ACQUISTION_SETTLE] = taskNewAcquisition // 拉新
	//jobs[md.KEY_CFG_CRON_ACQUISTION_SETTLE] = taskAcquisition        // 拉新
	//jobs[md.KEY_CFG_VERIFY] = taskVerify                             //团长

	//v7
	//jobs[md.KEY_CFG_CRON_JD] = taskOrderJd
	//jobs[md.KEY_CFG_CRON_JDFAILBYCREATETIME] = taskOrderJDFailByCreateTime //拼多多创建时间跟踪订单
	//jobs[md.KEY_CFG_CRON_JDBYCREATETIME] = taskOrderJDByCreateTime         //拼多多创建时间跟踪订单
	//jobs[md.KEY_CFG_CRON_JDBYSUCCESS] = taskOrderJDBySuccess               //拼多多创建时间跟踪订单
	//jobs[md.KEY_CFG_CRON_ORDER_SUCCESS_CHECK] = taskOrderSuccessCheck      //
	//jobs[md.KEY_CFG_CRON_JDBYSTATUS] = taskOrderJdStatus
	//jobs[md.KEY_CFG_CRON_JDREFUND] = taskJdOrderRefund //京东退款订单处理

	//v6
	jobs[md.KEY_CFG_CRON_TIKTOKLIFE] = taskOrderTikTokLife                          //抖音本地生活
	jobs[md.KEY_CFG_KUAISHOU_AUTH] = taskKuaishouAuth                               //团长
	jobs[md.KEY_CFG_TIK_TOK_TEAM_ORDER_PAY] = taskTikTokTeamOrder                   //团长
	jobs[md.KEY_CFG_TIK_TOK_TEAM_ORDER_UPDATE] = taskTikTokTeamOrderUpdate          //团长
	jobs[md.KEY_CFG_TIK_TOK_TEAM_USER_BIND_BUYINID] = taskTikTokTeamUserBindBuyinid //达人buyin_id
	jobs[md.KEY_CFG_KUAISHOU_TEAM_ORDER_PAY] = taskKuaishouTeamOrder                //团长
	jobs[md.KEY_CFG_KUAISHOU_TEAM_ORDER_UPDATE] = taskKuaishouTeamOrderUpdate       //团长
	jobs[md.KEY_CFG_CRON_AUTO_ADD_TIKTOK_GOODS] = taskAutoAddTikTokGoods            //
	jobs[md.KEY_CFG_CRON_KuaishouOwn] = taskOrderKuaishouOwn                        //
	jobs[md.KEY_CFG_CRON_KuaishouOwnCreate] = taskOrderKuaishouOwnCreate            //
	jobs[md.KEY_CFG_CRON_KUAISHOU] = taskOrderKuaishou                              //
	jobs[md.KEY_CFG_CRON_KUAISHOULIVE] = taskOrderKuaishouLive                      //
	jobs[md.KEY_CFG_CRON_TIKTOKCsjp] = taskOrderTIKTokCsjp                          //
	jobs[md.KEY_CFG_CRON_TIKTOKCsjpLive] = taskOrderTIKTokCsjpLive                  //
	jobs[md.KEY_CFG_CRON_TIKTOKOwnCsjp] = taskOrderTIKTokOwnCsjp                    //
	jobs[md.KEY_CFG_CRON_TIKTOKOwnCsjpLive] = taskOrderTIKTokOwnCsjpLive            //
	jobs[md.KEY_CFG_CRON_TIKTOKOwnCsjpActivity] = taskOrderTIKTokOwnCsjpActivity    //
	jobs[md.KEY_CFG_CRON_KUAISHOUOFFICIAL] = taskOrderKuaishouOfficial              //
	jobs[md.KEY_CFG_CRON_KUAISHOUOFFICIALLive] = taskOrderKuaishouOfficialLive      //

	//v8
	//jobs[md.KEY_CFG_CRON_MEITUANLM_START] = taskOrderMeituanLmStart //智盟返回的美团联盟
	//jobs[md.KEY_CFG_CRON_MEITUAN_START] = taskOrderMeituanStart     //智盟返回的美团联盟
	//jobs[md.KEY_CFG_CRON_MEITUAN] = taskOrderMeituan
	//jobs[md.KEY_CFG_CRON_MEITUANLM] = taskOrderMeituanLm               //智盟返回的美团联盟
	//jobs[md.KEY_CFG_CRON_STATIONMEITUANLM] = taskOrderStationMeituanLm //站长自己美团联盟
	//jobs[md.KEY_CFG_CRON_MEITUANOFFICIAL] = taskOrderMeituanOfficial   //站长自己美团联盟

	//v9
	//jobs[md.KEY_CFG_CRON_ELM] = taskOrderElm               //
	//jobs[md.KEY_CFG_CRON_HEYTEA] = taskOrderHeytea         //海威365喜茶
	//jobs[md.KEY_CFG_CRON_PIZZA] = taskOrderPizza           //海威365
	//jobs[md.KEY_CFG_CRON_WALLACE] = taskOrderWallace       //海威365
	//jobs[md.KEY_CFG_CRON_TOURISM] = taskOrderTourism       //海威365
	//jobs[md.KEY_CFG_CRON_FLOWERCAKE] = taskOrderFlowerCake //海威365
	//jobs[md.KEY_CFG_CRON_DELIVERY] = taskOrderDelivery     //海威365
	//jobs[md.KEY_CFG_CRON_BURGERKING] = taskOrderBurgerKing //海威365汉堡王
	//jobs[md.KEY_CFG_CRON_STARBUCKS] = taskOrderStarbucks   //海威365星巴克
	//jobs[md.KEY_CFG_CRON_MCDONALD] = taskOrderMcdonald     //海威365麦当劳
	//jobs[md.KEY_CFG_CRON_HWMOVIE] = taskOrderHwMovie       //海威365电影票
	//jobs[md.KEY_CFG_CRON_NAYUKI] = taskOrderNayuki         //海威365奈雪
	//jobs[md.KEY_CFG_CRON_TO_KFC] = taskOrderToKfc          //海威365
	//jobs[md.KEY_CFG_CRON_PAGODA] = taskOrderPagoda         //海威365
	//jobs[md.KEY_CFG_CRON_LUCKIN] = taskOrderLuckin         //海威365

	//v10

	////jobs[md.KEY_CFG_CRON_WPHREFUND] = taskWphOrderRefund //唯品会退款订单处理
	////jobs[md.KEY_CFG_CRON_VIP] = taskOrderVip
	//jobs[md.KEY_CFG_CRON_KFC] = taskOrderKfc
	//jobs[md.KEY_CFG_CRON_CINEMA] = taskOrderCinema
	//jobs[md.KEY_CFG_CRON_DUOMAI] = taskOrderDuomai //多麦跟单
	//jobs[md.KEY_CFG_CRON_PLAYLET_ORDER] = taskPlayletOrder
	//jobs[md.KEY_CFG_CRON_DIDI_ENERGY] = taskOrderDidiEnergy        //滴滴加油
	//jobs[md.KEY_CFG_CRON_T3_CAR] = taskOrderT3Car                  //T3打车
	//jobs[md.KEY_CFG_CRON_DIDI_ONLINE_CAR] = taskOrderDidiOnlineCar //滴滴网约车
	//jobs[md.KEY_CFG_CRON_KING_FLOWER] = taskOrderKingFlower        //滴滴网约车
	//jobs[md.KEY_CFG_CRON_DIDI_FREIGHT] = taskOrderDidiFreight      //滴滴货运
	//jobs[md.KEY_CFG_CRON_DIDI_CHAUFFEUR] = taskOrderDidiChauffeur  //滴滴代驾
	//jobs[md.KEY_CFG_CRON_OILSTATION] = taskOrderOilstation
	//jobs[md.KEY_CFG_CRON_BRIGHTOILSTATION] = taskOrderBrightOilstation
	//jobs[md.KEY_CFG_CRON_SN] = taskOrderSuning
	//jobs[md.KEY_CFG_CRON_KL] = taskOrderKaola

	////原来的
	//jobs[md.KEY_CFG_CRON_PlayLet_Total] = taskPlayletTotal                   //
	//jobs[md.KEY_CFG_CRON_CHECK_GUIDE_STORE_ORDER] = taskCheckGuideStoreOrder //
	//jobs[md.KEY_CFG_CRON_FAST_REFUND] = taskOrderFastRefund                  //
	//jobs[md.KEY_CFG_CRON_FAST_SUCCESS] = taskOrderFastSuccess                //
	//jobs[md.KEY_CFG_CRON_DUOYOUORD] = taskOrderDuoYouOrd                     //
	//jobs[md.KEY_CFG_CRON_TASKBOX] = taskOrderTaskBoxOrd                      //
	//jobs[md.KEY_CFG_CRON_TASKBOXSECOND] = taskOrderTaskSecondOrd             //
	//jobs[md.KEY_CFG_CRON_CARD_CHECK_UPDATE] = taskCardCheckUpdate            //权益卡退款
	//jobs[md.KEY_CFG_CRON_CARD_UPDATE] = taskCardUpdate                       // 权益卡更新
	//jobs[md.KEY_CFG_CRON_ORDER_STAT] = taskOrderStat                         // 订单统计
	//jobs[md.KEY_CFG_CRON_GOODS_SHELF] = taskGoodsShelf                       //站内商品上下架
	//jobs[md.KEY_CFG_CRON_CARD_RETURN] = taskCardReturn                       //权益卡退款
	//jobs[md.KEY_CFG_CRON_DTKBRAND] = taskTaoKeBrandInfo                      // 大淘客品牌信息
	//jobs[md.KEY_CFG_CRON_PUBLISHER] = taskTaobaoPublisherInfo                // 淘宝备案信息绑定
	//jobs[md.KEY_CFG_CRON_AUTO_UN_FREEZE] = taskAutoUnFreeze                  // 定时解冻

	//先不用
	//jobs[md.ZhimengCronPlayletVideoOrder] = taskPlayletVideoOrder                             //
	//jobs[md.ZhimengCronPlayletVideoOrderYesterDay] = taskPlayletVideoOrderYesterday           //
	//jobs[md.ZhimengCronPlayletVideoOrderMonth] = taskPlayletVideoOrderMonth                   //
	//jobs[md.ZhimengCronPlayletAdvOrderMonth] = taskPlayletAdvOrderMonth                       //
	//jobs[md.ZhimengCronPlayletAdvOrder] = taskPlayletAdvOrder                                 //
	//jobs[md.ZhimengCronPlayletAdvOrderYesterDay] = taskPlayletAdvOrderYesterday               //
	//jobs[md.ZhimengCronPlayletAdvOrderYesterDayToMoney] = taskPlayletAdvOrderYesterdayToMoney //

}
