Commit bba430c9 by sunxiwang

会话存档添加会话映射关系

1 parent 576e32d9
package com.linkwechat.web.controller.wecom;
import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.common.utils.poi.ExcelUtil;
import com.linkwechat.wecom.domain.WeChatContactMapping;
import com.linkwechat.wecom.service.IWeChatContactMappingService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 聊天关系映射Controller
*
* @author ruoyi
* @date 2020-12-27
*/
@RestController
@RequestMapping("/system/mapping")
public class WeChatContactMappingController extends BaseController
{
@Autowired
private IWeChatContactMappingService weChatContactMappingService;
/**
* 查询聊天关系映射列表
*/
@PreAuthorize("@ss.hasPermi('system:mapping:list')")
@GetMapping("/list")
public TableDataInfo list(WeChatContactMapping weChatContactMapping)
{
startPage();
List<WeChatContactMapping> list = weChatContactMappingService.selectWeChatContactMappingList(weChatContactMapping);
return getDataTable(list);
}
/**
* 导出聊天关系映射列表
*/
@PreAuthorize("@ss.hasPermi('system:mapping:export')")
@Log(title = "聊天关系映射", businessType = BusinessType.EXPORT)
@GetMapping("/export")
public AjaxResult export(WeChatContactMapping weChatContactMapping)
{
List<WeChatContactMapping> list = weChatContactMappingService.selectWeChatContactMappingList(weChatContactMapping);
ExcelUtil<WeChatContactMapping> util = new ExcelUtil<WeChatContactMapping>(WeChatContactMapping.class);
return util.exportExcel(list, "mapping");
}
/**
* 获取聊天关系映射详细信息
*/
@PreAuthorize("@ss.hasPermi('system:mapping:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
return AjaxResult.success(weChatContactMappingService.selectWeChatContactMappingById(id));
}
/**
* 新增聊天关系映射
*/
@PreAuthorize("@ss.hasPermi('system:mapping:add')")
@Log(title = "聊天关系映射", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody WeChatContactMapping weChatContactMapping)
{
return toAjax(weChatContactMappingService.insertWeChatContactMapping(weChatContactMapping));
}
/**
* 修改聊天关系映射
*/
@PreAuthorize("@ss.hasPermi('system:mapping:edit')")
@Log(title = "聊天关系映射", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody WeChatContactMapping weChatContactMapping)
{
return toAjax(weChatContactMappingService.updateWeChatContactMapping(weChatContactMapping));
}
/**
* 删除聊天关系映射
*/
@PreAuthorize("@ss.hasPermi('system:mapping:remove')")
@Log(title = "聊天关系映射", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable Long[] ids)
{
return toAjax(weChatContactMappingService.deleteWeChatContactMappingByIds(ids));
}
}
......@@ -11,6 +11,8 @@ 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
......@@ -30,7 +32,7 @@ public class WeConversationArchiveController {
*/
@PreAuthorize("@ss.hasPermi('conversationArchive:InternalContact:list')")
@GetMapping("/getInternalContactList")
public R<PageInfo<JSONObject>> getInternalContactList(String userId, int pageSize, int pageNum) {
return R.ok(weConversationArchiveService.getInternalContactList(userId, pageSize, pageNum));
public R<List<JSONObject>> getInternalContactList(String userId) {
return R.ok(weConversationArchiveService.getInternalContactList(userId));
}
}
......@@ -206,6 +206,16 @@ public class WeConstans {
*/
public static final String DEL_FOLLOW_USER_SWITCH_CLOSE = "0";
public static final String DEL_FOLLOW_USER_SWITCH_OPEN = "1";
public static final String CONTACT_SEQ_KEY = "seq";
/**
* id类型 0:成员 1:客户,2:机器
*/
public static final Integer ID_TYPE_USER = 0;
public static final Integer ID_TYPE_EX = 1;
public static final Integer ID_TYPE_MACHINE = 2;
/**
* 企微回调事件类型路由
*/
......
......@@ -490,6 +490,23 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
}
/**
* 企微用户客户机器判断
* @param str
* @return
*/
public static int weCustomTypeJudgment(String str){
if (isNotEmpty(str)){
if (str.startsWith("wo") || str.startsWith("wm")){
return 1;
}
if (str.startsWith("wb")){
return 2;
}
}
return 0;
}
public static void main(String[] args) {
Integer[] arrays = { 1,2,3,4,5,6,7 };
String join = join(arrays, ",");
......
......@@ -17,8 +17,7 @@ import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
......@@ -75,6 +74,7 @@ public class FinanceUtils {
}
String content = Finance.GetContentFromSlice(slice);
JSONArray chatdataArr = JSONObject.parseObject(content).getJSONArray("chatdata");
log.info("开始执行数据解析:------------" + chatdataArr.toJSONString());
if (CollectionUtil.isNotEmpty(chatdataArr)) {
chatdataArr.stream().map(data -> (JSONObject) data).forEach(data -> {
try {
......@@ -83,27 +83,6 @@ public class FinanceUtils {
JSONObject jsonObject = decryptChatRecord(sdk, data.getString("encrypt_random_key"),
data.getString("encrypt_chat_msg"), privateKey);
jsonObject.put("seq", LocalSEQ);
jsonObject.put("totype","0");
if (StringUtils.isEmpty(jsonObject.getString("roomid"))){
JSONArray tolist = jsonObject.getJSONArray("tolist");
Optional.ofNullable(tolist).ifPresent(itemList ->{
List<String> isEType = itemList.stream().map(item -> (String) item).filter(item ->
StringUtils.isNotEmpty(item) &&
("wo".equals(item.substring(0, 2)) || "wm".equals(item.substring(0, 2))))
.collect(Collectors.toList());
List<String> isExtype = itemList.stream().map(item -> (String) item).filter(item ->
StringUtils.isNotEmpty(item) && "wb".equals(item.substring(0, 2)))
.collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(isEType)){
jsonObject.put("totype","1");
return;
}
if (CollectionUtil.isNotEmpty(isExtype)){
jsonObject.put("totype","2");
return;
}
});
}
elasticSearchEntity.setData(jsonObject);
elasticSearchEntity.setId(jsonObject.getString("msgid"));
resList.add(elasticSearchEntity);
......@@ -111,6 +90,7 @@ public class FinanceUtils {
e.printStackTrace();
}
});
log.info("数据解析完成:------------" + chatdataArr.toJSONString());
}
Finance.FreeSlice(slice);
return resList;
......@@ -136,11 +116,9 @@ public class FinanceUtils {
//解密
Finance.DecryptData(sdk, str, encrypt_chat_msg, msg);
String jsonDataStr = Finance.GetContentFromSlice(msg);
log.info("解析数据:------------" + jsonDataStr);
JSONObject realJsonData = JSONObject.parseObject(jsonDataStr);
String msgType = realJsonData.getString("msgtype");
if (StringUtils.isNotEmpty(msgType)){
if (StringUtils.isNotEmpty(msgType)) {
getSwitchType(realJsonData, msgType);
}
return realJsonData;
......@@ -175,7 +153,8 @@ public class FinanceUtils {
case "mixed":
setMediaMixedData(realJsonData, msgType);
break;
case "meeting_voice_call":case "voip_doc_share":
case "meeting_voice_call":
case "voip_doc_share":
setMediaMeetingVoiceCallData(realJsonData, msgType);
break;
default:
......@@ -184,7 +163,7 @@ public class FinanceUtils {
}
private static void setMediaMeetingVoiceCallData(JSONObject realJsonData, String msgType) {
JSONObject meetingVoiceCall = Optional.ofNullable(realJsonData.getJSONObject(msgType))
JSONObject meetingVoiceCall = Optional.ofNullable(realJsonData.getJSONObject(msgType))
.orElse(realJsonData.getJSONObject("content"));
String fileName = meetingVoiceCall.getString("filename");
getPath(realJsonData, msgType, fileName);
......@@ -193,13 +172,13 @@ public class FinanceUtils {
private static void setMediaMixedData(JSONObject realJsonData, String msgType) {
JSONObject mixedData = realJsonData.getJSONObject(msgType);
JSONArray items = mixedData.getJSONArray("item");
items.stream().map(item ->(JSONObject) item).forEach(item ->{
getSwitchType(item,item.getString("type"));
items.stream().map(item -> (JSONObject) item).forEach(item -> {
getSwitchType(item, item.getString("type"));
});
}
private static void setMediaFileData(JSONObject realJsonData, String msgType) {
JSONObject emotionData = Optional.ofNullable(realJsonData.getJSONObject(msgType))
JSONObject emotionData = Optional.ofNullable(realJsonData.getJSONObject(msgType))
.orElse(realJsonData.getJSONObject("content"));
String filename = emotionData.getString("filename");
//String fileext = emotionData.getString("fileext");
......@@ -208,30 +187,31 @@ public class FinanceUtils {
}
private static void setMediaImageData(JSONObject realJsonData,String msgType) {
String fileName = IdUtils.simpleUUID()+".jpg";
private static void setMediaImageData(JSONObject realJsonData, String msgType) {
String fileName = IdUtils.simpleUUID() + ".jpg";
getPath(realJsonData, msgType, fileName);
}
private static void setMediaVoiceData(JSONObject realJsonData, String msgType) {
String fileName = IdUtils.simpleUUID()+".mp3";
String fileName = IdUtils.simpleUUID() + ".mp3";
getPath(realJsonData, msgType, fileName);
}
private static void setMediaVideoData(JSONObject realJsonData, String msgType) {
String fileName = IdUtils.simpleUUID()+".mp4";
String fileName = IdUtils.simpleUUID() + ".mp4";
getPath(realJsonData, msgType, fileName);
}
private static void setMediaEmotionData(JSONObject realJsonData, String msgType) {
String fileName = "";
JSONObject emotionData = realJsonData.getJSONObject(msgType);
Integer type = emotionData.getInteger("type");
switch (type){
switch (type) {
case 1:
fileName = IdUtils.simpleUUID() +".gif";
fileName = IdUtils.simpleUUID() + ".gif";
break;
case 2:
fileName = IdUtils.simpleUUID() +".png";
fileName = IdUtils.simpleUUID() + ".png";
break;
default:
break;
......@@ -241,19 +221,19 @@ public class FinanceUtils {
private static void getPath(JSONObject realJsonData, String msgType, String fileName) {
String filePath = getFilePath(msgType);
JSONObject data = Optional.ofNullable(realJsonData.getJSONObject(msgType))
JSONObject data = Optional.ofNullable(realJsonData.getJSONObject(msgType))
.orElse(realJsonData.getJSONObject("content"));
String sdkfileid = data.getString("sdkfileid");
try {
getMediaData(sdkfileid,"","",filePath,fileName);
getMediaData(sdkfileid, "", "", filePath, fileName);
} catch (Exception e) {
e.printStackTrace();
}
data.put("attachment",filePath+"/"+fileName);
if(realJsonData.containsKey("content")){
realJsonData.put("content",data);
}else {
realJsonData.put("msgType",data);
data.put("attachment", filePath + "/" + fileName);
if (realJsonData.containsKey("content")) {
realJsonData.put("content", data);
} else {
realJsonData.put("msgType", data);
}
}
......@@ -271,21 +251,16 @@ public class FinanceUtils {
if (ret != 0) {
return;
}
/* int indexLen = Finance.GetIndexLen(media_data);
int dataLen = Finance.GetDataLen(media_data);
int isMediaDataFinish = Finance.IsMediaDataFinish(media_data);*/
try {
File f = new File(filePath);
if(!f.exists()){
if (!f.exists()) {
f.mkdirs();
}
File file = new File(filePath,fileName);
if (!file.isDirectory()){
File file = new File(filePath, fileName);
if (!file.isDirectory()) {
file.createNewFile();
}
FileOutputStream outputStream = new FileOutputStream(file, true);
//File file = new File("D:\\浏览器下载\\media_data\\"+ IdUtils.simpleUUID()+".jpg");
//FileOutputStream outputStream = new FileOutputStream(file, true);
outputStream.write(Finance.GetData(media_data));
outputStream.close();
} catch (Exception e) {
......
......@@ -34,6 +34,10 @@
<groupId>com.linkwechat</groupId>
<artifactId>linkwe-common</artifactId>
</dependency>
<dependency>
<groupId>com.linkwechat</groupId>
<artifactId>linkwe-wecom</artifactId>
</dependency>
</dependencies>
......
......@@ -5,9 +5,12 @@ import com.alibaba.fastjson.JSONObject;
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.redis.RedisCache;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.service.IWeChatContactMappingService;
import com.tencent.wework.FinanceUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
......@@ -17,7 +20,9 @@ import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
/**
* 定时任务调度测试
......@@ -29,6 +34,10 @@ import java.util.concurrent.atomic.AtomicLong;
public class RyTask {
@Autowired
private ElasticSearch elasticSearch;
@Autowired
private RedisCache redisCache;
@Autowired
private IWeChatContactMappingService weChatContactMappingService;
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
......@@ -47,20 +56,27 @@ public class RyTask {
log.info("执行有参方法: params:{},{}", corpId, secret);
//创建索引
elasticSearch.createIndex2(WeConstans.WECOM_FINANCE_INDEX,elasticSearch.getFinanceMapping());
AtomicLong index = new AtomicLong(0);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SortBuilder<?> sortBuilderPrice = SortBuilders.fieldSort("seq").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("seq")+1);
});
//从缓存中获取消息标识
AtomicLong index = new AtomicLong((Long) Optional.ofNullable(redisCache.getCacheObject(WeConstans.CONTACT_SEQ_KEY)).orElse(0L));
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);
}
log.info(">>>>>>>seq:{}",index.get());
FinanceUtils.initSDK(corpId, secret);
List<ElasticSearchEntity> chatDataList = FinanceUtils.getChatData(index.get(), "", "");
List<ElasticSearchEntity> chatDataList = FinanceUtils.getChatData(index.get(),
"",
"");
if (CollectionUtil.isNotEmpty(chatDataList)){
try {
weChatContactMappingService.saveWeChatContactMapping(chatDataList);
elasticSearch.insertBatch(WeConstans.WECOM_FINANCE_INDEX, chatDataList);
} catch (Exception e) {
log.error("消息处理异常:ex:{}", e);
......
package com.linkwechat.wecom.domain;
import com.linkwechat.common.annotation.Excel;
import com.linkwechat.common.core.domain.BaseEntity;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
* 聊天关系映射对象 we_chat_contact_mapping
*
* @author ruoyi
* @date 2020-12-27
*/
@Data
public class WeChatContactMapping extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 主键id */
private Long id;
/** 发送人id */
@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;
/** 群聊id */
@Excel(name = "群聊id")
private String roomId;
/** 是否为客户 0-成员 1-客户 */
@Excel(name = "是否为客户 0-成员 1-客户")
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();
}
}
package com.linkwechat.wecom.mapper;
import com.linkwechat.wecom.domain.WeChatContactMapping;
import java.util.List;
/**
* 聊天关系映射Mapper接口
*
* @author ruoyi
* @date 2020-12-27
*/
public interface WeChatContactMappingMapper
{
/**
* 查询聊天关系映射
*
* @param id 聊天关系映射ID
* @return 聊天关系映射
*/
public WeChatContactMapping selectWeChatContactMappingById(Long id);
/**
* 查询聊天关系映射列表
*
* @param weChatContactMapping 聊天关系映射
* @return 聊天关系映射集合
*/
public List<WeChatContactMapping> selectWeChatContactMappingList(WeChatContactMapping weChatContactMapping);
/**
* 新增聊天关系映射
*
* @param weChatContactMapping 聊天关系映射
* @return 结果
*/
public int insertWeChatContactMapping(WeChatContactMapping weChatContactMapping);
/**
* 修改聊天关系映射
*
* @param weChatContactMapping 聊天关系映射
* @return 结果
*/
public int updateWeChatContactMapping(WeChatContactMapping weChatContactMapping);
/**
* 删除聊天关系映射
*
* @param id 聊天关系映射ID
* @return 结果
*/
public int deleteWeChatContactMappingById(Long id);
/**
* 批量删除聊天关系映射
*
* @param ids 需要删除的数据ID
* @return 结果
*/
public int deleteWeChatContactMappingByIds(Long[] ids);
}
package com.linkwechat.wecom.service;
import com.alibaba.fastjson.JSONObject;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import com.linkwechat.wecom.domain.WeChatContactMapping;
import java.util.List;
/**
* 聊天关系映射Service接口
*
* @author ruoyi
* @date 2020-12-27
*/
public interface IWeChatContactMappingService
{
/**
* 查询聊天关系映射
*
* @param id 聊天关系映射ID
* @return 聊天关系映射
*/
public WeChatContactMapping selectWeChatContactMappingById(Long id);
/**
* 查询聊天关系映射列表
*
* @param weChatContactMapping 聊天关系映射
* @return 聊天关系映射集合
*/
public List<WeChatContactMapping> selectWeChatContactMappingList(WeChatContactMapping weChatContactMapping);
/**
* 新增聊天关系映射
*
* @param weChatContactMapping 聊天关系映射
* @return 结果
*/
public int insertWeChatContactMapping(WeChatContactMapping weChatContactMapping);
/**
* 修改聊天关系映射
*
* @param weChatContactMapping 聊天关系映射
* @return 结果
*/
public int updateWeChatContactMapping(WeChatContactMapping weChatContactMapping);
/**
* 批量删除聊天关系映射
*
* @param ids 需要删除的聊天关系映射ID
* @return 结果
*/
public int deleteWeChatContactMappingByIds(Long[] ids);
/**
* 删除聊天关系映射信息
*
* @param id 聊天关系映射ID
* @return 结果
*/
public int deleteWeChatContactMappingById(Long id);
/**
* 保存
* @param query
*/
public void saveWeChatContactMapping(List<ElasticSearchEntity> query);
}
......@@ -3,6 +3,8 @@ package com.linkwechat.wecom.service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import java.util.List;
/**
* @author sxw
* @description 会话存档业务接口
......@@ -15,5 +17,5 @@ public interface IWeConversationArchiveService {
* @param userId
* @return
*/
PageInfo<JSONObject> getInternalContactList(String userId, int pageSize, int pageNum);
List<JSONObject> getInternalContactList(String userId);
}
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.WeChatContactMapping;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeUser;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* 聊天关系映射Service业务层处理
*
* @author ruoyi
* @date 2020-12-27
*/
@Slf4j
@Service
public class WeChatContactMappingServiceImpl implements IWeChatContactMappingService {
@Autowired
private WeChatContactMappingMapper weChatContactMappingMapper;
@Autowired
private WeUserMapper weUserMapper;
@Autowired
private WeCustomerMapper weCustomerMapper;
/**
* 查询聊天关系映射
*
* @param id 聊天关系映射ID
* @return 聊天关系映射
*/
@Override
public WeChatContactMapping selectWeChatContactMappingById(Long id) {
return weChatContactMappingMapper.selectWeChatContactMappingById(id);
}
/**
* 查询聊天关系映射列表
*
* @param weChatContactMapping 聊天关系映射
* @return 聊天关系映射
*/
@Override
public List<WeChatContactMapping> selectWeChatContactMappingList(WeChatContactMapping weChatContactMapping) {
return weChatContactMappingMapper.selectWeChatContactMappingList(weChatContactMapping);
}
/**
* 新增聊天关系映射
*
* @param weChatContactMapping 聊天关系映射
* @return 结果
*/
@Override
public int insertWeChatContactMapping(WeChatContactMapping weChatContactMapping) {
return weChatContactMappingMapper.insertWeChatContactMapping(weChatContactMapping);
}
/**
* 修改聊天关系映射
*
* @param weChatContactMapping 聊天关系映射
* @return 结果
*/
@Override
public int updateWeChatContactMapping(WeChatContactMapping weChatContactMapping) {
return weChatContactMappingMapper.updateWeChatContactMapping(weChatContactMapping);
}
/**
* 批量删除聊天关系映射
*
* @param ids 需要删除的聊天关系映射ID
* @return 结果
*/
@Override
public int deleteWeChatContactMappingByIds(Long[] ids) {
return weChatContactMappingMapper.deleteWeChatContactMappingByIds(ids);
}
/**
* 删除聊天关系映射信息
*
* @param id 聊天关系映射ID
* @return 结果
*/
@Override
public int deleteWeChatContactMappingById(Long id) {
return weChatContactMappingMapper.deleteWeChatContactMappingById(id);
}
@Override
public void saveWeChatContactMapping(List<ElasticSearchEntity> query) {
/*List<String> toListIds = query.stream().map(chatData -> JSONObject.parseObject(JSONObject.toJSONString(chatData.getData())))
.filter(chatData -> CollectionUtil.isNotEmpty(chatData.getJSONArray("tolist")))
.map(chatData -> chatData.getJSONArray("tolist"))
.map(item -> JSONArray.parseArray(item.toJSONString(), String.class))
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toCollection(ArrayList::new));*/
query.stream().forEach(elasticSearchEntity -> {
//发送人映射数据
WeChatContactMapping fromWeChatContactMapping = new WeChatContactMapping();
//接收人映射数据
WeChatContactMapping reveiceWeChatContactMapping = new WeChatContactMapping();
JSONObject chatData = JSONObject.parseObject(JSONObject.toJSONString(elasticSearchEntity.getData()));
int fromType = StringUtils.weCustomTypeJudgment(chatData.getString("from"));
fromWeChatContactMapping.setFromId(chatData.getString("from"));
getFromUserOrCustomInfo(fromWeChatContactMapping, chatData.getString("from"), fromType);
getReveiceUserOrCustomInfo(reveiceWeChatContactMapping, chatData.getString("from"), fromType);
JSONArray tolist = chatData.getJSONArray("tolist");
if (CollectionUtil.isNotEmpty(tolist)) {
//如果是单聊,tolist唯一,只有一个接收人,并判断接收人的类型
if (StringUtils.isEmpty(chatData.getString("roomid"))) {
String idStr = (String) tolist.get(0);
int reveiceType = StringUtils.weCustomTypeJudgment(idStr);
getFromUserOrCustomInfo(reveiceWeChatContactMapping, idStr, reveiceType);
getReveiceUserOrCustomInfo(fromWeChatContactMapping, idStr, fromType);
}else {
fromWeChatContactMapping.setRoomId(chatData.getString("roomid"));
}
}
});
}
/**
* 获取发送人成员或者客户详情
*
* @param id
* @return
*/
private WeChatContactMapping getFromUserOrCustomInfo(WeChatContactMapping weChatContactMapping, String id, int type) {
if (WeConstans.ID_TYPE_USER.equals(type)) {
WeUser weUser = weUserMapper.selectOne(new LambdaQueryWrapper<WeUser>().eq(WeUser::getUserId, id));
weChatContactMapping.setFromAvatar(weUser.getAvatarMediaid());
weChatContactMapping.setFromName(weUser.getName());
weChatContactMapping.setFromGender(weUser.getGender());
} else if (WeConstans.ID_TYPE_EX.equals(type)) {
WeCustomer weCustomer = weCustomerMapper.selectWeCustomerById(id);
weChatContactMapping.setFromAvatar(weCustomer.getAvatar());
weChatContactMapping.setFromName(weCustomer.getName());
weChatContactMapping.setFromGender(weCustomer.getGender());
} else if (WeConstans.ID_TYPE_MACHINE.equals(type)) {
}
return weChatContactMapping;
}
/**
* 获取接收人成员或者客户详情
*
* @param id
* @return
*/
private WeChatContactMapping getReveiceUserOrCustomInfo(WeChatContactMapping weChatContactMapping, String id, int type) {
if (WeConstans.ID_TYPE_USER.equals(type)) {
WeUser weUser = weUserMapper.selectOne(new LambdaQueryWrapper<WeUser>().eq(WeUser::getUserId, id));
weChatContactMapping.setReviceAvatar(weUser.getAvatarMediaid());
weChatContactMapping.setReviceName(weUser.getName());
weChatContactMapping.setReviceGender(weUser.getGender());
} else if (WeConstans.ID_TYPE_EX.equals(type)) {
WeCustomer weCustomer = weCustomerMapper.selectWeCustomerById(id);
weChatContactMapping.setReviceAvatar(weCustomer.getAvatar());
weChatContactMapping.setReviceName(weCustomer.getName());
weChatContactMapping.setReviceGender(weCustomer.getGender());
} else if (WeConstans.ID_TYPE_MACHINE.equals(type)) {
}
return weChatContactMapping;
}
}
......@@ -13,6 +13,12 @@ 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.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author sxw
* @description 会话存档业务实现类
......@@ -31,16 +37,21 @@ public class WeConversationArchiveServiceImpl implements IWeConversationArchiveS
* @return
*/
@Override
public PageInfo<JSONObject> getInternalContactList(String userId, int pageSize, int pageNum) {
public List<JSONObject> getInternalContactList(String userId) {
SearchSourceBuilder builder = new SearchSourceBuilder();
int from = (pageSize - 1) * pageNum;
builder.size(pageNum);
builder.from(from);
builder.size(1000);
builder.sort("msgtime", SortOrder.ASC);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termsQuery("tolist.keyword", userId))
.should(QueryBuilders.termQuery("tolist.keyword", userId))
.should(QueryBuilders.termQuery("from",userId))
.minimumShouldMatch(1)
.must(QueryBuilders.termQuery("type",1))
.must(QueryBuilders.termQuery("roomid", ""));
builder.query(boolQueryBuilder);
return elasticSearch.searchPage(WeConstans.WECOM_FINANCE_INDEX, builder, pageNum, pageSize, JSONObject.class);
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;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.linkwechat.wecom.mapper.WeChatContactMappingMapper">
<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="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
</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="roomId != null and roomId != ''"> and room_id = #{roomId}</if>
<if test="isCustom != null "> and is_custom = #{isCustom}</if>
</where>
</select>
<select id="selectWeChatContactMappingById" parameterType="Long" resultMap="WeChatContactMappingResult">
<include refid="selectWeChatContactMappingVo"/>
where id = #{id}
</select>
<insert id="insertWeChatContactMapping" parameterType="WeChatContactMapping" useGeneratedKeys="true" keyProperty="id">
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="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="roomId != null">#{roomId},</if>
<if test="isCustom != null">#{isCustom},</if>
</trim>
</insert>
<update id="updateWeChatContactMapping" parameterType="WeChatContactMapping">
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="roomId != null">room_id = #{roomId},</if>
<if test="isCustom != null">is_custom = #{isCustom},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteWeChatContactMappingById" parameterType="Long">
delete from we_chat_contact_mapping where id = #{id}
</delete>
<delete id="deleteWeChatContactMappingByIds" parameterType="String">
delete from we_chat_contact_mapping where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!