package com.hotwj.platform.securityManagement.securityMeeting.task;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hotwj.platform.securityManagement.securityMeeting.domain.HotSecurityMeeting;
import com.hotwj.platform.securityManagement.securityMeeting.mapper.HotSecurityMeetingMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
@RequiredArgsConstructor
public class HotSecurityMeetingStatusTask {

    private static final List<String> TARGET_MEETING_TYPES = Arrays.asList("produce", "example", "special", "offline");
    private static final String STATUS_NOT_STARTED = "1";
    private static final String STATUS_IN_PROGRESS = "2";
    private static final String STATUS_FINISHED = "3";

    private final HotSecurityMeetingMapper hotSecurityMeetingMapper;

    @Scheduled(cron = "0 * * * * ?")
    public void syncMeetingStatus() {
        Date now = new Date();
        updateToInProgress(now);
        updateToFinished(now);
    }

    private void updateToInProgress(Date now) {
        List<HotSecurityMeeting> meetings = hotSecurityMeetingMapper.selectList(
                Wrappers.<HotSecurityMeeting>lambdaQuery()
                        .in(HotSecurityMeeting::getMeetingType, TARGET_MEETING_TYPES)
                        .eq(HotSecurityMeeting::getMeetingStatus, STATUS_NOT_STARTED)
                        .isNotNull(HotSecurityMeeting::getStartTime)
                        .le(HotSecurityMeeting::getStartTime, now)
        );
        if (meetings.isEmpty()) {
            return;
        }
        List<Long> ids = meetings.stream().map(HotSecurityMeeting::getId).collect(Collectors.toList());
        int updated = hotSecurityMeetingMapper.update(
                null,
                Wrappers.<HotSecurityMeeting>lambdaUpdate()
                        .set(HotSecurityMeeting::getMeetingStatus, STATUS_IN_PROGRESS)
                        .in(HotSecurityMeeting::getId, ids)
                        .eq(HotSecurityMeeting::getMeetingStatus, STATUS_NOT_STARTED)
        );
        if (updated > 0) {
            log.info("安全会议状态更新为进行中，数量={}", updated);
        }
    }

    private void updateToFinished(Date now) {
        Date threshold = Date.from(now.toInstant().minus(1, ChronoUnit.HOURS));
        List<HotSecurityMeeting> meetings2 = hotSecurityMeetingMapper.selectList(Wrappers.<HotSecurityMeeting>lambdaQuery()
                .in(HotSecurityMeeting::getMeetingType, TARGET_MEETING_TYPES));
        List<HotSecurityMeeting> meetings = hotSecurityMeetingMapper.selectList(
                Wrappers.<HotSecurityMeeting>lambdaQuery()
                        .in(HotSecurityMeeting::getMeetingType, TARGET_MEETING_TYPES)
                        .eq(HotSecurityMeeting::getMeetingStatus, STATUS_IN_PROGRESS)
                        .isNotNull(HotSecurityMeeting::getEndTime)
                        .le(HotSecurityMeeting::getEndTime, threshold)
        );
        if (meetings.isEmpty()) {
            return;
        }
        List<Long> ids = meetings.stream().map(HotSecurityMeeting::getId).collect(Collectors.toList());
        int updated = hotSecurityMeetingMapper.update(
                null,
                Wrappers.<HotSecurityMeeting>lambdaUpdate()
                        .set(HotSecurityMeeting::getMeetingStatus, STATUS_FINISHED)
                        .in(HotSecurityMeeting::getId, ids)
                        .eq(HotSecurityMeeting::getMeetingStatus, STATUS_IN_PROGRESS)
        );
        if (updated > 0) {
            log.info("安全会议状态更新为已结束，数量={}", updated);
        }
    }
}
