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.valid.AddGroup;
import com.yonge.cooleshow.bbs.valid.UpdateGroup;
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.mybatis.support.PageUtil;

import io.swagger.annotations.*;
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 com.yonge.cooleshow.bbs.vo.BbsConfigLabelVo;
import com.yonge.cooleshow.bbs.dto.search.BbsConfigLabelSearch;
import com.yonge.cooleshow.bbs.entity.BbsConfigLabel;
import com.yonge.cooleshow.bbs.service.BbsConfigLabelService;

import java.util.List;

@RestController
@RequestMapping("/bbsConfigLabel")
@Api(value = "论坛标签设置", tags = "论坛标签设置")
public class BbsConfigLabelController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private BbsConfigLabelService bbsConfigLabelService;

    @ApiOperation(value = "查询全部")
    @GetMapping("/selectAll")
    public HttpResponseResult<List<BbsConfigLabel>> selectAll(Integer delFlag) {
        return succeed(bbsConfigLabelService.selectAll(delFlag));
    }

    @ApiOperation(value = "查询分页")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<BbsConfigLabelVo>> page(@RequestBody BbsConfigLabelSearch query) {
        IPage<BbsConfigLabelVo> pages = bbsConfigLabelService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public HttpResponseResult save(@Validated(AddGroup.class) @RequestBody BbsConfigLabel bbsConfigLabel) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        bbsConfigLabel.setCreateBy(user.getId());
        bbsConfigLabelService.save(bbsConfigLabel);
        return succeed();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public HttpResponseResult update(@Validated(UpdateGroup.class) @RequestBody BbsConfigLabel bbsConfigLabel) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        bbsConfigLabel.setUpdateBy(user.getId());
        bbsConfigLabelService.updateById(bbsConfigLabel);
        return succeed();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/remove")
    public HttpResponseResult remove(@Validated @RequestBody BbsConfigLabelSearch query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        bbsConfigLabelService.updateFlagByIds(query, user.getId());
        return succeed();
    }
}