package schedule

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

func AutoTaskReview() error {
	// 新建一个定时任务对象
	crontab := cron.New(cron.WithSeconds()) // 精确到秒
	spec := "0 */1 * * * ?"                 //cron表达式，每1分钟一次
	// "0 0 12 * * ?" 每天中午12点执行

	// 添加定时任务
	// 定时任务1  品牌种草API自动审核
	_, err1 := crontab.AddFunc(spec, AutoProjectReviewTask)
	if err1 != nil {
		return err1
	}
	// 定时任务2  电商带货API自动审核
	_, err2 := crontab.AddFunc(spec, AutoSelectionReviewTask)
	if err2 != nil {
		return err2
	}
	// 定时任务3  本地生活API自动审核
	_, err3 := crontab.AddFunc(spec, AutoLocalLifeReviewTask)
	if err3 != nil {
		return err3
	}

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

// 定时任务1  品牌种草API自动审核
func AutoProjectReviewTask() {
	log.Println("AutoProjectReviewTask running Start, Time :", time.Now())

	var reviewProjects []*entity.ReviewProject
	reviewProjects, _ = dao.ProjectReviewDao{}.GetProjectReviewByStatus(1)
	reviewService := review_service.GetConfig()
	for _, reviewProject := range reviewProjects {

		if reviewProject.TaskName != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewProject.TaskName)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.ProjectReviewDao{}.UpdateProjectReview(&entity.ReviewProject{ID: reviewProject.ID, Status: 3})
				continue
			}
		}

		if reviewProject.TaskDetail != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewProject.TaskDetail)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.ProjectReviewDao{}.UpdateProjectReview(&entity.ReviewProject{ID: reviewProject.ID, Status: 3})
				continue
			}
		}

		if reviewProject.ProductName != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewProject.ProductName)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.ProjectReviewDao{}.UpdateProjectReview(&entity.ReviewProject{ID: reviewProject.ID, Status: 3})
				continue
			}
		}

		if reviewProject.ProductDetail != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewProject.ProductDetail)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				dao.ProjectReviewDao{}.UpdateProjectReview(&entity.ReviewProject{ID: reviewProject.ID, Status: 3})
				continue
			}
		}

		if reviewProject.MainPhoto != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckImage(reviewProject.MainPhoto)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.ProjectReviewDao{}.UpdateProjectReview(&entity.ReviewProject{ID: reviewProject.ID, Status: 3})
				continue
			}
		}

		images := strings.Split(reviewProject.Images, ",")
		imageFlag := false
		for _, image := range images {
			if image != "" {
				var suggestion *string
				var reviewErr error
				i := 3
				for {
					suggestion, reviewErr = reviewService.CheckImage(image)
					if reviewErr == nil || i == 0 {
						break
					}
					i -= 1
				}
				if reviewErr != nil || "pass" != *suggestion {
					imageFlag = true
					break
				}
			}
		}
		if imageFlag {
			// 交给人工审核
			_ = dao.ProjectReviewDao{}.UpdateProjectReview(&entity.ReviewProject{ID: reviewProject.ID, Status: 3})
			continue
		}

		documentJobIds := strings.Split(reviewProject.DocumentJobIds, ",")
		documentFlag := 0 // 0通过 1未通过 2正在运行
		for _, documentJobId := range documentJobIds {
			if documentJobId != "" {
				var status *string
				var suggestion *string
				var reviewErr error
				i := 3
				for {
					status, suggestion, reviewErr = reviewService.CheckDocumentInfo(documentJobId)
					if reviewErr == nil || i == 0 {
						break
					}
					i -= 1
				}
				if reviewErr == nil && "running" == *status {
					documentFlag = 2
					break
				}
				if reviewErr != nil || "failed" == *status || "pass" != *suggestion {
					documentFlag = 1
					break
				}
			}
		}
		if documentFlag == 2 {
			continue
		} else if documentFlag == 1 {
			// 交给人工审核
			_ = dao.ProjectReviewDao{}.UpdateProjectReview(&entity.ReviewProject{ID: reviewProject.ID, Status: 3})
			continue
		}

		videoJobIds := strings.Split(reviewProject.VideoJobIds, ",")
		videoFlag := 0 // 0通过 1未通过 2正在运行
		for _, videoJobId := range videoJobIds {
			if videoJobId != "" {
				var status *string
				var suggestion *string
				var reviewErr error
				i := 3
				for {
					status, suggestion, reviewErr = reviewService.CheckVideoInfo(videoJobId)
					if reviewErr == nil || i == 0 {
						break
					}
					i -= 1
				}
				if reviewErr == nil && "running" == *status {
					videoFlag = 2
					break
				}
				if reviewErr != nil || "failed" == *status || "pass" != *suggestion {
					videoFlag = 1
					break
				}
			}
		}
		if videoFlag == 2 {
			continue
		} else if videoFlag == 1 {
			// 交给人工审核
			_ = dao.ProjectReviewDao{}.UpdateProjectReview(&entity.ReviewProject{ID: reviewProject.ID, Status: 3})
			continue
		}

		// 审核通过
		t := time.Now()
		_ = dao.ProjectReviewDao{}.UpdateProjectReview(&entity.ReviewProject{ID: reviewProject.ID, Status: 2})
		_ = dao.ProjectDAO{}.UpdateProject(entity.Project{ProjectId: reviewProject.ProjectID, ProjectStatus: 4, PassAt: t})
	}

	log.Println("AutoProjectReviewTask running End, Time :", time.Now())
}

