package com.platform.modules.live.task;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.platform.common.utils.DateUtils;
import com.platform.modules.live.constants.SceneConstants;
import com.platform.modules.live.entity.LiveCasterEntity;
import com.platform.modules.live.entity.LiveSceneEntity;
import com.platform.modules.live.entity.LiveStreamRecordTaskEntity;
import com.platform.modules.live.service.LiveCasterService;
import com.platform.modules.live.service.LiveSceneService;
import com.platform.modules.live.service.LiveStreamRecordTaskService;
import com.platform.plugs.common.rpc.ResponseResult;
import com.platform.plugs.common.rpc.RpcResponse;
import com.platform.plugs.live.LiveClient;
import com.platform.plugs.live.responseEntity.LiveRecordVodConfig;

/**
 * 定时清空录制配置
 * @Author: TONG
 * @CreateDate: 2019-04-21
 * 
 */
@Component("liveVodRecordDeleteTask")
public class LiveVodRecordDeleteTask {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private LiveSceneService liveSceneService;

	@Autowired
	private LiveCasterService liveCasterService;

	@Autowired
	private LiveClient liveClient;

	@Autowired
    private LiveStreamRecordTaskService liveStreamRecordTaskService;
	
	public void liveVodRecordDelete(){

		logger.info("定时清空录制配置，刷新时间为：" + DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN));

		RpcResponse<List<LiveRecordVodConfig>> response = liveClient.liveRecord().describeLiveRecordVodConfigs();
		if(ResponseResult.SUCCESS != response.getResult())
			return;
		List<LiveRecordVodConfig> configs = response.getResponse();
		if(configs == null || configs.size() <= 0) return;

		for(LiveRecordVodConfig config : configs) {
			String appName = config.getAppName();
			String streamName = config.getStreamName();
			LiveSceneEntity sceneEntity = null;
			if(SceneConstants.CASTER_APP_NAME.equals(appName)) {
				LiveCasterEntity casterEntity = liveCasterService.selectByAppNameAndStreamName(appName, streamName);
				if(casterEntity != null) {
					sceneEntity = liveSceneService.selectById(casterEntity.getSceneId());
				}
			} else {
				sceneEntity = liveSceneService.selectBySign(appName);
			}
			//不删除的情况1,2 continue，删除的情况3,4
			// 1、现场存在且不是回顾
			// 2、现场存在且是回顾且存在未完成转码任务
			// 3、现场存在且是回顾且不存在未完成的转码任务
			// 4、现场不存在
			if(sceneEntity != null) {// 现场不存在，或非回顾现场删除
				if(!SceneConstants.LIVE_STATE_REVIEW.equals(sceneEntity.getLiveState()) || (sceneEntity.getDeleteFlag().equals(1) && sceneEntity.getLiveEnd().after(new Date()))) {
					logger.info("现场存在非回顾或回收站未结束的现场，保留：" + appName + "	" + streamName);
					continue;//情况1，预告、直播中、空
				}
				//回顾现场判断是否存在未完成转码任务
				List<LiveStreamRecordTaskEntity> tasks = null;
				if(SceneConstants.CASTER_APP_NAME.equals(appName)) {
					tasks = liveStreamRecordTaskService.selectUnOkTasks(config.getDomainName(), appName, streamName);
				} else {
					tasks = liveStreamRecordTaskService.selectUnOkTasks(config.getDomainName(), appName);
				}
				if(tasks != null && tasks.size() > 0) {//情况2
					logger.info("现场存在是回顾，且有未完成转码任务，保留：" + appName + "	" + streamName);
					continue;
				}
				logger.info("现场存在是回顾，且没有未完成转码任务，删除：" + appName + "	" + streamName);
			} else {
				logger.info("现场不存在删除：" + appName + "	" + streamName);
			}
			liveClient.liveRecord().deleteLiveRecordVodConfig(appName, streamName);
		}
	}
}
