
package com.tmerclub.cloud.live.task;


import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.live.constant.LiveRoomStatusType;
import com.tmerclub.cloud.live.mapper.LiveRoomMapper;
import com.tmerclub.cloud.live.service.LiveRoomService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 直播的定时任务
 *
 * @author LHD
 */
@Component
public class LiveTask {

    private static final Logger logger = LoggerFactory.getLogger(LiveTask.class);

    @Autowired
    private LiveRoomService liveRoomService;
    @Autowired
    private LiveRoomMapper liveRoomMapper;

    /**
     * 定时改变直播间状态 半小时一次
     */
    @XxlJob("changeLiveRoomStatus")
    public void changeLiveRoomStatus() {
        // 查询要关播的直播间列表
        List<Long> endLiveRoomIds = liveRoomMapper.listLiveRoomByStatus(LiveRoomStatusType.FINISHED.value());
        if (CollUtil.isNotEmpty(endLiveRoomIds)) {
            // 更新直播间状态
            logger.info("直播间关播:{}", endLiveRoomIds);
            liveRoomService.updateLiveRoomStatus(LiveRoomStatusType.FINISHED.value(), endLiveRoomIds);
        }
    }
}
