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.SysAdvice;
import com.mcwl.resource.domain.WorkFlowComment;
import com.mcwl.resource.domain.WorkFlowCommentLike;
import com.mcwl.resource.mapper.WorkFlowCommentLikeMapper;
import com.mcwl.resource.mapper.WorkFlowCommentMapper;
import com.mcwl.resource.service.WorkFlowCommentLikeService;
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;

/**
 * @Author：ChenYan
 * @Project：McWl
 * @Package：com.mcwl.resource.service.impl
 * @Filename：ModelCommentLikeServiceImpl
 * @Description TODO
 * @Date：2025/1/12 12:01
 */
@Service
public class WorkFlowCommentLikeServiceImpl extends ServiceImpl<WorkFlowCommentLikeMapper, WorkFlowCommentLike> implements WorkFlowCommentLikeService {

    @Autowired
    private WorkFlowCommentMapper workFlowCommentMapper;

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

        // 添加点赞记录
        workFlowCommentLike = new WorkFlowCommentLike();
        workFlowCommentLike.setUserId(userId);
        workFlowCommentLike.setWorkFlowCommentId(commentId);
        baseMapper.insert(workFlowCommentLike);

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

        this.addLikeAdvice(workFlowComment);
    }


    private void addLikeAdvice(WorkFlowComment workFlowComment) {

        Long userId = SecurityUtils.getUserId();
        Long receiverUserId = workFlowComment.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(workFlowComment.getWorkFlowId());
        sysAdvice.setProductType(1);
        sysAdvice.setCommentId(workFlowComment.getId());
        sysAdvice.setIsRead(0);
        sysAdvice.setType(AdviceEnum.LIKE_REMIND);


    }

}
