package com.platform.modules.live.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.common.validator.Assert;
import com.platform.modules.live.aspect.LiveTenant;
import com.platform.modules.live.constants.SceneConstants;
import com.platform.modules.live.entity.LiveSceneEntity;
import com.platform.modules.live.entity.LiveSceneLiveInfoEntity;
import com.platform.modules.live.service.LiveChannelService;
import com.platform.modules.live.service.LiveSceneLiveInfoService;
import com.platform.modules.live.service.LiveSceneService;
import com.platform.modules.live.utils.ForeignProperties;
import com.platform.modules.live.utils.QrCodeUtils;
import com.platform.modules.live.vo.CasterConfigVo;
import com.platform.modules.live.vo.LiveCasterSubVo;
import com.platform.modules.live.vo.LiveInfoVo;
import com.platform.modules.live.vo.SceneEntityVo;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.plugs.common.rpc.RpcResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 现场表
 * 
 * @author TROY
 * @email 
 * @date 2019-01-19 14:50:50
 */
@RestController
@RequestMapping("/live/scene")
@Api(value = "(NEW)现场模块接口", tags = "(NEW)现场模块接口")
public class LiveSceneController extends AbstractController {
	@Autowired
	private LiveSceneService liveSceneService;
	@Autowired
	private LiveSceneLiveInfoService liveSceneLiveInfoService;
	@Autowired
	private LiveChannelService liveChannelService;

