package com.bonus.system.att.tasks;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.system.att.dao.AttGroupDao;
import com.bonus.system.att.dao.AttSourceDataDao;
import com.bonus.system.att.entity.*;
import com.bonus.system.att.utils.WorkdayCalculator;
import com.bonus.system.basic.domain.SysNotice;
import com.bonus.system.basic.service.SysNoticeService;
import com.bonus.system.holiday.entity.WorkReportBean;
import com.bonus.system.holiday.service.WorkReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @author fly
 * 消息推送
 *
 */
@Configuration
@EnableScheduling
@Slf4j
@EnableAsync
public class MsgTasks {


    /**
     * 消息推送库表
     */
    @Resource
    private SysNoticeService noticeService;

    /**
     * 工作安排报表
     */
    @Resource
    private WorkReportService workReportService;

    /**
     * 月异常报表消息推送
     * 每月1号7点执行的任务
     */
//    @Scheduled(fixedDelay = 300000)
    @Scheduled(cron = "0 0 7 1 * ?")
    @Async
    public void getMonthAttTask(){
        log.info("--------异常考勤消息推送定时器开启------");
        //1.查询月异常报表需要推送的人员（查询有权限查看这个模块的角色）
        List<Long> userList = noticeService.getUserIdByModelName("月异常报表");
        // 将 List 转换为数组
        Long[] userIds = userList.toArray(new Long[0]);
        SysNotice o = new SysNotice();
        o.setUserIds(userIds);
        o.setUserId(1L);
        o.setTitle("月异常考勤统计");
        o.setContent("月异常考勤统计");
        o.setType("62");
        // 获取当前日期
        DateTime currentDate = DateUtil.date();
        // 获取上个月
        DateTime previousMonthDate = DateUtil.offsetMonth(currentDate, -1);
        // 格式化为 yyyy-MM
        String formattedDate = DateUtil.format(previousMonthDate, "yyyy-MM");
        o.setValue(formattedDate);
        ////推送消息
        noticeService.insertNotice(o);
        log.info("--------异常考勤消息推送定时器开启------");
    }

    /**
     * 工作安排统计报表推送
     * 每月1号7点执行的任务
     */
//    @Scheduled(fixedDelay = 300000)
    @Scheduled(cron = "0 0 7 1 * ?")
//    @Scheduled(cron = "0 25 16 * * ?")
    @Async
    public void getWorkReportTask(){
        log.info("--------工作安排统计报表推送定时器开启------");
        // 获取当前日期
        DateTime currentDate = DateUtil.date();
        // 获取上个月
        DateTime previousMonthDate = DateUtil.offsetMonth(currentDate, -1);
        // 格式化为 yyyy-MM
        String month = DateUtil.format(previousMonthDate, "yyyy-MM");
        //查询数据
        List<WorkReportBean> list = workReportService.selectDataByMonth(month);
        //插入数据
        if (list.size() > 0) {
            for (WorkReportBean bean : list) {
                int re = workReportService.insertWorkReportData(bean);
                String[] chargeCheckIds = bean.getAttCheckId().split(",");
                Long[] userIds = Arrays.stream(chargeCheckIds).map(Long::valueOf).toArray(Long[]::new);
                SysNotice o = new SysNotice();
                o.setUserIds(userIds);
                o.setUserId(1L);
                o.setTitle("工作安排统计报表");
                o.setContent("工作安排统计报表");
                o.setType("72");
                o.setValue(bean.getMonth());
                ////推送消息
                noticeService.insertNotice(o);
            }
        }
        log.info("--------工作安排统计报表推送定时器开启------");
    }

    /**
     * 轮休临时外出异常统计消息推送
     * 每月1号7点执行的任务
     */
    @Scheduled(cron = "0 0 7 1 * ?")
    @Async
    public void getHolidayTask(){
        log.info("--------轮休临时外出异常推送定时器开启------");
        //1.查询月异常报表需要推送的人员（查询有权限查看这个模块的角色）
        List<Long> userList = noticeService.getUserIdByModelName("轮休临时外出异常报表");
        // 将 List 转换为数组
        Long[] userIds = userList.toArray(new Long[0]);
        SysNotice o = new SysNotice();
        o.setUserIds(userIds);
        o.setUserId(1L);
        o.setTitle("月轮休临时外出异常统计");
        o.setContent("月轮休临时外出异常统计");
        o.setType("63");
        // 获取当前日期
        DateTime currentDate = DateUtil.date();
        // 获取上个月
        DateTime previousMonthDate = DateUtil.offsetMonth(currentDate, -1);
        // 格式化为 yyyy-MM
        String formattedDate = DateUtil.format(previousMonthDate, "yyyy-MM");
        o.setValue(formattedDate);
        ////推送消息
        noticeService.insertNotice(o);
        log.info("--------轮休临时外出异常推送定时器开启------");
    }

}