package main

import (
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/pushdata"

	"github.com/beego/beego/v2/server/web"
	serviceNodeTask "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/node_task"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/notify"
	serviceSummary "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/summary_evaluation/service"
	serviceTask "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/service"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
	_ "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/consumer"
)

func main() {
	startNodeTask()
	startSummaryEvaluation()
	startConfirmEvaluationScore()
	go serviceTask.CheckYesterdayTaskRecord()
	go notify.RunTaskSmsNotify()
	//go notify.AppMessageRun()
	go consumer.Run()
	go pushdata.PushData()
	web.Run()
}

// 定时任务-间隔发送评估环节
func startNodeTask() {
	go func() {
		nodeTaskService := serviceNodeTask.NewNodeTaskService()

		var duration time.Duration
		if constant.Env == "prd" {
			duration = time.Minute * 5
		} else {
			duration = time.Minute * 1
		}
		timer := time.NewTimer(duration)
		for {
			<-timer.C
			nowTime := time.Now()
			if err := nodeTaskService.SendEvaluationNode(nowTime); err != nil {
				log.Logger.Error(err.Error())
			}
			timer.Reset(duration) // 重置定时
		}
	}()
}

// 定时任务-间隔检查时间,发送周期评估
func startSummaryEvaluation() {
	go func() {
		var duration time.Duration
		if constant.Env == "prd" {
			duration = time.Minute * 5
		} else {
			duration = time.Minute * 1
		}
		timer := time.NewTimer(duration)
		for {
			<-timer.C
			if err := serviceSummary.TaskSendSummaryEvaluationV2(); err != nil {
				log.Logger.Error(err.Error())
			}
			timer.Reset(duration) // 重置定时
		}
	}()
}

// 定时自动确认周期评估考核结果
func startConfirmEvaluationScore() {
	go func() {
		var duration time.Duration
		if constant.Env == "prd" {
			duration = time.Minute * 5
		} else {
			duration = time.Minute * 1
		}
		timer := time.NewTimer(duration)
		for {
			<-timer.C
			if err := serviceSummary.TaskConfirmScore(); err != nil {
				log.Logger.Error(err.Error())
			}
			timer.Reset(duration) // 重置定时
		}
	}()
}