package com.ruoyi.post.frequency.posreport.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.enums.process.ProcessResultEnums;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.post.controller.req.PostReportReq;
import com.ruoyi.post.frequency.posreport.entity.PostReport;
import com.ruoyi.post.frequency.posreport.mapper.PostReportMapper;
import com.ruoyi.post.frequency.posreport.service.PostReportService;
import com.ruoyi.post.frequency.post.entity.Post;
import com.ruoyi.post.frequency.post.service.PostService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.UserInfo;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 论坛举报Service业务层处理
 *
 * @author liwenlong
 * @date 2023-10-26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PostReportServiceImpl extends ServiceImpl<PostReportMapper, PostReport>  implements PostReportService
{
    @Autowired
    private PostReportMapper postReportMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private PostService postService;

    /**
     * 查询论坛举报
     *
     * @param id 论坛举报主键
     * @return 论坛举报
     */
    @Override
    public PostReport selectPostReportById(Long id)
    {
        PostReport s = postReportMapper.selectPostReportById(id);
        if (s != null){
            Set<UserVo> userVoSet = new HashSet<>();
            userVoSet.add(new UserVo(s.getUserType(),s.getUserId()));

            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet,null);


            //查询帖子信息
            Post post = postService.selectPostById(s.getPostId());
            s.setUserInfo(userMap.get(new UserVo(s.getUserType(),s.getUserId())));
            s.setPost(post);
        }
        return s;
    }

    /**
     * 查询论坛举报列表
     *
     * @param postReport 论坛举报
     * @return 论坛举报
     */
    @Override
    public List<PostReport> selectPostReportList(PostReport postReport)
    {
        List<PostReport> postReports = postReportMapper.selectPostReportList(postReport);

        if (postReports != null && postReports.size() > 0){

            Set<UserVo> userVoSet = postReports.stream().map(s -> new UserVo(s.getUserType(),s.getUserId())).collect(Collectors.toSet());

            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet,null);

            Set<Long> postIds = postReports.stream().map(PostReport::getPostId).collect(Collectors.toSet());

            //查询帖子信息
            Map<Long, Post> postMap =  postService.selectPostMap(postIds);

            postReports.stream().forEach(s ->{
                s.setUserInfo(userMap.get(new UserVo(s.getUserType(),s.getUserId())));
                s.setPost(postMap.get(s.getPostId()));
            });
        }

        return postReports;
    }

    /**
     * 新增论坛举报
     *
     * @param postReport 论坛举报
     * @return 结果
     */
    @Override
    public int insertPostReport(PostReport postReport)
    {
        postReport.setCreateTime(DateUtils.getNowDate());
        return postReportMapper.insertPostReport(postReport);
    }

    /**
     * 修改论坛举报
     *
     * @param postReport 论坛举报
     * @return 结果
     */
    @Override
    public int updatePostReport(PostReport postReport)
    {
        postReport.setUpdateTime(DateUtils.getNowDate());
        return postReportMapper.updatePostReport(postReport);
    }

    /**
     * 批量删除论坛举报
     *
     * @param ids 需要删除的论坛举报主键
     * @return 结果
     */
    @Override
    public int deletePostReportByIds(Long[] ids,Integer delFlag)
    {
        this.lambdaUpdate().set(PostReport::getDelFlag,delFlag).in(PostReport::getId,ids).update();
        return ids.length;
    }

    /**
     * 删除论坛举报信息
     *
     * @param id 论坛举报主键
     * @return 结果
     */
    @Override
    public int deletePostReportById(Long id)
    {
        this.lambdaUpdate().set(PostReport::getDelFlag,PublicCommon.删除).eq(PostReport::getId,id).update();
        return 1;
    }

    @Override
    public AjaxResult handle(PostReport postReport) {

        boolean update = this.lambdaUpdate().set(PostReport::getResult, postReport.getResult())
                .eq(PostReport::getResult, ProcessResultEnums.PENDING.getCode())
                .eq(PostReport::getId, postReport.getId()).update();
        //处理（审核通过）
        if (update && ObjectUtil.equal(ProcessResultEnums.APPROVED.getCode(), postReport.getResult())) {
//            PostReport byId = this.getById(postReport.getId());
//            postService.lambdaUpdate().set(Post::getDelFlag,PublicCommon.禁用).eq(Post::getId,byId.getPostId()).update();
        }
        return AjaxResult.success();
    }

    @Override
    public ResponseData postReport(PostReportReq req, UserVo userVo) {
        PostReport postReport = new PostReport().setPostId(req.getPostId())
                .setUserType(userVo.getUserType())
                .setUserId(userVo.getUserId())
                .setReason(req.getReason())
                .setContent(req.getContent())
                .setImages(req.getImages());
        this.save(postReport);

        return ResponseData.success();
    }
}
