大屏接口

dev_0531
yechenhao 6 years ago
parent 199b0b3585
commit a38a41ca1d

@ -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<PrivilageCpUserDO> privilageCpUserDOS = privilageCpUserDOMapper.selectList(null);
Map<String,PrivilageCpUserDO> cpUserIdMap = new HashMap<>();
@ -552,78 +564,131 @@ public class CustomerViewService {
WxCpExternalContactService externalContactService = wxCpService.getExternalContactService();
//查询企业微信已配置联系我的用户
List<String> cpUserIds = externalContactService.listFollowUser();
for (String cpUserId : cpUserIds) {
List<String> customerStrs;
try {
//获取客户联系人
customerStrs = externalContactService.listExternalContacts(cpUserId);
}catch (Exception e){
if (CollectionUtils.isNotEmpty(cpUserIds)){
//分组
final List<List<String>> 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<String> ids = afterPageList.get(i);
newFixedThreadPool.execute(new Runnable() {
@Override
public void run() {
try {
for (String cpUserId : ids) {
List<String> 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<String> 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<String> 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<PrivilageCpUserDO> qw = new QueryWrapper<>();
qw.ne("status",9);
List<PrivilageCpUserDO> privilageCpUserDOS = privilageCpUserDOMapper.selectList(null);
WxCpService wxCpService = WxCpConfiguration.getCpService(APPLICATIONID);
WxCpExternalContactService externalContactService = wxCpService.getExternalContactService();
//查询企业微信已配置联系我的用户
for (PrivilageCpUserDO privilageCpUserDO : privilageCpUserDOS) {
List<String> 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<List<PrivilageCpUserDO>> 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<PrivilageCpUserDO> cpUserDOS = afterPageList.get(i);
newFixedThreadPool.execute(new Runnable() {
@Override
public void run() {
try {
//查询企业微信已配置联系我的用户
for (PrivilageCpUserDO privilageCpUserDO : cpUserDOS) {
List<String> 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();
}
}
}

@ -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<PrivilageCpUserStoreDO> privilageCpUserStoreDOS = privilageCpUserStoreDOMapper.selectList(null);
if (CollectionUtils.isNotEmpty(privilageCpUserStoreDOS)){
for (PrivilageCpUserStoreDO privilageCpUserStoreDO:privilageCpUserStoreDOS){
String cpUserId = privilageCpUserStoreDOMapper.selectCpUserIdByStoreId(privilageCpUserStoreDO.getStoreId());
if (StringUtils.isBlank(cpUserId)){
continue;
}
List<WxCpUserExternalContactInfo> customers = null;
try {
customers = qwMailListManageService.getCustomer(cpUserId);
}catch (Exception e) {
System.out.println("查询联系人失败:"+cpUserId);
continue;
}
//分组
final List<List<PrivilageCpUserStoreDO>> 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<PrivilageCpUserStoreDO> 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<WxCpUserExternalContactInfo> 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<WxCpUserExternalContactInfo.FollowedUser> 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<tags.length;i++){
String groupName = tags[i].getGroupName();
if ("导购".equals(groupName)){
String tagName = tags[i].getTagName();
Long staffId = poiStoreStaffDOMapper.selectStaffIdByTag(privilageCpUserStoreDO.getStoreId(), tagName);
if (staffId != null){
PoiStoreStaff poiStoreStaff = poiStoreStaffDOMapper.selectById(staffId);
if (poiStoreStaff!=null){
turnBackDTO.setEaCode(poiStoreStaff.getStaffCode());
if (CollectionUtils.isNotEmpty(customers)){
for (WxCpUserExternalContactInfo customer:customers){
TurnBackDTO turnBackDTO = new TurnBackDTO();
WxCpUserExternalContactInfo.ExternalContact externalContact = customer.getExternalContact();
List<WxCpUserExternalContactInfo.FollowedUser> 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<tags.length;j++){
String groupName = tags[j].getGroupName();
if ("导购".equals(groupName)){
String tagName = tags[j].getTagName();
Long staffId = poiStoreStaffDOMapper.selectStaffIdByTag(cpUserStoreDO.getStoreId(), tagName);
if (staffId != null){
PoiStoreStaff poiStoreStaff = poiStoreStaffDOMapper.selectById(staffId);
if (poiStoreStaff!=null){
turnBackDTO.setEaCode(poiStoreStaff.getStaffCode());
}
}else{
//todo 绑定在标签导购上,后续删除
turnBackDTO.setEaCode(tagName);
}
}
}
}
}else{
//todo 绑定在标签导购上,后续删除
turnBackDTO.setEaCode(tagName);
Long joinTimeL = followedUser.getCreateTime();
Long time=new Long(joinTimeL);
String joinTime = sdf.format(time*1000);
turnBackDTO.setJoinTime(joinTime);
}
}
}
//type 1 微信type 2 企业微信
turnBackDTO.setType(externalContact.getType());
turnBackDTO.setUserId(cpUserId);
turnBackDTO.setName(externalContact.getName());
WxDataDTO wxDataDTO = new WxDataDTO();
wxDataDTO.setAvatarUrl(externalContact.getAvatar());
wxDataDTO.setUserId(externalContact.getExternalUserId());
wxDataDTO.setUnionId(externalContact.getUnionId());
turnBackDTO.setWxData(wxDataDTO);
customerService.turnBack(turnBackDTO);
}
Long joinTimeL = followedUser.getCreateTime();
Long time=new Long(joinTimeL);
String joinTime = sdf.format(time*1000);
turnBackDTO.setJoinTime(joinTime);
}
}
} catch (Exception e) {
log.error("同步客户明细", e);
} finally {
//每次减去一,避免死锁
countDownLatch.countDown();
}
//type 1 微信type 2 企业微信
turnBackDTO.setType(externalContact.getType());
turnBackDTO.setUserId(cpUserId);
turnBackDTO.setName(externalContact.getName());
WxDataDTO wxDataDTO = new WxDataDTO();
wxDataDTO.setAvatarUrl(externalContact.getAvatar());
wxDataDTO.setUserId(externalContact.getExternalUserId());
wxDataDTO.setUnionId(externalContact.getUnionId());
turnBackDTO.setWxData(wxDataDTO);
customerService.turnBack(turnBackDTO);
}
}
});
}
countDownLatch.await();
}
}

@ -0,0 +1,25 @@
package com.kiisoo.ic.utils;
import java.util.ArrayList;
import java.util.List;
public class CollectionUtils {
public static List portListByQuantity(List list, int quantity) {
if (list == null || list.size() == 0) {
return list;
}
if (quantity <= 0) {
new IllegalArgumentException("Wrong quantity.");
}
List wrapList = new ArrayList();
int count = 0;
while (count < list.size()) {
wrapList.add(list.subList(count, (count + quantity) > list.size() ? list.size() : count + quantity));
count += quantity;
}
return wrapList;
}
}
Loading…
Cancel
Save