package com.backendsys.modules.cms.article.controller;

import com.backendsys.modules.cms.article.entity.Article;
import com.backendsys.modules.cms.article.service.ArticleService;
import com.backendsys.modules.common.aspect.SysLog;
import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@Tag(name = "资讯管理")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Operation(summary = "获取资讯列表")
    @PreAuthorize("@sr.hasPermission('10.3')")
    @GetMapping("/api/cms/article/getArticleList")
    public Result getArticleList(@Validated Article article) {
        return Result.success().put("data", articleService.selectArticleList(article));
    }

    @Anonymous
    @Operation(summary = "获取资讯列表 (公开)")
    @GetMapping("/api/public/cms/article/getArticleList")
    public Result getArticleListPublic(@Validated Article article) {
        article.setStatus(1);
        return Result.success().put("data", articleService.selectArticleList(article));
    }

    @Operation(summary = "获取资讯详情")
    @PreAuthorize("@sr.hasPermission('10.3.1')")
    @GetMapping("/api/cms/article/getArticleDetail")
    public Result getArticleDetail(@Validated(Article.Detail.class) Article article) {
        return Result.success().put("data", articleService.selectArticleDetail(article));
    }

    @Anonymous
    @Operation(summary = "获取资讯详情 (公共)")
    @GetMapping("/api/public/cms/article/getArticleDetail")
    public Result getArticleDetailPublic(@Validated(Article.DetailByUid.class) Article article) {
        article.setStatus(1);
        return Result.success().put("data", articleService.selectArticleDetailPublic(article));
    }

    @SysLog("创建资讯")
    @Operation(summary = "创建资讯")
    @PreAuthorize("@sr.hasPermission('10.3.2')")
    @PostMapping("/api/cms/article/createArticle")
    public Result createArticle(@Validated(Article.Create.class) @RequestBody Article article) {
        return Result.success().put("data", articleService.insertArticle(article));
    }

    @SysLog("编辑资讯")
    @Operation(summary = "编辑资讯")
    @PreAuthorize("@sr.hasPermission('10.3.3')")
    @PutMapping("/api/cms/article/updateArticle")
    public Result updateArticle(@Validated(Article.Update.class) @RequestBody Article article) {
        return Result.success().put("data", articleService.updateArticle(article));
    }

    @SysLog("删除资讯")
    @Operation(summary = "删除资讯")
    @PreAuthorize("@sr.hasPermission('10.3.4')")
    @DeleteMapping("/api/cms/article/deleteArticle")
    public Result deleteArticle(@Validated(Article.Delete.class) @RequestBody Article article) {
        return Result.success().put("data", articleService.deleteArticle(article));
    }

    @SysLog("删除资讯 (批量)")
    @Operation(summary = "删除资讯 (批量)")
    @PreAuthorize("@sr.hasPermission('10.3.4')")
    @DeleteMapping("/api/cms/article/deleteArticleBatch")
    public Result deleteArticleBatch(@Validated(Article.DeleteBatch.class) @RequestBody Article article) {
        return Result.success().put("data", articleService.deleteArticleBatch(article));
    }

}
