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.BbsCollectVo;
import com.yonge.cooleshow.bbs.dto.search.BbsCollectSearch;
import com.yonge.cooleshow.bbs.entity.BbsCollect;
import com.yonge.cooleshow.bbs.service.BbsCollectService;

@RestController
@RequestMapping("/bbsCollect")
@Api(value = "收藏", tags = "收藏")
public class BbsCollectController extends BaseController {
	@Autowired
	private SysUserFeignService sysUserFeignService;
    @Autowired
    private BbsCollectService bbsCollectService;

	@GetMapping("/collect")
	@ApiOperation(value = "详情-收藏/取消收藏")
	public HttpResponseResult collect(@NotNull Long articleId) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		bbsCollectService.collect(user.getId(), articleId);
		return succeed();
	}

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

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