Commit 68c95a83 by danmo

会话存档扫描修改

1 parent 41765e42
...@@ -99,7 +99,7 @@ public class ElasticSearch { ...@@ -99,7 +99,7 @@ public class ElasticSearch {
// request.settings() 手工指定Setting // request.settings() 手工指定Setting
CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
if (!res.isAcknowledged()) { if (!res.isAcknowledged()) {
throw new RuntimeException("初始化失败"); log.info("初始化失败");
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -169,7 +169,7 @@ public class ElasticSearch { ...@@ -169,7 +169,7 @@ public class ElasticSearch {
*/ */
public void insertOrUpdateOne(String idxName, ElasticSearchEntity entity) { public void insertOrUpdateOne(String idxName, ElasticSearchEntity entity) {
IndexRequest request = new IndexRequest(idxName, "_doc"); IndexRequest request = new IndexRequest(idxName, "_doc");
log.error("Data : id={},entity={}", entity.getId(), JSON.toJSONString(entity.getData())); log.info("Data : id={},entity={}", entity.getId(), JSON.toJSONString(entity.getData()));
request.id(entity.getId()); request.id(entity.getId());
request.source(entity.getData(), XContentType.JSON); request.source(entity.getData(), XContentType.JSON);
// request.source(JSON.toJSONString(entity.getData()), XContentType.JSON); // request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
......
...@@ -4,7 +4,8 @@ import cn.hutool.core.collection.CollectionUtil; ...@@ -4,7 +4,8 @@ import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.linkwechat.common.config.RuoYiConfig; import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity; import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.DateUtils; import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.StringUtils; import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.uuid.IdUtils; import com.linkwechat.common.utils.uuid.IdUtils;
...@@ -17,6 +18,7 @@ import java.security.PrivateKey; ...@@ -17,6 +18,7 @@ import java.security.PrivateKey;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
/** /**
* @author sxw * @author sxw
...@@ -62,8 +64,8 @@ public class FinanceUtils { ...@@ -62,8 +64,8 @@ public class FinanceUtils {
* @param proxy 代理 * @param proxy 代理
* @param passwd 密码 * @param passwd 密码
*/ */
public static List<ElasticSearchEntity> getChatData(long seq, String proxy, String passwd) { public static List<JSONObject> getChatData(long seq, String proxy, String passwd, RedisCache redisCache) {
List<ElasticSearchEntity> resList = new ArrayList<>(); List<JSONObject> resList = new ArrayList<>();
long slice = Finance.NewSlice(); long slice = Finance.NewSlice();
int ret = Finance.GetChatData(sdk, seq, LIMIT, proxy, passwd, timeout, slice); int ret = Finance.GetChatData(sdk, seq, LIMIT, proxy, passwd, timeout, slice);
if (ret != 0) { if (ret != 0) {
...@@ -73,20 +75,18 @@ public class FinanceUtils { ...@@ -73,20 +75,18 @@ public class FinanceUtils {
String content = Finance.GetContentFromSlice(slice); String content = Finance.GetContentFromSlice(slice);
JSONArray chatdataArr = JSONObject.parseObject(content).getJSONArray("chatdata"); JSONArray chatdataArr = JSONObject.parseObject(content).getJSONArray("chatdata");
log.info("开始执行数据解析:------------"); log.info("开始执行数据解析:------------");
AtomicLong LocalSEQ = new AtomicLong();
if (CollectionUtil.isNotEmpty(chatdataArr)) { if (CollectionUtil.isNotEmpty(chatdataArr)) {
chatdataArr.stream().map(data -> (JSONObject) data).forEach(data -> { chatdataArr.stream().map(data -> (JSONObject) data).forEach(data -> {
try { try {
ElasticSearchEntity elasticSearchEntity = new ElasticSearchEntity(); LocalSEQ.set(data.getLong("seq"));
long LocalSEQ = data.getLong("seq");
JSONObject jsonObject = decryptChatRecord(sdk, data.getString("encrypt_random_key"), JSONObject jsonObject = decryptChatRecord(sdk, data.getString("encrypt_random_key"),
data.getString("encrypt_chat_msg"), privateKey); data.getString("encrypt_chat_msg"), privateKey);
if (jsonObject ==null){ if (jsonObject ==null){
return; return;
} }
jsonObject.put("seq", LocalSEQ); jsonObject.put("seq", LocalSEQ.get());
elasticSearchEntity.setData(jsonObject); resList.add(jsonObject);
elasticSearchEntity.setId(jsonObject.getString("msgid"));
resList.add(elasticSearchEntity);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -94,6 +94,7 @@ public class FinanceUtils { ...@@ -94,6 +94,7 @@ public class FinanceUtils {
log.info("数据解析完成:------------"); log.info("数据解析完成:------------");
} }
Finance.FreeSlice(slice); Finance.FreeSlice(slice);
redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY,LocalSEQ.get());
return resList; return resList;
} }
......
...@@ -67,18 +67,16 @@ public class RyTask { ...@@ -67,18 +67,16 @@ public class RyTask {
log.info(">>>>>>>seq:{}",index.get()); log.info(">>>>>>>seq:{}",index.get());
FinanceUtils.initSDK(corpId, secret); FinanceUtils.initSDK(corpId, secret);
List<ElasticSearchEntity> chatDataList = FinanceUtils.getChatData(index.get(), List<JSONObject> chatDataList = FinanceUtils.getChatData(index.get(),
"", "",
""); "", redisCache);
if (CollectionUtil.isNotEmpty(chatDataList)){ if (CollectionUtil.isNotEmpty(chatDataList)){
try { try {
elasticSearch.insertBatch(WeConstans.WECOM_FINANCE_INDEX, chatDataList); List<ElasticSearchEntity> elasticSearchEntities = weChatContactMappingService.saveWeChatContactMapping(chatDataList);
weChatContactMappingService.saveWeChatContactMapping(chatDataList); elasticSearch.insertBatch(WeConstans.WECOM_FINANCE_INDEX, elasticSearchEntities);
} catch (Exception e) { } catch (Exception e) {
log.error("消息处理异常:ex:{}", e); log.error("消息处理异常:ex:{}", e);
e.printStackTrace(); e.printStackTrace();
}finally {
setRedisCacheSeqValue(index);
} }
} }
} }
...@@ -90,7 +88,7 @@ public class RyTask { ...@@ -90,7 +88,7 @@ public class RyTask {
searchSourceBuilder.size(1); searchSourceBuilder.size(1);
List<JSONObject> searchResultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, searchSourceBuilder, JSONObject.class); List<JSONObject> searchResultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, searchSourceBuilder, JSONObject.class);
searchResultList.stream().findFirst().ifPresent(result ->{ searchResultList.stream().findFirst().ifPresent(result ->{
index.set(result.getLong(WeConstans.CONTACT_SEQ_KEY) + 1); index.set(result.getLong(WeConstans.CONTACT_SEQ_KEY));
}); });
redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY,index); redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY,index);
} }
......
package com.linkwechat.wecom.service; package com.linkwechat.wecom.service;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity; import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
...@@ -69,7 +70,7 @@ public interface IWeChatContactMappingService extends IService<WeChatContactMapp ...@@ -69,7 +70,7 @@ public interface IWeChatContactMappingService extends IService<WeChatContactMapp
* *
* @param query * @param query
*/ */
public void saveWeChatContactMapping(List<ElasticSearchEntity> query); public List<ElasticSearchEntity> saveWeChatContactMapping(List<JSONObject> query);
/** /**
* 按客户查询关系映射列表 * 按客户查询关系映射列表
......
...@@ -2,6 +2,7 @@ package com.linkwechat.wecom.service.impl; ...@@ -2,6 +2,7 @@ package com.linkwechat.wecom.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
...@@ -18,6 +19,7 @@ import com.linkwechat.wecom.client.WeUserClient; ...@@ -18,6 +19,7 @@ import com.linkwechat.wecom.client.WeUserClient;
import com.linkwechat.wecom.domain.WeChatContactMapping; import com.linkwechat.wecom.domain.WeChatContactMapping;
import com.linkwechat.wecom.domain.WeCustomer; import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeUser; import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.dto.WeUserDto;
import com.linkwechat.wecom.domain.dto.customer.CustomerGroupDetail; import com.linkwechat.wecom.domain.dto.customer.CustomerGroupDetail;
import com.linkwechat.wecom.domain.dto.msgaudit.GroupChatVo; import com.linkwechat.wecom.domain.dto.msgaudit.GroupChatVo;
import com.linkwechat.wecom.mapper.WeChatContactMappingMapper; import com.linkwechat.wecom.mapper.WeChatContactMappingMapper;
...@@ -160,33 +162,62 @@ public class WeChatContactMappingServiceImpl extends ServiceImpl<WeChatContactMa ...@@ -160,33 +162,62 @@ public class WeChatContactMappingServiceImpl extends ServiceImpl<WeChatContactMa
* @param query * @param query
*/ */
@Override @Override
public void saveWeChatContactMapping(List<ElasticSearchEntity> query) { public List<ElasticSearchEntity> saveWeChatContactMapping(List<JSONObject> query) {
query.stream().forEach(elasticSearchEntity -> { List<ElasticSearchEntity> resultList = new ArrayList<>();
query.stream().filter(chatData -> StringUtils.isNotEmpty(chatData.getString("from"))).forEach(chatData -> {
ElasticSearchEntity elasticSearchEntity = new ElasticSearchEntity();
elasticSearchEntity.setId(chatData.getString("msgid"));
//发送人映射数据 //发送人映射数据
WeChatContactMapping fromWeChatContactMapping = new WeChatContactMapping(); WeChatContactMapping fromWeChatContactMapping = new WeChatContactMapping();
JSONObject chatData = JSONObject.parseObject(JSONObject.toJSONString(elasticSearchEntity.getData()));
String fromId = chatData.getString("from"); String fromId = chatData.getString("from");
//发送人类型
int fromType = StringUtils.weCustomTypeJudgment(fromId); int fromType = StringUtils.weCustomTypeJudgment(fromId);
fromWeChatContactMapping.setFromId(fromId); fromWeChatContactMapping.setFromId(fromId);
getUserOrCustomerInfo(chatData, fromId, fromType, "fromInfo");
JSONArray tolist = chatData.getJSONArray("tolist"); JSONArray tolist = chatData.getJSONArray("tolist");
if (CollectionUtil.isNotEmpty(tolist)) { if (CollectionUtil.isNotEmpty(tolist)) {
//如果是单聊,tolist唯一,只有一个接收人,并判断接收人的类型 //如果是单聊,tolist唯一,只有一个接收人,并判断接收人的类型
if (StringUtils.isEmpty(chatData.getString("roomid"))) { if (StringUtils.isEmpty(chatData.getString("roomid"))) {
String idStr = String.valueOf(tolist.get(0)); String idStr = String.valueOf(tolist.get(0));
//接收人类型
int reveiceType = StringUtils.weCustomTypeJudgment(idStr); int reveiceType = StringUtils.weCustomTypeJudgment(idStr);
fromWeChatContactMapping.setReceiveId(idStr); fromWeChatContactMapping.setReceiveId(idStr);
//获取接收人信息
getUserOrCustomerInfo(chatData, idStr, reveiceType, "toListInfo");
fromWeChatContactMapping.setIsCustom(reveiceType); fromWeChatContactMapping.setIsCustom(reveiceType);
} else { } else {
fromWeChatContactMapping.setRoomId(chatData.getString("roomid")); fromWeChatContactMapping.setRoomId(chatData.getString("roomid"));
CustomerGroupDetail customerGroupDetail = weCustomerGroupClient
.groupChatDetail(new CustomerGroupDetail().new Params(chatData.getString("roomid")));
GroupChatVo groupChatVo = new GroupChatVo();
BeanUtil.copyProperties(customerGroupDetail.getGroup_chat().get(0), groupChatVo);
chatData.put("roomInfo",JSONObject.parse(JSONObject.toJSONString(groupChatVo)));
} }
} }
//接收人映射数据 //接收人映射数据
WeChatContactMapping reveiceWeChatContactMapping = from2ReveiceData(fromType, fromWeChatContactMapping); WeChatContactMapping reveiceWeChatContactMapping = from2ReveiceData(fromType, fromWeChatContactMapping);
insertWeChatContactMapping(fromWeChatContactMapping); insertWeChatContactMapping(fromWeChatContactMapping);
insertWeChatContactMapping(reveiceWeChatContactMapping); insertWeChatContactMapping(reveiceWeChatContactMapping);
elasticSearchEntity.setData(chatData);
resultList.add(elasticSearchEntity);
}); });
return resultList;
}
private void getUserOrCustomerInfo(JSONObject chatData, String fromId, int fromType, String key) {
//获取发送人信息
if (WeConstans.ID_TYPE_USER.equals(fromType)) {
//成员信息
WeUser weUser = weUserMapper.selectOne(new LambdaQueryWrapper<WeUser>().eq(WeUser::getUserId, fromId));
chatData.put(key, JSONObject.parse(JSONObject.toJSONString(weUser)));
} else if (WeConstans.ID_TYPE_EX.equals(fromType)) {
//获取外部联系人信息
WeCustomer weCustomer = weCustomerMapper.selectWeCustomerById(fromId);
chatData.put(key,JSONObject.parse(JSONObject.toJSONString(weCustomer)));
} else if (WeConstans.ID_TYPE_MACHINE.equals(fromType)) {
//拉去机器人信息暂不处理
}
} }
@Override @Override
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!