WeSensitiveServiceImpl.java
16.5 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package com.linkwechat.wecom.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import com.linkwechat.common.core.elasticsearch.ElasticSearch;
import com.linkwechat.common.core.page.PageDomain;
import com.linkwechat.common.core.page.TableSupport;
import com.linkwechat.common.enums.MessageType;
import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeMessagePushClient;
import com.linkwechat.wecom.domain.WeCorpAccount;
import com.linkwechat.wecom.domain.WeSensitive;
import com.linkwechat.wecom.domain.WeSensitiveAuditScope;
import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.dto.WeMessagePushDto;
import com.linkwechat.wecom.domain.dto.message.TextMessageDto;
import com.linkwechat.wecom.domain.query.WeSensitiveHitQuery;
import com.linkwechat.wecom.mapper.WeSensitiveMapper;
import com.linkwechat.wecom.service.IWeCorpAccountService;
import com.linkwechat.wecom.service.IWeSensitiveAuditScopeService;
import com.linkwechat.wecom.service.IWeSensitiveService;
import com.linkwechat.wecom.service.IWeUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* 敏感词设置Service业务层处理
*
* @author ruoyi
* @date 2020-12-29
*/
@Service
@Slf4j
public class WeSensitiveServiceImpl implements IWeSensitiveService {
@Autowired
private WeSensitiveMapper weSensitiveMapper;
@Autowired
private IWeSensitiveAuditScopeService sensitiveAuditScopeService;
@Autowired
private ElasticSearch elasticSearch;
@Autowired
private IWeUserService weUserService;
@Autowired
private WeMessagePushClient weMessagePushClient;
@Autowired
private IWeCorpAccountService weCorpAccountService;
@Value("${wecome.chatKey}")
private String chartKey;
/**
* 查询敏感词设置
*
* @param id 敏感词设置ID
* @return 敏感词设置
*/
@Override
public WeSensitive selectWeSensitiveById(Long id) {
return weSensitiveMapper.selectWeSensitiveById(id);
}
/**
* 查询敏感词设置列表
*
* @param weSensitive 敏感词设置
* @return 敏感词设置
*/
@Override
public List<WeSensitive> selectWeSensitiveList(WeSensitive weSensitive) {
return weSensitiveMapper.selectWeSensitiveList(weSensitive);
}
/**
* 新增敏感词设置
*
* @param weSensitive 敏感词设置
* @return 结果
*/
@Override
@Transactional
public int insertWeSensitive(WeSensitive weSensitive) {
weSensitive.setCreateBy(SecurityUtils.getUsername());
weSensitive.setCreateTime(DateUtils.getNowDate());
int insertResult = weSensitiveMapper.insertWeSensitive(weSensitive);
if (insertResult > 0) {
if (CollectionUtils.isNotEmpty(weSensitive.getAuditUserScope())) {
if (weSensitive.getId() != null) {
weSensitive.getAuditUserScope().forEach(scope -> {
scope.setSensitiveId(weSensitive.getId());
});
sensitiveAuditScopeService.insertWeSensitiveAuditScopeList(weSensitive.getAuditUserScope());
}
}
}
return insertResult;
}
/**
* 修改敏感词设置
*
* @param weSensitive 敏感词设置
* @return 结果
*/
@Override
@Transactional
public int updateWeSensitive(WeSensitive weSensitive) {
weSensitive.setUpdateBy(SecurityUtils.getUsername());
weSensitive.setUpdateTime(DateUtils.getNowDate());
int updateResult = weSensitiveMapper.updateWeSensitive(weSensitive);
if (updateResult > 0 && weSensitive.getAuditUserScope() != null) {
//删除原有关联信息
sensitiveAuditScopeService.deleteAuditScopeBySensitiveId(weSensitive.getId());
if (CollectionUtils.isNotEmpty(weSensitive.getAuditUserScope())) {
weSensitive.getAuditUserScope().forEach(scope -> {
scope.setSensitiveId(weSensitive.getId());
});
sensitiveAuditScopeService.insertWeSensitiveAuditScopeList(weSensitive.getAuditUserScope());
}
}
return updateResult;
}
/**
* 批量删除敏感词设置
*
* @param ids 需要删除的敏感词设置ID
* @return 结果
*/
@Override
public int deleteWeSensitiveByIds(Long[] ids) {
List<WeSensitive> sensitiveList = weSensitiveMapper.selectWeSensitiveByIds(ids);
sensitiveList.forEach(sensitive -> {
sensitive.setDelFlag(1);
sensitive.setUpdateBy(SecurityUtils.getUsername());
sensitive.setUpdateTime(DateUtils.getNowDate());
});
return weSensitiveMapper.batchUpdateWeSensitive(sensitiveList);
}
/**
* 批量删除敏感词配置数据
*
* @param ids 敏感词设置ID
* @return 结果
*/
@Override
@Transactional
public int destroyWeSensitiveByIds(Long[] ids) {
int deleteResult = weSensitiveMapper.deleteWeSensitiveByIds(ids);
if (deleteResult > 0) {
//删除关联数据
sensitiveAuditScopeService.deleteAuditScopeBySensitiveIds(ids);
}
return deleteResult;
}
@Override
public void hitSensitive(List<JSONObject> entityList) {
log.info("执行敏感词命中过滤,time=[{}]", System.currentTimeMillis());
//获取所有的敏感词规则
List<WeSensitive> allSensitiveRules = weSensitiveMapper.selectWeSensitiveList(new WeSensitive());
//根据规则过滤命中
if (CollectionUtils.isNotEmpty(allSensitiveRules)) {
allSensitiveRules.parallelStream().forEach(weSensitive -> {
List<JSONObject> jsonList = Lists.newArrayList();
List<String> patternWords = Arrays.asList(weSensitive.getPatternWords().split(","));
List<String> users = getScopeUsers(weSensitive.getAuditUserScope());
patternWords.forEach(patternWord -> {
jsonList.addAll(hitSensitiveInES(patternWord, users));
});
//将命中结果插入es
addHitSensitiveList(jsonList, weSensitive);
});
}
}
@Override
public PageInfo<JSONObject> getHitSensitiveList(WeSensitiveHitQuery weSensitiveHitQuery) {
elasticSearch.createIndex2(WeConstans.WECOM_SENSITIVE_HIT_INDEX, getSensitiveHitMapping());
List<String> userIds = Lists.newArrayList();
if (weSensitiveHitQuery.getScopeType().equals(WeConstans.USE_SCOP_BUSINESSID_TYPE_USER)) {
userIds.add(weSensitiveHitQuery.getAuditScopeId());
} else {
List<String> userIdList = weUserService.selectWeUserList(WeUser.builder().department(new String[]{weSensitiveHitQuery.getAuditScopeId()}).build())
.stream().filter(Objects::nonNull).map(WeUser::getUserId).collect(Collectors.toList());
userIds.addAll(userIdList);
}
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum() == null ? 1 : pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize() == null ? 10 : pageDomain.getPageSize();
SearchSourceBuilder builder = new SearchSourceBuilder();
int from = (pageNum - 1) * pageSize;
builder.size(pageSize);
builder.from(from);
builder.sort("msgtime", SortOrder.DESC);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
BoolQueryBuilder userBuilder = QueryBuilders.boolQuery();
userIds.forEach(user -> {
userBuilder.should(QueryBuilders.termQuery("from.keyword", user));
});
userBuilder.minimumShouldMatch(1);
boolQueryBuilder.must(userBuilder);
if (StringUtils.isNotBlank(weSensitiveHitQuery.getKeyword())) {
BoolQueryBuilder keywordBuilder = QueryBuilders.boolQuery().should(QueryBuilders.matchPhraseQuery("text.content", weSensitiveHitQuery.getKeyword()));
boolQueryBuilder.must(keywordBuilder);
}
builder.query(boolQueryBuilder);
PageInfo<JSONObject> pageInfo = elasticSearch.searchPage(WeConstans.WECOM_SENSITIVE_HIT_INDEX, builder, pageNum, pageSize, JSONObject.class);
return hitPageInfoHandler(pageInfo);
}
private PageInfo<JSONObject> hitPageInfoHandler(PageInfo<JSONObject> pageInfo) {
List<JSONObject> jsonList = pageInfo.getList();
if (CollectionUtils.isNotEmpty(jsonList)) {
List<JSONObject> newList = jsonList.stream().map(j -> {
JSONObject json = new JSONObject();
String userId = j.getString("from");
WeUser user = new WeUser();
user.setUserId(userId);
List<WeUser> uList = weUserService.selectWeUserList(user);
if (CollectionUtils.isNotEmpty(uList)) {
json.put("from", uList.get(0).getName());
json.put("content", j.getJSONObject("text").getString("content"));
json.put("msgtime", j.getString("msgtime"));
json.put("status", j.getString("status"));
json.put("patternWords", j.getString("pattern_words"));
}
return json;
}).collect(Collectors.toList());
pageInfo.setList(newList);
}
return pageInfo;
}
private void addHitSensitiveList(List<JSONObject> json, WeSensitive weSensitive) {
elasticSearch.createIndex2(WeConstans.WECOM_SENSITIVE_HIT_INDEX, getSensitiveHitMapping());
//批量提交插入记录
if (CollectionUtils.isNotEmpty(json)) {
List<ElasticSearchEntity> list = json.stream().filter(Objects::nonNull).map(j -> {
ElasticSearchEntity ese = new ElasticSearchEntity();
j.put("status", "0");
ese.setData(j);
ese.setId(j.getString("msgid"));
return ese;
}).collect(Collectors.toList());
elasticSearch.insertBatchAsync(WeConstans.WECOM_SENSITIVE_HIT_INDEX, list, this::sendMessage, weSensitive);
}
}
private void sendMessage(Object listObj, Object weSensitiveObj) {
WeSensitive weSensitive = (WeSensitive) weSensitiveObj;
List<ElasticSearchEntity> list = (List<ElasticSearchEntity>) listObj;
if (weSensitive.getAlertFlag().equals(1) && CollectionUtils.isNotEmpty(list)) {
//发送消息通知给相应的审计人
WeCorpAccount weCorpAccount = weCorpAccountService.findValidWeCorpAccount();
String auditUserId = weSensitive.getAuditUserId();
String content = "有消息触发敏感词,请登录系统及时处理!";
TextMessageDto textMessageDto = new TextMessageDto();
textMessageDto.setContent(content);
WeMessagePushDto pushDto = new WeMessagePushDto();
pushDto.setTouser(auditUserId);
pushDto.setMsgtype(MessageType.TEXT.getMessageType());
pushDto.setText(textMessageDto);
weMessagePushClient.sendMessageToUser(pushDto, weCorpAccount.getAgentId());
//批量更新
list = list.stream().peek(entity -> {
Map map = entity.getData();
map.put("status", "1");
entity.setData(map);
}).collect(Collectors.toList());
elasticSearch.updateBatch(WeConstans.WECOM_SENSITIVE_HIT_INDEX, list);
}
}
private List<String> getScopeUsers(List<WeSensitiveAuditScope> scopeList) {
List<String> users = Lists.newArrayList();
scopeList.forEach(scope -> {
if (scope.getScopeType().equals(WeConstans.USE_SCOP_BUSINESSID_TYPE_USER)) {
users.add(scope.getAuditScopeId());
} else {
List<String> userIdList = weUserService.selectWeUserList(WeUser.builder().department(new String[]{scope.getAuditScopeId()}).build())
.stream().filter(Objects::nonNull).map(WeUser::getUserId).collect(Collectors.toList());
users.addAll(userIdList);
}
});
return users;
}
private List<JSONObject> hitSensitiveInES(String patternWord, List<String> users) {
int pieceSize = userPiecesCount(users);
if (pieceSize != -1) {
List<JSONObject> resultList = Lists.newArrayList();
for (int i = 0; i < pieceSize; i++) {
List<String> subUsers = users.subList(i + i * pieceSize, i + pieceSize - 1);
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.sort("msgtime", SortOrder.DESC);
BoolQueryBuilder userBuilder = QueryBuilders.boolQuery();
subUsers.parallelStream().forEach(user -> userBuilder.should(QueryBuilders.termQuery("from", user)));
userBuilder.minimumShouldMatch(1);
BoolQueryBuilder searchBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery("text.content", patternWord)).must(userBuilder);
builder.query(searchBuilder);
List<JSONObject> list = elasticSearch.search(chartKey, builder, JSONObject.class);
list.parallelStream().forEach(j -> j.put("pattern_words", patternWord));
resultList.addAll(list);
}
return resultList;
}
return Lists.newArrayList();
}
private int userPiecesCount(List<String> users) {
if (CollectionUtils.isNotEmpty(users)) {
if (users.size() % WeConstans.SENSITIVE_USER_PIECE != 0) {
return users.size() / WeConstans.SENSITIVE_USER_PIECE + 1;
} else {
return users.size() / WeConstans.SENSITIVE_USER_PIECE;
}
}
return -1;
}
private XContentBuilder getSensitiveHitMapping() {
try {
//创建索引
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject("msgid")
.field("type", "keyword")
.endObject()
.startObject("seq")
.field("type", "long")
.endObject()
.startObject("action")
.field("type", "keyword")
.endObject()
.startObject("from")
.field("type", "keyword")
.endObject()
.startObject("roomid")
.field("type", "keyword")
.endObject()
.startObject("msgtime")
.field("type", "long")
.endObject()
.startObject("msgtype")
.field("type", "keyword")
.endObject()
.startObject("status")
.field("type", "keyword")
.endObject()
.startObject("pattern_words")
.field("type", "keyword")
.endObject()
.startObject("text")
.startObject("content")
.field("type", "text")
.endObject()
.endObject()
.endObject()
.endObject();
return mapping;
} catch (Exception e) {
log.warn("create sensitive-hit mapping failed, exception={}", ExceptionUtils.getStackTrace(e));
}
return null;
}
}