diff --git a/src/main/java/com/kiisoo/ic/customer/service/CustomerViewService.java b/src/main/java/com/kiisoo/ic/customer/service/CustomerViewService.java index 9a237b4..1e0f077 100644 --- a/src/main/java/com/kiisoo/ic/customer/service/CustomerViewService.java +++ b/src/main/java/com/kiisoo/ic/customer/service/CustomerViewService.java @@ -17,17 +17,26 @@ import com.kiisoo.ic.generalize.mapper.PoiCustomerContactDataStatMapper; import com.kiisoo.ic.generalize.mapper.RetailCompanyMapper; import com.kiisoo.ic.generalize.service.RetailCompanyService; import com.kiisoo.ic.store.entity.PoiStore; +import com.kiisoo.ic.store.entity.PoiStoreStaff; +import com.kiisoo.ic.store.entity.PrivilageCpUserStoreDO; import com.kiisoo.ic.store.mapper.PoiStoreDOMapper; +import com.kiisoo.ic.synchronous.entity.TurnBackDTO; +import com.kiisoo.ic.synchronous.entity.WxDataDTO; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.cp.api.WxCpExternalContactService; import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.bean.WxCpUserExternalContactInfo; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -541,7 +550,10 @@ public class CustomerViewService { return result; } - public void syncAllCount() throws WxErrorException { + public void syncAllCount() throws WxErrorException, InterruptedException { + //初始化线程 + ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(8); + //查询现有企业微信账号 List privilageCpUserDOS = privilageCpUserDOMapper.selectList(null); Map cpUserIdMap = new HashMap<>(); @@ -552,78 +564,131 @@ public class CustomerViewService { WxCpExternalContactService externalContactService = wxCpService.getExternalContactService(); //查询企业微信已配置联系我的用户 List cpUserIds = externalContactService.listFollowUser(); - for (String cpUserId : cpUserIds) { - List customerStrs; - try { - //获取客户联系人 - customerStrs = externalContactService.listExternalContacts(cpUserId); - }catch (Exception e){ + + if (CollectionUtils.isNotEmpty(cpUserIds)){ + //分组 + final List> afterPageList = com.kiisoo.ic.utils.CollectionUtils.portListByQuantity(cpUserIds, 100); + //定义线程池长度 + int threadPoolSize = afterPageList.size(); + //定义countDownLatch + final CountDownLatch countDownLatch = new CountDownLatch(threadPoolSize); + for (int i = 0; i < afterPageList.size(); i++) { + List ids = afterPageList.get(i); + newFixedThreadPool.execute(new Runnable() { + @Override + public void run() { + try { + for (String cpUserId : ids) { + List customerStrs; + try { + //获取客户联系人 + customerStrs = externalContactService.listExternalContacts(cpUserId); + }catch (Exception e){ + PrivilageCpUserDO privilageCpUserDO = cpUserIdMap.get(cpUserId); + if (privilageCpUserDO != null){ + privilageCpUserDO.setStatus(9); + privilageCpUserDOMapper.updateById(privilageCpUserDO); + } + continue; + } + if (CollectionUtils.isEmpty(customerStrs)){ + continue; + } + PrivilageCpUserDO privilageCpUserDO = cpUserIdMap.get(cpUserId); + if (privilageCpUserDO == null){ + //企业微信账号不存在,创建一个账号 + privilageCpUserDO = new PrivilageCpUserDO(); + privilageCpUserDO.setCpUserId(cpUserId); + privilageCpUserDO.setStatus(1); + privilageCpUserDOMapper.insert(privilageCpUserDO); + } + //批量添加账号 + for (String customerStr : customerStrs) { + CpUserCustomerRelationDO cpUserCustomerRelationDO = privilageCpUserDOMapper.selectCpUserCustomerRelation(customerStr, cpUserId, privilageCpUserDO.getId()); + if (cpUserCustomerRelationDO == null){ + privilageCpUserDOMapper.insertCpUserCustomerRelation(customerStr,cpUserId,privilageCpUserDO.getId()); + } + } + //移除已经请求过的数据库内账号 + cpUserIdMap.remove(cpUserId); + } + } catch (Exception e) { + log.error("同步客户明细", e); + } finally { + //每次减去一,避免死锁 + countDownLatch.countDown(); + } + } + }); + } + countDownLatch.await(); + Set removeCpUserIds = cpUserIdMap.keySet(); + for (String cpUserId:removeCpUserIds){ PrivilageCpUserDO privilageCpUserDO = cpUserIdMap.get(cpUserId); if (privilageCpUserDO != null){ - privilageCpUserDO.setStatus(9); - privilageCpUserDOMapper.updateById(privilageCpUserDO); - } - continue; - } - if (CollectionUtils.isEmpty(customerStrs)){ - continue; - } - PrivilageCpUserDO privilageCpUserDO = cpUserIdMap.get(cpUserId); - if (privilageCpUserDO == null){ - //企业微信账号不存在,创建一个账号 - privilageCpUserDO = new PrivilageCpUserDO(); - privilageCpUserDO.setCpUserId(cpUserId); - privilageCpUserDO.setStatus(1); - privilageCpUserDOMapper.insert(privilageCpUserDO); - } - //批量添加账号 - for (String customerStr : customerStrs) { - CpUserCustomerRelationDO cpUserCustomerRelationDO = privilageCpUserDOMapper.selectCpUserCustomerRelation(customerStr, cpUserId, privilageCpUserDO.getId()); - if (cpUserCustomerRelationDO == null){ - privilageCpUserDOMapper.insertCpUserCustomerRelation(customerStr,cpUserId,privilageCpUserDO.getId()); + //删除 + privilageCpUserDOMapper.deleteById(privilageCpUserDO.getId()); } } - //移除已经请求过的数据库内账号 - cpUserIdMap.remove(cpUserId); - } - Set removeCpUserIds = cpUserIdMap.keySet(); - for (String cpUserId:removeCpUserIds){ - PrivilageCpUserDO privilageCpUserDO = cpUserIdMap.get(cpUserId); - if (privilageCpUserDO != null){ - //删除 - privilageCpUserDOMapper.deleteById(privilageCpUserDO.getId()); - } } } - public void syncAliveCount() throws WxErrorException { + public void syncAliveCount() throws WxErrorException, InterruptedException { + //初始化线程 + ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(8); + //查询现有企业微信账号 QueryWrapper qw = new QueryWrapper<>(); qw.ne("status",9); List privilageCpUserDOS = privilageCpUserDOMapper.selectList(null); WxCpService wxCpService = WxCpConfiguration.getCpService(APPLICATIONID); WxCpExternalContactService externalContactService = wxCpService.getExternalContactService(); - //查询企业微信已配置联系我的用户 - for (PrivilageCpUserDO privilageCpUserDO : privilageCpUserDOS) { - List customerStrs; - try { - //获取客户联系人 - customerStrs = externalContactService.listExternalContacts(privilageCpUserDO.getCpUserId()); - }catch (Exception e){ - privilageCpUserDO.setStatus(9); - privilageCpUserDOMapper.updateById(privilageCpUserDO); - continue; - } - if (CollectionUtils.isEmpty(customerStrs)){ - continue; - } - //批量添加账号 - for (String customerStr : customerStrs) { - CpUserCustomerRelationDO cpUserCustomerRelationDO = privilageCpUserDOMapper.selectCpUserCustomerRelation(customerStr, privilageCpUserDO.getCpUserId(), privilageCpUserDO.getId()); - if (cpUserCustomerRelationDO == null){ - privilageCpUserDOMapper.insertCpUserCustomerRelation(customerStr,privilageCpUserDO.getCpUserId(),privilageCpUserDO.getId()); - } + + if (CollectionUtils.isNotEmpty(privilageCpUserDOS)){ + //分组 + final List> afterPageList = com.kiisoo.ic.utils.CollectionUtils.portListByQuantity(privilageCpUserDOS, 100); + //定义线程池长度 + int threadPoolSize = afterPageList.size(); + //定义countDownLatch + final CountDownLatch countDownLatch = new CountDownLatch(threadPoolSize); + for (int i = 0; i < afterPageList.size(); i++) { + List cpUserDOS = afterPageList.get(i); + newFixedThreadPool.execute(new Runnable() { + @Override + public void run() { + try { + //查询企业微信已配置联系我的用户 + for (PrivilageCpUserDO privilageCpUserDO : cpUserDOS) { + List customerStrs; + try { + //获取客户联系人 + customerStrs = externalContactService.listExternalContacts(privilageCpUserDO.getCpUserId()); + }catch (Exception e){ + privilageCpUserDO.setStatus(9); + privilageCpUserDOMapper.updateById(privilageCpUserDO); + continue; + } + if (CollectionUtils.isEmpty(customerStrs)){ + continue; + } + //批量添加账号 + for (String customerStr : customerStrs) { + CpUserCustomerRelationDO cpUserCustomerRelationDO = privilageCpUserDOMapper.selectCpUserCustomerRelation(customerStr, privilageCpUserDO.getCpUserId(), privilageCpUserDO.getId()); + if (cpUserCustomerRelationDO == null){ + privilageCpUserDOMapper.insertCpUserCustomerRelation(customerStr,privilageCpUserDO.getCpUserId(),privilageCpUserDO.getId()); + } + } + } + } catch (Exception e) { + log.error("同步客户明细", e); + } finally { + //每次减去一,避免死锁 + countDownLatch.countDown(); + } + } + }); } + countDownLatch.await(); } } } diff --git a/src/main/java/com/kiisoo/ic/employee/service/EmployeeService.java b/src/main/java/com/kiisoo/ic/employee/service/EmployeeService.java index 9437a28..6bbfb88 100644 --- a/src/main/java/com/kiisoo/ic/employee/service/EmployeeService.java +++ b/src/main/java/com/kiisoo/ic/employee/service/EmployeeService.java @@ -22,6 +22,7 @@ import com.kiisoo.ic.store.mapper.PrivilageCpUserStoreDOMapper; import com.kiisoo.ic.synchronous.entity.TurnBackDTO; import com.kiisoo.ic.synchronous.entity.WxDataDTO; import com.kiisoo.ic.wx.service.QWMailListManageService; +import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.cp.bean.WxCpUser; import me.chanjar.weixin.cp.bean.WxCpUserExternalContactInfo; @@ -43,6 +44,9 @@ import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.List; import java.util.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -59,6 +63,7 @@ import static java.util.regex.Pattern.*; * @Version: v1 */ @Service +@Slf4j public class EmployeeService { @Autowired @@ -463,80 +468,107 @@ public class EmployeeService { } public void syncCustoemr() throws Exception { + //初始化线程 + ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(8); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + //查询所有店长 List privilageCpUserStoreDOS = privilageCpUserStoreDOMapper.selectList(null); if (CollectionUtils.isNotEmpty(privilageCpUserStoreDOS)){ - for (PrivilageCpUserStoreDO privilageCpUserStoreDO:privilageCpUserStoreDOS){ - String cpUserId = privilageCpUserStoreDOMapper.selectCpUserIdByStoreId(privilageCpUserStoreDO.getStoreId()); - if (StringUtils.isBlank(cpUserId)){ - continue; - } - List customers = null; - try { - customers = qwMailListManageService.getCustomer(cpUserId); - }catch (Exception e) { - System.out.println("查询联系人失败:"+cpUserId); - continue; - } + //分组 + final List> afterPageList = com.kiisoo.ic.utils.CollectionUtils.portListByQuantity(privilageCpUserStoreDOS, 100); + //定义线程池长度 + int threadPoolSize = afterPageList.size(); + //定义countDownLatch + final CountDownLatch countDownLatch = new CountDownLatch(threadPoolSize); + for (int i = 0; i < afterPageList.size(); i++) { + List cpUserStoreDOS = afterPageList.get(i); + newFixedThreadPool.execute(new Runnable() { + @Override + public void run() { + try { + for (PrivilageCpUserStoreDO cpUserStoreDO : cpUserStoreDOS) { + String cpUserId = privilageCpUserStoreDOMapper.selectCpUserIdByStoreId(cpUserStoreDO.getStoreId()); + if (StringUtils.isBlank(cpUserId)){ + continue; + } + List customers = null; + try { + customers = qwMailListManageService.getCustomer(cpUserId); + }catch (Exception e) { + System.out.println("查询联系人失败:"+cpUserId); + continue; + } - if (CollectionUtils.isNotEmpty(customers)){ - for (WxCpUserExternalContactInfo customer:customers){ - TurnBackDTO turnBackDTO = new TurnBackDTO(); - - WxCpUserExternalContactInfo.ExternalContact externalContact = customer.getExternalContact(); - List followedUsers = customer.getFollowedUsers(); - - turnBackDTO.setEaCode(""); - if (CollectionUtils.isNotEmpty(followedUsers)){ - for (WxCpUserExternalContactInfo.FollowedUser followedUser:followedUsers){ - if (cpUserId.equals(followedUser.getUserId())){ - String state = followedUser.getState(); - WxCpUserExternalContactInfo.Tag[] tags = followedUser.getTags(); - if (StringUtils.isNotBlank(state)){ - //判断是否有导购码 - turnBackDTO.setEaCode(state); - }else if(tags != null && tags.length > 0){ - //判断是否有打tag - //todo 根据tag获取导购码 - for (int i = 0;i followedUsers = customer.getFollowedUsers(); + + turnBackDTO.setEaCode(""); + if (CollectionUtils.isNotEmpty(followedUsers)){ + for (WxCpUserExternalContactInfo.FollowedUser followedUser:followedUsers){ + if (cpUserId.equals(followedUser.getUserId())){ + String state = followedUser.getState(); + WxCpUserExternalContactInfo.Tag[] tags = followedUser.getTags(); + if (StringUtils.isNotBlank(state)){ + //判断是否有导购码 + turnBackDTO.setEaCode(state); + }else if(tags != null && tags.length > 0){ + //判断是否有打tag + //todo 根据tag获取导购码 + for (int j = 0;j list.size() ? list.size() : count + quantity)); + count += quantity; + } + + return wrapList; + } +}