package org.springblade.swzdr.timedTask;

import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.swzdr.pojo.entity.RyXsEntity;
import org.springblade.swzdr.service.IRyInfoService;
import org.springblade.swzdr.service.IRyXsService;
import org.springblade.swzdr.service.IRyXspzService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 反诈预警 定时任务
 */
@Slf4j
@Configuration
@EnableScheduling
public class SwzdrTimedTask extends BladeController {

	private IRyXsService xsService;

	private IRyXspzService xspzService;

	private IRyInfoService ryInfoService;

	/**
	 * 每天凌晨2点
	 * 国保期间写实任务
	 *
	 */
	@Transactional
	@Scheduled(cron = "0 0 2 * * ?")
	protected void gbXsrw() {
		// 查看swzdr_ry_xspz表，看今天在不在国保期间
		int isGbsj = xspzService.isGbsj();
		// 今天在国保期间 下发写实任务
		if (isGbsj >= 1) {
			xsrw();
		}
	}

	/**
	 * 每月1日凌晨2点
	 * 每月写实任务
	 */
	@Transactional
	@Scheduled(cron = "0 0 2 1 * ?")
	protected void myXsrw() {
		// 查看swzdr_ry_xspz表，看今天在不在国保期间
		int isGbsj = xspzService.isGbsj();
		// 今天不在国保期间 下发每月的写实任务
		if (isGbsj == 0) {
			xsrw();
		}
	}

	private void xsrw() {
		// 遍历涉稳重点人
		List<Long> userIdList = ryInfoService.getSwzdrList();
		// 下发任务
		for (Long userId : userIdList) {
			RyXsEntity ryXsEntity = new RyXsEntity();
			ryXsEntity.setId(userId);
			xsService.save(ryXsEntity);
		}
	}
}
