package com.xiaoniu.subsystem.business.task;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoniu.subsystem.business.domain.Project;
import com.xiaoniu.subsystem.business.service.IProjectService;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 计划定时任务
 * @author: 孙松涛
 * @createTime: 2024/12/17 17:18
 */
@AllArgsConstructor
@Component
public class ProjectTask {

	private IProjectService projectService;


	/**
	 * @description: 待培训计划修改培训中
	 * @author: 孙松涛
	 * @createTime: 2024/12/17 17:16
	 */
	@ApiOperationSupport(order = 1)
	@Scheduled(cron = "0 0 0 * * ? ")
	public void setProjectToBeTrained() {
		List<Project> list = projectService.list(new LambdaQueryWrapper<Project>()
				.eq(Project::getIsDistribution, 1)
				.le(Project::getTrainingTime, new Date()));
		if (CollectionUtils.isNotEmpty(list)){
			projectService.updateBatchById(list.stream().peek(l ->
					l.setIsDistribution(2)
			).collect(Collectors.toList()));
		}
	}

	/**
	 * @description: 培训中计划修改已培训
	 * @author: 孙松涛
	 * @createTime: 2024/12/17 17:16
	 */
	@ApiOperationSupport(order = 2)
	@Scheduled(cron = "0 0 0 * * ? ")
	public void setProjectInTraining() {
		List<Project> list = projectService.list(new LambdaQueryWrapper<Project>()
				.eq(Project::getIsDistribution, 2)
				.lt(Project::getEndTime, new Date()));
		if (CollectionUtils.isNotEmpty(list)){
			projectService.updateBatchById(list.stream().peek(l ->
					l.setIsDistribution(3)
			).collect(Collectors.toList()));
		}
	}

}
