Commit d2eadcd6 by sunxiwang

1、企业配置添加会话存档密钥配置

2、会话存档拉去修改
3、修改es查询接口和会话映射关系接口
1 parent 4f8b0d94
Showing 19 changed files with 316 additions and 192 deletions
......@@ -3,6 +3,7 @@ package com.linkwechat.web.controller.wecom;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.linkwechat.common.core.domain.ConversationArchiveQuery;
import com.linkwechat.wecom.service.IWeConversationArchiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -11,8 +12,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author sxw
* @description 会话存档controller
......@@ -26,13 +25,33 @@ public class WeConversationArchiveController {
private IWeConversationArchiveService weConversationArchiveService;
/**
* 获取内部联系人列表
* 获取单聊会话数据接口
*
* @param query 入参
* @param /fromId 发送人id
* @param /reveiceId 接收人id
* @return
*/
@PreAuthorize("@ss.hasPermi('conversationArchive:chatContact:list')")
@GetMapping("/getChatContactList")
public R<PageInfo<JSONObject>> getChatContactList(ConversationArchiveQuery query) {
return R.ok(weConversationArchiveService.getChatContactList(query));
}
/**
* 获取群聊会话数据接口
*
* @param query 入参
* @param /fromId 发送人id
* @param /room 接收人id
* @return
*/
@PreAuthorize("@ss.hasPermi('conversationArchive:InternalContact:list')")
@GetMapping("/getInternalContactList")
public R<List<JSONObject>> getInternalContactList(String userId) {
return R.ok(weConversationArchiveService.getInternalContactList(userId));
@PreAuthorize("@ss.hasPermi('conversationArchive:chatRoomContact:list')")
@GetMapping("/getChatRoomContactList")
public R<PageInfo<JSONObject>> getChatRoomContactList(ConversationArchiveQuery query) {
return R.ok(weConversationArchiveService.getChatRoomContactList(query));
}
}
......@@ -132,7 +132,7 @@ xss:
# elasticsearch配置
elasticsearch:
schema: http
address: 129.211.173.127:9200
address: localhost
connectTimeout: 10000
socketTimeout: 10000
connectionRequestTimeout: 10000
......@@ -169,11 +169,15 @@ wecome:
- /externalcontact/groupchat/transfer
- /externalcontact/get_unassigned_list
- /externalcontact/send_welcome_msg
- /msgaudit/get_permit_user_list
- /externalcontact/unionid_to_external_userid
fileUplodUrl: /media/upload,/media/uploadimg
needProviderTokenUrl:
- /service/get_login_info
needChatTokenUrl:
- /msgaudit/get_permit_user_list
- /msgaudit/check_single_agree
- /msgaudit/check_room_agree
- /msgaudit/groupchat/get
#企微回调通知配置
callBack:
appIdOrCorpId:
......
......@@ -30,4 +30,7 @@ public class WeComeConfig {
/** 需要供应商token的url*/
private String[] needProviderTokenUrl;
/** 会话存档所需token 的url */
private String[] needChatTokenUrl;
}
......@@ -29,6 +29,11 @@ public class WeConstans {
*/
public static final String WE_PROVIDER_ACCESS_TOKEN = "we_provider_access_token";
/**
* 会话存档相关token
*/
public static final String WE_CHAT_ACCESS_TOKEN = "we_chat_access_token";
/**
* 企业微信接口返回成功code
......
package com.linkwechat.common.core.elasticsearch;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import lombok.extern.slf4j.Slf4j;
......@@ -89,7 +90,7 @@ public class ElasticSearch {
public void createIndex2(String idxName, XContentBuilder builder) {
try {
if (!this.indexExist(idxName)) {
log.error(" idxName={} 已经存在,idxSql={}", idxName, builder);
//log.error(" idxName={} 已经存在,idxSql={}", idxName, builder);
return;
}
CreateIndexRequest request = new CreateIndexRequest(idxName);
......@@ -265,6 +266,8 @@ public class ElasticSearch {
res.add(JSON.parseObject(hit.getSourceAsString(), c));
}
// 封装分页
PageHelper.startPage(1,2);
PageInfo<T> page = new PageInfo<>();
page.setList(res);
page.setPageNum(pageNum);
......
......@@ -74,7 +74,7 @@ public class FinanceUtils {
}
String content = Finance.GetContentFromSlice(slice);
JSONArray chatdataArr = JSONObject.parseObject(content).getJSONArray("chatdata");
log.info("开始执行数据解析:------------" + chatdataArr.toJSONString());
log.info("开始执行数据解析:------------");
if (CollectionUtil.isNotEmpty(chatdataArr)) {
chatdataArr.stream().map(data -> (JSONObject) data).forEach(data -> {
try {
......@@ -82,6 +82,9 @@ public class FinanceUtils {
long LocalSEQ = data.getLong("seq");
JSONObject jsonObject = decryptChatRecord(sdk, data.getString("encrypt_random_key"),
data.getString("encrypt_chat_msg"), privateKey);
if (jsonObject ==null){
return;
}
jsonObject.put("seq", LocalSEQ);
elasticSearchEntity.setData(jsonObject);
elasticSearchEntity.setId(jsonObject.getString("msgid"));
......
......@@ -57,16 +57,12 @@ public class RyTask {
//创建索引
elasticSearch.createIndex2(WeConstans.WECOM_FINANCE_INDEX,elasticSearch.getFinanceMapping());
//从缓存中获取消息标识
AtomicLong index = new AtomicLong((Long) Optional.ofNullable(redisCache.getCacheObject(WeConstans.CONTACT_SEQ_KEY)).orElse(0L));
Object seqObject = Optional.ofNullable(redisCache.getCacheObject(WeConstans.CONTACT_SEQ_KEY)).orElse(0L);
Long seqLong = Long.valueOf(String.valueOf(seqObject));
AtomicLong index = new AtomicLong(seqLong);
if (index.get() == 0){
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SortBuilder<?> sortBuilderPrice = SortBuilders.fieldSort(WeConstans.CONTACT_SEQ_KEY).order( SortOrder.DESC);
searchSourceBuilder.sort(sortBuilderPrice);
List<JSONObject> searchResultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, searchSourceBuilder, JSONObject.class);
searchResultList.stream().findFirst().ifPresent(result ->{
index.set(result.getLong(WeConstans.CONTACT_SEQ_KEY) + 1);
});
redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY,index);
setRedisCacheSeqValue(index);
}
log.info(">>>>>>>seq:{}",index.get());
......@@ -81,8 +77,21 @@ public class RyTask {
} catch (Exception e) {
log.error("消息处理异常:ex:{}", e);
e.printStackTrace();
}finally {
setRedisCacheSeqValue(index);
}
index.incrementAndGet();
}
}
private void setRedisCacheSeqValue(AtomicLong index){
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SortBuilder<?> sortBuilderPrice = SortBuilders.fieldSort(WeConstans.CONTACT_SEQ_KEY).order( SortOrder.DESC);
searchSourceBuilder.sort(sortBuilderPrice);
searchSourceBuilder.size(1);
List<JSONObject> searchResultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, searchSourceBuilder, JSONObject.class);
searchResultList.stream().findFirst().ifPresent(result ->{
index.set(result.getLong(WeConstans.CONTACT_SEQ_KEY) + 1);
});
redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY,index);
}
}
package com.linkwechat.wecom.client;
import com.dtflys.forest.annotation.DataObject;
import com.dtflys.forest.annotation.DataVariable;
import com.dtflys.forest.annotation.Request;
import com.linkwechat.wecom.domain.dto.msgaudit.WeMsgAuditDto;
import com.linkwechat.wecom.domain.vo.WeMsgAuditVo;
/**
* @author sxw
......@@ -17,7 +19,7 @@ public interface WeMsgAuditClient {
@Request(url = "/msgaudit/get_permit_user_list",
type = "POST"
)
WeMsgAuditDto getPermitUserList(@DataObject WeMsgAuditDto msgAuditDto);
WeMsgAuditDto getPermitUserList(@DataVariable WeMsgAuditDto msgAuditDto);
/**
* 单聊 获取会话中外部成员的同意情况
......@@ -27,7 +29,7 @@ public interface WeMsgAuditClient {
@Request(url = "/msgaudit/check_single_agree",
type = "POST"
)
WeMsgAuditDto checkSingleAgree(@DataObject WeMsgAuditDto msgAuditDto);
WeMsgAuditDto checkSingleAgree(@DataVariable WeMsgAuditDto msgAuditDto);
/**
* 群聊 获取群会话中外部成员的同意情况
......@@ -37,15 +39,15 @@ public interface WeMsgAuditClient {
@Request(url = "/msgaudit/check_room_agree",
type = "POST"
)
WeMsgAuditDto checkRoomAgree(@DataObject WeMsgAuditDto msgAuditDto);
WeMsgAuditDto checkRoomAgree(@DataVariable WeMsgAuditDto msgAuditDto);
/**
* 获取会话内容存档内部群信息
* @param msgAuditDto
* @param weMsgAuditVo
* @return
*/
@Request(url = "/msgaudit/groupchat/get",
type = "POST"
)
WeMsgAuditDto getGroupChat(@DataObject WeMsgAuditDto msgAuditDto);
WeMsgAuditDto getGroupChat(@DataVariable WeMsgAuditVo weMsgAuditVo);
}
package com.linkwechat.wecom.domain;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.linkwechat.common.annotation.Excel;
import com.linkwechat.common.core.domain.BaseEntity;
import com.linkwechat.wecom.domain.dto.msgaudit.WeMsgAuditDto;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
......@@ -14,7 +14,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
* @date 2020-12-27
*/
@Data
public class WeChatContactMapping extends BaseEntity
public class WeChatContactMapping
{
private static final long serialVersionUID = 1L;
......@@ -25,53 +25,35 @@ public class WeChatContactMapping extends BaseEntity
@Excel(name = "发送人id")
private String fromId;
/** 发送人头像 */
@Excel(name = "发送人头像")
private String fromAvatar;
/** 发送人姓名 */
@Excel(name = "发送人姓名")
private String fromName;
/** 发送人性别 0-未知 1-男性 2-女性 */
@Excel(name = "发送人性别 0-未知 1-男性 2-女性")
private Integer fromGender;
/** 接收人头像 */
@Excel(name = "接收人头像")
private String reviceAvatar;
/** 接收人姓名 */
@Excel(name = "接收人姓名")
private String reviceName;
/** 接收人性别 0-未知 1-男性 2-女性 */
@Excel(name = "接收人性别 0-未知 1-男性 2-女性")
private Integer reviceGender;
/** 接收人id */
@Excel(name = "接收人id")
private String reviceId;
private String receiveId;
/** 群聊id */
@Excel(name = "群聊id")
private String roomId;
/** 是否为客户 0-成员 1-客户 */
@Excel(name = "是否为客户 0-成员 1-客户")
/** 是否为客户 0-成员 1-客户 2-机器人*/
@Excel(name = "是否为客户 0-成员 1-客户 2-机器人")
private Integer isCustom;
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("fromId", getFromId())
.append("reviceAvatar", getReviceAvatar())
.append("reviceName", getReviceName())
.append("reviceGender", getReviceGender())
.append("reviceId", getReviceId())
.append("roomId", getRoomId())
.append("isCustom", getIsCustom())
.toString();
}
/** 是否为客户 0-内部 1-外部 2-群聊*/
@TableField(exist = false)
private Integer searchType;
/** 内部接收人信息 */
@TableField(exist = false)
private WeUser weUser;
/** 外部接收人信息 */
@TableField(exist = false)
private WeCustomer weCustomer;
/** 群信息 */
@TableField(exist = false)
private WeMsgAuditDto roomInfo;
/** 最后一条聊天数据 */
@TableField(exist = false)
private JSONObject finalChatContext;
}
......@@ -65,6 +65,9 @@ public class WeCorpAccount extends BaseEntity
@ApiModelProperty("服务商密钥")
private String providerSecret;
@ApiModelProperty("会话存档密钥")
private String chatSecret;
@ApiModelProperty("企业微信扫码登陆回调地址")
private String wxQrLoginRedirectUri;
......
......@@ -60,6 +60,8 @@ public class WeAccessTokenInterceptor implements Interceptor{
token=iWeAccessTokenService.findContactAccessToken();
}else if(Arrays.asList(weComeConfig.getNeedProviderTokenUrl()).contains(uri)){ //需要供应商token
token=iWeAccessTokenService.findProviderAccessToken();
}else if(Arrays.asList(weComeConfig.getNeedChatTokenUrl()).contains(uri)){ //需要会话存档token
token=iWeAccessTokenService.findChatAccessToken();
}else{
token=iWeAccessTokenService.findCommonAccessToken();
}
......
......@@ -15,5 +15,7 @@ public interface IWeAccessTokenService {
public String findProviderAccessToken();
public String findChatAccessToken();
public void removeToken();
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.linkwechat.wecom.service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.linkwechat.common.core.domain.ConversationArchiveQuery;
import java.util.List;
......@@ -13,9 +14,29 @@ import java.util.List;
public interface IWeConversationArchiveService {
/**
* 根据用户ID 获取对应内部联系人列表
*
* @param userId
* @param query 入参
* @param /fromId 发送人id
* @param /reveiceId 接收人id
* @return
*/
List<JSONObject> getInternalContactList(String userId);
PageInfo<JSONObject> getChatContactList(ConversationArchiveQuery query);
PageInfo<JSONObject> getChatRoomContactList(ConversationArchiveQuery query);
/**
* 查询最早聊天记录
* @param fromId
* @param receiveId
* @return
*/
JSONObject getFinalChatContactInfo(String fromId, String receiveId);
/**
* 查询最早群聊记录
* @param fromId
* @param roomId
* @return
*/
JSONObject getFinalChatRoomContactInfo(String fromId, String roomId);
}
......@@ -67,7 +67,10 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
return findAccessToken(WeConstans.WE_PROVIDER_ACCESS_TOKEN);
}
@Override
public String findChatAccessToken() {
return findAccessToken(WeConstans.WE_CHAT_ACCESS_TOKEN);
}
private String findAccessToken(String accessTokenKey){
......@@ -94,6 +97,10 @@ public class WeAccessTokenServiceImpl implements IWeAccessTokenService {
WeAccessTokenDtoDto providerToken = accessTokenClient.getProviderToken(wxCorpAccount.getCorpId(), wxCorpAccount.getProviderSecret());
token=providerToken.getProvider_access_token();
expires_in=providerToken.getExpires_in();
}else if (WeConstans.WE_CHAT_ACCESS_TOKEN.equals(accessTokenKey)){
WeAccessTokenDtoDto weAccessTokenDtoDto = accessTokenClient.getToken(wxCorpAccount.getCorpId(),wxCorpAccount.getChatSecret());
token=weAccessTokenDtoDto.getAccess_token();
expires_in=weAccessTokenDtoDto.getExpires_in();
}
if(StringUtils.isNotEmpty(token)){
......
......@@ -8,21 +8,22 @@ import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCustomerClient;
import com.linkwechat.wecom.client.WeMsgAuditClient;
import com.linkwechat.wecom.client.WeUserClient;
import com.linkwechat.wecom.domain.WeChatContactMapping;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.dto.WeUserDto;
import com.linkwechat.wecom.domain.dto.customer.ExternalUserDetail;
import com.linkwechat.wecom.domain.vo.WeMsgAuditVo;
import com.linkwechat.wecom.mapper.WeChatContactMappingMapper;
import com.linkwechat.wecom.mapper.WeCustomerMapper;
import com.linkwechat.wecom.mapper.WeUserMapper;
import com.linkwechat.wecom.service.IWeChatContactMappingService;
import com.linkwechat.wecom.service.IWeConversationArchiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
/**
* 聊天关系映射Service业务层处理
......@@ -43,6 +44,10 @@ public class WeChatContactMappingServiceImpl implements IWeChatContactMappingSer
private WeUserClient weUserClient;
@Autowired
private WeCustomerClient weCustomerClient;
@Autowired
private WeMsgAuditClient weMsgAuditClient;
@Autowired
private IWeConversationArchiveService weConversationArchiveService;
/**
* 查询聊天关系映射
......@@ -63,7 +68,30 @@ public class WeChatContactMappingServiceImpl implements IWeChatContactMappingSer
*/
@Override
public List<WeChatContactMapping> selectWeChatContactMappingList(WeChatContactMapping weChatContactMapping) {
return weChatContactMappingMapper.selectWeChatContactMappingList(weChatContactMapping);
List<WeChatContactMapping> weChatMappingList = weChatContactMappingMapper.selectWeChatContactMappingList(weChatContactMapping);
Optional.ofNullable(weChatMappingList).ifPresent(weChatMappingListVo -> {
weChatMappingListVo.stream().forEach(item -> {
if (StringUtils.isNotEmpty(item.getReceiveId())) {
if (WeConstans.ID_TYPE_USER.equals(item.getIsCustom())) {
//成员信息
item.setWeUser(weUserMapper.selectOne(new LambdaQueryWrapper<WeUser>().eq(WeUser::getUserId, item.getReceiveId())));
} else if (WeConstans.ID_TYPE_EX.equals(item.getIsCustom())) {
//获取外部联系人信息
item.setWeCustomer(weCustomerMapper.selectWeCustomerById(item.getReceiveId()));
} else if (WeConstans.ID_TYPE_MACHINE.equals(item.getIsCustom())) {
//拉去机器人信息暂不处理
}
item.setFinalChatContext(weConversationArchiveService.getFinalChatContactInfo(item.getFromId(), item.getReceiveId()));
} else if (StringUtils.isNotEmpty(item.getRoomId())) {
//获取群信息
WeMsgAuditVo weMsgAuditVo = new WeMsgAuditVo();
weMsgAuditVo.setRoomid(item.getRoomId());
item.setRoomInfo(weMsgAuditClient.getGroupChat(weMsgAuditVo));
item.setFinalChatContext(weConversationArchiveService.getFinalChatRoomContactInfo(item.getFromId(), item.getRoomId()));
}
});
});
return weChatMappingList;
}
/**
......@@ -74,7 +102,7 @@ public class WeChatContactMappingServiceImpl implements IWeChatContactMappingSer
*/
@Override
public int insertWeChatContactMapping(WeChatContactMapping weChatContactMapping) {
List<WeChatContactMapping> list = selectWeChatContactMappingList(weChatContactMapping);
List<WeChatContactMapping> list = weChatContactMappingMapper.selectWeChatContactMappingList(weChatContactMapping);
if (CollectionUtil.isEmpty(list)) {
return weChatContactMappingMapper.insertWeChatContactMapping(weChatContactMapping);
}
......@@ -128,12 +156,7 @@ public class WeChatContactMappingServiceImpl implements IWeChatContactMappingSer
String fromId = chatData.getString("from");
int fromType = StringUtils.weCustomTypeJudgment(fromId);
JSONObject fromData = getUserOrCustomInfo(fromId, fromType);
fromWeChatContactMapping.setFromId(fromId);
fromWeChatContactMapping.setFromAvatar(fromData.getString("avatar"));
fromWeChatContactMapping.setFromName(fromData.getString("name"));
fromWeChatContactMapping.setFromGender(fromData.getInteger("gender"));
JSONArray tolist = chatData.getJSONArray("tolist");
if (CollectionUtil.isNotEmpty(tolist)) {
......@@ -141,11 +164,7 @@ public class WeChatContactMappingServiceImpl implements IWeChatContactMappingSer
if (StringUtils.isEmpty(chatData.getString("roomid"))) {
String idStr = String.valueOf(tolist.get(0));
int reveiceType = StringUtils.weCustomTypeJudgment(idStr);
JSONObject reveiceData = getUserOrCustomInfo(idStr, reveiceType);
fromWeChatContactMapping.setReviceId(idStr);
fromWeChatContactMapping.setReviceAvatar(reveiceData.getString("avatar"));
fromWeChatContactMapping.setReviceName(reveiceData.getString("name"));
fromWeChatContactMapping.setReviceGender(reveiceData.getInteger("gender"));
fromWeChatContactMapping.setReceiveId(idStr);
fromWeChatContactMapping.setIsCustom(reveiceType);
} else {
fromWeChatContactMapping.setRoomId(chatData.getString("roomid"));
......@@ -172,55 +191,10 @@ public class WeChatContactMappingServiceImpl implements IWeChatContactMappingSer
if (StringUtils.isNotEmpty(fromWeChatContactMapping.getRoomId())) {
reveiceWeChatContactMapping.setFromId(fromWeChatContactMapping.getRoomId());
} else {
reveiceWeChatContactMapping.setFromId(fromWeChatContactMapping.getReviceId());
reveiceWeChatContactMapping.setFromAvatar(fromWeChatContactMapping.getReviceAvatar());
reveiceWeChatContactMapping.setFromName(fromWeChatContactMapping.getReviceName());
reveiceWeChatContactMapping.setFromGender(fromWeChatContactMapping.getReviceGender());
reveiceWeChatContactMapping.setFromId(fromWeChatContactMapping.getReceiveId());
}
reveiceWeChatContactMapping.setReviceId(fromWeChatContactMapping.getFromId());
reveiceWeChatContactMapping.setReviceAvatar(fromWeChatContactMapping.getFromAvatar());
reveiceWeChatContactMapping.setReviceName(fromWeChatContactMapping.getFromName());
reveiceWeChatContactMapping.setReviceGender(fromWeChatContactMapping.getFromGender());
reveiceWeChatContactMapping.setReceiveId(fromWeChatContactMapping.getFromId());
reveiceWeChatContactMapping.setIsCustom(fromType);
return reveiceWeChatContactMapping;
}
/**
* 获取成员或者客户详情
*
* @param id 用户id
* @param type
* @return
*/
private JSONObject getUserOrCustomInfo(String id, int type) {
JSONObject tempData = new JSONObject();
if (WeConstans.ID_TYPE_USER.equals(type)) {
WeUser weUser = weUserMapper.selectOne(new LambdaQueryWrapper<WeUser>().eq(WeUser::getUserId, id));
if (weUser == null) {
WeUserDto userDto = weUserClient.getUserByUserId(id);
tempData.put("avatar", userDto.getAvatar());
tempData.put("name", userDto.getName());
tempData.put("gender", userDto.getGender());
} else {
tempData.put("avatar", weUser.getAvatarMediaid());
tempData.put("name", weUser.getName());
tempData.put("gender", weUser.getGender());
}
} else if (WeConstans.ID_TYPE_EX.equals(type)) {
WeCustomer weCustomer = weCustomerMapper.selectWeCustomerById(id);
if (weCustomer == null) {
ExternalUserDetail externalUserDetail = weCustomerClient.get(id);
tempData.put("avatar", externalUserDetail.getExternal_contact().getAvatar());
tempData.put("name", externalUserDetail.getExternal_contact().getName());
tempData.put("gender", externalUserDetail.getExternal_contact().getGender());
} else {
tempData.put("avatar", weCustomer.getAvatar());
tempData.put("name", weCustomer.getName());
tempData.put("gender", weCustomer.getGender());
}
} else if (WeConstans.ID_TYPE_MACHINE.equals(type)) {
}
return tempData;
}
}
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.ConversationArchiveQuery;
import com.linkwechat.common.core.elasticsearch.ElasticSearch;
import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.service.IWeConversationArchiveService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
......@@ -13,11 +17,8 @@ import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author sxw
......@@ -33,25 +34,122 @@ public class WeConversationArchiveServiceImpl implements IWeConversationArchiveS
/**
* 根据用户ID 获取对应内部联系人列表
*
* @param userId
* @param query 入参
* @return
*/
@Override
public List<JSONObject> getInternalContactList(String userId) {
public PageInfo<JSONObject> getChatContactList(ConversationArchiveQuery query) {
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.size(1000);
int from = (query.getPageSize() - 1) * query.getPageNum();
builder.size(query.getPageNum());
builder.from(from);
builder.sort("msgtime", SortOrder.ASC);
BoolQueryBuilder fromBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("roomid", ""))
.must(QueryBuilders.matchQuery("from", query.getFromId()))
.must(QueryBuilders.matchQuery("tolist.keyword", query.getReceiveId()));
BoolQueryBuilder toLsitBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("roomid", ""))
.must(QueryBuilders.matchQuery("from", query.getReceiveId()))
.must(QueryBuilders.matchQuery("tolist.keyword", query.getFromId()));
//查询聊天类型
if (StringUtils.isNotEmpty(query.getMsgType())) {
fromBuilder.must(QueryBuilders.termQuery("msgtype", query.getMsgType()));
toLsitBuilder.must(QueryBuilders.termQuery("msgtype", query.getMsgType()));
}
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.should(fromBuilder)
.should(toLsitBuilder)
.minimumShouldMatch(1);
//时间范围查询
if (StringUtils.isNotEmpty(query.getBeginTime()) && StringUtils.isNotEmpty(query.getEndTime())) {
Date beginTime = DateUtils.dateTime(query.getBeginTime(), DateUtils.YYYY_MM_DD);
Date endTime = DateUtils.dateTime(query.getEndTime(), DateUtils.YYYY_MM_DD);
boolQueryBuilder.filter(QueryBuilders.rangeQuery("msgtime").gte(beginTime).lte(endTime));
}
builder.query(boolQueryBuilder);
return elasticSearch.searchPage(WeConstans.WECOM_FINANCE_INDEX, builder, query.getPageNum(), query.getPageSize(), JSONObject.class);
}
@Override
public PageInfo<JSONObject> getChatRoomContactList(ConversationArchiveQuery query) {
SearchSourceBuilder builder = new SearchSourceBuilder();
int from = (query.getPageSize() - 1) * query.getPageNum();
builder.size(query.getPageNum());
builder.from(from);
builder.sort("msgtime", SortOrder.ASC);
BoolQueryBuilder fromBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("roomid", query.getRoomId()))
.must(QueryBuilders.matchQuery("from", query.getFromId()));
BoolQueryBuilder roomidBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("roomid", query.getRoomId()))
.must(QueryBuilders.matchQuery("tolist.keyword", query.getFromId()));
//查询聊天类型
if (StringUtils.isNotEmpty(query.getMsgType())) {
fromBuilder.must(QueryBuilders.termQuery("msgtype", query.getMsgType()));
roomidBuilder.must(QueryBuilders.termQuery("msgtype", query.getMsgType()));
}
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.should(fromBuilder)
.should(roomidBuilder);
//时间范围查询
if (StringUtils.isNotEmpty(query.getBeginTime()) && StringUtils.isNotEmpty(query.getEndTime())) {
Date beginTime = DateUtils.dateTime(query.getBeginTime(), DateUtils.YYYY_MM_DD);
Date endTime = DateUtils.dateTime(query.getEndTime(), DateUtils.YYYY_MM_DD);
boolQueryBuilder.filter(QueryBuilders.rangeQuery("msgtime").gte(beginTime).lte(endTime));
}
builder.query(boolQueryBuilder);
return elasticSearch.searchPage(WeConstans.WECOM_FINANCE_INDEX, builder, query.getPageNum(), query.getPageSize(), JSONObject.class);
}
@Override
public JSONObject getFinalChatContactInfo(String fromId, String receiveId) {
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.sort("msgtime", SortOrder.ASC);
builder.size(1);
BoolQueryBuilder fromBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("roomid", ""))
.must(QueryBuilders.matchQuery("from", fromId))
.must(QueryBuilders.matchQuery("tolist.keyword", receiveId));
BoolQueryBuilder toLsitBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("roomid", ""))
.must(QueryBuilders.matchQuery("from", receiveId))
.must(QueryBuilders.matchQuery("tolist.keyword", fromId));
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.should(fromBuilder)
.should(toLsitBuilder)
.minimumShouldMatch(1);
builder.query(boolQueryBuilder);
List<JSONObject> resultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, builder, JSONObject.class);
if (CollectionUtil.isNotEmpty(resultList)) {
return resultList.get(0);
} else {
return null;
}
}
@Override
public JSONObject getFinalChatRoomContactInfo(String fromId, String roomId) {
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.sort("msgtime", SortOrder.ASC);
builder.size(1);
BoolQueryBuilder fromBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("roomid", roomId))
.must(QueryBuilders.matchQuery("from", fromId));
BoolQueryBuilder roomidBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("roomid", roomId))
.must(QueryBuilders.matchQuery("tolist.keyword", fromId));
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.should(QueryBuilders.termQuery("tolist.keyword", userId))
.should(QueryBuilders.termQuery("from",userId))
.minimumShouldMatch(1)
.must(QueryBuilders.termQuery("type",1))
.must(QueryBuilders.termQuery("roomid", ""));
.should(fromBuilder)
.should(roomidBuilder);
builder.query(boolQueryBuilder);
List<JSONObject> searchList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, builder, JSONObject.class);
Map<String, List<JSONObject>> from = Optional.ofNullable(searchList)
.orElseGet(ArrayList::new).stream()
.collect(Collectors.groupingBy(item -> item.getString("from")));
return null;
List<JSONObject> resultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, builder, JSONObject.class);
if (CollectionUtil.isNotEmpty(resultList)) {
return resultList.get(0);
} else {
return null;
}
}
}
......@@ -7,34 +7,35 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<resultMap type="WeChatContactMapping" id="WeChatContactMappingResult">
<result property="id" column="id" />
<result property="fromId" column="from_id" />
<result property="fromAvatar" column="from_avatar" />
<result property="fromName" column="from_name" />
<result property="fromGender" column="from_gender" />
<result property="reviceAvatar" column="revice_avatar" />
<result property="reviceName" column="revice_name" />
<result property="reviceGender" column="revice_gender" />
<result property="reviceId" column="revice_id" />
<result property="receiveId" column="receive_id" />
<result property="roomId" column="room_id" />
<result property="isCustom" column="is_custom" />
</resultMap>
<sql id="selectWeChatContactMappingVo">
select id, from_id, from_avatar, from_name, from_gender, revice_avatar, revice_name, revice_gender, revice_id, room_id, is_custom from we_chat_contact_mapping
select id, from_id, receive_id, room_id, is_custom from we_chat_contact_mapping
</sql>
<select id="selectWeChatContactMappingList" parameterType="WeChatContactMapping" resultMap="WeChatContactMappingResult">
<include refid="selectWeChatContactMappingVo"/>
<where>
<if test="fromId != null and fromId != ''"> and from_id = #{fromId}</if>
<if test="fromAvatar != null and fromAvatar != ''"> and from_avatar = #{fromAvatar}</if>
<if test="fromName != null and fromName != ''"> and from_name like concat('%', #{fromName}, '%')</if>
<if test="fromGender != null "> and from_gender = #{fromGender}</if>
<if test="reviceAvatar != null and reviceAvatar != ''"> and revice_avatar = #{reviceAvatar}</if>
<if test="reviceName != null and reviceName != ''"> and revice_name like concat('%', #{reviceName}, '%')</if>
<if test="reviceGender != null "> and revice_gender = #{reviceGender}</if>
<if test="reviceId != null and reviceId != ''"> and revice_id = #{reviceId}</if>
<if test="receiveId != null and receiveId != ''"> and receive_id = #{receiveId}</if>
<if test="roomId != null and roomId != ''"> and room_id = #{roomId}</if>
<if test="isCustom != null "> and is_custom = #{isCustom}</if>
<choose>
<when test="searchType != null and searchType == 0">
and is_custom = 0
</when>
<when test="searchType != null and searchType == 1">
and is_custom in (1,2)
</when>
<when test="searchType != null and searchType == 2">
and room_id is not null
</when>
<when test="searchType != null and searchType == 3">
and is_custom in (0,1,2)
</when>
</choose>
</where>
</select>
......@@ -47,25 +48,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
insert into we_chat_contact_mapping
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="fromId != null and fromId != ''">from_id,</if>
<if test="fromAvatar != null">from_avatar,</if>
<if test="fromName != null">from_name,</if>
<if test="fromGender != null">from_gender,</if>
<if test="reviceAvatar != null">revice_avatar,</if>
<if test="reviceName != null">revice_name,</if>
<if test="reviceGender != null">revice_gender,</if>
<if test="reviceId != null and reviceId != ''">revice_id,</if>
<if test="receiveId != null and receiveId != ''">receive_id,</if>
<if test="roomId != null">room_id,</if>
<if test="isCustom != null">is_custom,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="fromId != null and fromId != ''">#{fromId},</if>
<if test="fromAvatar != null">#{fromAvatar},</if>
<if test="fromName != null">#{fromName},</if>
<if test="fromGender != null">#{fromGender},</if>
<if test="reviceAvatar != null">#{reviceAvatar},</if>
<if test="reviceName != null">#{reviceName},</if>
<if test="reviceGender != null">#{reviceGender},</if>
<if test="reviceId != null and reviceId != ''">#{reviceId},</if>
<if test="receiveId != null and receiveId != ''">#{receiveId},</if>
<if test="roomId != null">#{roomId},</if>
<if test="isCustom != null">#{isCustom},</if>
</trim>
......@@ -75,13 +64,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
update we_chat_contact_mapping
<trim prefix="SET" suffixOverrides=",">
<if test="fromId != null and fromId != ''">from_id = #{fromId},</if>
<if test="fromAvatar != null">from_avatar = #{fromAvatar},</if>
<if test="fromName != null">from_name = #{fromName},</if>
<if test="fromGender != null">from_gender = #{fromGender},</if>
<if test="reviceAvatar != null">revice_avatar = #{reviceAvatar},</if>
<if test="reviceName != null">revice_name = #{reviceName},</if>
<if test="reviceGender != null">revice_gender = #{reviceGender},</if>
<if test="reviceId != null and reviceId != ''">revice_id = #{reviceId},</if>
<if test="receiveId != null and receiveId != ''">receive_id = #{receiveId},</if>
<if test="roomId != null">room_id = #{roomId},</if>
<if test="isCustom != null">is_custom = #{isCustom},</if>
</trim>
......
......@@ -17,13 +17,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="updateTime" column="update_time" />
<result property="contactSecret" column="contact_secret" />
<result property="agentId" column="agent_id" />
<result property="chatSecret" column="chat_secret" />
<result property="providerSecret" column="provider_secret" />
<result property="customerChurnNoticeSwitch" column="customer_churn_notice_switch" />
<result property="wxQrLoginRedirectUri" column="wx_qr_login_redirect_uri"/>
</resultMap>
<sql id="selectWeCorpAccountVo">
select id,provider_secret,company_name, corp_id, corp_secret,agent_id ,status, del_flag, create_by, create_time, update_by, update_time,contact_secret, customer_churn_notice_switch,wx_qr_login_redirect_uri from we_corp_account
select id,chat_secret,provider_secret,company_name, corp_id, corp_secret,agent_id ,status, del_flag, create_by, create_time, update_by, update_time,contact_secret, customer_churn_notice_switch,wx_qr_login_redirect_uri from we_corp_account
</sql>
<select id="selectWeCorpAccountList" parameterType="WeCorpAccount" resultMap="WeCorpAccountResult">
......@@ -54,6 +55,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="contactSecret != null ">contact_secret,</if>
<if test="chatSecret != null ">chat_secret,</if>
<if test="customerChurnNoticeSwitch != null ">customer_churn_notice_switch,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
......@@ -67,6 +69,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="contactSecret != null ">#{contactSecret},</if>
<if test="chatSecret != null ">#{chatSecret},</if>
<if test="customerChurnNoticeSwitch != null ">#{customerChurnNoticeSwitch},</if>
</trim>
</insert>
......@@ -84,6 +87,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="contactSecret != null ">contact_secret = #{contactSecret},</if>
<if test="chatSecret != null ">chat_secret = #{chatSecret},</if>
<if test="customerChurnNoticeSwitch != null ">customer_churn_notice_switch = #{customerChurnNoticeSwitch},</if>
</trim>
where id = #{id}
......
......@@ -159,7 +159,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="selectWeCustomerById" parameterType="String" resultMap="WeCustomerResult">
<select id="selectWeCustomerById" parameterType="String" resultType="WeCustomer">
<include refid="selectWeCustomerVo"/>
where external_userid = #{externalUserId}
</select>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!