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.ModelImageComment;
import com.mcwl.resource.domain.ModelImageCommentLike;
import com.mcwl.resource.domain.SysAdvice;
import com.mcwl.resource.mapper.ModelImageCommentLikeMapper;
import com.mcwl.resource.mapper.ModelImageCommentMapper;
import com.mcwl.resource.service.ModelImageCommentLikeService;
import com.mcwl.system.domain.enums.AdviceEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 图片评论点赞
 */
@Service
@RequiredArgsConstructor
public class ModelImageCommentLikeServiceImpl extends ServiceImpl<ModelImageCommentLikeMapper, ModelImageCommentLike> implements ModelImageCommentLikeService {

    private final ModelImageCommentMapper modelImageCommentMapper;

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

        // 添加点赞记录
        modelImageCommentLike = new ModelImageCommentLike();
        modelImageCommentLike.setUserId(userId);
        modelImageCommentLike.setModelImageCommentId(commentId);
        modelImageCommentLike.setCreateBy(SecurityUtils.getUsername());
        modelImageCommentLike.setUpdateBy(SecurityUtils.getUsername());
        modelImageCommentLike.setUpdateTime(new Date());
        baseMapper.insert(modelImageCommentLike);

        // 更新图片点赞数
        modelImageComment.setLikeNum(modelImageComment.getLikeNum() + 1);
        modelImageCommentMapper.updateById(modelImageComment);

        this.addLikeAdvice(modelImageComment);
    }


    private void addLikeAdvice(ModelImageComment modelImageComment) {

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

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

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

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


    }



}
