package com.ruoyi.web.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.spi.CheckSensitiveWordSpi;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.Bo.CommentArticleRequest;
import com.ruoyi.mall.domain.Bo.CommentDelRequest;
import com.ruoyi.mall.domain.Bo.CommentReplyRequest;
import com.ruoyi.mall.domain.Vo.BusinessArticleVo;
import com.ruoyi.mall.domain.Vo.QueryArtilceCommentListResponse;
import com.ruoyi.mall.domain.Vo.QueryArtilceReplyListResponse;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import com.ruoyi.mall.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @ClassName 论坛模块
 * @Description 论坛模块接口
 * @Author
 * @Date 2023/10/17
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/Article")
public class ApiArticleController {

    @Resource
    private IBusinessArticleService articleService;

    @Resource
    private IBusinessArticleLikeService articleLikeService;

    @Resource
    private IBusinessArticleCommentService commentService;

    @Resource
    private MallMemberUserMapper memberUserMapper;

    @Resource
    private CheckSensitiveWordSpi checkSensitiveWordSpi;

    @Resource
    private IArticleTagService articleTagService;

    /**帖子详情
     * @param userId　当前登录用户id
     * @param articleId　当前登录用户id
     */
    @GetMapping("/getArticleDetail")
    public AjaxResult getArticleDetail(@NotNull(message = "article.getArticleDetail.userId.isnull") Long userId,@NotNull(message = "article.getArticleDetail.articleId.isnull")Long articleId) {
        try{
            //// 一级评论列表(评论人头像 名字 内容 时间） 单独接口请求
            //List<>
            return AjaxResult.success().put("data",articleService.selectDetailForXcx(userId,articleId));
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("查询帖子详情异常");
        }

    }
    /**
     * 帖子列表
     * @param pageNum 页码
     * @param pageSize 页大小
     * @param userId　当前登录用户id
     * @param content　搜索框内容
     * @param orderByColumn　需要排序的字段
     * @param descFlag　时间排序desc/asc
     * @return
     */
    @GetMapping("/getArticleList")
    public AjaxResult getArticleList(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize,
                                     @NotNull(message = "article.getArticleList.userId.isnull") Long userId,
                                     String content,
                                     String orderByColumn,
                                     String descFlag) {
        try{
            PageHelper.startPage(pageNum, pageSize);
            List<BusinessArticleVo> list = articleService.selectListForXcx(userId,content,orderByColumn,descFlag);
            PageInfo<BusinessArticleVo> pageInfo = new PageInfo<>(list);
            return AjaxResult.success().put("data", pageInfo);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("查询帖子列表异常");
        }
    }

