zss
2 天以前 645fe0426017b280aa107b9363d37dc85470601b
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.ruoyi.project.system.service.impl;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.domain.SysNotice;
import com.ruoyi.project.system.mapper.SysNoticeMapper;
import com.ruoyi.project.system.service.ISysNoticeService;
 
/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl  extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {
 
    @Autowired
    private SysNoticeMapper noticeMapper;
 
    @Autowired
    private SysUserMapper userMapper;
 
    @Autowired
    @Lazy
    private ISysNoticeService sysNoticeService;
 
    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }
 
    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public IPage<SysNotice> selectNoticeList(SysNotice notice, Page page) {
        return noticeMapper.selectNoticeList(notice, page);
    }
 
    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice)
    {
        return noticeMapper.insertNotice(notice);
    }
 
    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        return noticeMapper.updateNotice(notice);
    }
 
    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        return noticeMapper.deleteNoticeById(noticeId);
    }
 
    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }
 
    @Override
    public Long getCount(Long consigneeId) {
        return noticeMapper.selectCount(Wrappers.<SysNotice>lambdaQuery()
                .eq(SysNotice::getStatus, "0")
                .eq(SysNotice::getConsigneeId, consigneeId));
    }
 
    @Override
    public int readAll() {
        Long userId = SecurityUtils.getUserId();
        return noticeMapper.update(null, Wrappers.<SysNotice>lambdaUpdate()
                .eq(SysNotice::getConsigneeId, userId)
                .eq(SysNotice::getStatus, "0")
                .set(SysNotice::getStatus, "1"));
    }
 
    @Override
    public void simpleNoticeByRoles(final String title, String message, Long tenantId, List<String> needPushRoles,
                                             final String jumpPath) {
        Long userId = SecurityUtils.getUserId();
        if (StrUtil.isBlank(message) || CollectionUtils.isEmpty(needPushRoles)) {
            return;
        }
        List<String> rolesWithAdmin = new ArrayList<>(needPushRoles);
        rolesWithAdmin.add("管理员");
        List<SysNotice> collect = rolesWithAdmin.stream()
                .flatMap(it -> userMapper.getUserByRole(it).stream())
                .map(it -> convertSysNotice(title, message, it,tenantId, jumpPath, userId))
                .collect(Collectors.toList());
        sysNoticeService.insertBatch(collect);
    }
 
    @Override
    public void simpleNoticeByPerms(String title, String message, Long tenantId, List<String> needPerms,
                                             String jumpPath) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        if (StrUtil.isBlank(message) || CollectionUtils.isEmpty(needPerms)) {
            return;
        }
        List<SysNotice> collect = userMapper.getUserByPerms(needPerms).stream().map(it -> convertSysNotice(title, message, it,tenantId, jumpPath,  userId)).collect(Collectors.toList());
        sysNoticeService.insertBatch(collect);
    }
 
    @Override
    public void simpleNoticeByUser(String title, String message,  List<Long> consigneeId, Long tenantId,
                                    String jumpPath) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<SysNotice> sysNotices = consigneeId.stream().map(it -> convertSysNotice(title, message, it,tenantId, jumpPath, userId)).collect(Collectors.toList());
        sysNoticeService.saveBatch(sysNotices);
    }
 
    @Override
    public void simpleNoticeAll(String title, String message,  Long tenantId,String jumpPath) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<SysNotice> collect = userMapper.selectList(null).stream().map(it -> convertSysNotice(title, message, it.getUserId(),tenantId, jumpPath,  userId)).collect(Collectors.toList());
        sysNoticeService.saveBatch(collect);
    }
 
    @Override
    public void insertBatch(List<SysNotice> noticesList) {
        if(CollectionUtils.isEmpty(noticesList)){
            return;
        }
        // 排除掉自己
        Long userId = SecurityUtils.getUserId();
        List<SysNotice> noticesListNew = noticesList.stream().filter(Objects::nonNull).filter(it -> !Objects.equals(it.getConsigneeId(), userId)).collect(Collectors.toList());
        sysNoticeService.saveBatch(noticesListNew);
 
    }
 
    private SysNotice convertSysNotice(String title,String message,Long consigneeId, Long tenantId,String jumpPath,Long currentUserId) {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeType("1");
        sysNotice.setNoticeTitle(title);//标题
        sysNotice.setNoticeContent(message);
        sysNotice.setStatus("0");
        sysNotice.setConsigneeId(consigneeId);
        sysNotice.setSenderId(currentUserId);
        sysNotice.setJumpPath(jumpPath);
        sysNotice.setTenantId(tenantId);
        return sysNotice;
    }
}