package com.mcwl.resource.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.exception.ServiceException;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.common.utils.StringUtils;
import com.mcwl.resource.domain.ModelComment;
import com.mcwl.resource.domain.ModelCommentLike;
import com.mcwl.resource.domain.SysAdvice;
import com.mcwl.resource.mapper.ModelCommentLikeMapper;
import com.mcwl.resource.mapper.ModelCommentMapper;
import com.mcwl.resource.service.ModelCommentLikeService;
import com.mcwl.system.domain.enums.AdviceEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 模型评论点赞
 */
@Service
public class ModelCommentLikeServiceImpl extends ServiceImpl<ModelCommentLikeMapper, ModelCommentLike> implements ModelCommentLikeService {

    @Autowired
    private ModelCommentMapper modelCommentMapper;

    @Override
    @Transactional
    public void like(Long commentId) {
        ModelComment modelComment = modelCommentMapper.selectById(commentId);
        if (Objects.isNull(modelComment)) {
            throw new ServiceException("该评论不存在");
        }
        Long userId = SecurityUtils.getUserId();
        ModelCommentLike modelCommentLike = baseMapper.getLikeComment(userId, commentId);
        if (Objects.nonNull(modelCommentLike)) {
            if (Objects.equals(modelCommentLike.getDelFlag(), "0")) {
                modelCommentLike.setDelFlag("2");
                int likeNum = modelComment.getLikeNum() - 1;
                likeNum = Math.max(likeNum, 0);
                modelComment.setLikeNum(likeNum);
            } else {
                modelCommentLike.setDelFlag("0");
                modelComment.setLikeNum(modelComment.getLikeNum() + 1);
                this.addLikeAdvice(modelComment);
            }
            // 更新点赞记录
            baseMapper.updateDelFlagById(modelCommentLike);
            // 更新图片评论点赞数
            modelCommentMapper.updateById(modelComment);
            return;
        }

        // 添加点赞记录
        modelCommentLike = new ModelCommentLike();
        modelCommentLike.setUserId(userId);
        modelCommentLike.setModelCommentId(commentId);
        baseMapper.insert(modelCommentLike);

        // 更新模型点赞数
        modelComment.setLikeNum(modelComment.getLikeNum() + 1);
        modelCommentMapper.updateById(modelComment);

        this.addLikeAdvice(modelComment);
    }


    private void addLikeAdvice(ModelComment modelComment) {

        Long userId = SecurityUtils.getUserId();
        Long receiverUserId = modelComment.getUserId();

        if (Objects.equals(userId, receiverUserId)) {
            return;
        }

        String content = StringUtils.format("恭喜！{}点赞了您的评论",
                SecurityUtils.getLoginUser().getUser().getNickName());

        SysAdvice sysAdvice = new SysAdvice();
        sysAdvice.setSenderId(userId);
        sysAdvice.setReceiverId(receiverUserId);
        sysAdvice.setContent(content);
        sysAdvice.setProductId(modelComment.getModelId());
        sysAdvice.setProductType(0);
        sysAdvice.setCommentId(modelComment.getId());
        sysAdvice.setIsRead(0);
        sysAdvice.setType(AdviceEnum.LIKE_REMIND);


    }


}
