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.toolset.base.page.PageInfo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;

import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.mybatis.support.PageUtil;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.yonge.cooleshow.bbs.vo.BbsFollowVo;
import com.yonge.cooleshow.bbs.dto.search.BbsFollowSearch;
import com.yonge.cooleshow.bbs.entity.BbsFollow;
import com.yonge.cooleshow.bbs.service.BbsFollowService;

@RestController
@RequestMapping("/bbsFollow")
@Api(value = "关注", tags = "关注")
public class BbsFollowController extends BaseController {
    @Autowired
    private BbsFollowService bbsFollowService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@GetMapping("/follow")
	@ApiOperation(value = "详情-关注/取消关注")
	public HttpResponseResult follow(@NotNull Long followId) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		bbsFollowService.follow(user.getId(), followId);
		return succeed();
	}

	/**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    public HttpResponseResult<BbsFollowVo> detail(@PathVariable("id") Long id) {
    	return succeed(bbsFollowService.detail(id));
	}
    
    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入bbsFollowSearch")
    public HttpResponseResult<PageInfo<BbsFollowVo>> page(@RequestBody BbsFollowSearch query) {
		IPage<BbsFollowVo> pages = bbsFollowService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
	}
    
    /**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入bbsFollow")
	public HttpResponseResult save(@Valid @RequestBody BbsFollow bbsFollow) {
    	return status(bbsFollowService.save(bbsFollow));
	}
    
    /**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入bbsFollow")
	public HttpResponseResult update(@Valid @RequestBody BbsFollow bbsFollow) {
        return status(bbsFollowService.updateById(bbsFollow));
	}
    
    /**
	 * 新增或修改
	 */
    @PostMapping("/submit")
    @ApiOperation(value = "新增或修改", notes = "传入bbsFollow")
	public HttpResponseResult submit(@Valid @RequestBody BbsFollow bbsFollow) {
        return status(bbsFollowService.saveOrUpdate(bbsFollow));
    }

 	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public HttpResponseResult remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        if (StringUtil.isEmpty(ids)) {
			return failed("参数不能为空");
		}
		return status(bbsFollowService.removeByIds(StringUtil.toLongList(ids)));
	}
}