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

import com.backendsys.modules.cms.article.entity.ArticleCategory;
import com.backendsys.modules.cms.article.service.ArticleCategoryService;
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 ArticleCategoryController {

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Operation(summary = "获取资讯分类列表")
    @PreAuthorize("@sr.hasPermission('10')")
    @GetMapping("/api/cms/article/getArticleCategoryList")
    public Result getArticleCategoryList(@Validated ArticleCategory articleCategory) {
        return Result.success().put("data", articleCategoryService.selectArticleCategoryList(articleCategory));
    }

    @Anonymous
    @Operation(summary = "获取资讯分类列表 (公开)")
    @GetMapping("/api/public/cms/article/getArticleCategoryList")
    public Result getArticleCategoryListPublic(@Validated ArticleCategory articleCategory) {
        articleCategory.setStatus(1);
        return Result.success().put("data", articleCategoryService.selectArticleCategoryListPublic(articleCategory));
    }

    @Operation(summary = "获取资讯分类详情")
    @PreAuthorize("@sr.hasPermission('10.2.1')")
    @GetMapping("/api/cms/article/getArticleCategoryDetail")
    public Result getArticleCategoryDetail(@Validated(ArticleCategory.Detail.class) ArticleCategory articleCategory) {
        return Result.success().put("data", articleCategoryService.selectArticleCategoryDetail(articleCategory));
    }

    @SysLog("创建资讯分类")
    @Operation(summary = "创建资讯分类")
    @PreAuthorize("@sr.hasPermission('10.2.2')")
    @PostMapping("/api/cms/article/createArticleCategory")
    public Result createArticleCategory(@Validated(ArticleCategory.Create.class) @RequestBody ArticleCategory articleCategory) {
        return Result.success().put("data", articleCategoryService.insertArticleCategory(articleCategory));
    }

    @SysLog("编辑资讯分类")
    @Operation(summary = "编辑资讯分类")
    @PreAuthorize("@sr.hasPermission('10.2.3')")
    @PutMapping("/api/cms/article/updateArticleCategory")
    public Result updateArticleCategory(@Validated(ArticleCategory.Update.class) @RequestBody ArticleCategory articleCategory) {
        return Result.success().put("data", articleCategoryService.updateArticleCategory(articleCategory));
    }

    @SysLog("删除资讯分类")
    @Operation(summary = "删除资讯分类")
    @PreAuthorize("@sr.hasPermission('10.2.4')")
    @DeleteMapping("/api/cms/article/deleteArticleCategory")
    public Result deleteArticleCategory(@Validated(ArticleCategory.Delete.class) @RequestBody ArticleCategory articleCategory) {
        return Result.success().put("data", articleCategoryService.deleteArticleCategory(articleCategory));
    }

}
