package com.ruoyi.article.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.article.domain.Article; import com.ruoyi.article.domain.ArticleComments; import com.ruoyi.article.domain.MemberLike; import com.ruoyi.article.controller.forepart.dto.MemberLikeDTO; import com.ruoyi.article.mapper.ArticleMapper; import com.ruoyi.article.mapper.MemberLikeMapper; import com.ruoyi.article.service.IArticleCommentsService; import com.ruoyi.article.service.IMemberLikeService; import com.ruoyi.common.core.exception.ServiceException; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** *

* 用户点赞关联表 服务实现类 *

* * @author mitao * @since 2024-05-27 */ @Service public class MemberLikeServiceImpl extends ServiceImpl implements IMemberLikeService { @Resource private ArticleMapper articleMapper; @Resource private IArticleCommentsService articleCommentsService; @Override public void saveMemberLike(MemberLikeDTO memberLikeDTO) { if (memberLikeDTO.getMemberId()==null) { throw new ServiceException("用户ID不能为空"); } if (memberLikeDTO.getState()==null) { throw new ServiceException("类型不能为空"); } LambdaQueryWrapper< MemberLike> wrapper= Wrappers.lambdaQuery(); wrapper.eq(MemberLike::getMemberId,memberLikeDTO.getMemberId()); wrapper.eq(MemberLike::getArticleId,memberLikeDTO.getArticleId()); wrapper.eq(MemberLike::getType,memberLikeDTO.getType()); wrapper.eq( MemberLike::getDelFlag,0); if (memberLikeDTO.getState()==1){ List< MemberLike> memberLikes = this.list(wrapper); if (memberLikes.size()==0){ MemberLike memberLike=new MemberLike(); memberLike.setMemberId(memberLikeDTO.getMemberId()); memberLike.setArticleId(memberLikeDTO.getArticleId()); memberLike.setType(memberLikeDTO.getType()); if (memberLikeDTO.getType()==1){ Article byId = articleMapper.selectById(memberLikeDTO.getArticleId()); byId.setLikeCount(byId.getLikeCount()+1); articleMapper.updateById(byId); } if (memberLikeDTO.getType()==2){ ArticleComments byId = articleCommentsService.getById(memberLikeDTO.getArticleId()); byId.setLikeCount(byId.getLikeCount()+1); articleCommentsService.saveOrUpdate(byId); } this.save(memberLike); } }else{ List< MemberLike> memberLikes = this.list(wrapper); if (memberLikes.size()>0){ for (MemberLike memberLike:memberLikes){ this.removeById(memberLike); if (memberLikeDTO.getType()==1){ Article byId = articleMapper.selectById(memberLikeDTO.getArticleId()); byId.setLikeCount(byId.getLikeCount()-1); articleMapper.updateById(byId); } if (memberLikeDTO.getType()==2){ ArticleComments byId = articleCommentsService.getById(memberLikeDTO.getArticleId()); byId.setLikeCount(byId.getLikeCount()-1); articleCommentsService.saveOrUpdate(byId); } if (memberLikeDTO.getType()==3){ ArticleComments byId = articleCommentsService.getById(memberLikeDTO.getArticleId()); byId.setLikeCount(byId.getLikeCount()-1); articleCommentsService.saveOrUpdate(byId); } } } } } }