liyong
50 分钟以前 8f5be003e52fdbf034d1955216143bd184ebbdb4
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
package cn.iocoder.yudao.module.im.service.channel;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.im.controller.admin.manager.channel.vo.channel.ImChannelPageReqVO;
import cn.iocoder.yudao.module.im.controller.admin.manager.channel.vo.channel.ImChannelSaveReqVO;
import cn.iocoder.yudao.module.im.dal.dataobject.channel.ImChannelDO;
import cn.iocoder.yudao.module.im.dal.mysql.channel.ImChannelMapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.im.enums.ErrorCodeConstants.*;
 
/**
 * IM 频道 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ImChannelServiceImpl implements ImChannelService {
 
    @Resource
    private ImChannelMapper channelMapper;
    @Resource
    @Lazy // 延迟加载,解决循环依赖
    private ImChannelMaterialService channelMaterialService;
 
    // ==================== 用户端 ====================
 
    @Override
    public List<ImChannelDO> getChannelListByStatus(Integer status) {
        return channelMapper.selectListByStatusOrderBySort(status);
    }
 
    @Override
    public List<ImChannelDO> getChannelList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return channelMapper.selectByIds(ids);
    }
 
    @Override
    public ImChannelDO validateChannelExists(Long id) {
        ImChannelDO channel = channelMapper.selectById(id);
        if (channel == null) {
            throw exception(IM_CHANNEL_NOT_EXISTS);
        }
        return channel;
    }
 
    // ==================== 管理后台 ====================
 
    @Override
    public PageResult<ImChannelDO> getChannelPage(ImChannelPageReqVO reqVO) {
        return channelMapper.selectPage(reqVO);
    }
 
    @Override
    public ImChannelDO getChannel(Long id) {
        return channelMapper.selectById(id);
    }
 
    @Override
    public Long createChannel(ImChannelSaveReqVO reqVO) {
        // 校验 code 唯一
        validateCodeUnique(null, reqVO.getCode());
 
        // 插入
        ImChannelDO channel = BeanUtils.toBean(reqVO, ImChannelDO.class);
        channelMapper.insert(channel);
        return channel.getId();
    }
 
    @Override
    public void updateChannel(ImChannelSaveReqVO reqVO) {
        // 1.1 校验存在
        validateChannelExists(reqVO.getId());
        // 1.2 校验 code 唯一
        validateCodeUnique(reqVO.getId(), reqVO.getCode());
 
        // 2. 更新
        ImChannelDO updateObj = BeanUtils.toBean(reqVO, ImChannelDO.class);
        channelMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteChannel(Long id) {
        // 1.1 校验存在
        validateChannelExists(id);
        // 1.2 防止误删频道导致历史素材 / 消息回查不到归属
        if (channelMaterialService.getMaterialCountByChannelId(id) > 0) {
            throw exception(IM_CHANNEL_HAS_MATERIAL);
        }
 
        // 2. 删除频道
        channelMapper.deleteById(id);
    }
 
    private void validateCodeUnique(Long id, String code) {
        ImChannelDO exist = channelMapper.selectByCode(code);
        if (exist == null) {
            return;
        }
        if (id == null || ObjUtil.notEqual(exist.getId(), id)) {
            throw exception(IM_CHANNEL_CODE_DUPLICATED, code);
        }
    }
 
}