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.ArticleConvert;
import com.wzj.soopin.content.domain.bo.ArticleBO;
import com.wzj.soopin.content.domain.po.Article;
import com.wzj.soopin.content.domain.vo.ArticleVO;
import com.wzj.soopin.content.service.IArticleService;
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")
@RequiredArgsConstructor
public class ArticleController {

    private final IArticleService service;
    private final ArticleConvert convert;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public R<IPage<ArticleVO>> page(@RequestBody ArticleBO bo, @RequestBody Page page) {
        Page<Article> pages = service.page(page, bo.toWrapper());
        return R.ok(convert.toVO(pages));
    }

    @Operation(summary = "查询列表")
    @PostMapping("/list")
    public R< List<ArticleVO>> list(@RequestBody ArticleBO bo, @RequestBody Page page) {
        List<ArticleVO> articleList = service.selectVoList( bo.toWrapper());
        return R.ok(articleList);
    }

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

    @Operation(summary = "详情")
    @GetMapping(value = "/{id}")
    public R<ArticleVO> 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 ArticleBO 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));
    }


    @Operation(summary = "轮播列表")
    @PostMapping("/carousel")
    public R< List<ArticleVO>> carousel(@RequestBody ArticleBO bo, @RequestBody Page page) {
        bo.setType(1);//轮播图
        List<ArticleVO> articleList = service.selectVoList( bo.toWrapper());
        return R.ok(articleList);
    }

}