// 定时任务2  电商带货API自动审核
func AutoSelectionReviewTask() {
	log.Println("AutoSelectionInvalidTask running Start, Time :", time.Now())

	var reviewSelections []*entity.ReviewSelection
	reviewSelections, _ = dao.SelectionReviewDao{}.GetSelectionReviewByStatus(1)
	reviewService := review_service.GetConfig()
	for _, reviewSelection := range reviewSelections {

		if reviewSelection.TaskName != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewSelection.TaskName)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.SelectionReviewDao{}.UpdateSelectionReview(&entity.ReviewSelection{ID: reviewSelection.ID, Status: 3})
				continue
			}
		}

		if reviewSelection.ProductName != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewSelection.ProductName)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.SelectionReviewDao{}.UpdateSelectionReview(&entity.ReviewSelection{ID: reviewSelection.ID, Status: 3})
				continue
			}
		}

		if reviewSelection.ProductDetail != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewSelection.ProductDetail)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				dao.SelectionReviewDao{}.UpdateSelectionReview(&entity.ReviewSelection{ID: reviewSelection.ID, Status: 3})
				continue
			}
		}

		if reviewSelection.MainPhoto != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckImage(reviewSelection.MainPhoto)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.SelectionReviewDao{}.UpdateSelectionReview(&entity.ReviewSelection{ID: reviewSelection.ID, Status: 3})
				continue
			}
		}

		images := strings.Split(reviewSelection.Images, ",")
		imageFlag := false
		for _, image := range images {
			if image != "" {
				var suggestion *string
				var reviewErr error
				i := 3
				for {
					suggestion, reviewErr = reviewService.CheckImage(image)
					if reviewErr == nil || i == 0 {
						break
					}
					i -= 1
				}
				if reviewErr != nil || "pass" != *suggestion {
					imageFlag = true
					break
				}
			}
		}
		if imageFlag {
			// 交给人工审核
			_ = dao.SelectionReviewDao{}.UpdateSelectionReview(&entity.ReviewSelection{ID: reviewSelection.ID, Status: 3})
			continue
		}

		documentJobIds := strings.Split(reviewSelection.DocumentJobIds, ",")
		documentFlag := 0 // 0通过 1未通过 2正在运行
		for _, documentJobId := range documentJobIds {
			if documentJobId != "" {
				var status *string
				var suggestion *string
				var reviewErr error
				i := 3
				for {
					status, suggestion, reviewErr = reviewService.CheckDocumentInfo(documentJobId)
					if reviewErr == nil || i == 0 {
						break
					}
					i -= 1
				}
				if reviewErr == nil && "running" == *status {
					documentFlag = 2
					break
				}
				if reviewErr != nil || "failed" == *status || "pass" != *suggestion {
					documentFlag = 1
					break
				}
			}
		}
		if documentFlag == 2 {
			continue
		} else if documentFlag == 1 {
			// 交给人工审核
			_ = dao.SelectionReviewDao{}.UpdateSelectionReview(&entity.ReviewSelection{ID: reviewSelection.ID, Status: 3})
			continue
		}

		videoJobIds := strings.Split(reviewSelection.VideoJobIds, ",")
		videoFlag := 0 // 0通过 1未通过 2正在运行
		for _, videoJobId := range videoJobIds {
			if videoJobId != "" {
				var status *string
				var suggestion *string
				var reviewErr error
				i := 3
				for {
					status, suggestion, reviewErr = reviewService.CheckVideoInfo(videoJobId)
					if reviewErr == nil || i == 0 {
						break
					}
					i -= 1
				}
				if reviewErr == nil && "running" == *status {
					videoFlag = 2
					break
				}
				if reviewErr != nil || "failed" == *status || "pass" != *suggestion {
					videoFlag = 1
					break
				}
			}
		}
		if videoFlag == 2 {
			continue
		} else if videoFlag == 1 {
			// 交给人工审核
			_ = dao.SelectionReviewDao{}.UpdateSelectionReview(&entity.ReviewSelection{ID: reviewSelection.ID, Status: 3})
			continue
		}

		// 审核通过
		t := time.Now()
		_ = dao.SelectionReviewDao{}.UpdateSelectionReview(&entity.ReviewSelection{ID: reviewSelection.ID, Status: 2})
		// 非悬赏任务审核通过直接变为执行中
		selection, _ := dao.SelectionInfoDAO{}.GetSelectionInfoById(reviewSelection.SelectionID)
		if selection != nil && selection.TaskMode == 2 {
			_ = dao.SelectionInfoDAO{}.UpdateSelectionInfo(entity.SelectionInfo{SelectionID: reviewSelection.SelectionID, SelectionStatus: 6, PassAt: t})
		} else {
			_ = dao.SelectionInfoDAO{}.UpdateSelectionInfo(entity.SelectionInfo{SelectionID: reviewSelection.SelectionID, SelectionStatus: 4, PassAt: t})
		}
	}

	log.Println("AutoSelectionInvalidTask running End, Time :", time.Now())
}

