package com.ruoyi.frequency.votelike.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.DictConstant;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.VoteLikeReq;
import com.ruoyi.enums.message.InteractionMessageEnums;
import com.ruoyi.frequency.vote.entity.Vote;
import com.ruoyi.frequency.vote.mapper.VoteMapper;
import com.ruoyi.frequency.vote.service.VoteService;
import com.ruoyi.frequency.votecomment.entity.VoteComment;
import com.ruoyi.frequency.votecomment.service.VoteCommentService;
import com.ruoyi.frequency.votelike.entity.VoteLike;
import com.ruoyi.frequency.votelike.mapper.VoteLikeMapper;
import com.ruoyi.frequency.votelike.service.VoteLikeService;
import com.ruoyi.message.frequency.interactionmessagerecord.service.InteractionMessageRecordService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 投票点赞Service业务层处理
 *
 * @author liwenlong
 * @date 2024-04-10
 */
@Service
public class VoteLikeServiceImpl extends ServiceImpl<VoteLikeMapper, VoteLike> implements VoteLikeService {
    @Autowired
    private VoteLikeMapper voteLikeMapper;

    /**
     * 查询投票点赞
     *
     * @param id 投票点赞主键
     * @return 投票点赞
     */
    @Override
    public VoteLike selectVoteLikeById(Long id) {
        return voteLikeMapper.selectVoteLikeById(id);
    }

    /**
     * 查询投票点赞列表
     *
     * @param voteLike 投票点赞
     * @return 投票点赞
     */
    @Override
    public List<VoteLike> selectVoteLikeList(VoteLike voteLike) {
        return voteLikeMapper.selectVoteLikeList(voteLike);
    }

    /**
     * 新增投票点赞
     *
     * @param voteLike 投票点赞
     * @return 结果
     */
    @Override
    public int insertVoteLike(VoteLike voteLike) {
        voteLike.setCreateData();
        return voteLikeMapper.insert(voteLike);
    }

    /**
     * 修改投票点赞
     *
     * @param voteLike 投票点赞
     * @return 结果
     */
    @Override
    public int updateVoteLike(VoteLike voteLike) {
        voteLike.setUpdateData();
        return voteLikeMapper.updateVoteLike(voteLike);
    }

    /**
     * 批量删除投票点赞
     *
     * @param ids 需要删除的投票点赞主键
     * @return 结果
     */
    @Override
    public int deleteVoteLikeByIds(Long[] ids, Integer delFlag) {
        this.lambdaUpdate().set(VoteLike::getDelFlag, delFlag).in(VoteLike::getId, ids).update();
        return ids.length;
    }

    @Autowired
    private BusinessUtil businessUtil;

    @Autowired
    private VoteCommentService voteCommentService;


    @Override
    public ResponseData voteLike(VoteLikeReq req, UserVo userVo) {

        VoteLike voteLike = new VoteLike(req);

        //更新帖子点赞记录
        voteLikeMapper.voteLike(voteLike, userVo);

        voteLike = voteLikeMapper.selectOne(new QueryWrapper<VoteLike>().lambda()
                .eq(VoteLike::getObjectId, req.getObjectId())
                .eq(VoteLike::getType, req.getType())
                .eq(VoteLike::getUserType, userVo.getUserType())
                .eq(VoteLike::getUserId, userVo.getUserId())
        );
        //是否点赞
        Boolean isLike = voteLike.getDelFlag().equals(PublicCommon.启用);

        //是否是对自己的点赞或者踩

        //精彩评论 1 投票 2 投票评论
        if (isLike && req.getType() == 2) {
            //点赞量基础量（超过）
            Integer likeNum = Integer.valueOf(businessUtil.getDict(DictConstant.点赞好评基础量).getDictValue());

            VoteComment voteComment = voteCommentService.getById(req.getObjectId());

            Vote vote = voteService.getById(voteComment.getVoteId());

            if (ObjectUtil.isEmpty(vote.getCommentId()) && voteComment.getParentId() == 0L && voteComment.getLikeNum() >= likeNum) {
                voteService.lambdaUpdate().set(Vote::getCommentId, voteComment.getId())
                        .eq(Vote::getId, voteComment.getVoteId()).update();
            }

        }

        //更新数量
        switch (req.getType()) {
            case 1:
                Vote vote = new Vote().setId(req.getObjectId());
                vote.setUpdateTime(DateUtils.getNowDate());
                voteService.updateById(vote);

                //修改点赞数量
                voteMapper.updateVote(new Vote().setId(req.getObjectId()).setLikeNum(isLike ? 1 : -1));

                break;
            case 2:
                voteCommentService.updateVoteComment(new VoteComment().setId(req.getObjectId()).setLikeNum(isLike ? 1 : -1));
                break;
        }

        //推送消息
        pushMessage(req, userVo, isLike);
        return ResponseData.success();
    }


    /**
     * 推送消息
     *
     * @param req
     * @param userVo
     * @param isPush
     */
    private void pushMessage(VoteLikeReq req, UserVo userVo, Boolean isPush) {
        if (!isPush || req.getType() == 1) {
            return;
        }
        InteractionMessageEnums enums = InteractionMessageEnums.LIKE_VOTE_COMMENT;


        //接收消息用户
        UserVo sendUserVo = null;

        //评论点赞
        VoteComment voteComment = voteCommentService.getById(req.getObjectId());
        Long voteId = voteComment.getVoteId();
        sendUserVo = new UserVo(voteComment.getUserType(), voteComment.getUserId());

        //互动消息
        interactionMessageRecordService.addInteractionMessage(userVo, sendUserVo, PublicCommon.InteractionMessage.点赞, voteId, isPush, enums);
    }

    @Autowired
    private InteractionMessageRecordService interactionMessageRecordService;

    @Autowired
    private VoteService voteService;

    @Autowired
    private VoteMapper voteMapper;


}
