package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.ArticleCategoryService;
import com.gxwebsoft.cms.entity.ArticleCategory;
import com.gxwebsoft.cms.param.ArticleCategoryParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
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 2022-11-22 17:49:15
 */
@Api(tags = "文章分类表管理")
@RestController
@RequestMapping("/api/cms/article-category")
public class ArticleCategoryController extends BaseController {
    @Resource
    private ArticleCategoryService articleCategoryService;

    @PreAuthorize("hasAuthority('cms:articleCategory:list')")
    @OperationLog
    @ApiOperation("分页查询文章分类表")
    @GetMapping("/page")
    public ApiResult<PageResult<ArticleCategory>> page(ArticleCategoryParam param) {
        PageParam<ArticleCategory, ArticleCategoryParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
        page.setDefaultOrder("sort_number");
        return success(articleCategoryService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(articleCategoryService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('cms:articleCategory:list')")
    @OperationLog
    @ApiOperation("查询全部文章分类表")
    @GetMapping()
    public ApiResult<List<ArticleCategory>> list(ArticleCategoryParam param) {
        PageParam<ArticleCategory, ArticleCategoryParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
        page.setDefaultOrder("sort_number");
        return success(articleCategoryService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(articleCategoryService.listRel(param));
    }

    @PreAuthorize("hasAuthority('cms:articleCategory:list')")
    @OperationLog
    @ApiOperation("根据id查询文章分类表")
    @GetMapping("/{id}")
    public ApiResult<ArticleCategory> get(@PathVariable("id") Integer id) {
        return success(articleCategoryService.getById(id));
        // 使用关联查询
        //return success(articleCategoryService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:articleCategory:save')")
    @OperationLog
    @ApiOperation("添加文章分类表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ArticleCategory articleCategory) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          articleCategory.setUserId(loginUser.getUserId());
        }
        if (articleCategoryService.save(articleCategory)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:articleCategory:update')")
    @OperationLog
    @ApiOperation("修改文章分类表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ArticleCategory articleCategory) {
        if (articleCategoryService.updateById(articleCategory)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:articleCategory:remove')")
    @OperationLog
    @ApiOperation("删除文章分类表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (articleCategoryService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:articleCategory:save')")
    @OperationLog
    @ApiOperation("批量添加文章分类表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ArticleCategory> list) {
        if (articleCategoryService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:articleCategory:update')")
    @OperationLog
    @ApiOperation("批量修改文章分类表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ArticleCategory> batchParam) {
        if (batchParam.update(articleCategoryService, "category_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:articleCategory:remove')")
    @OperationLog
    @ApiOperation("批量删除文章分类表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (articleCategoryService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
