AutoreplyService.java
9.7 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
/******************************************************************
*
* Java Lib For JavaWeb, Powered By Shenzhen WEI YI KE JI .
*
* Copyright (c) 2017-2027 Digital Telemedia Co.,Ltd
* http://www.d-telemedia.com/
*
* Package: com.w1hd.zzhnc.service.impl
*
* Filename: ProjectServiceImpl.java
*
* Description: TODO(用一句话描述该文件做什么)
*
* Copyright: Copyright (c) 2017-2027
*
* Company: Theyeasy Telemedia Co.,Ltd
*
* @author: hm
*
* @version: 1.0.0
*
* Create at: 2017年10月22日 下午5:53:36
*
* Revision:
*
* 2017年10月22日 下午5:53:36
* - first revision
*
*****************************************************************/
package com.w1hd.zzhnc.service;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.RandomUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.testng.util.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.w1hd.zzhnc.dao.AutoreplyDao;
import com.w1hd.zzhnc.model.Autoreply;
import com.w1hd.zzhnc.util.CommonUtil;
import com.w1hd.zzhnc.util.KeywordUtil;
import com.w1hd.zzhnc.util.PageResults;
import com.w1hd.zzhnc.util.StringUtil;
import com.w1hd.zzhnc.util.TulingUtil;
import com.w1hd.zzhnc.util.keyword.RegexKeywordFilter;
import com.w1hd.zzhnc.util.keyword.ReplaceStrategy;
import com.w1hd.zzhnc.vo.Vo_msg;
import tk.mybatis.mapper.entity.Example;
/**
* @ClassName ProjectServiceImpl
* @Description TODO(这里用一句话描述这个类的作用)
* @author hm
* @Date 2017年10月22日 下午5:53:36
* @version 1.0.0
*/
@Service
public class AutoreplyService {
Logger log = LoggerFactory.getLogger(AutoreplyService.class);
@Autowired
AutoreplyDao autoreplyDao;
List<RegexKeywordFilter> filterList;
static boolean inited = false;
public void init(boolean hard) {
if (hard) {
inited = false;
}
init();
}
public void init() {
if (inited) {
return;
}
filterList = Lists.newArrayList();
System.out.println("强制重新获得关键词");
Example example = new Example(Autoreply.class);
example.createCriteria().andEqualTo("deleted", 0);
List<Autoreply> autoreplyList = autoreplyDao.selectByExample(example);
autoreplyList.stream().forEach(e -> {
String[] keys = e.getKeywords().split("\\|");
RegexKeywordFilter filter = new RegexKeywordFilter();
for (String key : keys) {
System.out.println("keys" + key);
filter.add(key);
}
filter.compile();
filterList.add(filter);
});
inited = true;
}
@Autowired
RedisTemplate<String, String> redisTemplate;
public Autoreply autoreply(String string) {
System.out.println("关键字查找 ————>" + string);
List<Set> sets = Lists.newArrayList();
for (RegexKeywordFilter filter : filterList) {
Set<String> keys = Sets.newHashSet();
filter.replace(string, new ReplaceStrategy() {
public String replaceWith(String keyword) {
keys.add(keyword);
sets.add(keys);
return "";
}
});
}
Collections.sort(sets, new Comparator<Set>() {
public int compare(Set o1, Set o2) {
if (o1.size() > o2.size()) {
return -1;
}
if (o1.size() == o2.size()) {
return 0;
}
return 1;
}
});
boolean eques = false;
log.info("sets:{}", sets);
if (sets.isEmpty()) {
return null;
}
for (Set s : sets) {
if (s.contains(string)) {
eques = true;
}
}
List<String> list = Lists.newArrayList();
Set<String> set = sets.get(0);
if (!eques) {
list.addAll(sets.get(0));
} else {
list.add(string);
}
List<Autoreply> autoreplys = autoreplyDao.findByKeys(list, null);
if (autoreplys.isEmpty()) {
return null;
}
if (autoreplys.size() == 1) {
return autoreplys.get(0);
}
Collections.sort(autoreplys, new Comparator<Autoreply>() {
public int compare(Autoreply o1, Autoreply o2) {
if (o1.getSort() < o2.getSort()) {
return -1;
}
if (o1.getSort() == o2.getSort()) {
return RandomUtils.nextInt(0, 2) - 1;
}
return 1;
}
});
return autoreplys.get(0);
}
public PageResults<Autoreply> getAutoReplyList(Integer page, String keyword, Boolean isRedirectStaff,
Integer projectId) {
Example example = new Example(Autoreply.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("deleted", false);
if (!Strings.isNullOrEmpty(keyword)) {
criteria.andCondition(" (contents like \"%" + keyword + "%\" or keyWords like \"%" + keyword + "%\")");
}
if (projectId != null && projectId >= 0) {
criteria.andEqualTo("projectId", projectId);
}
if (isRedirectStaff != null) {
criteria.andEqualTo("isredirectstaff", isRedirectStaff);
}
RowBounds row = new RowBounds((page - 1) * 10, 10);
example.setOrderByClause(" id desc ");
List<Autoreply> list = autoreplyDao.selectByExampleAndRowBounds(example, row);
int total = autoreplyDao.selectCountByExample(example);
PageResults<Autoreply> pageresult = new PageResults<>();
pageresult.setPage(page);
pageresult.setPageSize(10);
pageresult.setRows(list);
pageresult.setTotal(total);
return pageresult;
}
public String addAutoReply(String keywords, boolean isRedirectStaff, String content, Integer projectId,
String projectName, Integer sort) {
if (Strings.isNullOrEmpty(keywords))
return "关键字设置不能为空";
int row = 0;
try {
Autoreply reply = new Autoreply();
reply.setContents(content);
reply.setKeywords(keywords);
reply.setDeleted(false);
reply.setIsredirectstaff(isRedirectStaff);
reply.setUpdatedtime(CommonUtil.getTime());
reply.setProjectId(projectId);
reply.setSort(sort);
reply.setProjectName(projectName);
row = autoreplyDao.insertSelective(reply);
} catch (Exception e) {
e.printStackTrace();
System.out.println("error:" + e.getMessage());
}
return row > 0 ? "ok" : "添加失败,数据异常";
}
public String updateAutoReply(Integer id, String keywords, boolean isRedirectStaff, String content,
Integer projectId, String projectName, Integer sort) {
if (StringUtil.isZearoOrNull(id))
return "id 不能为空";
if (Strings.isNullOrEmpty(keywords))
return "关键字设置不能为空";
Autoreply reply = autoreplyDao.selectByPrimaryKey(id);
if (reply == null)
return "修改失败,数据为空";
reply.setContents(content);
reply.setKeywords(keywords);
reply.setIsredirectstaff(isRedirectStaff);
reply.setUpdatedtime(CommonUtil.getTime());
reply.setProjectId(projectId);
reply.setProjectName(projectName);
reply.setSort(sort);
int row = autoreplyDao.updateByPrimaryKeySelective(reply);
return row > 0 ? "ok" : "修改失败,数据异常";
}
public String deleteAutoReply(Integer id) {
if (StringUtil.isZearoOrNull(id))
return "id 不能为空";
Autoreply reply = autoreplyDao.selectByPrimaryKey(id);
if (reply == null)
return "删除失败,数据为空";
reply.setDeleted(true);
int row = autoreplyDao.updateByPrimaryKeySelective(reply);
return row > 0 ? "ok" : "删除失败,数据异常";
}
public Autoreply getAutoReply(Integer id) {
if (StringUtil.isZearoOrNull(id))
return null;
return autoreplyDao.selectByPrimaryKey(id);
}
public String KeywordsToString() {
Example example = new Example(Autoreply.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("deleted", false);
List<Autoreply> list = autoreplyDao.selectByExample(example);
// String
// regex="(?<no1>[23|2莞|2]+)|(?<no2>[买房|东莞|万科]+)|(?<no3>[a|b|cc]+)|(?<no4>[客服|房价|咨询]+)";
String result = "";
StringBuffer sb = new StringBuffer();
if (list != null && list.size() > 0) {
// List<String> keywordsStr=list.stream().map(e -> new String(
// (e.getKeywords() == null ? "" :
// e.getKeywords()+"|"))).collect(Collectors.toList());
for (Autoreply auto : list) {
String group = "(?<no_" + auto.getId() + ">[" + auto.getKeywords() + "])" + ",";
sb.append(group);
}
result = sb.toString().replaceAll(",|,", "|");
if (result.endsWith("|")) {
result = result.substring(0, result.length() - 1);
}
System.out.println(result);
}
KeywordUtil.setKeywordStr(result);
return result;
}
public HashMap<Object, Object> autoreply(String content, Integer projectId, String fromUserName) {
Autoreply autoreply = null;
if (autoreply == null) {
autoreply = autoreply(content);
}
HashMap<Object, Object> result = Maps.newHashMap();
result.put("type", 2);
if (autoreply == null) {
Vo_msg answer = TulingUtil.getAnswer(content, fromUserName);
autoreply = new Autoreply();
if (answer != null && answer.code == 0) {
String contents = answer.msg;
autoreply.setContents(contents);
} else {
autoreply.setContents(getLastReply());
;
}
autoreply.setIsredirectstaff(false);
result.put("type", 3);
}
result.put("autoreply", autoreply);
return result;
}
public static String getLastReply() {
String allMsg = "你难住我了。。。/难过/大哭|世界那么大,我要出去走走/奋斗/奋斗/奋斗|....|今天天气真好呀!/龇牙/龇牙|让我想想.../阴险|我要提高下智商了/拳头/拳头/拳头|/抠鼻|/睡着|/睡着/睡着|skdfiqwkmqwemlqwdkl 哎呀,碰到键盘了。。。";
String[] randomMsg = allMsg.split("\\|");
int index = RandomUtils.nextInt(0, randomMsg.length - 1);
return randomMsg[index];
}
}