package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.ArticleCommentService;
import com.gxwebsoft.cms.entity.ArticleComment;
import com.gxwebsoft.cms.param.ArticleCommentParam;
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 2023-10-22 15:47:09
 */
@Api(tags = "文章评论")
@RestController
@RequestMapping("/api/cms/article-comment")
public class ArticleCommentController extends BaseController {
    @Resource
    private ArticleCommentService articleCommentService;

    @PreAuthorize("hasAuthority('cms:articleComment:list')")
    @ApiOperation("分页查询文章评论表")
    @GetMapping("/page")
    public ApiResult<PageResult<ArticleComment>> page(ArticleCommentParam param) {
        PageParam<ArticleComment, ArticleCommentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(articleCommentService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(articleCommentService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('cms:articleComment:list')")
    @ApiOperation("查询全部文章评论表")
    @GetMapping()
    public ApiResult<List<ArticleComment>> list(ArticleCommentParam param) {
        PageParam<ArticleComment, ArticleCommentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(articleCommentService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(articleCommentService.listRel(param));
    }

    @PreAuthorize("hasAuthority('cms:articleComment:list')")
    @ApiOperation("根据id查询文章评论表")
    @GetMapping("/{id}")
    public ApiResult<ArticleComment> get(@PathVariable("id") Integer id) {
        return success(articleCommentService.getById(id));
        // 使用关联查询
        //return success(articleCommentService.getByIdRel(id));
    }

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

    @PreAuthorize("hasAuthority('cms:articleComment:update')")
    @ApiOperation("修改文章评论表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ArticleComment articleComment) {
        if (articleCommentService.updateById(articleComment)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @PreAuthorize("hasAuthority('cms:articleComment:save')")
    @ApiOperation("批量添加文章评论表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ArticleComment> list) {
        if (articleCommentService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:articleComment:update')")
    @ApiOperation("批量修改文章评论表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ArticleComment> batchParam) {
        if (batchParam.update(articleCommentService, "comment_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
