package schedule

import (
	"github.com/robfig/cron/v3"
	"log"
	"time"
	"youngee_b_api/app/dao"
	"youngee_b_api/app/entity"
)

func AutoTaskExecute() error {
	// 新建一个定时任务对象
	crontab := cron.New(cron.WithSeconds()) // 精确到秒
	spec := "0 */10 * * * ?"                //cron表达式，每10分钟一次

	// 添加定时任务
	// 定时任务1  初稿未审稿自动执行
	_, err1 := crontab.AddFunc(spec, AutoSketchExecuteTask)
	if err1 != nil {
		return err1
	}
	// 定时任务2  链接未质检自动执行
	_, err2 := crontab.AddFunc(spec, AutoLinkExecuteTask)
	if err2 != nil {
		return err2
	}
	// 定时任务3  数据未质检自动执行
	_, err3 := crontab.AddFunc(spec, AutoDataExecuteTask)
	if err3 != nil {
		return err3
	}

	// 启动定时器
	crontab.Start()
	// 定时任务是另起协程执行的,这里使用 select 简单阻塞.需要根据实际情况进行控制
	//select {} //阻塞主线程停止
	return nil
}

// 定时任务1  初稿未审稿自动执行
func AutoSketchExecuteTask() {
	log.Println("AutoSketchExecuteTask running Start, Time :", time.Now())
	var sketchInfos []*entity.SketchInfo
	err := dao.Db.Model(&entity.SketchInfo{}).Where("is_review = ? and is_ok = ?", 0, 0).Select("sketch_id, task_id, auto_agree_at, task_type").Find(&sketchInfos).Error
	if err != nil {
		log.Println(err.Error())
		return
	}
	var sketchIds []int64
	var projectTaskIds []string
	var localTaskIds []string
	for _, sketchInfo := range sketchInfos {
		sketchId := sketchInfo.SketchID
		if time.Now().After(sketchInfo.AutoAgreeAt) {
			sketchIds = append(sketchIds, sketchId)
			if sketchInfo.TaskType == 1 {
				projectTaskIds = append(projectTaskIds, sketchInfo.TaskID)
			} else if sketchInfo.TaskType == 2 {
				localTaskIds = append(localTaskIds, sketchInfo.TaskID)
			}
		}
	}
	dao.Db.Model(&entity.SketchInfo{}).Where("sketch_id in ?", sketchIds).Updates(&entity.SketchInfo{
		IsReview:      1,
		IsOk:          1,
		AgreeAt:       time.Now(),
		BOperatorType: 1,
	})
	dao.Db.Model(&entity.ProjectTaskInfo{}).Where("task_id in ?", projectTaskIds).Updates(&entity.ProjectTaskInfo{
		TaskStage:    11,
		SketchStatus: 5,
	})
	dao.Db.Model(&entity.LocalLifeTaskInfo{}).Where("task_id in ?", localTaskIds).Updates(&entity.LocalLifeTaskInfo{
		TaskStage:    11,
		SketchStatus: 5,
	})
	log.Println("AutoSketchExecuteTask running End, Time :", time.Now())
}

// 定时任务2  链接未质检自动执行
func AutoLinkExecuteTask() {
	log.Println("AutoLinkExecuteTask running Start, Time :", time.Now())
	var LinInfos []*entity.LinkInfo
	err := dao.Db.Model(&entity.LinkInfo{}).Where("is_review = ? and is_ok = ?", 0, 0).Select("link_id, task_id, auto_agree_at, task_type").Find(&LinInfos).Error
	if err != nil {
		log.Println(err.Error())
		return
	}
	var linkIds []int64
	var projectTaskIds []string
	var localTaskIds []string
	for _, LinInfo := range LinInfos {
		linkId := LinInfo.LinkID
		if time.Now().After(LinInfo.AutoAgreeAt) {
			linkIds = append(linkIds, linkId)
			if LinInfo.TaskType == 1 {
				projectTaskIds = append(projectTaskIds, LinInfo.TaskID)
			} else if LinInfo.TaskType == 2 {
				localTaskIds = append(localTaskIds, LinInfo.TaskID)
			}
		}
	}
	dao.Db.Model(&entity.LinkInfo{}).Where("link_id in ?", linkIds).Updates(&entity.LinkInfo{
		IsReview:      1,
		IsOk:          1,
		AgreeAt:       time.Now(),
		BOperatorType: 1,
	})
	dao.Db.Model(&entity.ProjectTaskInfo{}).Where("task_id in ?", projectTaskIds).Updates(&entity.ProjectTaskInfo{
		TaskStage:  13,
		LinkStatus: 5,
	})
	dao.Db.Model(&entity.LocalLifeTaskInfo{}).Where("task_id in ?", localTaskIds).Updates(&entity.LocalLifeTaskInfo{
		TaskStage:  13,
		LinkStatus: 5,
	})
	log.Println("AutoLinkExecuteTask running End, Time :", time.Now())
}

// 定时任务3  数据未质检自动执行
func AutoDataExecuteTask() {
	log.Println("AutoDataExecuteTask running Start, Time :", time.Now())
	var dataInfos []*entity.DataInfo
	err := dao.Db.Model(&entity.DataInfo{}).Where("is_review = ? and is_ok = ?", 0, 0).Select("data_id, task_id, auto_agree_at, task_type").Find(&dataInfos).Error
	if err != nil {
		log.Println(err.Error())
		return
	}
	var dataIds []int64
	var projectTaskIds []string
	var localTaskIds []string
	for _, dataInfo := range dataInfos {
		dataId := dataInfo.DataID
		if time.Now().After(dataInfo.AutoAgreeAt) {
			dataIds = append(dataIds, dataId)
			if dataInfo.TaskType == 1 {
				projectTaskIds = append(projectTaskIds, dataInfo.TaskID)
			} else if dataInfo.TaskType == 2 {
				localTaskIds = append(localTaskIds, dataInfo.TaskID)
			}
		}
	}
	dao.Db.Model(&entity.DataInfo{}).Where("data_id in ?", dataIds).Updates(&entity.DataInfo{
		IsReview:      1,
		IsOk:          1,
		AgreeAt:       time.Now(),
		BOperatorType: 1,
	})
	dao.Db.Model(&entity.ProjectTaskInfo{}).Where("task_id in ?", projectTaskIds).Updates(&entity.ProjectTaskInfo{
		TaskStage:  15,
		DataStatus: 5,
	})
	dao.Db.Model(&entity.LocalLifeTaskInfo{}).Where("task_id in ?", localTaskIds).Updates(&entity.LocalLifeTaskInfo{
		TaskStage:  15,
		DataStatus: 5,
	})
	log.Println("AutoDataExecuteTask running End, Time :", time.Now())
}
