admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.yeshi.fanli.service.impl.user.cloud;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.user.cloud.UserCloudGroupMapper;
import com.yeshi.fanli.entity.bus.user.cloud.UserCloudGroup;
import com.yeshi.fanli.exception.user.cloud.UserCloudGroupException;
import com.yeshi.fanli.service.inter.user.cloud.UserCloudGroupService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.annotation.RequestSerializableByKeyService;
 
 
@Service
public class UserCloudGroupServiceImpl implements UserCloudGroupService {
 
    @Resource
    private UserCloudGroupMapper userCloudGroupMapper;
 
    
    @Override
    @RequestSerializableByKeyService(key = "#uid")
    public void addCircle(Long uid) {
        List<UserCloudGroup> list = userCloudGroupMapper.listByUidAndType(uid, UserCloudGroup.TYPE_CIRCLE);
        if (list != null && list.size() > 0)
            return;
        
        UserCloudGroup cloudGroup = new UserCloudGroup();
        cloudGroup.setUid(uid);
        cloudGroup.setType(UserCloudGroup.TYPE_CIRCLE);
        cloudGroup.setState(false);
        cloudGroup.setCreateTime(new Date());
        userCloudGroupMapper.insertSelective(cloudGroup);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    @RequestSerializableByKeyService(key = "#uid")
    public void addGroup(Long uid, String groupId, String groupName, int maxNum){
        List<UserCloudGroup> list = userCloudGroupMapper.listByUidAndType(uid, UserCloudGroup.TYPE_GROUP);
        if (list != null && list.size() >= maxNum)
            return;
        
        boolean exist = false;
        for (UserCloudGroup userCloudGroup: list) {
            if (groupId.equals(userCloudGroup.getGroupId())) {
                exist = true;
                // 群名为空
                if (StringUtil.isNullOrEmpty(groupName)) {
                    return;
                }
                
                // 群名字变化
                if (!groupName.equals(userCloudGroup.getGroupName())) {
                    UserCloudGroup update = new UserCloudGroup();
                    update.setGroupName(groupName);
                    update.setUpdateTime(new Date());
                    userCloudGroupMapper.updateByPrimaryKeySelective(update);
                }
                break;
            } 
        }
        
        if (exist) 
            return;
        
        // 群名为空
        if (StringUtil.isNullOrEmpty(groupName)) {
            groupName = "本群未命名名称";
        }
        
        UserCloudGroup cloudGroup = new UserCloudGroup();
        cloudGroup.setUid(uid);
        cloudGroup.setType(UserCloudGroup.TYPE_GROUP);
        cloudGroup.setState(false);
        cloudGroup.setCreateTime(new Date());
        cloudGroup.setGroupId(groupId);
        cloudGroup.setGroupName(groupName);
        userCloudGroupMapper.insertSelective(cloudGroup);
    }
    
    @Override
    public void switchCircleState(long uid, boolean state) throws UserCloudGroupException{
        List<UserCloudGroup> list = userCloudGroupMapper.listByUidAndType(uid, UserCloudGroup.TYPE_CIRCLE);
        if (list == null || list.size() == 0)
            throw new UserCloudGroupException(1, "该记录已不存在");
        
        if (list.get(0).getUid() != uid)
            throw new UserCloudGroupException(1, "该记录已不存在");
        
        UserCloudGroup update = new UserCloudGroup();
        update.setId(list.get(0).getId());
        update.setState(state);
        update.setUpdateTime(new Date());
        userCloudGroupMapper.updateByPrimaryKeySelective(update);
    }
    
    
    @Override
    public void switchGroupState(long uid, long id, boolean state) throws UserCloudGroupException{
        UserCloudGroup cloudGroup = userCloudGroupMapper.selectByPrimaryKey(id);
        if (cloudGroup == null || cloudGroup.getUid() != uid)
            throw new UserCloudGroupException(1, "该记录已不存在");
        
        UserCloudGroup update = new UserCloudGroup();
        update.setId(id);
        update.setState(state);
        update.setUpdateTime(new Date());
        userCloudGroupMapper.updateByPrimaryKeySelective(update);
    }
    
    @Override
    public void deleteGroup(long uid, long id) throws UserCloudGroupException{
        UserCloudGroup cloudGroup = userCloudGroupMapper.selectByPrimaryKey(id);
        if (cloudGroup == null || cloudGroup.getUid() != uid)
            throw new UserCloudGroupException(1, "该记录已不存在");
        
        if (cloudGroup.getUid() != uid) {
            throw new UserCloudGroupException(1, "该群已不存在");
        }
        userCloudGroupMapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public void deleteGroupByUid(Long uid){
        userCloudGroupMapper.deleteGroupByUid(uid);
    }
    
    @Override
    public List<UserCloudGroup> listByUid(Long uid) {
        return userCloudGroupMapper.listByUid(uid);
    }
    
    @Override
    public List<UserCloudGroup> listGroupByUid(Long uid) {
        return userCloudGroupMapper.listGroupByUid(uid);
    }
}