package com.platform.modules.live.controller;

import java.util.Map;

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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.common.annotation.SysLog;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.modules.live.entity.LiveCommentEntity;
import com.platform.modules.live.service.LiveCommentService;
import com.platform.modules.live.utils.CommentSourceEnum;
import com.platform.modules.sys.controller.AbstractController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 现场评论
 * 
 * @author weiyan
 * @email 
 * @date 2019-02-15 17:49:07
 */
@RestController
@RequestMapping("/live/comment")
@Api(value = "现场评论管理", tags = "现场评论管理")
public class LiveCommentController extends AbstractController{
	@Autowired
	private LiveCommentService liveCommentService;
	
	/**
	 * 列表
	 */
	
	@GetMapping("/list")
	@RequiresPermissions("live:comment: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 = "source", value = "评论来源，不填为全选：<br> 1用户 2后台 3第三方", allowableValues = "1,2,3", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "text", value = "评论内容", paramType = "query", dataType = "String")})
	@ApiResponses({@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveCommentEntity.class, responseContainer = "List")})
	public R list(@ApiIgnore @RequestParam(required = false) Map<String, Object> params){
		params.put("isDelete", 0);
		PageUtils page =  liveCommentService.queryPageByParams(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info")
	@RequiresPermissions("live:comment:info")
	@ApiOperation("评论信息")
	@ApiResponses({@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveCommentEntity.class)})
	public R info(@RequestParam("id") Integer id){
		if(id==null) {
			return R.error("评论ID不能为空");
		}
		return R.ok().put("info", liveCommentService.info(id));
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存评论信息")
	@PostMapping("/save")
	@RequiresPermissions("live:comment:save")
	@ApiOperation("保存评论信息")
	public R save(LiveCommentEntity liveComment){
		//现场ID
		if(liveComment == null || liveComment.getSceneId()==null) {
			return R.error("现场ID不能为空");
		}
		//评论来源
		liveComment.setSource(CommentSourceEnum.BACKGROUD.getKey());
		liveCommentService.save(liveComment, getUserId());
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改评论信息")
	@PostMapping("/update")
	@RequiresPermissions("live:comment:update")
	@ApiOperation("修改评论信息")
	public R update(LiveCommentEntity liveComment){
		//评论Id
		if(liveComment == null || liveComment.getId()==null) {
			return R.error("评论ID不能为空");
		}
//		//现场ID
//		if(liveComment.getSceneId()==null) {
//			return R.error("现场ID不能为空");
//		}
		liveCommentService.update(liveComment,getUserId());
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除评论信息")
	@PostMapping("/delete")
	@RequiresPermissions("live:comment:delete")
	@ApiOperation("删除评论信息")
	public R delete(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("评论ID不能为空");
		}
		liveCommentService.deleteBatch(ids,getUserId());
		return R.ok();
	}
	
	/**
	 * 发布
	 */
	@SysLog("发布评论信息")
	@PostMapping("/release")
	@RequiresPermissions("live:comment:release")
	@ApiOperation("发布评论信息")
	public R release(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("评论ID不能为空");
		}
		liveCommentService.releaseBatch(ids,getUserId());
		return R.ok();
	}
	
	/**
	 * 取消发布
	 */
	@SysLog("取消发布评论信息")
	@PostMapping("/cancelRelease")
	@RequiresPermissions("live:comment:cancelRelease")
	@ApiOperation("取消发布评论信息")
	public R cancelRelease(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("评论ID不能为空");
		}
		liveCommentService.cancelReleaseBatch(ids,getUserId());
		return R.ok();
	}
	
}
