|
|
@@ -0,0 +1,271 @@ |
|
|
|
package com.xueyi.nlt.nlt.template; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.cloud.nacos.NacosConfigManager; |
|
|
|
import com.alibaba.fastjson2.JSONArray; |
|
|
|
import com.alibaba.fastjson2.JSONException; |
|
|
|
import com.alibaba.fastjson2.JSONObject; |
|
|
|
import com.alibaba.nacos.api.exception.NacosException; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
|
|
|
import com.xueyi.common.core.constant.digitalman.SkillConstants; |
|
|
|
import com.xueyi.common.core.web.result.AjaxResult; |
|
|
|
import com.xueyi.nlt.api.nlt.domain.vo.CoversationSessionVo; |
|
|
|
import com.xueyi.nlt.nlt.domain.vo.MeetingParamVo; |
|
|
|
import com.xueyi.system.api.digitalmans.domain.dto.DmVisitorsDto; |
|
|
|
import com.xueyi.system.api.staff.feign.RemoteVisitorsController; |
|
|
|
import net.sourceforge.pinyin4j.PinyinHelper; |
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; |
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; |
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; |
|
|
|
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.ConversionNotSupportedException; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
@Service("visitor-order") |
|
|
|
public class VisitorTemplate implements BaseTemplate{ |
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(MeetingOrderTemplate.class); |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RemoteVisitorsController remoteVisitorsController; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
NacosConfigManager nacosConfigManager; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RedisTemplate<Object,Object> objectRedisTemplate; |
|
|
|
|
|
|
|
private static final List<MeetingParamVo> DATE_PARAMS = new ArrayList<>(); |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
private void init(){ |
|
|
|
try { |
|
|
|
String meetingParams = nacosConfigManager.getConfigService().getConfig("meeting_date_and_hour", "DEFAULT_GROUP", 5000); |
|
|
|
if (StringUtils.isNotBlank(meetingParams)) { |
|
|
|
try { |
|
|
|
DATE_PARAMS.addAll(JSONArray.parseArray(meetingParams, MeetingParamVo.class)); |
|
|
|
DATE_PARAMS.forEach(item -> { |
|
|
|
// 将item.getContent()按照空格分割成数组,然后将数组转换成流,最后将流转化成整数数组赋值给item.getCompareArray() |
|
|
|
item.setCompareArray(Arrays.stream(item.getContent().split(" ")).mapToInt(Integer::parseInt).toArray()); |
|
|
|
// item.setCompareArray(Arrays.stream(item.getContent().split(" "))); |
|
|
|
String[] split = item.getContent().split(" "); |
|
|
|
if (split != null && split.length > 0) { |
|
|
|
int i = Integer.parseInt(split[0]); |
|
|
|
if (i > -1) { |
|
|
|
item.setMinute(i); |
|
|
|
} else { |
|
|
|
item.setMinute(-1); |
|
|
|
} |
|
|
|
i = Integer.parseInt(split[1]); |
|
|
|
if (i > -1) { |
|
|
|
item.setHour(i); |
|
|
|
} else { |
|
|
|
item.setHour(-1); |
|
|
|
} |
|
|
|
i = Integer.parseInt(split[2]); |
|
|
|
if (i > -1) { |
|
|
|
item.setDay(i); |
|
|
|
} else { |
|
|
|
item.setDay(-1); |
|
|
|
} |
|
|
|
i = Integer.parseInt(split[3]); |
|
|
|
if (i > -1) { |
|
|
|
item.setMonth(i); |
|
|
|
} else { |
|
|
|
item.setMonth(-1); |
|
|
|
} |
|
|
|
i = Integer.parseInt(split[4]); |
|
|
|
if (i > -1) { |
|
|
|
item.setDayOfWeek(i); |
|
|
|
} else { |
|
|
|
item.setDayOfWeek(-1); |
|
|
|
} |
|
|
|
i = Integer.parseInt(split[5]); |
|
|
|
if (i > -1) { |
|
|
|
item.setOffset(i); |
|
|
|
} else { |
|
|
|
item.setOffset(-1); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} catch (JSONException e) { |
|
|
|
log.error("解析日期参数失败", e); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (JSONException | NacosException e) { |
|
|
|
log.error("解析日期参数失败", e); |
|
|
|
} |
|
|
|
} |
|
|
|
@Override |
|
|
|
public JSONObject handle(String dev, String content) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public JSONObject handle(String devId, String content, Long tenantId) { |
|
|
|
CoversationSessionVo session = (CoversationSessionVo) objectRedisTemplate.opsForValue().get("group:device" + ":" + devId + ":" +"session"); |
|
|
|
if (session == null) { |
|
|
|
session = new CoversationSessionVo(); |
|
|
|
session.setCategory("visitorAppoint"); |
|
|
|
session.setFormat(new JSONObject()); |
|
|
|
session.getFormat().put("skillCode", SkillConstants.SkillType.CREATE_VISITOR_INFO.getCode()); |
|
|
|
} |
|
|
|
|
|
|
|
CoversationSessionVo dateSession = processRegexDatetime(content,session); |
|
|
|
if (dateSession != null ){ |
|
|
|
session = dateSession; |
|
|
|
} |
|
|
|
|
|
|
|
CoversationSessionVo nameSession = processRegexName(content, session, tenantId); |
|
|
|
if (nameSession != null){ |
|
|
|
session = nameSession; |
|
|
|
} |
|
|
|
|
|
|
|
if(!content.startsWith("##")){ |
|
|
|
String regex = "1\\d{10}"; |
|
|
|
Pattern pattern = Pattern.compile(regex); |
|
|
|
Matcher matcher = pattern.matcher(content); |
|
|
|
if(matcher.find()){ |
|
|
|
session.getFormat().put("phoneNumber", matcher.group()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
objectRedisTemplate.opsForValue().set("group:device" + ":" + devId + ":" +"session", session, 1, TimeUnit.MINUTES); |
|
|
|
session.getFormat().put("skillCode", SkillConstants.SkillType.CREATE_VISITOR_INFO.getCode()); |
|
|
|
session.getFormat().put("action", "VisitorAppoint"); |
|
|
|
return session.getFormat(); |
|
|
|
} |
|
|
|
|
|
|
|
public CoversationSessionVo processRegexDatetime(String content, CoversationSessionVo session) { |
|
|
|
List<Integer[]> timeList = new ArrayList<>(); |
|
|
|
DATE_PARAMS.forEach(item-> { |
|
|
|
if (content.matches(item.getName())) { |
|
|
|
int[] tmp = item.getCompareArray(); |
|
|
|
// 将int[]转化成Integer[] |
|
|
|
Integer[] tmpInteger = new Integer[tmp.length]; |
|
|
|
for (int i = 0; i < tmp.length; i++) { |
|
|
|
tmpInteger[i] = tmp[i]; |
|
|
|
} |
|
|
|
timeList.add(tmpInteger); |
|
|
|
} |
|
|
|
}); |
|
|
|
if (timeList.size() == 0) { |
|
|
|
//没有匹配正则,返回 |
|
|
|
return session; |
|
|
|
|
|
|
|
} |
|
|
|
Integer results[] = new Integer[6]; |
|
|
|
// 计算二维矩阵每列最大值 |
|
|
|
for (Integer[] item : timeList) { |
|
|
|
for (int i = 0; i < item.length; i++) { |
|
|
|
if (results[i] == null) { |
|
|
|
results[i] = item[i]; |
|
|
|
} else { |
|
|
|
if (results[i] < item[i]) { |
|
|
|
results[i] = item[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (results[2] > 0) { |
|
|
|
DateTimeFormatter resultDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
|
|
|
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
|
|
if (!session.getFormat().containsKey("date")) { |
|
|
|
session.getFormat().put("date",resultDateFormatter.format(LocalDateTime.now())); |
|
|
|
} |
|
|
|
LocalDateTime date = LocalDateTime.parse(session.getFormat().getString("date") + " 00:00:00",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
|
|
|
date = date.withDayOfMonth(results[2]); |
|
|
|
if (results[3] > 0) { |
|
|
|
date = date.withMonth(results[3]); |
|
|
|
} |
|
|
|
session.getFormat().put("date",resultDateFormatter.format(date)); |
|
|
|
} |
|
|
|
if (results[4] > 0) { |
|
|
|
LocalDateTime date = LocalDateTime.now(); |
|
|
|
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
|
|
|
int offset = results[4] - date.getDayOfWeek().getValue(); |
|
|
|
date = date.plusDays(offset); |
|
|
|
session.getFormat().put("date",dateFormatter.format(date)); |
|
|
|
} |
|
|
|
if (results[5] >= 0) { |
|
|
|
LocalDateTime date = LocalDateTime.now(); |
|
|
|
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
|
|
|
date = date.plusDays(results[5]); |
|
|
|
session.getFormat().put("date",dateFormatter.format(date)); |
|
|
|
} |
|
|
|
return session; |
|
|
|
} |
|
|
|
|
|
|
|
public CoversationSessionVo processRegexName(String content, CoversationSessionVo session, Long tenantId){ |
|
|
|
if(!StringUtils.isEmpty(content)){ |
|
|
|
if(content.startsWith("##")){ |
|
|
|
content = content.replace("##", ""); |
|
|
|
JSONArray result = remoteVisitorsController.selectByTenantApi(tenantId).toJson().getJSONArray("data"); |
|
|
|
for(int i = 0; i < result.size(); i++){ |
|
|
|
JSONObject temp = result.getJSONObject(i); |
|
|
|
if(content.equals(temp.getString("id"))){ |
|
|
|
session.getFormat().put("confirmedName", temp); |
|
|
|
return session; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(content.startsWith("@@")){ |
|
|
|
JSONObject newVisitor = new JSONObject(); |
|
|
|
newVisitor.put("name", content.replace("@@", "")); |
|
|
|
session.getFormat().put("confirmedName", newVisitor); |
|
|
|
return session; |
|
|
|
} |
|
|
|
} |
|
|
|
JSONArray result = remoteVisitorsController.selectByTenantApi(tenantId).toJson().getJSONArray("data"); |
|
|
|
JSONArray newResult = new JSONArray(); |
|
|
|
for(int i = 0; i < result.size(); i++){ |
|
|
|
JSONObject temp = result.getJSONObject(i); |
|
|
|
if(match(content, temp.getString("name"))){ |
|
|
|
newResult.add(temp); |
|
|
|
} |
|
|
|
} |
|
|
|
session.getFormat().put("nameList", newResult); |
|
|
|
return session; |
|
|
|
} |
|
|
|
|
|
|
|
public boolean match(String content, String regex){ |
|
|
|
String contentPinyin = toPinyin(content); |
|
|
|
String regexPinyin = toPinyin(regex); |
|
|
|
return contentPinyin.contains(regexPinyin); |
|
|
|
} |
|
|
|
|
|
|
|
public static String toPinyin(String chinese){ |
|
|
|
String pinyinStr = ""; |
|
|
|
char[] newChar = chinese.toCharArray(); |
|
|
|
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); |
|
|
|
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); |
|
|
|
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); |
|
|
|
for (int i = 0; i < newChar.length; i++) { |
|
|
|
if (newChar[i] > 19967 && newChar[i] < 40959) { |
|
|
|
try { |
|
|
|
pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0]; |
|
|
|
} catch (BadHanyuPinyinOutputFormatCombination e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
pinyinStr += newChar[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
return pinyinStr; |
|
|
|
} |
|
|
|
} |