package com.ruoyi.system.service;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.HolidayUtils;
import com.ruoyi.system.domain.KaoqinRecord;
import com.ruoyi.system.mapper.KaoqinRecordMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class Task {

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private KaoqinRecordMapper kaoqinRecordMapper;

    /***
     * 定时插入未打卡数据
     */
    @Async
    //@Scheduled(cron = "0 0/2 * * * ? ")
    @Scheduled(cron = "0 1 0 * * ? ")
    public void thirdNucleicAcid() {
        //获取前一天的日期
        String data = DateUtils.plusSeconds(DateUtils.getDate(),-1,"yyyy-MM-dd");
        //判断前一天是否是法定节假日  0 上班 1周末 2节假日
        String jsonResult = HolidayUtils.request(data);
        if(!"0".equals(jsonResult)){
            //是节假日的话不需要插入数据，直接返回
            return;
        }
        //获取所有有部门的人员
        List<SysUser> userList = userMapper.selectUserListNoDept();
        //获取前一天的所有考勤记录
        KaoqinRecord kaoqinRecord = new KaoqinRecord();
        kaoqinRecord.setKaTime(data);
        List<KaoqinRecord> kaoqinRecordList = kaoqinRecordMapper.selectKaoqinRecordList(kaoqinRecord);
        for (SysUser sysUser : userList) {
            boolean bl = true;
            for (KaoqinRecord record : kaoqinRecordList) {
                if(record.getUserId().equals(sysUser.getUserId())){
                    bl = false;
                }
            }
            if(bl){
                //将日期拆分成年月日的数组
                String[] datas = data.split("-");
                KaoqinRecord kaoqinRecords = new KaoqinRecord();
                kaoqinRecords.setUserId(sysUser.getUserId());
                kaoqinRecords.setUserName(sysUser.getNickName());
                kaoqinRecords.setDeptId(sysUser.getDeptId());
                kaoqinRecords.setDeptName(sysUser.getDept().getDeptName());
                kaoqinRecords.setAncestors(sysUser.getDept().getAncestors());
                kaoqinRecords.setKaYear(datas[0]);
                kaoqinRecords.setKaMonth(datas[1]);
                kaoqinRecords.setKaDay(datas[2]);
                kaoqinRecords.setKaWeek(DateUtils.dateToWeek(data));
                kaoqinRecords.setKaTime(data);
                kaoqinRecords.setKaTypeAmIn("5");
                kaoqinRecords.setKaTypeAmOut("5");
                kaoqinRecords.setKaTypePmIn("5");
                kaoqinRecords.setKaTypePmOut("5");
                kaoqinRecords.setKaStatus("2");
                kaoqinRecords.setCreateTime(DateUtils.getNowDate());
                kaoqinRecordMapper.insertKaoqinRecord(kaoqinRecords);
            }

        }

    }
    //0 上班 1周末 2节假日
    /*public static void main(String[] args) {
        String data = DateUtils.plusSeconds(DateUtils.getDate(),-1,"yyyy-MM-dd");
        System.out.println(data);
    }*/


}
