package com.gxwebsoft.cms.controller;

import com.gxwebsoft.cms.entity.Article;
import com.gxwebsoft.cms.entity.ArticleComment;
import com.gxwebsoft.cms.param.ArticleCommentParam;
import com.gxwebsoft.cms.param.ArticleParam;
import com.gxwebsoft.cms.service.ArticleCommentService;
import com.gxwebsoft.cms.service.ArticleService;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.love.socketio.cache.ClientCache;
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-07-07 14:14:35
 */
@Api(tags = "文章评论表管理")
@RestController
@RequestMapping("/api/cms/article-comment")
public class ArticleCommentController extends BaseController {
    @Resource
    private ArticleCommentService articleCommentService;
    @Resource
    private ArticleService articleService;

  @Resource
  private ClientCache clientCache;


    @PreAuthorize("hasAuthority('cms:articleComment:list')")
    @OperationLog
    @ApiOperation("获取未读评论数")
    @GetMapping("/unread")
    public ApiResult<Integer> page() {
        User loginUser = getLoginUser();
        return success(articleCommentService.getUserUnReadCount(loginUser.getUserId()));
    }

    @PreAuthorize("hasAuthority('cms:articleComment:list')")
    @OperationLog
    @ApiOperation("分页查询文章评论表")
    @GetMapping("/page")
    public ApiResult<PageResult<ArticleComment>> page(ArticleCommentParam param) {
        User loginUser = getLoginUser();
        // 使用关联查询
        if(loginUser != null){
          param.setLoginUserId(getLoginUserId());
        }
        // 使用关联查询
        return success(articleCommentService.pageRel(param));
    }

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

    @PreAuthorize("hasAuthority('cms:articleComment:list')")
    @OperationLog
    @ApiOperation("根据id查询文章评论表")
    @GetMapping("/{id}")
    public ApiResult<PageResult<Article>> get(@PathVariable("id") Integer id) {
        final ArticleComment comment = articleCommentService.getByIdRel(id);
        // 查询文章
        ArticleParam param = new ArticleParam();
        param.setArticleId(comment.getArticleId());
        comment.setStatus(1);
        articleCommentService.updateById(comment);
        final PageResult<Article> result = articleService.pageRel(param);
        return success(result);
    }

    @PreAuthorize("hasAuthority('cms:articleComment:save')")
    @OperationLog
    @ApiOperation("添加文章评论表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ArticleComment articleComment) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          articleComment.setUserId(loginUser.getUserId());
        }
        if (articleCommentService.save(articleComment)) {
            // 累加文章评论数量
          final Article article = articleService.getById(articleComment.getArticleId());
          article.setCommentNumbers(articleComment.getCountComment() + 1);
          articleService.updateById(article);
          // 获取未读评论数
            int count = articleCommentService.getUserUnReadCount(articleComment.getToUserId());
            if(!articleComment.getUserId().equals(articleComment.getToUserId())){
                clientCache.sendUserEvent(articleComment.getToUserId() + "", "pyq", count);
            }
          return success("发表成功",article);
        }
        return fail("发表失败");
    }

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

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

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

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

}
