package com.ruoyi.web.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.mall.domain.*;
import com.ruoyi.web.controller.spi.CheckSensitiveWordSpi;
import com.ruoyi.mall.domain.Bo.PublishArticleRequest;
import com.ruoyi.mall.domain.Vo.BusinessArticleVo;
import com.ruoyi.mall.mapper.MallMemberUserMapper;
import com.ruoyi.mall.service.IBusinessArticleCommentService;
import com.ruoyi.mall.service.IBusinessArticleLikeService;
import com.ruoyi.mall.service.IBusinessArticleService;
import com.ruoyi.mall.service.IMemberMessageService;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName 我的-帖子接口
 * @Description 帖子接口
 * @Author
 * @Date 2023/10/17
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/myArticle")
public class ApiMyArticleController {

    @Resource
    private IBusinessArticleService articleService;

    @Resource
    private IBusinessArticleLikeService articleLikeService;

    @Resource
    private IBusinessArticleCommentService commentService;

    @Resource
    private MallMemberUserMapper memberUserMapper;

    @Resource
    private CheckSensitiveWordSpi checkSensitiveWordSpi;

    @Resource
    private IMemberMessageService articleMessageService;

    /**删除评论
     *
     * @param businessArticleComment
     * @return
     */
    @PostMapping("/deleteComment")
    public AjaxResult deleteComment(@RequestBody BusinessArticleComment businessArticleComment) {
        try {//真删 非逻辑
            if(null == businessArticleComment.getCommentId()){
                return AjaxResult.error(500,"删除目标-评论编号不能为空");
            }
            return AjaxResult.success().put("data",commentService.deleteBusinessArticleCommentByCommentId(businessArticleComment.getCommentId()));
        }catch(Exception e){
            e.printStackTrace();
            return AjaxResult.error("删除操作异常");
        }
    }
    /**
     * 删除帖子
     * @param map 地图 articleId 帖子id
     * @return {@link AjaxResult}
     */
    @PostMapping("/deleteArticle")
    public AjaxResult deleteArticle(@RequestBody Map<String, Object> map) {
        try {
            Long articleId = MapUtil.getLong(map, "articleId");
            //不删帖子相关消息 在从消息表打开帖子详情时判断
            //articleMessageService.updateDelFlagByArticleId(articleId);
            //逻辑删帖子
            Long[] longs = {articleId};
            return AjaxResult.success().put("data", articleService.deleteBusinessArticleByArticleIds(longs));
        }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());
            checkSensitiveWordSpi.checkSensitiveWords(memberUserVo.getOpenid(), businessArticle.getArticleText(), 3);
            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 userId 用户id
     * @return {@link AjaxResult}
     */
    @GetMapping("/userArticleList")
    public AjaxResult userArticleList(@NotNull(message = "{api.user.message.articleMessageCount.userId.is.null}") Long userId,
                                      @RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        try{
            PageHelper.startPage(pageNum, pageSize);
            OfflineServices offlineServices = new OfflineServices();
            //List<OfflineServices> list = offlineServicesService.selectOfflineServicesList(offlineServices);
            List<BusinessArticleVo> myArticleVos = articleService.selectListForXcxMy(userId);
            PageInfo<BusinessArticleVo> pageInfo = new PageInfo<>(myArticleVos);
            return AjaxResult.success().put("data",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询用户帖子列表异常");
        }

    }
    /**
      *用户帖子详情
     */
    @GetMapping("/articleDetail")
    public AjaxResult userArticleList(@NotNull(message = "{api.myArticledetail.userId.is.null}") Long userId,@NotNull(message = "{api..myArticledetail.articleId.is.null}") Long articleId) {
        try{
            BusinessArticleVo businessArticleVo = articleService.selectDetailForXcxMy(userId, articleId);
            return AjaxResult.success().put("data",businessArticleVo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询用户帖子详情异常");
        }

    }
    /**
     * 用户消息列表
     *
     * @param userId 用户id
     * @return {@link AjaxResult}
     */
    @GetMapping("/articleMessage/list")
    public AjaxResult articleMessageList(@NotNull(message = "{api.user.message.articleMessageList.userId.is.null}") Long userId,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        try{
            PageHelper.startPage(pageNum, pageSize);
            MemberMessage memberMessage = new MemberMessage();
            memberMessage.setUserId(userId);
            List<MemberMessage> memberMessages = articleMessageService.selectMemberMessageList(memberMessage);
            PageInfo<MemberMessage> pageInfo = new PageInfo<>(memberMessages);
            return AjaxResult.success().put("data",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询用户消息列表异常");
        }
    }

    /**
     * 帖子消息未读计数
     *
     * @param userId 用户id
     * @return {@link AjaxResult}
     */
    @GetMapping("/articleMessage/noReadCount")
    public AjaxResult notReadCount(@NotNull(message = "{api.user.message.articleMessageCount.userId.is.null}") Long userId) {
        try{
            return AjaxResult.success().put("data",articleMessageService.noReadCount(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取未读消息数据异常");
        }
    }
    /**
     * 消息_查看帖子详情
     * @param messageId 消息id
     * @param userId 当前用户id
     * @param articleId 帖子id
     * @return {@link AjaxResult}
     */
    @GetMapping("/articleMessage/articleInfo")
    public AjaxResult articleInfo( @NotNull(message = "{api.message.articleMessage.messageId.is.null}")Long messageId,@NotNull(message = "{api.user.message.articleMessage.articleInfo.articleId.is.null}") Long articleId,Long userId) {
        try {
            // todo 不删帖子相关消息 在从消息表打开帖子详情时判断
            BusinessArticleVo article = articleService.selectDetailForXcxMy(userId,articleId);
            if(null == article){
                return AjaxResult.error("帖子已不在");
            }
            //消息状态为 已读
            articleMessageService.readMessage(messageId);
            //返回详情
            return AjaxResult.success().put("data",article);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("查询帖子详情异常");
        }
    }
}
