package com.wzj.soopin.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.content.convert.ArticleCategoryConvert;
import com.wzj.soopin.content.domain.bo.ArticleCategoryBO;
import com.wzj.soopin.content.domain.po.ArticleCategory;
import com.wzj.soopin.content.domain.vo.ArticleCategoryVO;
import com.wzj.soopin.content.service.IArticleCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户封禁
 */
@Tag(name = "文章分类管理")
@RestController
@RequestMapping("/cms/article/category")
@RequiredArgsConstructor
public class ArticleCategoryController {

    private final IArticleCategoryService service;
    private final ArticleCategoryConvert convert;

    @Operation(summary = "查询列表")
    @PostMapping("/page")
    public R<IPage<ArticleCategoryVO>> page(@RequestBody ArticleCategoryBO bo, @RequestBody Page page) {
        Page<ArticleCategory> pages = service.page(page, bo.toWrapper());
        return R.ok(convert.toVO(pages));
    }

    @Operation(summary = "查询列表")
    @PostMapping("/list")
    public R< List<ArticleCategoryVO>> list(@RequestBody ArticleCategoryBO bo, @RequestBody Page page) {
        List<ArticleCategory> articleList = service.list( bo.toWrapper());
        return R.ok(convert.toVO(articleList));
    }
    @Operation(summary = "查询树形列表")
    @PostMapping("/tree")
    public R< List<ArticleCategoryVO>> tree(@RequestBody ArticleCategoryBO bo ) {
        List<ArticleCategoryVO> articleList = service.tree( bo.toWrapper());
        return R.ok( articleList);
    }

    @Operation(summary = "新增")
    @Log(title = "新增 ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R add(@RequestBody ArticleCategoryBO bo) {
        return R.ok(service.save(convert.toPo(bo)));
    }

    @Operation(summary = "详情")
    @GetMapping(value = "/{id}")
    public R<ArticleCategoryVO> getInfo(@PathVariable("id") Long id) {
        return R.ok(convert.toVO(service.getById(id)));
    }

    @Operation(summary = "修改")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R update(@RequestBody ArticleCategoryBO bo) {
        service.saveOrUpdate(convert.toPo(bo));
        return R.ok();
    }


    @Operation(summary = "删除")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Object> remove(@PathVariable Long id) {
        return R.ok(service.removeById(id));
    }
}
