package com.ruoyi.framework.task.confRoom;

import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.plant.domain.TConfInfo;
import com.ruoyi.project.plant.domain.TConfRoom;
import com.ruoyi.project.plant.domain.TMtActionlist;
import com.ruoyi.project.plant.domain.TMtKeymaintenance;
import com.ruoyi.project.plant.mapper.TConfInfoMapper;
import com.ruoyi.project.plant.mapper.TConfRoomMapper;
import com.ruoyi.project.plant.service.*;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("confInfoTask")
public class ConfInfoTask extends BaseController
{
    @Autowired
    private ITConfInfoService tConfInfoService;
    @Autowired
    private ITConfRoomService tConfRoomService;
    @Resource
    private TConfRoomMapper tConfRoomMapper;
    @Resource
    private TConfInfoMapper tConfInfoMapper;

    public void confInfoTask(Long RoomId)
    {
        TConfRoom room = tConfRoomMapper.selectTConfRoomById(RoomId);
        ;
        //周一
        TConfInfo week1 = new TConfInfo();
        week1.setRoomName(room.getRoomName());
        week1.setRoomId(RoomId);
        week1.setBeginDatetime(getTime(DayOfWeek.MONDAY,8,30));
        week1.setEndDatetime(getTime(DayOfWeek.MONDAY,9,0));
        week1.setSubject("早调会");
        checktime(week1);
        //周2
        TConfInfo week2 = new TConfInfo();
        week2.setRoomName(room.getRoomName());
        week2.setRoomId(RoomId);
        week2.setBeginDatetime(getTime(DayOfWeek.TUESDAY,8,30));
        week2.setEndDatetime(getTime(DayOfWeek.TUESDAY,9,0));
        week2.setSubject("早调会");
        checktime(week2);
        //周一
        TConfInfo week3 = new TConfInfo();
        week3.setRoomName(room.getRoomName());
        week3.setRoomId(RoomId);
        week3.setBeginDatetime(getTime(DayOfWeek.WEDNESDAY,8,30));
        week3.setEndDatetime(getTime(DayOfWeek.WEDNESDAY,9,0));
        week3.setSubject("早调会");
        checktime(week3);
        //周一
        TConfInfo week4 = new TConfInfo();
        week4.setRoomName(room.getRoomName());
        week4.setRoomId(RoomId);
        week4.setBeginDatetime(getTime(DayOfWeek.THURSDAY,8,30));
        week4.setEndDatetime(getTime(DayOfWeek.THURSDAY,9,0));
        week4.setSubject("早调会");
        checktime(week4);
        //周一
        TConfInfo week5 = new TConfInfo();
        week5.setRoomName(room.getRoomName());
        week5.setRoomId(RoomId);
        week5.setBeginDatetime(getTime(DayOfWeek.FRIDAY,8,30));
        week5.setEndDatetime(getTime(DayOfWeek.FRIDAY,9,0));
        week5.setSubject("早调会");
        checktime(week5);

    }
    public void checktime(TConfInfo tConfInfo) {
        List<TConfInfo> list = tConfInfoMapper.selectTConfInfoByTime(tConfInfo);
        if (list.size() > 0) {

        }else {
            tConfInfoMapper.insertTConfInfo(tConfInfo);
        }

    };


    private Date getTime(DayOfWeek monday, int h, int m) {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 计算下周一的日期
        LocalDateTime nextMonday = now.with(TemporalAdjusters.next(monday));

        // 获取下周一的早上8点半
        LocalDateTime localDateTime = nextMonday.with(LocalTime.of(h, m));

        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        return date;
    }



}
