门店号管理

dev_0531
LegnaYet 6 years ago
parent 0f26f307dc
commit d3ceaa04e0

@ -107,31 +107,6 @@ public class CustomerViewController extends BaseController {
}
}
/**
*
* @return VO
*/
@RequestMapping(value = "/sync/all/count", method = RequestMethod.GET)
public void syncAllCount(){
try{
customerViewService.syncAllCount();
}catch (Exception e){
log.error("全量同步总数出错", e);
}
}
/**
*
* @return VO
*/
@RequestMapping(value = "/sync/alive/count", method = RequestMethod.GET)
public void syncAliveCount(){
try{
customerViewService.syncAliveCount();
}catch (Exception e){
log.error("批量同步总数出错", e);
}
}
/**
*

@ -568,150 +568,6 @@ public class CustomerViewService {
return result;
}
public void syncAllCount() throws WxErrorException, InterruptedException {
//初始化线程
ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(8);
//查询现有企业微信账号
List<PrivilageCpUserDO> privilageCpUserDOS = privilageCpUserDOMapper.selectList(null);
Map<String,PrivilageCpUserDO> cpUserIdMap = new HashMap<>();
for (PrivilageCpUserDO privilageCpUserDO : privilageCpUserDOS) {
cpUserIdMap.put(privilageCpUserDO.getCpUserId(),privilageCpUserDO);
}
WxCpService wxCpService = WxCpConfiguration.getCpService(APPLICATIONID);
WxCpExternalContactService externalContactService = wxCpService.getExternalContactService();
//查询企业微信已配置联系我的用户
List<String> cpUserIds = externalContactService.listFollowUser();
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){
//删除
privilageCpUserDOMapper.deleteById(privilageCpUserDO.getId());
}
}
}
}
public void syncAliveCount() throws WxErrorException, InterruptedException {
//初始化线程
ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(8);
//查询现有企业微信账号
List<PrivilageCpUserStoreDO> relations = privilageCpUserStoreDOMapper.selectList(null);
List<Long> ids = relations.stream().map(relation -> relation.getCpUserId()).collect(Collectors.toList());
QueryWrapper<PrivilageCpUserDO> qw = new QueryWrapper<>();
qw.ne("status",9).in("id",ids);
List<PrivilageCpUserDO> privilageCpUserDOS = privilageCpUserDOMapper.selectList(null);
WxCpService wxCpService = WxCpConfiguration.getCpService(APPLICATIONID);
WxCpExternalContactService externalContactService = wxCpService.getExternalContactService();
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();
}
}
/**
*
* @return

@ -1,4 +1,4 @@
package com.kiisoo.ic.behavior.count;
package com.kiisoo.ic.job.behavior;
import com.lmax.disruptor.EventFactory;

@ -1,4 +1,4 @@
package com.kiisoo.ic.behavior.count;
package com.kiisoo.ic.job.behavior;
import com.kiisoo.ic.generalize.mapper.PoiCustomerContactDataStatMapper;
import com.kiisoo.ic.store.mapper.PrivilageCpUserStoreDOMapper;

@ -1,6 +1,5 @@
package com.kiisoo.ic.behavior.count;
package com.kiisoo.ic.job.behavior;
import com.kiisoo.ic.employee.mapper.PrivilageCpUserDOMapper;
import com.kiisoo.ic.generalize.mapper.PoiCustomerContactDataStatMapper;
import com.kiisoo.ic.store.mapper.PrivilageCpUserStoreDOMapper;
import com.kiisoo.ic.store.mapper.StorePromotionDataDOMapper;

@ -1,4 +1,4 @@
package com.kiisoo.ic.behavior.count;
package com.kiisoo.ic.job.behavior;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@ -1,4 +1,4 @@
package com.kiisoo.ic.behavior.count;
package com.kiisoo.ic.job.behavior;
import com.kiisoo.ic.employee.entity.PrivilageCpUserDO;
import com.kiisoo.ic.employee.mapper.PrivilageCpUserDOMapper;

@ -1,5 +1,6 @@
package com.kiisoo.ic.store.service;
package com.kiisoo.ic.job.behavior;
import com.kiisoo.ic.store.service.StorePromotionDataService;
import com.kiisoo.ic.utils.DateUtils;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
@ -28,17 +29,13 @@ public class StorePromotionDataJob {
* service
*/
@Autowired
private StorePromotionDataService storePromotionDataService;
private CustomerBehaviorJob customerBehaviorJob;
@Scheduled(cron = "0 0 1 * * ?")
public void syncData(){
try {
log.info("<--获取联系客户统计数据定时任务执行-->");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<Date> getdays = DateUtils.getdays(DateUtils.addDay(new Date(),-3), DateUtils.addDay(new Date(),-1));
for (Date date:getdays){
storePromotionDataService.syncCustomerBlackListByDate(date,date);
}
customerBehaviorJob.handle(DateUtils.addDay(new Date(),-3),DateUtils.addDay(new Date(),-1));
log.info("<--获取联系客户统计数据定时任务结束-->");
} catch (Exception e) {
log.error("获取联系客户统计数据任务出错", e);

@ -1,4 +1,4 @@
package com.kiisoo.ic.behavior.count;
package com.kiisoo.ic.job.behavior;
import java.text.SimpleDateFormat;
import lombok.extern.slf4j.Slf4j;
@ -8,8 +8,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Date;
@Controller
@RequestMapping("/job")
@Slf4j

@ -287,184 +287,6 @@ public class StorePromotionDataService {
}
}
/**
*
*
* @throws Exception
*/
@Transactional(rollbackFor = Exception.class)
public void syncCustomerBlackList() throws Exception {
Date startDate = DateUtils.addDay(new Date(), -1);
Date endDate = DateUtils.addDay(new Date(), -1);
//获取token
String token = mainService.getAccessToken();
List<StorePromotionDataDO> cusList = storePromotionDataDOMapper.selectCustomer();
cusList.forEach(customer -> {
// 返回
try {
String userId = customer.getUserId();
WxCusInfoReqDO wxCusInfoReqDO = new WxCusInfoReqDO();
wxCusInfoReqDO.setAccess_token(token);
wxCusInfoReqDO.setUserid(userId);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//todo 分开写便于同步历史数据 最大跨度30天 最久当天往前180天 上线时改为System.currentTimeMillis()
wxCusInfoReqDO.setStart_time(startDate.getTime() / 1000);
wxCusInfoReqDO.setEnd_time(endDate.getTime() / 1000);
WxCusInfoRespDO wxCusInfoRespDO = getWxCusInfo(wxCusInfoReqDO);
if (wxCusInfoRespDO == null) {
return;
}
wxCusInfoRespDO.setStoreId(customer.getStoreId());
wxCusInfoRespDO.setStatTime(startDate);
QueryWrapper<PoiCustomerContactDataStat> qw = new QueryWrapper();
qw.eq("store_id", customer.getStoreId()).eq("stat_time", startDate).last("limit 1");
PoiCustomerContactDataStat poiCustomerContactDataStat = poiCustomerContactDataStatMapper.selectOne(qw);
int syncRes = 0;
if (poiCustomerContactDataStat == null) {
syncRes = storePromotionDataDOMapper.insertStorePromotionData(wxCusInfoRespDO);
} else {
Long newApplyCnt = 0L;
if (wxCusInfoRespDO.getNew_apply_cnt() != null) {
newApplyCnt = new Long(wxCusInfoRespDO.getNew_apply_cnt());
}
poiCustomerContactDataStat.setNewApplyCnt(newApplyCnt);
Long avgReplyTime = 0L;
if (wxCusInfoRespDO.getAvg_reply_time() != null) {
avgReplyTime = new Long(wxCusInfoRespDO.getAvg_reply_time());
}
poiCustomerContactDataStat.setAvgReplyTime(avgReplyTime);
Long chatCny = 0L;
if (wxCusInfoRespDO.getChat_cnt() != null) {
chatCny = new Long(wxCusInfoRespDO.getChat_cnt());
}
poiCustomerContactDataStat.setChatCnt(chatCny);
Long messageCnt = 0L;
if (wxCusInfoRespDO.getMessage_cnt() != null) {
messageCnt = new Long(wxCusInfoRespDO.getMessage_cnt());
}
poiCustomerContactDataStat.setMessageCnt(messageCnt);
Float replyPercentage = 0F;
if (wxCusInfoRespDO.getReply_percentage() != null) {
replyPercentage = wxCusInfoRespDO.getReply_percentage();
}
poiCustomerContactDataStat.setReplyPercentage(replyPercentage);
Long newContactCnt = 0L;
if (wxCusInfoRespDO.getNew_contact_cnt() != null) {
newContactCnt = new Long(wxCusInfoRespDO.getNew_contact_cnt());
}
poiCustomerContactDataStat.setNewContactCnt(newContactCnt);
Long negativeFeedbackCnt = 0L;
if (wxCusInfoRespDO.getNegative_feedback_cnt() != null) {
negativeFeedbackCnt = new Long(wxCusInfoRespDO.getNegative_feedback_cnt());
}
poiCustomerContactDataStat.setNegativeFeedbackCnt(negativeFeedbackCnt);
syncRes = poiCustomerContactDataStatMapper.updateById(poiCustomerContactDataStat);
}
if (syncRes == Constants.INSERT_FAIL) {
throw new RuntimeException("向数据库导入客户统计数据时出错");
}
} catch (Exception e) {
log.error("请求企业微信api获取客户统计数据或日期转换出错", e);
}
});
}
public void syncCustomerBlackListByDate(Date startDate, Date endDate) throws Exception {
//获取token
String token = mainService.getAccessToken();
List<PrivilageCpUserDO> privilageCpUserDOS = privilageCpUserDOMapper.selectList(null);
privilageCpUserDOS.forEach(privilageCpUserDO -> {
// 返回
try {
Long storeId = privilageCpUserStoreDOMapper.selectOneByCpUserId(privilageCpUserDO.getCpUserId());
Long regionId = null;
if (storeId == null){
regionId = privilageCpUserDO.getId();
}
WxCusInfoReqDO wxCusInfoReqDO = new WxCusInfoReqDO();
wxCusInfoReqDO.setAccess_token(token);
wxCusInfoReqDO.setUserid(privilageCpUserDO.getCpUserId());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//todo 分开写便于同步历史数据 最大跨度30天 最久当天往前180天 上线时改为System.currentTimeMillis()
wxCusInfoReqDO.setStart_time(startDate.getTime() / 1000);
wxCusInfoReqDO.setEnd_time(endDate.getTime() / 1000);
WxCusInfoRespDO wxCusInfoRespDO = getWxCusInfo(wxCusInfoReqDO);
if (wxCusInfoRespDO == null) {
return;
}
wxCusInfoRespDO.setStoreId(storeId);
wxCusInfoRespDO.setRegionId(regionId);
wxCusInfoRespDO.setStatTime(startDate);
QueryWrapper<PoiCustomerContactDataStat> qw = new QueryWrapper();
qw.eq("store_id", storeId).eq("region_id", regionId).eq("stat_time", startDate).last("limit 1");
PoiCustomerContactDataStat poiCustomerContactDataStat = poiCustomerContactDataStatMapper.selectOne(qw);
int syncRes = 0;
if (poiCustomerContactDataStat == null) {
syncRes = storePromotionDataDOMapper.insertStorePromotionData(wxCusInfoRespDO);
} else {
Long newApplyCnt = 0L;
if (wxCusInfoRespDO.getNew_apply_cnt() != null) {
newApplyCnt = new Long(wxCusInfoRespDO.getNew_apply_cnt());
}
poiCustomerContactDataStat.setNewApplyCnt(newApplyCnt);
Long avgReplyTime = 0L;
if (wxCusInfoRespDO.getAvg_reply_time() != null) {
avgReplyTime = new Long(wxCusInfoRespDO.getAvg_reply_time());
}
poiCustomerContactDataStat.setAvgReplyTime(avgReplyTime);
Long chatCny = 0L;
if (wxCusInfoRespDO.getChat_cnt() != null) {
chatCny = new Long(wxCusInfoRespDO.getChat_cnt());
}
poiCustomerContactDataStat.setChatCnt(chatCny);
Long messageCnt = 0L;
if (wxCusInfoRespDO.getMessage_cnt() != null) {
messageCnt = new Long(wxCusInfoRespDO.getMessage_cnt());
}
poiCustomerContactDataStat.setMessageCnt(messageCnt);
Float replyPercentage = 0F;
if (wxCusInfoRespDO.getReply_percentage() != null) {
replyPercentage = wxCusInfoRespDO.getReply_percentage();
}
poiCustomerContactDataStat.setReplyPercentage(replyPercentage);
Long newContactCnt = 0L;
if (wxCusInfoRespDO.getNew_contact_cnt() != null) {
newContactCnt = new Long(wxCusInfoRespDO.getNew_contact_cnt());
}
poiCustomerContactDataStat.setNewContactCnt(newContactCnt);
Long negativeFeedbackCnt = 0L;
if (wxCusInfoRespDO.getNegative_feedback_cnt() != null) {
negativeFeedbackCnt = new Long(wxCusInfoRespDO.getNegative_feedback_cnt());
}
poiCustomerContactDataStat.setNegativeFeedbackCnt(negativeFeedbackCnt);
syncRes = poiCustomerContactDataStatMapper.updateById(poiCustomerContactDataStat);
}
if (syncRes == Constants.INSERT_FAIL) {
throw new RuntimeException("向数据库导入客户统计数据时出错");
}
} catch (Exception e) {
log.error("请求企业微信api获取客户统计数据或日期转换出错", e);
}
});
}
public PageInfo<PoiCustomerContactDataStat> listUserBehaviorData(Long userId,Long companyId,Long shopId,Date startDate, Date endDate,Integer pageNum,Integer pageSize){
Set<Long> shopIds = new HashSet<>();
@ -512,21 +334,6 @@ public class StorePromotionDataService {
}
/**
*
*
* @param wxCusInfoReqDO
* @return
* @throws WxErrorException
*/
public WxCusInfoRespDO getWxCusInfo(WxCusInfoReqDO wxCusInfoReqDO) throws WxErrorException {
String url = mainService.getWxCpConfigStorage().getApiUrl("/cgi-bin/externalcontact/get_user_behavior_data");
String result = mainService.post(url, wxCusInfoReqDO.toJson());
JSONObject parseObject = JSON.parseObject(result);
String[] behavior_data = JSONObject.parseObject(parseObject.getString("behavior_data"), String[].class);
return JSONObject.parseObject(behavior_data[0], WxCusInfoRespDO.class);
}
/**
*
*

Loading…
Cancel
Save