package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.ArticleService;
import com.gxwebsoft.cms.entity.Article;
import com.gxwebsoft.cms.param.ArticleParam;
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 WebSoft
 * @since 2022-11-16 11:40:27
 */
@Api(tags = "文章记录表管理")
@RestController
@RequestMapping("/api/cms/article")
public class ArticleController extends BaseController {
    @Resource
    private ArticleService articleService;

    @PreAuthorize("hasAuthority('cms:article:list')")
    @OperationLog
    @ApiOperation("分页查询文章记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<Article>> page(ArticleParam param) {
        // 使用关联查询
        return success(articleService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('cms:article:list')")
    @OperationLog
    @ApiOperation("查询全部文章记录表")
    @GetMapping()
    public ApiResult<List<Article>> list(ArticleParam param) {
        PageParam<Article, ArticleParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        return success(articleService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(articleService.listRel(param));
    }

    @PreAuthorize("hasAuthority('cms:article:list')")
    @OperationLog
    @ApiOperation("根据id查询文章记录表")
    @GetMapping("/{id}")
    public ApiResult<Article> get(@PathVariable("id") Integer id) {
//        return success(articleService.getById(id));
        // 使用关联查询
        Article article = articleService.getByIdRel(id);
        article.setArticleId(id);
        article.setVirtualViews(article.getVirtualViews()+1);
        articleService.saveOrUpdate(article);
        return success(article);
    }

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

    @PreAuthorize("hasAuthority('cms:article:update')")
    @OperationLog
    @ApiOperation("修改文章记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Article article) {
        if (articleService.updateById(article)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

    @PreAuthorize("hasAuthority('cms:article:update')")
    @OperationLog
    @ApiOperation("批量修改文章记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Article> batchParam) {
        if (batchParam.update(articleService, "article_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
