package com.persagy.calendar.schedule;

import cn.hutool.core.collection.CollectionUtil;
import com.persagy.calendar.constant.WorkCalendarConstant;
import com.persagy.calendar.controller.CommonController;
import com.persagy.calendar.pojo.dto.WorkCalendar;
import com.persagy.calendar.pojo.vo.WorkCalendarDateQueryVO;
import com.persagy.calendar.service.IWorkCalendarService;
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.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * 每日定时解析工作历作息规则
 *
 * @author zhangqiankun
 * @version 1.0.0
 * @company persagy
 * @date 2023/1/14 14:36
 */
@Slf4j
@Component
public class WorkDateCalendarSchedule {

    @Autowired
    private CommonController commonController;

    @Autowired
    private IWorkCalendarService workCalendarService;

    @Scheduled(cron = "0 30 0 * * ?")
    //@Scheduled(cron = "0/30 * * * * ?")
    public void updateAlarmIsolationInfo() throws Exception {
        // 获取所有的集团和项目信息
        List<WorkCalendar> workCalendars = this.workCalendarService.lambdaQuery().eq(WorkCalendar::getDelFlag, WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR)
                .groupBy(Arrays.asList(WorkCalendar::getGroupCode, WorkCalendar::getProjectId)).select(WorkCalendar::getGroupCode, WorkCalendar::getProjectId)
                .list();
        if (CollectionUtil.isNotEmpty(workCalendars)) {
            String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            for (WorkCalendar workCalendar : workCalendars) {
                WorkCalendarDateQueryVO queryVO = new WorkCalendarDateQueryVO();
                queryVO.setUserId("systemId");
                queryVO.setGroupCode(workCalendar.getGroupCode());
                queryVO.setProjectId(workCalendar.getProjectId());
                queryVO.setWorkDateStart(now);
                queryVO.setWorkDateEnd(now);
                this.commonController.handleParseCalendarRule(queryVO);
            }
        }
    }

}
