package com.ruoyi.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Console;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.bgt.domain.BgtProjectRecruit;
import com.ruoyi.bgt.domain.BgtProjectRecruitApply;
import com.ruoyi.bgt.service.IBgtProjectRecruitApplyService;
import com.ruoyi.bgt.service.IBgtProjectRecruitService;
import com.ruoyi.common.enums.RecruitApplyStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务定时任务
 *
 * @author ruoyi
 */
@Component("businessTask")
public class BusinessTask
{
	@Autowired
	private IBgtProjectRecruitService recruitService;

	@Autowired
	private IBgtProjectRecruitApplyService recruitApplyService;
	/**
	 * 招工任务招工时间结束自动拒绝未选择的务工者
	 */
    public void recruitRefuse()
    {
		Console.log("开始招工拒绝任务");
		List<String> status = Arrays.asList(RecruitApplyStatus.SIGN_UP.getCode(), RecruitApplyStatus.BGT_PASS.getCode());
		List<BgtProjectRecruit> recruitList = recruitService.list(Wrappers.<BgtProjectRecruit>lambdaQuery()
			.lt(BgtProjectRecruit::getRecruitEndTime, LocalDate.now()));
		if(CollectionUtil.isNotEmpty(recruitList)){
			List<Long> recruitIds = recruitList.stream().map(BgtProjectRecruit::getId).collect(Collectors.toList());
			recruitApplyService.<BgtProjectRecruitApply>lambdaUpdate()
				.in(BgtProjectRecruitApply::getRecruitId, recruitIds)
				.in(BgtProjectRecruitApply::getStatus,status)
				.set(BgtProjectRecruitApply::getStatus,RecruitApplyStatus.BGT_REFUSE.getCode())
				.update();
		}
		Console.log("招工拒绝任务结束，此次拒绝任务个数："+recruitList.size());
	}

}