    /**发布帖子
     *
     * @param businessArticle
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BusinessArticle businessArticle) {

        try {
            //敏感词
            MallMemberUser memberUserVo = memberUserMapper.selectMallMemberUserById(businessArticle.getUserId());
            if(StringUtils.isNotBlank( businessArticle.getArticleText())) {
                checkSensitiveWordSpi.checkSensitiveWords(memberUserVo.getOpenid(), businessArticle.getArticleText(), 3);
            }
            if(StringUtils.isNotBlank( businessArticle.getArticleTag())) {
                checkSensitiveWordSpi.checkSensitiveWords(memberUserVo.getOpenid(), businessArticle.getArticleTag(), 3);
            }
                //增
            int i = articleService.insertBusinessArticle(businessArticle);
            if(i>0){
                return AjaxResult.success("发布成功");
            }else{
                return AjaxResult.error("发布失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("发布异常");
        }
    }


    /**编辑帖子
     *（帖子id必传）
     * @param businessArticle
     * @return
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BusinessArticle businessArticle) {
        try {
            //敏感词
            MallMemberUser memberUserVo = memberUserMapper.selectMallMemberUserById(businessArticle.getUserId());
            if(StringUtils.isNotBlank(businessArticle.getArticleText())){
                checkSensitiveWordSpi.checkSensitiveWords(memberUserVo.getOpenid(), businessArticle.getArticleText(), 3);
            }
            if(StringUtils.isNotBlank(businessArticle.getArticleTag() )){
                checkSensitiveWordSpi.checkSensitiveWords(memberUserVo.getOpenid(), businessArticle.getArticleTag(), 3);
            }

            //改
            int i = articleService.updateBusinessArticle(businessArticle);
            if (i > 0) {
                return AjaxResult.success("编辑成功");
            } else {
                return AjaxResult.error("编辑失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("编辑异常");
        }
    }

    /**逻辑删除帖子
     *
     * @param businessArticle
     * @return
     */
    @PostMapping("/del")
    public AjaxResult del(@RequestBody BusinessArticle businessArticle) {
        try{
            if(null == businessArticle.getArticleId()){
                return AjaxResult.error(500,"删除目标-帖子编号不能为空");
            }
            //增
            Long[] ids = new Long[1];
            ids[0] = businessArticle.getArticleId();
            int i = articleService.deleteBusinessArticleByArticleIds(ids);
            if(i>0){
                return AjaxResult.success("删除成功");
            }else{
                return AjaxResult.error("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }

    }


    /**热门话题
     *取前20
     * @return
     */
    @GetMapping("/hotTagList")
    public AjaxResult hotTagList(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        try{
            ArticleTag param = new ArticleTag();
            PageHelper.startPage(pageNum, pageSize);
            List<ArticleTag> articleTags = articleTagService.selectArticleTagList(param);
            PageInfo<ArticleTag> pageInfo = new PageInfo<>(articleTags);
            return AjaxResult.success().put("data", pageInfo);
        }catch(Exception e){
            e.printStackTrace();
            return AjaxResult.error("操作异常");
        }

    }

    /**
     * 点赞/取消点赞帖子
     *
     */
    @PostMapping("/likeAndCancelLikes")
    public AjaxResult likeAndCancelLikes(@RequestBody BusinessArticle businessArticle) {
        try{
            return AjaxResult.success().put("data",articleLikeService.likeAndCancelLikes(businessArticle.getArticleId(),businessArticle.getUserId()));
        }catch(Exception e){
            e.printStackTrace();
            return AjaxResult.error("操作异常");
        }
    }




    /**查询评论的回复列表
     *
     * @param pageNum 页码
     * @param pageSize 页大小
     * @param commentId 评论id
     * @return
     */
    @GetMapping("/queryArtilceReplyList")
    public AjaxResult queryArtilceReplyList(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                           @NotNull(message = "{api.article.queryArtilceReplyList.commentId,is.null}")Long commentId) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<QueryArtilceReplyListResponse> queryArtilceReplyListResponses = commentService.queryArtilceReplyList(commentId);
            PageInfo<QueryArtilceReplyListResponse> pageInfo = new PageInfo<>(queryArtilceReplyListResponses);
            return AjaxResult.success().put("data", pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("回复评论列表异常");
        }

    }
    /**
     * 查询帖子的一级评论列表
     *
     * @param artilceId 帖子id
     * @return {@link AjaxResult}
     */
    @GetMapping("/queryArtilceCommentList")
    public AjaxResult queryArtilceCommentList(@RequestParam(defaultValue = "1") Integer pageNum,
                                             @RequestParam(defaultValue = "10") Integer pageSize,
                                             @NotNull(message = "{api.article.queryArtilceCommentList.artilceId,is.null}")Long artilceId) {

        try {
            PageHelper.startPage(pageNum, pageSize);
            List<QueryArtilceCommentListResponse> queryArtilceCommentListResponses = commentService.queryArtilceCommentList(artilceId);
            PageInfo<QueryArtilceCommentListResponse> pageInfo = new PageInfo<>(queryArtilceCommentListResponses);
            return AjaxResult.success().put("data", pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("评论列表异常");
        }
    }


    /**
     * 回复评论
     *
     * @param commentReplyRequest 回复评论请求
     * @return {@link AjaxResult}
     */
    @PostMapping("/replyComment")
    public AjaxResult replyComment(@RequestBody CommentReplyRequest commentReplyRequest) {
        try{
            //敏感词
            //用户
            MallMemberUser memberUserVo = memberUserMapper.selectMallMemberUserById(commentReplyRequest.getUserId());
            checkSensitiveWordSpi.checkSensitiveWords(memberUserVo.getOpenid(),commentReplyRequest.getCommentText(),2);

            return AjaxResult.success().put("data",commentService.replyComment(commentReplyRequest));
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("回复评论异常");
        }

    }
    /**
     * 评论帖子
     *
     * @param commentArticleRequest 评论帖子请求
     * @return {@link AjaxResult}
     */
    @PostMapping("/commentArticle")
    public AjaxResult commentArticle(@RequestBody CommentArticleRequest commentArticleRequest) {
        try{
            //敏感词
            //用户
            MallMemberUser memberUserVo = memberUserMapper.selectMallMemberUserById(commentArticleRequest.getUserId());
            checkSensitiveWordSpi.checkSensitiveWords(memberUserVo.getOpenid(),commentArticleRequest.getCommentText(),2);
            return AjaxResult.success().put("data",commentService.commentArticle(commentArticleRequest));
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("评论异常");
        }
    }

    /**删除评论
     *
     * @param commentDelRequest
     * @return
     */
    @PostMapping("/delComment")
    public AjaxResult delComment(@RequestBody CommentDelRequest commentDelRequest) {
        try{
            String s = commentService.delCommentArticle(commentDelRequest);
            if(s.equals("删除成功")){
                return AjaxResult.success(s);
            }else{
                return AjaxResult.error(s);
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("删除评论异常");
        }
}
}
