Commit 8317ef6a by danmo

客户群异步同步操作

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