package com.fs.app.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.live.domain.Live;
import com.fs.live.service.ILiveService;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@AllArgsConstructor
public class Task {

    private final ILiveService liveService;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void selectSopUserLogsListByTime() {
        LocalDateTime now = LocalDateTime.now();
        List<Live> list = liveService.list(new QueryWrapper<Live>().ne("status", 3));
        list.forEach(live -> {
            if (now.isAfter(live.getStartTime()) && now.isBefore(live.getFinishTime())) {
                live.setStatus(2);
            } else if (now.isBefore(live.getStartTime())) {
                live.setStatus(1);
            } else if (now.isAfter(live.getFinishTime())) {
                live.setStatus(3);
            }
        });
        if(!list.isEmpty()){
            liveService.updateBatchById(list);
        }
    }
}