// 定时任务3  本地生活API自动审核
func AutoLocalLifeReviewTask() {
	log.Println("AutoLocalLifeInvalidTask running Start, Time :", time.Now())

	var reviewLocalLifes []*entity.ReviewLocalLife
	reviewLocalLifes, _ = dao.LocalLifeReviewDao{}.GetLocalReviewByStatus(1)
	reviewService := review_service.GetConfig()
	for _, reviewLocalLife := range reviewLocalLifes {

		if reviewLocalLife.TaskName != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewLocalLife.TaskName)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
				continue
			}
		}

		if reviewLocalLife.TaskDetail != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewLocalLife.TaskDetail)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
				continue
			}
		}

		if reviewLocalLife.StoreName != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewLocalLife.StoreName)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
				continue
			}
		}
		if reviewLocalLife.StoreDetail != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewLocalLife.StoreDetail)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
				continue
			}
		}
		if reviewLocalLife.StoreMainPhoto != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckImage(reviewLocalLife.StoreMainPhoto)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
				continue
			}
		}

		if reviewLocalLife.TeamBuyingName != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewLocalLife.TeamBuyingName)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
				continue
			}
		}
		if reviewLocalLife.TeamBuyingDetail != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckContent(reviewLocalLife.TeamBuyingDetail)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
				continue
			}
		}
		if reviewLocalLife.TeamBuyingMainPhoto != "" {
			var suggestion *string
			var reviewErr error
			i := 3
			for {
				suggestion, reviewErr = reviewService.CheckImage(reviewLocalLife.TeamBuyingMainPhoto)
				if reviewErr == nil || i == 0 {
					break
				}
				i -= 1
			}
			if reviewErr != nil || "pass" != *suggestion {
				// 交给人工审核
				_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
				continue
			}
		}

		images := strings.Split(reviewLocalLife.Images, ",")
		imageFlag := false
		for _, image := range images {
			if image != "" {
				var suggestion *string
				var reviewErr error
				i := 3
				for {
					suggestion, reviewErr = reviewService.CheckImage(image)
					if reviewErr == nil || i == 0 {
						break
					}
					i -= 1
				}
				if reviewErr != nil || "pass" != *suggestion {
					imageFlag = true
					break
				}
			}
		}
		if imageFlag {
			// 交给人工审核
			_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
			continue
		}

		documentJobIds := strings.Split(reviewLocalLife.DocumentJobIds, ",")
		documentFlag := 0 // 0通过 1未通过 2正在运行
		for _, documentJobId := range documentJobIds {
			if documentJobId != "" {
				var status *string
				var suggestion *string
				var reviewErr error
				i := 3
				for {
					status, suggestion, reviewErr = reviewService.CheckDocumentInfo(documentJobId)
					if reviewErr == nil || i == 0 {
						break
					}
					i -= 1
				}
				if reviewErr == nil && "running" == *status {
					documentFlag = 2
					break
				}
				if reviewErr != nil || "failed" == *status || "pass" != *suggestion {
					documentFlag = 1
					break
				}
			}
		}
		if documentFlag == 2 {
			continue
		} else if documentFlag == 1 {
			// 交给人工审核
			_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
			continue
		}

		videoJobIds := strings.Split(reviewLocalLife.VideoJobIds, ",")
		videoFlag := 0 // 0通过 1未通过 2正在运行
		for _, videoJobId := range videoJobIds {
			if videoJobId != "" {
				var status *string
				var suggestion *string
				var reviewErr error
				i := 3
				for {
					status, suggestion, reviewErr = reviewService.CheckVideoInfo(videoJobId)
					if reviewErr == nil || i == 0 {
						break
					}
					i -= 1
				}
				if reviewErr == nil && "running" == *status {
					videoFlag = 2
					break
				}
				if reviewErr != nil || "failed" == *status || "pass" != *suggestion {
					videoFlag = 1
					break
				}
			}
		}
		if videoFlag == 2 {
			continue
		} else if videoFlag == 1 {
			// 交给人工审核
			_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 3})
			continue
		}

		// 审核通过
		t := time.Now()
		_ = dao.LocalLifeReviewDao{}.UpdateLocalReview(&entity.ReviewLocalLife{ID: reviewLocalLife.ID, Status: 2})
		_ = dao.LocalLifeDao{}.UpdateLocal(entity.LocalLifeInfo{LocalID: reviewLocalLife.LocalID, TaskStatus: 4, PassAt: t})
	}

	log.Println("AutoLocalLifeInvalidTask running End, Time :", time.Now())
}
