package com.ruoyi.zhushi.job;


import com.ruoyi.zhushi.entity.DkAttendanceGroupAndUser;
import com.ruoyi.zhushi.entity.DkCheckInRecord;
import com.ruoyi.zhushi.mapper.DkAttendanceGroupAndUserMapper;
import com.ruoyi.zhushi.mapper.DkRecordMapper;
import com.ruoyi.zhushi.util.Constans;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class DkJob {

    @Autowired
    private DkRecordMapper dkRecordMapper;
    @Autowired
    private DkAttendanceGroupAndUserMapper dkAttendanceGroupAndUserMapper;

    // 每天18执行
    //@Scheduled(cron = "0 0 2 * * ?")
    @Scheduled(cron = "${dk.schedule.cron}")
    public void executeTask() throws Exception {
        // 任务逻辑
        log.info("打卡初始化定时任务开始：" + LocalDateTime.now());
        try {
            // 获取所有员工
            List<DkAttendanceGroupAndUser> list = dkAttendanceGroupAndUserMapper.selectList();
            for (DkAttendanceGroupAndUser user : list) {
                DkCheckInRecord dkCheckInRecord = new DkCheckInRecord();
                dkCheckInRecord.setSysUserId(user.getUserId());
                dkCheckInRecord.setSysUserName(user.getUserName());
                dkCheckInRecord.setNickName(user.getNickName());
                dkCheckInRecord.setCheckInTime(LocalDateTime.now());
                dkCheckInRecord.setCheckInStatus(Constans.CHECK_IN_STATUS_0);
                dkCheckInRecord.setClockInStatus(Constans.CHECK_IN_STATUS_0);
                dkCheckInRecord.setClockOutStatus(Constans.CHECK_IN_STATUS_0);
                dkCheckInRecord.setAttendanceGroupId(user.getAttendanceTeamId());
                dkCheckInRecord.setAttendanceGroupName(user.getAttendanceGroupName());
                dkRecordMapper.insertOrUpdate(dkCheckInRecord);
            }
            log.info("打卡初始化定时任务结束：" + LocalDateTime.now());
            // 具体任务逻辑
        } catch (Exception e) {
            log.error("打卡初始化定时任务失败：" + e.getMessage(),e);
        }
    }

}
