package com.platform.modules.live.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.Query;
import com.platform.modules.live.dao.LiveSceneLiveInfoExtDao;
import com.platform.modules.live.entity.LiveSceneLiveInfoExtEntity;
import com.platform.modules.live.service.LiveSceneLiveInfoExtService;
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.CasterVideoResource;
import com.platform.plugs.live.responseEntity.LiveRecordVodConfig;


@Service("liveSceneLiveInfoExtService")
@Transactional
public class LiveSceneLiveInfoExtServiceImpl extends ServiceImpl<LiveSceneLiveInfoExtDao, LiveSceneLiveInfoExtEntity> implements LiveSceneLiveInfoExtService {
	@Autowired
    private LiveClient liveClient;

    @Override
    @Transactional(readOnly = true)
    public PageUtils queryPage(Map<String, Object> params) {
        Page<LiveSceneLiveInfoExtEntity> page = this.selectPage(
                new Query<LiveSceneLiveInfoExtEntity>(params).getPage(),
                new EntityWrapper<LiveSceneLiveInfoExtEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(readOnly = true)
    public LiveSceneLiveInfoExtEntity selectByInfoId(Integer infoId) {
        return selectOne(new EntityWrapper<LiveSceneLiveInfoExtEntity>().eq("info_id", infoId));
    }

    @Override
    public void saveOrUpdate(LiveSceneLiveInfoExtEntity infoExtEntity) {
        if(infoExtEntity.getId() == null)
            insert(infoExtEntity);
        else updateById(infoExtEntity);
    }
   
    /**
     * todo weiyan 20200228 兼容导播台播放器
     * 查询视频源
     * @param casterId 导播台ID
     * @return
     */
    @Override
	public RpcResponse<List<CasterVideoResource>> describeCasterVideoResources(String casterId){
    	RpcResponse<List<CasterVideoResource>> rpcResponse = liveClient.liveCaster().describeCasterVideoResources(casterId);
    	if(ResponseResult.SUCCESS == rpcResponse.getResult()){
    		List<CasterVideoResource> resourcesList = rpcResponse.getResponse();
    		if(resourcesList != null && resourcesList.size() > 0) {
    			for(CasterVideoResource resources : resourcesList){
    				if(StringUtils.isBlank(resources.getLiveStreamUrl())) continue;
    				LiveSceneLiveInfoExtEntity bean = selectOne(new EntityWrapper<LiveSceneLiveInfoExtEntity>().eq("play_url_rmtp", resources.getLiveStreamUrl()));
    				if(bean==null || bean.getId()==null) continue;
    				resources.setLiveStreamFlvUrl(bean.getPlayUrlFlv());
    				
    			}
    		}
    	}
		
    	return rpcResponse;
    }
    
}
