|
|
@@ -1,6 +1,7 @@
|
|
|
package com.cyksj.service.corp.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
@@ -19,18 +20,24 @@ import com.cyksj.model.entity.*;
|
|
|
import com.cyksj.model.request.corp.Attachments;
|
|
|
import com.cyksj.model.request.corp.CorpXmlMessage;
|
|
|
import com.cyksj.model.request.corp.CorpXmlOutMessage;
|
|
|
-import com.cyksj.model.response.corp.CorpExternalContactInfo;
|
|
|
-import com.cyksj.model.response.corp.CorpUserExternalTagGroupList;
|
|
|
import com.cyksj.model.response.ExternalContact;
|
|
|
import com.cyksj.model.response.FollowedUser;
|
|
|
+import com.cyksj.model.response.corp.*;
|
|
|
+import com.cyksj.model.views.CorpUserSearchView;
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
import com.cyksj.service.corp.WxCorpOps;
|
|
|
import com.cyksj.service.corp.msg.CorpEventActionService;
|
|
|
import com.cyksj.service.mange.coupon.CouponCommonService;
|
|
|
import com.cyksj.service.market.MarketFrontService;
|
|
|
import com.cyksj.service.relation.GroupRelationFrontService;
|
|
|
import com.cyksj.service.sys.SysConfigService;
|
|
|
+import com.ejlchina.searcher.BeanSearcher;
|
|
|
+import com.ejlchina.searcher.param.Operator;
|
|
|
+import com.ejlchina.searcher.util.MapUtils;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -81,6 +88,16 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
|
|
|
private final CouponCommonService couponCommonService;
|
|
|
|
|
|
+ private final WxCorpAppMapper wxCorpAppMapper;
|
|
|
+
|
|
|
+ private final CorpGroupChatMapper corpGroupChatMapper;
|
|
|
+
|
|
|
+ private final RedisService redisService;
|
|
|
+
|
|
|
+ private final CorpGroupChatMemberMapper corpGroupChatMemberMapper;
|
|
|
+
|
|
|
+ private final BeanSearcher beanSearcher;
|
|
|
+
|
|
|
private static final String DEFAULT_MSG = "您好,欢迎加入银河录像局";
|
|
|
|
|
|
/**
|
|
|
@@ -286,6 +303,196 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public CorpXmlOutMessage saveCorpGroupChat(CorpXmlMessage message, Map<String, Object> context) throws Exception {
|
|
|
+ String corpId = message.getToUserName();
|
|
|
+ WxCorpApp wxCorpApp = wxCorpAppMapper.selectOne(Wrappers.lambdaQuery(WxCorpApp.class)
|
|
|
+ .eq(WxCorpApp::getCorpId, corpId).last("limit 1"));
|
|
|
+ if (wxCorpApp == null) {
|
|
|
+ log.error("企业微信:{}配置不存在", corpId);
|
|
|
+ }
|
|
|
+ if (initCorpGroupsChat(wxCorpApp)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ saveCorpGroupChatDetail(wxCorpApp, message);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化企业微信客户群
|
|
|
+ */
|
|
|
+ public Boolean initCorpGroupsChat(WxCorpApp wxCorpApp) {
|
|
|
+ List<CorpGroupChat> corpGroupChats = corpGroupChatMapper.selectList(null);
|
|
|
+ if (corpGroupChats.isEmpty()) {
|
|
|
+ String key = RedisService.key.CORP_GROUP_CHAT_LIST.getNameFormat(wxCorpApp.getCorpId());
|
|
|
+ if (redisService.setNx(key, wxCorpApp.getCorpId(), 60 * 60 * 24l)) {
|
|
|
+ THREAD_POOL.execute(() -> {
|
|
|
+ try {
|
|
|
+ getChatGroupChatListDetail(wxCorpApp, "", 100);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ redisService.del(key);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取企业客户群列表信息
|
|
|
+ */
|
|
|
+ private void getChatGroupChatListDetail(WxCorpApp wxCorpApp, String cursor, Integer limit) throws Exception {
|
|
|
+ String corpId = wxCorpApp.getCorpId();
|
|
|
+ CorpGroupChatListDetail list = wxCorpOps.getGroupChatList(corpId, cursor, limit);
|
|
|
+ predicateOrSaveCorpGroupChat(wxCorpApp,list);
|
|
|
+ if (StrUtil.isNotEmpty(list.getNextCursor())) {
|
|
|
+ getChatGroupChatListDetail(wxCorpApp, list.getNextCursor(), limit);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void predicateOrSaveCorpGroupChat(WxCorpApp wxCorpApp, CorpGroupChatListDetail list) {
|
|
|
+ list.getGroupChatList().stream().forEach(obj -> {
|
|
|
+ CorpXmlMessage message = new CorpXmlMessage();
|
|
|
+ message.setChatId(obj.getChatId());
|
|
|
+ try {
|
|
|
+ saveCorpGroupChatDetail(wxCorpApp, message);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveCorpGroupChatDetail(WxCorpApp wxCorpApp, CorpXmlMessage message) throws Exception {
|
|
|
+ String corpId = wxCorpApp.getCorpId();
|
|
|
+ CorpGroupChatDetail detail = wxCorpOps.getCorpGroupChatDetail(corpId, message);
|
|
|
+ //企业客户群信息
|
|
|
+ CorpGroupChat corpGroupChat = CorpGroupChat.builder()
|
|
|
+ .chatId(detail.getChatId())
|
|
|
+ .chatName(detail.getName())
|
|
|
+ .wxCorpId(wxCorpApp.getId())
|
|
|
+ .corpId(corpId)
|
|
|
+ .owner(detail.getOwner())
|
|
|
+ .groupCreateTime(DateTime.of(detail.getCreateTime() * 1000))
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ //企业客户群
|
|
|
+ corpGroupChatMapper.insert(corpGroupChat);
|
|
|
+ final Long groupChatId =corpGroupChat.getId();
|
|
|
+ //企业客户群成员信息
|
|
|
+ List<CorpGroupChatMemberObj> memberList = detail.getMemberList();
|
|
|
+ corpGroupSyncYhUserId(memberList, wxCorpApp.getId(), groupChatId);
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CorpXmlOutMessage updateCorpGroupChat(CorpXmlMessage message, Map<String, Object> context) throws Exception {
|
|
|
+ String corpId = message.getToUserName();
|
|
|
+ WxCorpApp wxCorpApp = wxCorpAppMapper.selectOne(Wrappers.lambdaQuery(WxCorpApp.class)
|
|
|
+ .eq(WxCorpApp::getCorpId, corpId).last("limit 1"));
|
|
|
+ if (wxCorpApp == null) {
|
|
|
+ log.error("企业微信:{}配置不存在", corpId);
|
|
|
+ }
|
|
|
+ if (initCorpGroupsChat(wxCorpApp)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ CorpGroupChatDetail detail = wxCorpOps.getCorpGroupChatDetail(corpId, message);
|
|
|
+
|
|
|
+ THREAD_POOL.execute(()->{
|
|
|
+ List<String> corpGroupEvent = List.of("change_owner", "change_name");
|
|
|
+ if (corpGroupEvent.contains(message.getUpdateDetail())) {
|
|
|
+ CorpGroupChat entity = corpGroupChatMapper.selectOne(Wrappers.lambdaQuery(CorpGroupChat.class)
|
|
|
+ .eq(CorpGroupChat::getWxCorpId, wxCorpApp.getId())
|
|
|
+ .eq(CorpGroupChat::getChatId, message.getChatId()).last("limit 1"));
|
|
|
+ if ("change_owner".equals(message.getUpdateDetail())) {
|
|
|
+ log.info("{}群主变更事件", message.getChatId());
|
|
|
+ entity.setOwner(detail.getOwner());
|
|
|
+ }
|
|
|
+ if ("change_name".equals(message.getUpdateDetail())) {
|
|
|
+ log.info("{}群名修改事件", message.getChatId());
|
|
|
+ entity.setChatName(detail.getName());
|
|
|
+ }
|
|
|
+ corpGroupChatMapper.updateById(entity);
|
|
|
+ }
|
|
|
+ List<String> memberEvent = List.of("del_member", "add_member");
|
|
|
+ if (memberEvent.contains(message.getUpdateDetail())) {
|
|
|
+ CorpGroupChat corpGroupChat = corpGroupChatMapper.selectOne(Wrappers.lambdaQuery(CorpGroupChat.class)
|
|
|
+ .eq(CorpGroupChat::getWxCorpId, wxCorpApp.getId())
|
|
|
+ .eq(CorpGroupChat::getChatId, detail.getChatId()).last("limit 1"));
|
|
|
+ if (corpGroupChat == null) {
|
|
|
+ log.info("企业微信客户群:{} 库中不存在", detail.getChatId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<String> userList = corpGroupChatMemberMapper.getAllMemberByCorpIdAndChatId(corpGroupChat.getId());
|
|
|
+ if ("del_member".equals(message.getUpdateDetail())) {
|
|
|
+ log.info("客户退群{}事件", message.getChatId());
|
|
|
+ //存在库里面没有群成员
|
|
|
+ List<String> collect = detail.getMemberList().stream().map(CorpGroupChatMemberObj::getUserId).collect(Collectors.toList());
|
|
|
+ userList.removeAll(collect);
|
|
|
+ Lists.partition(userList, 100).stream().forEach(objs -> corpGroupChatMemberMapper.batchDeleteByChatIdAndUserId(corpGroupChat.getId(), objs));
|
|
|
+ }
|
|
|
+ if ("add_member".equals(message.getUpdateDetail())) {
|
|
|
+ log.info("客户入群{}事件", message.getChatId());
|
|
|
+ List<CorpGroupChatMemberObj> addCollect = detail.getMemberList().stream().filter(obj -> !userList.contains(obj.getUserId())).collect(Collectors.toList());
|
|
|
+ corpGroupSyncYhUserId(addCollect, wxCorpApp.getId(), corpGroupChat.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void corpGroupSyncYhUserId(List<CorpGroupChatMemberObj> memberList, Long wxCorpId, Long groupChatId) {
|
|
|
+ Lists.partition(memberList, 100).forEach(members -> {
|
|
|
+ List<String> corpUserIds = members.stream().map(CorpGroupChatMemberObj::getUserId).collect(Collectors.toList());
|
|
|
+ Map<String, List<CorpUserSearchView>> corpUserIdMap = beanSearcher.searchAll(CorpUserSearchView.class, MapUtils.builder()
|
|
|
+ .field(CorpUserSearchView::getWxCorpId, wxCorpId)
|
|
|
+ .field(CorpUserSearchView::getCorpUserId, corpUserIds).op(Operator.InList).build())
|
|
|
+ .stream().collect(Collectors.groupingBy(CorpUserSearchView::getCorpUserId));
|
|
|
+ members.forEach(data -> {
|
|
|
+ List<CorpUserSearchView> corpUserSearchViews = corpUserIdMap.get(data.getUserId());
|
|
|
+ Long yhUserId = null;
|
|
|
+ if (CollUtil.isNotEmpty(corpUserSearchViews)) {
|
|
|
+ yhUserId = corpUserSearchViews.get(0).getUserId();
|
|
|
+ }
|
|
|
+ if (yhUserId.intValue() == 0) {
|
|
|
+ yhUserId = corpUserMapper.selectUserIdByCropUserId(wxCorpId, data.getUserId());
|
|
|
+ }
|
|
|
+ data.setYhUserId(yhUserId);
|
|
|
+ });
|
|
|
+ corpGroupChatMemberMapper.batchInsert(groupChatId, wxCorpId, members);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CorpXmlOutMessage delCorpGroupChat(CorpXmlMessage message) {
|
|
|
+ //删除企业客户群及关联关系
|
|
|
+ String corpId = message.getToUserName();
|
|
|
+ WxCorpApp wxCorpApp = wxCorpAppMapper.selectOne(Wrappers.lambdaQuery(WxCorpApp.class)
|
|
|
+ .eq(WxCorpApp::getCorpId, corpId).last("limit 1"));
|
|
|
+ if (wxCorpApp == null) {
|
|
|
+ log.error("企业客服群删除,企业微信配置:{}不存在", corpId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ corpGroupChatMapper.update(null, Wrappers.lambdaUpdate(CorpGroupChat.class)
|
|
|
+ .set(CorpGroupChat::getDeleted, false)
|
|
|
+ .eq(CorpGroupChat::getWxCorpId, wxCorpApp.getId())
|
|
|
+ .eq(CorpGroupChat::getChatId, message.getChatId()));
|
|
|
+
|
|
|
+ CorpGroupChat corpGroupChat = corpGroupChatMapper.selectOne(Wrappers.lambdaQuery(CorpGroupChat.class)
|
|
|
+ .eq(CorpGroupChat::getChatId, message.getChatId())
|
|
|
+ .eq(CorpGroupChat::getWxCorpId, wxCorpApp.getId()));
|
|
|
+ if (corpGroupChat != null) {
|
|
|
+ corpGroupChat.setDeleted(false);
|
|
|
+ corpGroupChatMapper.updateById(corpGroupChat);
|
|
|
+
|
|
|
+ corpGroupChatMemberMapper.update(null, Wrappers.lambdaUpdate(CorpGroupChatMember.class)
|
|
|
+ .set(CorpGroupChatMember::getDeleted, false)
|
|
|
+ .eq(CorpGroupChatMember::getGroupChatId, corpGroupChat.getId()));
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 修改客户成员关系,客户标签
|
|
|
*/
|