package org.springblade.modules.userJwd.task;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.modules.sqjw.jwtdzrq.service.IJwtdZrqService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.userJwd.service.UserJwdService;
import org.springblade.modules.userJwd.vo.UserJwdVO;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * 考勤 定时任务
 */
@Configuration
@EnableScheduling
@AllArgsConstructor
@Slf4j
public class UserJwdTask extends BladeController {

	private final UserJwdService userJwdService;

	private final IJwtdZrqService jwtdZrqService;

	/**
	 * 每日23时
	 */
	@Scheduled(cron = "0 0 23 * * ?")
//	@Scheduled(cron = "0 0/1 * * * ?")
	private void identityConfirm() {
		// 获取当天userId
		List<User> userIdList = userJwdService.getUserList();
		for (User user : userIdList) {
			// 获取打卡范围
			List<String> dkfwList = jwtdZrqService.getDkfw(user.getDeptId());
			// 获取所有打卡记录
			List<UserJwdVO> userJwdList = userJwdService.getByUserId(user.getId());
			// 判断打卡记录是否在所属社区内
			for (UserJwdVO userJwd : userJwdList) {
				// 每个所属社区都有可能
				for (String dkfw : dkfwList) {
					if (userJwdService.isContained(userJwd.getJwd(), dkfw)) {
						// 在范围内
						userJwdService.updateStatus(userJwd);
						break;
					}
				}
			}
		}
	}

}
