Commit 8317ef6a by danmo

客户群异步同步操作

1 parent b8c7d23e
...@@ -11,6 +11,8 @@ import com.linkwechat.wecom.service.IWeGroupMemberService; ...@@ -11,6 +11,8 @@ import com.linkwechat.wecom.service.IWeGroupMemberService;
import com.linkwechat.wecom.service.IWeGroupService; import com.linkwechat.wecom.service.IWeGroupService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -59,10 +61,14 @@ public class WeGroupController extends BaseController { ...@@ -59,10 +61,14 @@ public class WeGroupController extends BaseController {
@PreAuthorize("@ss.hasPermi('customerManage:group:sync')") @PreAuthorize("@ss.hasPermi('customerManage:group:sync')")
@GetMapping({"/synchWeGroup"}) @GetMapping({"/synchWeGroup"})
public AjaxResult synchWeGroup(){ public AjaxResult synchWeGroup(){
try {
SecurityContext context = SecurityContextHolder.getContext();
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
SecurityContextHolder.setContext(context);
weGroupService.synchWeGroup(); weGroupService.synchWeGroup();
} catch (Exception e) {
e.printStackTrace();
}
return AjaxResult.success(WeConstans.SYNCH_TIP); return AjaxResult.success(WeConstans.SYNCH_TIP);
} }
......
...@@ -2,8 +2,6 @@ package com.linkwechat.wecom.service.impl; ...@@ -2,8 +2,6 @@ package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkwechat.common.constant.WeConstans; import com.linkwechat.common.constant.WeConstans;
...@@ -14,11 +12,16 @@ import com.linkwechat.wecom.domain.WeAllocateGroup; ...@@ -14,11 +12,16 @@ import com.linkwechat.wecom.domain.WeAllocateGroup;
import com.linkwechat.wecom.domain.WeGroup; import com.linkwechat.wecom.domain.WeGroup;
import com.linkwechat.wecom.domain.WeGroupMember; import com.linkwechat.wecom.domain.WeGroupMember;
import com.linkwechat.wecom.domain.dto.AllocateWeGroupDto; import com.linkwechat.wecom.domain.dto.AllocateWeGroupDto;
import com.linkwechat.wecom.domain.dto.customer.*; import com.linkwechat.wecom.domain.dto.customer.CustomerGroupDetail;
import com.linkwechat.wecom.domain.dto.customer.CustomerGroupList;
import com.linkwechat.wecom.domain.dto.customer.CustomerGroupMember;
import com.linkwechat.wecom.domain.vo.WeLeaveUserInfoAllocateVo; import com.linkwechat.wecom.domain.vo.WeLeaveUserInfoAllocateVo;
import com.linkwechat.wecom.mapper.WeGroupMapper; import com.linkwechat.wecom.mapper.WeGroupMapper;
import com.linkwechat.wecom.service.*; import com.linkwechat.wecom.service.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -33,8 +36,7 @@ import java.util.stream.Collectors; ...@@ -33,8 +36,7 @@ import java.util.stream.Collectors;
* @Date: create in 2020/9/22 0022 0:03 * @Date: create in 2020/9/22 0022 0:03
*/ */
@Service @Service
public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> implements IWeGroupService { public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper, WeGroup> implements IWeGroupService {
@Autowired @Autowired
...@@ -69,6 +71,7 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple ...@@ -69,6 +71,7 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple
/** /**
* 离职员工群分配 * 离职员工群分配
*
* @param weLeaveUserInfoAllocateVo * @param weLeaveUserInfoAllocateVo
* @return * @return
*/ */
...@@ -78,10 +81,10 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple ...@@ -78,10 +81,10 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple
List<WeGroup> weGroups List<WeGroup> weGroups
= this.list(new LambdaQueryWrapper<WeGroup>().eq(WeGroup::getOwner, weLeaveUserInfoAllocateVo.getHandoverUserid())); = this.list(new LambdaQueryWrapper<WeGroup>().eq(WeGroup::getOwner, weLeaveUserInfoAllocateVo.getHandoverUserid()));
if(CollectionUtil.isNotEmpty(weGroups)){ if (CollectionUtil.isNotEmpty(weGroups)) {
List<WeAllocateGroup> weAllocateGroups=new ArrayList<>(); List<WeAllocateGroup> weAllocateGroups = new ArrayList<>();
//更改本地群主 //更改本地群主
weGroups.stream().forEach(k->{ weGroups.stream().forEach(k -> {
k.setOwner(weLeaveUserInfoAllocateVo.getTakeoverUserid()); k.setOwner(weLeaveUserInfoAllocateVo.getTakeoverUserid());
weAllocateGroups.add(WeAllocateGroup.builder() weAllocateGroups.add(WeAllocateGroup.builder()
.allocateTime(new Date()) .allocateTime(new Date())
...@@ -92,13 +95,13 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple ...@@ -92,13 +95,13 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple
}); });
this.saveOrUpdateBatch(weGroups); this.saveOrUpdateBatch(weGroups);
//分配记录保存 //分配记录保存
if(CollectionUtil.isNotEmpty(weAllocateGroups)){ if (CollectionUtil.isNotEmpty(weAllocateGroups)) {
if(iWeAllocateGroupService.saveOrUpdateBatch(weAllocateGroups)){ if (iWeAllocateGroupService.saveOrUpdateBatch(weAllocateGroups)) {
//同步企业微信端 //同步企业微信端
weUserClient.allocateGroup( weUserClient.allocateGroup(
AllocateWeGroupDto.builder() AllocateWeGroupDto.builder()
.chat_id_list(ArrayUtil.toArray(weAllocateGroups.stream().map(WeAllocateGroup::getChatId).collect(Collectors.toList()),String.class)) .chat_id_list(ArrayUtil.toArray(weAllocateGroups.stream().map(WeAllocateGroup::getChatId).collect(Collectors.toList()), String.class))
.new_owner(weLeaveUserInfoAllocateVo.getTakeoverUserid()) .new_owner(weLeaveUserInfoAllocateVo.getTakeoverUserid())
.build() .build()
); );
...@@ -114,27 +117,28 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple ...@@ -114,27 +117,28 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple
* 客户群同步 * 客户群同步
*/ */
@Override @Override
@Async
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void synchWeGroup() { public void synchWeGroup() {
CustomerGroupList customerGroupList = CustomerGroupList customerGroupList =
weCustomerGroupClient.groupChatLists(new CustomerGroupList().new Params()); weCustomerGroupClient.groupChatLists(new CustomerGroupList().new Params());
if(customerGroupList.getErrcode().equals(WeConstans.WE_SUCCESS_CODE) if (customerGroupList.getErrcode().equals(WeConstans.WE_SUCCESS_CODE)
&& CollectionUtil.isNotEmpty(customerGroupList.getGroup_chat_list())){ && CollectionUtil.isNotEmpty(customerGroupList.getGroup_chat_list())) {
List<WeGroup> weGroups=new ArrayList<>(); List<WeGroup> weGroups = new ArrayList<>();
List<WeGroupMember> weGroupMembers=new ArrayList<>(); List<WeGroupMember> weGroupMembers = new ArrayList<>();
customerGroupList.getGroup_chat_list().stream().forEach(k->{ customerGroupList.getGroup_chat_list().stream().forEach(k -> {
CustomerGroupDetail customerGroupDetail = weCustomerGroupClient.groupChatDetail( CustomerGroupDetail customerGroupDetail = weCustomerGroupClient.groupChatDetail(
new CustomerGroupDetail().new Params(k.getChat_id()) new CustomerGroupDetail().new Params(k.getChat_id())
); );
if(customerGroupDetail.getErrcode().equals(WeConstans.WE_SUCCESS_CODE) if (customerGroupDetail.getErrcode().equals(WeConstans.WE_SUCCESS_CODE)
&& CollectionUtil.isNotEmpty(customerGroupDetail.getGroup_chat())){ && CollectionUtil.isNotEmpty(customerGroupDetail.getGroup_chat())) {
customerGroupDetail.getGroup_chat().stream().forEach(kk->{ customerGroupDetail.getGroup_chat().stream().forEach(kk -> {
weGroups.add( weGroups.add(
WeGroup.builder() WeGroup.builder()
.chatId(kk.getChat_id()) .chatId(kk.getChat_id())
...@@ -146,8 +150,8 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple ...@@ -146,8 +150,8 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple
.build() .build()
); );
List<CustomerGroupMember> memberLists = kk.getMember_list(); List<CustomerGroupMember> memberLists = kk.getMember_list();
if(CollectionUtil.isNotEmpty(memberLists)){ if (CollectionUtil.isNotEmpty(memberLists)) {
memberLists.stream().forEach(member->{ memberLists.stream().forEach(member -> {
//unionid不为空,获取非好友客户相关消息 //unionid不为空,获取非好友客户相关消息
...@@ -171,8 +175,6 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple ...@@ -171,8 +175,6 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple
// } // }
// //
// } // }
weGroupMembers.add( weGroupMembers.add(
WeGroupMember.builder() WeGroupMember.builder()
.chatId(kk.getChat_id()) .chatId(kk.getChat_id())
...@@ -192,17 +194,33 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple ...@@ -192,17 +194,33 @@ public class WeGroupServiceImpl extends ServiceImpl<WeGroupMapper,WeGroup> imple
} }
}); });
/**
* 此处不同步成员和客户理由
* 1、如果是存量同步,不存在新客户新成员问题
* 2、增量同步的,成员和客户增删改群变更都会走回调通知,不用考虑新增成员和客户回调通知没有处理问题
*/
//同步组织架构成员 //同步组织架构成员
weUserService.synchWeUser(); //weUserService.synchWeUser();
//同步客户 //同步客户
weCustomerService.synchWeCustomer(); //weCustomerService.synchWeCustomer();
this.saveOrUpdateBatch(weGroups); this.saveOrUpdateBatch(weGroups);
iWeGroupMemberService.remove(new LambdaQueryWrapper<WeGroupMember>().in(WeGroupMember::getChatId, List<WeGroupMember> weGroupMemberList = iWeGroupMemberService.list(new LambdaQueryWrapper<WeGroupMember>().in(WeGroupMember::getChatId,
weGroups.stream().map(WeGroup::getChatId).collect(Collectors.toList()))); weGroups.stream().map(WeGroup::getChatId).collect(Collectors.toList())));
iWeGroupMemberService.saveOrUpdateBatch(weGroupMembers); //存量去重
if (CollectionUtil.isNotEmpty(weGroupMemberList)){
List<WeGroupMember> groupMemberList = weGroupMembers.stream().filter(e -> {
for (WeGroupMember weGroupMember : weGroupMemberList) {
if (e.getUserId().equals(weGroupMember.getUserId())) {
return false;
}
}
return true;
}).collect(Collectors.toList());
iWeGroupMemberService.saveBatch(groupMemberList);
}else {
iWeGroupMemberService.saveBatch(weGroupMembers);
}
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!