rentaiming
2024-06-03 42f795894094c4d9541381da31357d4bbf93fea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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;
 
/**
 * <p>
 * 用户点赞关联表 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-05-27
 */
@Service
public class MemberLikeServiceImpl extends ServiceImpl<MemberLikeMapper, MemberLike> 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);
                }
                if (memberLikeDTO.getType()==3){
                    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);
                    }
                }
            }
 
        }
    }
}