package com.platform.modules.live.controller;

import com.platform.common.annotation.SysLog;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.modules.live.aspect.LiveTenant;
import com.platform.modules.live.entity.LiveReportEntity;
import com.platform.modules.live.service.LiveReportService;
import com.platform.modules.live.utils.ReportTypeEnum;
import com.platform.modules.sys.controller.AbstractController;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 直播报道
 * 
 * @author weiyan
 * @email 
 * @date 2019-01-19 12:13:39
 */
@RestController
@RequestMapping("/live/report")
@Api(value = "直播报道管理", tags = "直播报道管理")
public class LiveReportController extends AbstractController{
	@Autowired
	private LiveReportService liveReportService;
	
	/**
	 * 列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("live:report:list")
	@ApiOperation("报道列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "页数", defaultValue = "1", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "limit", value = "显示行数", defaultValue = "10", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "sceneId", value = "现场ID", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "reportType", value = "报道类型，不填为全选：<br>报道类型 1视频 2音频 3图文 4文字", allowableValues = "1,2,3,4", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "content", value = "报道内容", paramType = "query", dataType = "String")})
	@ApiResponses({@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveReportEntity.class, responseContainer = "List")})
	public R list(@ApiIgnore @RequestParam(required = false) Map<String, Object> params){
		params.put("isDelete", 0);
		PageUtils page =  liveReportService.queryPageByParams(params, false);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info")
	@RequiresPermissions("live:report:info")
	@ApiOperation("报道信息")
	@ApiResponses({@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveReportEntity.class)})
	public R info(@RequestParam("id")Integer id){
		return R.ok().put("info", liveReportService.info(id));
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存报道信息")
	@PostMapping("/save")
	@RequiresPermissions("live:report:save")
	@ApiOperation("保存报道信息")
	@LiveTenant("sceneId")
	public R save(@ModelAttribute LiveReportEntity liveReport){
		//现场ID
		if(liveReport == null || liveReport.getSceneId()==null) {
			return R.error("现场新闻ID不能为空");
		}
		//报道类型
		if(liveReport.getReportType()==null) {
			return R.error("报道类型不能为空");
		}
		boolean materialCheck = (liveReport.getReportType().equals(ReportTypeEnum.AUDIO) || liveReport.getReportType()
				.equals(ReportTypeEnum.VIDEO)) && (StringUtils.isBlank(liveReport.getMediaUrl()));
		if (materialCheck) {
			return R.error("请上传媒体资源");
		}
		liveReportService.save(liveReport, getUserId());
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改报道信息")
	@PostMapping("/update")
	@RequiresPermissions("live:report:update")
	@ApiOperation("修改报道信息")
	@LiveTenant("sceneId")
	public R update(@ModelAttribute LiveReportEntity liveReport, MultipartFile materialFile, MultipartFile[] imgFile,
					Integer[] imgFileIndex){
		//报道Id
		if(liveReport == null || liveReport.getId()==null) {
			return R.error("报道ID不能为空");
		}
		//现场ID
		if(liveReport.getSceneId()==null) {
			return R.error("现场新闻ID不能为空");
		}
		//报道类型
		if(liveReport.getReportType()==null) {
			return R.error("报道类型不能为空");
		}
		try {
			liveReportService.update(liveReport, getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("报道更新失败");
		}
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除报道信息")
	@PostMapping("/delete")
	@RequiresPermissions("live:report:delete")
	@ApiOperation("删除报道信息")
	@LiveTenant(value = "ids", tenantTableClazz = LiveReportService.class)
	public R delete(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("报道ID不能为空");
		}
		liveReportService.deleteBatch(ids, getUserId());
		return R.ok();
	}
	
	/**
	 * 发布
	 */
	@SysLog("发布报道信息")
	@PostMapping("/release")
	@RequiresPermissions("live:report:release")
	@ApiOperation("发布报道信息")
	@LiveTenant(value = "ids", tenantTableClazz = LiveReportService.class)
	public R release(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("报道ID不能为空");
		}
		liveReportService.releaseBatch(ids,getUserId());
		return R.ok();
	}
	
	/**
	 * 取消发布
	 */
	@SysLog("取消发布报道信息")
	@PostMapping("/cancelRelease")
	@RequiresPermissions("live:report:cancelRelease")
	@ApiOperation("取消发布报道信息")
	@LiveTenant(value = "ids", tenantTableClazz = LiveReportService.class)
	public R cancelRelease(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("报道ID不能为空");
		}
		liveReportService.cancelReleaseBatch(ids, getUserId());
		return R.ok();
	}
	
	/**
	 * 置顶
	 */
	@SysLog("报道信息置顶")
	@PostMapping("/top")
	@RequiresPermissions("live:report:top")
	@ApiOperation("报道信息置顶")
	@LiveTenant(value = "ids", tenantTableClazz = LiveReportService.class)
	public R top(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("报道ID不能为空");
		}
		liveReportService.topBatch(ids,getUserId());
		return R.ok();
	}
	/**
	 * 取消置顶
	 */
	@SysLog("取消报道信息置顶")
	@PostMapping("/cancelTop")
	@RequiresPermissions("live:report:cancelTop")
	@ApiOperation("取消报道信息置顶")
	@LiveTenant(value = "ids", tenantTableClazz = LiveReportService.class)
	public R cancelTop(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("报道ID不能为空");
		}
		liveReportService.cancelTopBatch(ids, getUserId());
		return R.ok();
	}
	/**
	 * 排序
	 */
	@SysLog("报道信息排序")
	@PostMapping("/priority")
	@RequiresPermissions("live:report:priority")
	@ApiOperation("排序")
	@LiveTenant(value = "ids", tenantTableClazz = LiveReportService.class)
	public R priority(Integer[] ids,Integer[] prioritys){
		if(ids==null || ids.length==0){
			return R.error("报道ID不能为空");
		}
		if(prioritys==null || prioritys.length==0){
			return R.error("报道排序不能为空");
		}
		liveReportService.priority(ids, prioritys, getUserId());
		return R.ok();
	}

	@SysLog("更新报道信息索引")
	@GetMapping("/index/rebuild")
	@RequiresPermissions("live:report:index:rebuild")
	public R rebuild(Integer begin, Integer end) {
		R r;
		try {
			r = liveReportService.indexRebuild(begin, end);
		} catch (Exception e) {
			e.printStackTrace();
			r = R.error("系统内部错误");
		}
		return r;
	}

}
