package com.yonge.cooleshow.bbs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.bbs.dto.ReplyAuditDto;
import com.yonge.cooleshow.bbs.dto.search.BbsReplySearch;
import com.yonge.cooleshow.bbs.entity.BbsReply;
import com.yonge.cooleshow.bbs.service.BbsReplyService;
import com.yonge.cooleshow.bbs.vo.BbsReplyAuditVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/bbsReply")
@Api(value = "文章评论", tags = "内容评论")
public class BbsReplyController extends BaseController {

    @Autowired
    private BbsReplyService bbsReplyService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@PostMapping("/reply")
	@ApiOperation(value = "详情-评论文章")
	public HttpResponseResult reply(@Validated @RequestBody BbsReply reply) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		reply.setUserId(user.getId());
		bbsReplyService.replyArticle(reply);
		return succeed();
	}

	@PostMapping("/audit/list")
	@ApiOperation(value = "后台-评论审核列表")
	public HttpResponseResult<PageInfo<BbsReplyAuditVo>> auditList(@RequestBody BbsReplySearch query) {

		IPage<BbsReplyAuditVo> pages = bbsReplyService.selectAuditListPage(PageUtil.getPage(query), query);
		return succeed(PageUtil.pageInfo(pages));
	}

	@PostMapping("/remove/{id}")
	@ApiOperation(value = "后台-评论删除", notes = "传入id")
	public HttpResponseResult remove(@PathVariable Long id) {
        if (id == null) {
			return failed("参数不能为空");
		}
		return status(bbsReplyService.removeById(id));
	}



	@PostMapping("/audit")
	@ApiOperation(value = "后台-评论审核")
	public HttpResponseResult audit(@RequestBody @Valid ReplyAuditDto auditDto) {

		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		auditDto.setVerityUserId(user.getId());
		return status(bbsReplyService.audit(auditDto));
	}
}