	/**
	 * 分页数据
	 */
	@GetMapping("/page")
	@RequiresPermissions("live:livescene:page")
	@ApiOperation("现场分页数据")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "标题", name = "title", paramType = "query", dataType = "String"),
			@ApiImplicitParam(value = "现场类型，1:视频现场,2:音频现场,3:图片现场,4:文字现场,5:混合现场,6:精品现场", name = "sceneType", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "现场状态，draft：草稿；check：待审；prod：成品 cycle：回收站", name = "ltype", paramType = "query", dataType = "String"),
            @ApiImplicitParam(value = "直播状态，0：预告；1：正在直播；2回顾",name = "liveState",paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(value = "页码", name = "pageNo", defaultValue = "1", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "页面大小", name = "pageSize", defaultValue = "15",paramType = "query", dataType = "int")})
	public R page(String title, Integer sceneType, String ltype,Integer liveState, Integer pageNo, Integer pageSize, Long tenantId){
		if(StringUtils.isNotBlank(ltype) && (ltype=="prod" || ltype.equals("prod"))){
			Page<SceneEntityVo> page =  liveSceneService.pageProd(title, sceneType, ltype,liveState, null==pageNo?0:pageNo, null==pageSize?15:pageSize, tenantId);
			return R.ok().put("page", page);
		}else{
			Page page =  liveSceneService.page(title, sceneType, ltype,liveState, null==pageNo?0:pageNo, null==pageSize?15:pageSize, tenantId);
			return R.ok().put("page", page);
		}
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info")
	@RequiresPermissions("live:livescene:info")
	@ApiOperation("现场详情接口")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	public R info(Integer id){
		if (null == id) {
			return R.error("参数错误");
		}
		return R.ok().put("info", liveSceneService.info(id));
	}

	@PostMapping("/selectLiveInfoList")
	@ApiOperation("现场记者列表数据")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场id", name = "sceneId", defaultValue = "",paramType = "query", dataType = "int")})
	public R selectListBySceneId(Integer sceneId){
		List<LiveCasterSubVo> list =  liveSceneLiveInfoService.selectListBySceneId(sceneId);
		return R.ok().put("list", list);
	}
	/**
	 * 保存
	 */
	@SysLog("保存现场信息")
	@PostMapping("/save")
	@RequiresPermissions("live:livescene:save")
	@ApiOperation(value = "现场保存接口", notes = "现场保存接口", produces = MediaType.APPLICATION_JSON_VALUE)
	public R save(@ModelAttribute SceneEntityVo liveScene){
		try {
			liveScene.setCreatorId(null!=getUserId()?getUserId().intValue():null);
			liveSceneService.save(liveScene);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统内部错误");
		}

		return R.ok("现场保存成功");
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改现场信息")
	@PostMapping("/update")
	@RequiresPermissions("live:livescene:update")
	@ApiOperation("现场更新接口")
	@LiveTenant
	public R update(@ModelAttribute SceneEntityVo liveScene){
		String r = null;
		try {
			r = liveSceneService.update(liveScene, getUser());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统内部错误");
		}
		return R.ok(r);
	}

	/**
	 * 送审
	 */
	@SysLog("送审现场信息")
	@PostMapping("/submit")
	@RequiresPermissions("live:livescene:submit")
	@ApiOperation("现场送审接口")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	@LiveTenant
	public R submit(Integer id){
		if (null==id ) {
			return R.error("参数错误");
		}
		try {
			liveSceneService.submit(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统内部错误");
		}
		return R.ok("提交成功");
	}

	/**
	 * 审核
	 */
	@SysLog("审核现场信息")
	@PostMapping("/check")
	@RequiresPermissions("live:livescene:check")
	@ApiOperation("现场审核接口")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "审核状态，0:不通过;1:通过", name = "pass", paramType = "query", dataType = "int")})
	// TODO 报业操作
	public R check(Integer id, Integer pass){
		if (null==id ) {
			return R.error("参数错误");
		}
		try {
			liveSceneService.check(id, pass, getUser());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统内部错误");
		}
		return R.ok("审核完成");
	}

	/**
	 * 删除
	 */
	@SysLog("删除现场信息")
	@PostMapping("/delete")
	@RequiresPermissions("live:livescene:delete")
	@ApiOperation("现场删除接口")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "ids", paramType = "query", allowMultiple=true,  dataType = "int")})
	public R delete(Integer[] ids){
		if (null==ids || ids.length<=0) {
			return R.error("参数错误");
		}
		try {
			liveSceneService.deleteBatch(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统内部错误");
		}
		return R.ok();
	}
	
	@SysLog("现场开播")
	@PostMapping("/startLive")
	@RequiresPermissions("live:livescene:startLive")
	@ApiOperation("开播")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	@LiveTenant
	public R startLive(Integer id){
		if (null == id) {
			return R.error("参数错误");
		}
		return liveSceneService.broadcast(id, true);
	}

	@SysLog("现场停播")
	@PostMapping("/stopLive")
	@RequiresPermissions("live:livescene:stopLive")
	@ApiOperation("停播")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	@LiveTenant
	public R stopLive(Integer id){
		if (null == id) {
			return R.error("参数错误");
		}
		return liveSceneService.broadcast(id, false);
	}

	@SysLog("导播台前置接口")
	@PostMapping("/distribute")
	@RequiresPermissions("live:livescene:caster")
	@ApiOperation("导播台前置接口")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	@LiveTenant
	public R distribute(Integer id){
		return liveSceneService.distribute(id);
	}

	@GetMapping("/getLiveStreamsOnlineList")
	@RequiresPermissions("live:livescene:caster")
	@ApiOperation("获取现场在线流列表")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	public R getLiveStreamsOnlineList(Integer id) {
		return liveSceneService.getLiveStreamsOnlineList(id);
	}

//	@PostMapping("/startCasterScene")
//	@RequiresPermissions("live:livescene:caster")
//	@ApiOperation("开启预监")
//	@ApiImplicitParams({
//			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
//	public R startCasterScene(Integer id) {
//		return liveSceneService.startCasterScene(id);
//	}
//
//	@PostMapping("/stopCasterScene")
//	@RequiresPermissions("live:livescene:caster")
//	@ApiOperation("停止预监")
//	@ApiImplicitParams({
//			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
//	public R stopCasterScene(Integer id) {
//		return liveSceneService.stopCasterScene(id);
//	}

	@SysLog("配置导播台")
	@PostMapping("/setCasterConfig")
	@RequiresPermissions("live:livescene:caster")
	@ApiOperation("配置导播台，全量覆盖配置信息，若指定参数置为空则清除导播台该项配置。")
//	@ApiImplicitParams({
//			@ApiImplicitParam(value = "导播台设置", name = "casterConfig", paramType = "json", dataType = "CasterConfigVo")})
	@LiveTenant("sceneId")
	public RpcResponse setCasterConfig(@RequestBody CasterConfigVo casterConfig) {
		return liveSceneService.setCasterConfig(casterConfig.getSceneId(), casterConfig.getCasterConfig());
	}

	@SysLog("导播台开始直播")
	@PostMapping("/startCaster")
	@RequiresPermissions("live:livescene:caster")
	@ApiOperation("开始直播")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	@LiveTenant
	public RpcResponse startCaster(Integer id) {
		return liveSceneService.startCaster(id);
	}

	@SysLog("现场开始直播")
	@PostMapping("/copyCasterSceneConfig")
	@RequiresPermissions("live:livescene:caster")
	@ApiOperation("开始直播")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "导播台ID", name = "casterId", paramType = "query", dataType = "String"),
			@ApiImplicitParam(value = "PVW场景ID", name = "fromSceneId", paramType = "query", dataType = "String"),
			@ApiImplicitParam(value = "PGM场景ID", name = "toSceneId", paramType = "query", dataType = "String")})
	@LiveTenant
	public R copyCasterSceneConfig(Integer id, String casterId, String fromSceneId, String toSceneId) {
		return liveSceneService.copyCasterSceneConfig(id, casterId, fromSceneId, toSceneId);
	}

	@SysLog("导播台停止直播")
	@PostMapping("/stopCaster")
	@RequiresPermissions("live:livescene:caster")
	@ApiOperation("导播台停止直播")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "是否关闭现场", name = "close", paramType = "query", dataType = "boolean")})
	@LiveTenant
	public R stopCaster(Integer id, Boolean close) {
		return liveSceneService.stopCaster(id, close);
	}

	@GetMapping("/casterStreamsUrl")
	@RequiresPermissions("live:livescene:caster")
	@ApiOperation("获取导播台播流地址")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	public R casterStreamsUrl(Integer id) {
		return liveSceneService.getCasterStreamsUrl(id);
	}

	@GetMapping("/stream/list")
	@RequiresPermissions("live:livescene:stream:list")
	@ApiOperation(value = "流管理-列表",produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "sceneId", paramType = "query", dataType = "int")})
	public R streamList(@ApiIgnore @RequestParam(required = false) Map<String, Object> params) {
		List<LiveInfoVo> list = null;
		try {
			list = liveSceneService.streamList(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统内部错误");
		}
		return R.ok().put("list", list);
	}

	@SysLog("创建现场直播流")
	@PostMapping("/stream/create")
	@RequiresPermissions("live:livescene:stream:create")
	@ApiOperation(value = "流管理-创建", notes = "流管理-创建",produces = MediaType.APPLICATION_JSON_VALUE)
	@LiveTenant("sceneId")
	public R createStream(@ModelAttribute LiveSceneLiveInfoEntity liveSceneLiveInfoEntity) {
		if (liveSceneLiveInfoEntity == null || liveSceneLiveInfoEntity.getSceneId() == null || liveSceneLiveInfoEntity.getInfoType() == null) {
			return R.error("缺少必要参数");
		}
		return liveSceneService.createStream(liveSceneLiveInfoEntity);

	}

	@SysLog("修改现场直播流")
    @PostMapping("/stream/update")
    @RequiresPermissions("live:livescene:stream:update")
    @ApiOperation(value = "流管理-更新", produces = MediaType.APPLICATION_JSON_VALUE)
	@LiveTenant("sceneId")
    public R updateStream(@ModelAttribute LiveSceneLiveInfoEntity liveSceneLiveInfoEntity) {
        if (liveSceneLiveInfoEntity == null || liveSceneLiveInfoEntity.getId() == null) {
            return R.error("缺少必要参数");
        }
        return liveSceneService.updateStream(liveSceneLiveInfoEntity);

    }

	@SysLog("现场直播流-第三方流开始拉流")
	@PostMapping("/stream/pull/start")
	@RequiresPermissions("live:livescene:stream:pull:start")
	@ApiOperation(value = "流管理-第三方流开始拉流", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(value = "流ID", name = "id", paramType = "query", dataType = "int")})
	public R pullStart(Integer id){
		if (null == id) {
			return R.error("参数错误");
		}
		return liveSceneService.streamPullStart(id);
	}

	@SysLog("现场直播流-第三方流结束拉流")
	@PostMapping("/stream/pull/stop")
	@RequiresPermissions("live:livescene:stream:pull:stop")
	@ApiOperation(value = "流管理-第三方流结束拉流", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(value = "流ID", name = "id", paramType = "query", dataType = "int")})
	public R pullEnd(Integer id){
		if (null == id) {
			return R.error("参数错误");
		}
		return liveSceneService.streamPullEnd(id);
	}

	/**
	 * 信息
	 */
	@GetMapping("/streamInfo")
	@RequiresPermissions("live:livescene:stream:info")
	@ApiOperation(value = "流管理-详情", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(value = "流ID", name = "id", paramType = "query", dataType = "int")})
	public R streamInfo(Integer id){
		if (null == id) {
			return R.error("参数错误");
		}
		return liveSceneService.streamInfo(id);
	}


	@SysLog("删除现场直播流")
	@PostMapping("/stream/delete")
	@ApiOperation(value = "流管理-删除", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequiresPermissions("live:livescene:stream:delete")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场信息id", name = "infoIds", paramType = "query", allowMultiple=true,  dataType = "int")})
	@LiveTenant(value = "infoIds", tenantTableClazz = LiveSceneLiveInfoService.class)
	public R deleteStream(Integer[] infoIds) {
		if (null == infoIds || infoIds.length <= 0) {
			return R.error("参数错误");
		}
		return liveSceneService.deleteStream(infoIds);
	}
	@GetMapping("/stream/reporters")
	@ApiOperation(value = "流管理-记者列表", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequiresPermissions("live:livescene:stream:reporters")
	public R reporterList(Integer sid){
		return liveSceneService.reporterList(sid);
	}

	@SysLog("现场播放")
	@GetMapping("/streamPlay")
	@RequiresPermissions("live:livescene:streamPlay")
	@ApiOperation(value = "现场播放接口", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场id", name = "sceneId", paramType = "query", dataType = "int")})
	public R streamPlay(Integer sceneId){
		if (sceneId == null) {
			return R.error("参数错误");
		}
		return liveSceneService.streamPlay(sceneId);
	}

	@SysLog("设置现场流显示状态")
	@PostMapping("/streamShow")
	@RequiresPermissions("live:livescene:streamShow")
	@ApiOperation(value = "现场流显示/不显示", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场id", name = "sceneId", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "流id", name = "infoIds", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "是否显示，0：不显示，1：显示", name = "isShow", paramType = "query", dataType = "int")})
	public R streamShow(Integer sceneId, Integer[] infoIds, Integer isShow){
		if (sceneId == null || infoIds == null || infoIds.length <= 0 || isShow == null) {
			return R.error("参数错误");
		}
		liveSceneService.streamShow(sceneId, infoIds,isShow);
		return R.ok();
	}

	@GetMapping("/index/rebuild")
	@RequiresPermissions("live:livescene:index:rebuild")
	public R rebuild(Integer begin) {
		R r;
		try {
			r = liveSceneService.indexRebuild(begin);
		} catch (Exception e) {
		    e.printStackTrace();
			r = R.error("系统内部错误");
		}
		return r;
	}
	
	/**
	 * 置顶
	 */
	@SysLog("现场置顶")
	@PostMapping("/top")
	@RequiresPermissions("live:livescene:top")
	@ApiOperation("现场信息置顶")
	public R top(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("现场ID不能为空");
		}
		liveSceneService.topBatch(ids,getUserId());
		return R.ok();
	}
	/**
	 * 取消置顶
	 */
	@SysLog("取消现场置顶")
	@PostMapping("/cancelTop")
	@RequiresPermissions("live:livescene:cancelTop")
	@ApiOperation("取消现场信息置顶")
	public R cancelTop(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("现场ID不能为空");
		}
		liveSceneService.cancelTopBatch(ids, getUserId());
		return R.ok();
	}
	/**
	 * 排序
	 */
	@SysLog("现场排序")
	@PostMapping("/priority")
	@RequiresPermissions("live:livescene:priority")
	@ApiOperation("现场排序")
	public R priority(Integer[] ids,Integer[] prioritys){
		if(ids==null || ids.length==0){
			return R.error("现场ID不能为空");
		}
		liveSceneService.priority(ids, prioritys, getUserId());
		return R.ok();
	}
	
	@GetMapping("/channels")
	@ApiOperation(value = "获取所有栏目", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequiresPermissions("live:livescene:channels")
	public R channels(Integer sid){
		// return R.ok().put("list", liveChannelService.getAllList());
		return R.ok().put("list", liveChannelService.getAllChannel());
	}

	@Autowired
	private ForeignProperties foreignProperties;

	@GetMapping("/qrcode")
	@RequiresPermissions("live:livescene:info")
	@ApiOperation("现场详情生成二维码接口")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	public String qrcode(Integer id, HttpServletResponse response) {
		String content = "";
		if(id == null) {
			content = "参数错误：id为空";
		} else {
			LiveSceneEntity sceneEntity = liveSceneService.selectById(id);
			if(sceneEntity == null) {
				content = "未找到现场，id：" + id;
			} else {
				// 域名
				content += foreignProperties.getWapurl() + "/live/";
				Integer sceneType = sceneEntity.getSceneType();
				String sceneTypeName = SceneConstants.SceneType.getPathByKey(sceneType);
				if(StringUtils.isBlank(sceneTypeName))
					content = "现场类型错误";
				else
					content += sceneTypeName + "/detail/" + id;
			}
		}
		try {
			OutputStream os = response.getOutputStream();
            BufferedImage image = QrCodeUtils.encode(content, os);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "png",outputStream);
            return Base64.encodeBase64String(outputStream.toByteArray());
        } catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@SysLog("现场还原")
	@PostMapping("/cycle/recycle")
	@RequiresPermissions("live:livescene:cycle:recycle")
	@ApiOperation("还原现场")
	public R recycle(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("现场ID不能为空");
		}
		liveSceneService.recycle(ids);
		return R.ok();
	}
	
	@SysLog("现场彻底删除")
	@PostMapping("/cycle/delete")
	@RequiresPermissions("live:livescene:cycle:delete")
	@ApiOperation("彻底删除现场")
	public R cycleDelete(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("现场ID不能为空");
		}
		liveSceneService.cycleDelete(ids);
		return R.ok();
	}

	@SysLog("拖动排序")
	@PostMapping("/dragSort")
	@RequiresPermissions("live:livescene:page")
	@ApiOperation("拖动排序")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "栏目ID", name = "cid", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "现场ID", name = "sceneIds", paramType = "query", dataType = "int[]"),
			@ApiImplicitParam(value = "排序号", name = "dragSorts", paramType = "query", dataType = "int[]")
	})
	public R dragSort(Integer cid, Integer[] sceneIds, long[] dragSorts){

		if (null == cid || null == sceneIds || sceneIds.length == 0 || null == dragSorts || dragSorts.length == 0)
			return R.error("参数不能为空");

		return liveSceneService.dragSort(cid, sceneIds, dragSorts);
	}

	@GetMapping("/sortScenes")
	@RequiresPermissions("live:livescene:page")
	@ApiOperation("获取排序现场列表")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "栏目ID", name = "cid", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "页码", name = "pageNo", defaultValue = "1", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "页面大小", name = "pageSize", defaultValue = "10", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "标题", name = "title", paramType = "query", dataType = "String"),
			@ApiImplicitParam(value = "现场类型，1：视频现场；2：音频现场；3：图片现场；4：文字现场；5：混合现场", name = "sceneType", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "直播状态，0：预告；1：正在直播；2：回顾", name = "liveState", paramType = "query", dataType = "int"),
			@ApiImplicitParam(value = "机构ID", name = "tenantId", paramType = "query", dataType = "int")
	})
	public R sortScenes(Integer cid, Integer pageSize, Integer pageNo, String title, Integer sceneType, Integer liveState, Long tenantId){

		if (null == cid)
			return R.error("必填参数不能为空");

		PageUtils page = liveSceneService.sortScenePage(cid, null == pageSize ? 10 : pageSize, null == pageNo ? 1 : pageNo, StringUtils.isBlank(title) ? null : title, sceneType, liveState, null, tenantId);

		return R.ok().put("page", page);
	}

	@PostMapping("/lockSort")
	@ApiOperation("拖动排序锁定与解锁")
	@RequiresPermissions("live:livescene:page")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "flag", value = "标识，操作中：true，操作结束：false", paramType = "query", dataType = "boolean"),
			@ApiImplicitParam(name = "cid", value = "栏目ID", paramType = "query", dataType = "int")

	})
	public R lockSort(boolean flag, Integer cid){

		if (null == cid)
			return R.error("必填参数不能为空");

		liveSceneService.lockSort(flag, cid);
		return R.ok();
	}

	@PostMapping("/isLockSort")
	@ApiOperation("拖动排序解锁判断")
	@RequiresPermissions("live:livescene:page")
	@ApiImplicitParam(name = "cid", value = "栏目ID", paramType = "query", dataType = "int")
	public R unlockSort(Integer cid){

		if (null == cid)
			return R.error("必填参数不能为空");

		boolean result = liveSceneService.isLockSort(cid);

		if (result){
			return R.ok();
		} else {
			return R.error("拖动排序中，请稍后操作");
		}
	}

	/**
	 * 重置为预告状态
	 *
	 * @param id 直播id
	 * @return 是否重置成功
	 */
	@SysLog("重置为预告状态")
	@PostMapping("/resetLiveState")
	//@RequiresPermissions("live:livescene:startLive")
	@ApiOperation("重置为预告状态")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "现场ID", name = "id", paramType = "query", dataType = "int")})
	@LiveTenant
	public R resetLiveState(Integer id) {
		Assert.isNull(id, "现场id不能为空");
		LiveSceneEntity entity = liveSceneService.selectById(id);
		if (null == entity) {
			return R.error("无法找到该现场");
		}
		if (!SceneConstants.SCENE_PROD.equals(entity.getSceneState())) {
			return R.error("非成品现场不可执行重置状态操作");
		}
		if (entity.getLiveEnd().before(new Date())) {
			return R.error("该现场结束时间已超过当前时间，请修改后再尝试重置状态操作");
		}
		return liveSceneService.resetLiveState(entity);
	}

}
