package com.ruoyi.zhushi.job;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.zhushi.entity.DkAttendanceGroup;
import com.ruoyi.zhushi.entity.DkCheckInRecord;
import com.ruoyi.zhushi.mapper.DkRecordMapper;
import com.ruoyi.zhushi.util.Constans;
import com.ruoyi.zhushi.util.HolidayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class DkJob {

    @Autowired
    private DkRecordMapper dkRecordMapper;

    // 每天18执行
    @Scheduled(cron = "0 0 2 * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void executeTask() throws ParseException {
        // 任务逻辑
        System.out.println("执行工作日18点定时任务：" + System.currentTimeMillis());
        try {
            // 判断当天时间是否为节假日，为0的时候是工作日
//            String yearHoliday = HolidayUtil.isWorkingDay(LocalDate.now().toString());
//            if(yearHoliday.equals("0")){
                // 获取所有员工
            List<JSONObject> jsonObjects = dkRecordMapper.queryAllUsers();
            for (JSONObject jsonObject : jsonObjects) {
                // 获取员工id
                Long userId = jsonObject.getLong("userId");
                // 如果没有员工id，则跳过
                if (userId == null){
                    continue;
                }
                // 获取员工姓名
                String userName = jsonObject.getString("userName");

                // 获取员工昵称
                String nickName = jsonObject.getString("nickName");

                // 根据userId 查询考勤组信息
                DkAttendanceGroup appDTO = dkRecordMapper.queryConfigByUserId(userId);
                // 如果没有考勤组信息，则跳过
                if(appDTO == null){
                    continue;
                }
                // 获取员工打卡记录根据员工id
//                List<DkCheckInRecord> records = dkRecordMapper.getRecordHistory(userId);
//                if(records == null || records.size() == 0){
                DkCheckInRecord dkCheckInRecord = new DkCheckInRecord();
                dkCheckInRecord.setSysUserId(userId);
                dkCheckInRecord.setSysUserName(userName);
                dkCheckInRecord.setNickName(nickName);
                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(appDTO.getId());
                dkCheckInRecord.setAttendanceGroupName(appDTO.getName());
                dkRecordMapper.insertOrUpdate(dkCheckInRecord);
//                }

            }
//            }

            System.out.println("执行工作日18点定时任务：" + System.currentTimeMillis());
            // 具体任务逻辑
        } catch (Exception e) {
            System.out.println("执行工作日18点定时任务失败：" + e.getMessage());
        }
    }

    // 每周一到周五18点执行
//    @Scheduled(cron = "0 0 18 ? * MON-FRI")
    public void executeWeekdayTask() {
        System.out.println("执行工作日18点定时任务：" + System.currentTimeMillis());
        try {

            // 获取所有员工
            List<JSONObject> jsonObjects = dkRecordMapper.queryAllUsers();
            for (JSONObject jsonObject : jsonObjects) {
                // 获取员工id
                Long userId = jsonObject.getLong("userId");
                // 如果没有员工id，则跳过
                if (userId == null){
                    continue;
                }
                // 获取员工姓名
                String userName = jsonObject.getString("userName");

                // 根据userId 查询考勤组信息
                DkAttendanceGroup appDTO = dkRecordMapper.queryConfigByUserId(userId);
                // 如果没有考勤组信息，则跳过
                if(appDTO == null){
                    continue;
                }
                // 获取员工打卡记录根据员工id
                List<DkCheckInRecord> records = dkRecordMapper.getRecordHistory(userId);
                if(records == null || records.size() == 0){
                    DkCheckInRecord dkCheckInRecord = new DkCheckInRecord();
                    dkCheckInRecord.setSysUserId(userId);
                    dkCheckInRecord.setSysUserName(userName);
                    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(appDTO.getId());
                    dkCheckInRecord.setAttendanceGroupName(appDTO.getName());
                    dkRecordMapper.insertOrUpdate(dkCheckInRecord);
                }

            }

            System.out.println("执行工作日18点定时任务：" + System.currentTimeMillis());
            // 具体任务逻辑
        } catch (Exception e) {
            System.out.println("执行工作日18点定时任务：" + System.currentTimeMillis());
        }
    }



//    @Scheduled(cron = "0/10 * * * * MON-FRI") // 每分钟执行
//    public void testTask() {
//        System.out.println("执行工作日18点定时任务：" + System.currentTimeMillis());
//        try {
//            // 获取所有员工
//            List<JSONObject> jsonObjects = dkRecordMapper.queryAllUsers();
//            for (JSONObject jsonObject : jsonObjects) {
//                // 获取员工id
//                Long userId = jsonObject.getLong("userId");
//                // 如果没有员工id，则跳过
//                if (userId == null){
//                    continue;
//                }
//                // 获取员工姓名
//                String userName = jsonObject.getString("userName");
//
//                // 根据userId 查询考勤组信息
//                DkAttendanceGroup appDTO = dkRecordMapper.queryConfigByUserId(userId);
//                // 如果没有考勤组信息，则跳过
//                if(appDTO == null){
//                    continue;
//                }
//                // 获取员工打卡记录根据员工id
//                List<DkCheckInRecord> records = dkRecordMapper.getRecordHistory(userId);
//                if(records == null || records.size() == 0){
//                    DkCheckInRecord dkCheckInRecord = new DkCheckInRecord();
//                    dkCheckInRecord.setSysUserId(userId);
//                    dkCheckInRecord.setSysUserName(userName);
//                    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(appDTO.getId());
//                    dkCheckInRecord.setAttendanceGroupName(appDTO.getName());
//                    dkRecordMapper.insertOrUpdate(dkCheckInRecord);
//                }
//
//            }
//
//            System.out.println("执行工作日18点定时任务：" + System.currentTimeMillis());
//            // 具体任务逻辑
//        } catch (Exception e) {
//            System.out.println("执行工作日18点定时任务：" + System.currentTimeMillis());
//        }
//    }

}
