package com.gxwebsoft.cms.controller;

import com.gxwebsoft.cms.entity.ArticleContent;
import com.gxwebsoft.cms.param.ArticleContentParam;
import com.gxwebsoft.cms.service.ArticleContentService;
import com.gxwebsoft.common.core.web.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文章控制器
 *
 * @author 科技小王子
 * @since 2024-03-13 20:28:29
 */
@Api(tags = "文章内容")
@RestController
@RequestMapping("/api/cms/article-content")
public class ArticleContentController extends BaseController {
    @Resource
    private ArticleContentService articleContentService;

    @ApiOperation("分页查询文章")
    @GetMapping("/page")
    public ApiResult<PageResult<ArticleContent>> page(ArticleContentParam param) {
        PageParam<ArticleContent, ArticleContentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(articleContentService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(articleContentService.pageRel(param));
    }

    @ApiOperation("查询全部文章")
    @GetMapping()
    public ApiResult<List<ArticleContent>> list(ArticleContentParam param) {
        PageParam<ArticleContent, ArticleContentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(articleContentService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(articleContentService.listRel(param));
    }

    @ApiOperation("根据id查询文章")
    @GetMapping("/{id}")
    public ApiResult<ArticleContent> get(@PathVariable("id") Integer id) {
        return success(articleContentService.getById(id));
        // 使用关联查询
        //return success(articleContentService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:article:save')")
    @ApiOperation("添加文章")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ArticleContent articleContent) {
        if (articleContentService.save(articleContent)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改文章")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ArticleContent articleContent) {
        if (articleContentService.updateById(articleContent)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除文章")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (articleContentService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加文章")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ArticleContent> list) {
        if (articleContentService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改文章")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ArticleContent> batchParam) {
        if (batchParam.update(articleContentService, "")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除文章")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (articleContentService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
