提交 5befd1cf 编写于 作者: 水库浪子

会话存档相关参数抽取优化,定时任务相关统计类整合;

上级 91032f33
......@@ -7,8 +7,11 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableAsync;
import java.io.IOException;
/**
* 启动程序
*
......@@ -20,7 +23,7 @@ import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync
@EnableDiscoveryClient
public class LinkWeChatApplication {
public static void main(String[] args) {
public static void main(String[] args){
SpringApplication.run(LinkWeChatApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ LinkWeChat启动成功 ლ(´ڡ`ლ)゙ \n" +
......
......@@ -255,7 +255,6 @@ forest:
wecome:
chatKey: finance_new
serverUrl: https://qyapi.weixin.qq.com/
weComePrefix: cgi-bin
noAccessTokenUrl:
......@@ -316,6 +315,3 @@ wecome:
# JS SDK身份校验成功后的redirect url
authorizeRedirectUrl:
weixin:
appid: wxa57479bcd3f15461
secret: f23f4becdcac9819f292a3754fd8f84e
\ No newline at end of file
......@@ -114,5 +114,9 @@ public class WeCorpAccount extends BaseEntity
private String fissionGroupUrl;
@ApiModelProperty("会话私钥")
private String financePrivateKey;
}
package com.linkwechat.common.utils;
import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
......@@ -224,4 +225,31 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
public static Long strToDate(int days, Integer type) {
Long time = null;
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
Calendar cale = Calendar.getInstance();
cale.add(Calendar.DATE, days);
String tarday = new SimpleDateFormat("yyyy-MM-dd").format(cale.getTime());
if (type.equals(0)) {
tarday += " 00:00:00";
} else {
tarday += " 23:59:59";
}
// String转Date
try {
date = format2.parse(tarday);
System.out.println(date.getTime());
time = date.getTime() / 1000;
System.out.println(time.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
}
......@@ -2,7 +2,12 @@ package com.tencent.wework;
import com.linkwechat.common.utils.OsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
@Slf4j
public class Finance {
public native static long NewSdk();
......@@ -120,7 +125,12 @@ public class Finance {
System.loadLibrary("libcurl");
System.loadLibrary("WeWorkFinanceSdk");
} else {
System.load("/app/projects/libWeWorkFinanceSdk_Java.so");
try {
System.load( new ClassPathResource("lib/libWeWorkFinanceSdk_Java.so").getURI().getPath());
} catch (IOException e) {
log.error("linux下libWeWorkFinanceSdk_Java加载出错:"+e.getMessage());
}
// System.load("/app/projects/libWeWorkFinanceSdk_Java.so");
}
}
......
......@@ -30,7 +30,6 @@ import java.util.concurrent.atomic.AtomicLong;
* @date 2020/12/2 16:01
**/
@Slf4j
public class FinanceUtils {
/**
* NewSdk返回的sdk指针
......@@ -56,33 +55,6 @@ public class FinanceUtils {
}
}
private final static String privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIEpAIBAAKCAQEAjGwIQIMGDJonMQdX3OJzZN0YVoYbORK1FU/8vhwdI9ixxSGW\n" +
"nrAQc6r8k3g1QTXzmKAxdVlAa1OGZCpl5X5A6C9JwDhFpuN+SKJxfxtHNv1oBk0r\n" +
"LnDftsdlibZFQn3fiMomxKyU8eujWBe31+pLQXaTSdSHcNhb+NrBa7AtNUr/c7bK\n" +
"nnFUZHDpXFB4Z0BhK1cRh2NN7ppLNnIJXpduGrwGtLdx++LsBaX2vjO4qW/DKP2Z\n" +
"3gZON1tGoofS1IPhCg0naoRN3lL7ctFAyxOr2ualGOc6hpvBGfjnBKU42aoaKQEi\n" +
"7tVJHChonZXMRcm4mdPu5371IzPeR3NYlP3ESQIDAQABAoIBAEux8MDV4GzqrDXB\n" +
"v5tSduHoTTZXFFWvv+29MIFKpmqsZSfe81KXlkbD5WOZaRu9+ZJMyFx5wEAaxlc4\n" +
"g7UyWcrPsvHrY6CTAYAmEQhzq+/4qhq7fouKTQ46boHNv4gYUmdqkXtzjNbcLzFN\n" +
"l8zMf8TKJpul9VzfRLThN2LVlYkkETcCukzZrK+aSCi6E/dL+noL1ndZYmWF5kbB\n" +
"lk881x5CdbusYBtoxfh6ie0EXfCyK4duX3OdvXRcSpTdqiqTcrtt9oinh4ZHV930\n" +
"BCTPkGGz5UL3C243W3dlzuonNXqwl++FCSyRMVONYSgb4NRJn4fvehjGtXyAv3Sz\n" +
"nSAVoAECgYEA9dhYk4DT2Hclaqo/0ugQvUp7oIPN9qFgrZ6nU94SHkdrBqhFrCmz\n" +
"32GeYGSsQJQx3T9Kuip7oGX9AOKn3f30NrcuAe9VzzWAkCKQgQgmtMVySAPWCVr9\n" +
"4MH77hc3ijxr6iGd70BQ7EDyVQGRvZqOLl1wu7yZ9vhbVHA87KpgHf0CgYEAkjjn\n" +
"mNudh+rs8ibrGu+md8bXJdxHtIbncXF5VsV7wi4HlHfKnWqy+z2If1akm2iacnsi\n" +
"XDVqyYNExdtoxmbPc/R9ZJ8jP0FLPoWACcjP03r9H+5eiXrmniZbElGshnwKHFFh\n" +
"ZlY1xPAXFgEjh10DDG1wK6/9eB13o+NdP7c7yz0CgYEA3HujNOadF84DaJ43j1dB\n" +
"bAKYzWRoG4CvzAY12ymW5kD244mWssjT4MsT1y9yrJg8AEj7I+tl5HyTY2/jTVys\n" +
"4UV3pZSYSLWbD6lwH8jHrehGjf+ivdE1T5Wp8+YukXOO9PQhSKlN6hR1QXee1YT/\n" +
"buWubTDAhJZIknn2qepDOaECgYA/OnITLCwTYOey0ldpn2V5d7cC/RzYmFkuOeZB\n" +
"OkYIsoS+k4o+xau9bl8+yQWG2hWnGU3DV6nkl2m+sOC+oihkuL0cLKA+MwrJb0rt\n" +
"cDNF/HHGQ/oHMujUTDunUT/sK2jn09ztNqwri7I/5qApYXP3BL+zdHeDYCKi8Kca\n" +
"7S3i9QKBgQCgKgQf8u+oRojMH14iJssAyNEhZgmJ85s/BeiUmMuW5K8iZZ5pnVuq\n" +
"8nc/IJGVPHRTObdGCd1L4d5UERiX46Z1BjYv/nuK5DScDcB3mq6K9VXJLNE+MxwW\n" +
"wOVT+6FjmBRucGXczAkWXCH7++y6gYzE+OuRIdv5sVkE24gNG7sawg==\n" +
"-----END RSA PRIVATE KEY-----";
/**
* 拉取聊天记录
......@@ -90,8 +62,9 @@ public class FinanceUtils {
* @param seq 消息的seq值,标识消息的序号
* @param proxy 代理
* @param passwd 密码
* @param privateKey 私钥
*/
public static List<JSONObject> getChatData(long seq, String proxy, String passwd, RedisCache redisCache) {
public static List<JSONObject> getChatData(long seq, String proxy, String passwd, RedisCache redisCache,String privateKey) {
List<JSONObject> resList = new ArrayList<>();
long slice = Finance.NewSlice();
int ret = Finance.GetChatData(sdk, seq, WeConstans.LIMIT, proxy, passwd, timeout, slice);
......
package com.linkwechat.quartz.task;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @description: 客户画像
* @author: HaoN
* @create: 2021-05-02 21:08
**/
@Slf4j
@Component("customerPortraitTask")
public class CustomerPortraitTask {
}
package com.linkwechat.quartz.task;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.entity.WeCorpAccount;
import com.linkwechat.common.core.elasticsearch.ElasticSearch;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
import com.linkwechat.wecom.mapper.WeCustomerMessageTimeTaskMapper;
import com.linkwechat.wecom.service.*;
import com.tencent.wework.FinanceUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicLong;
/**
* 定时任务调度测试
*
* @author ruoyi
*/
* @description: 会话存档定时拉取
* @author: HaoN
* @create: 2021-05-31 10:39
**/
@Slf4j
@Component("ryTask")
public class RyTask {
@Autowired
private ElasticSearch elasticSearch;
@Autowired
private RedisCache redisCache;
@Component("FinanceTask")
public class FinanceTask {
@Autowired
private IWeChatContactMappingService weChatContactMappingService;
ElasticSearch elasticSearch;
@Autowired
private IWeSensitiveService weSensitiveService;
RedisCache redisCache;
@Autowired
private IWeSensitiveActHitService weSensitiveActHitService;
IWeChatContactMappingService weChatContactMappingService;
@Autowired
private IWeCustomerService weCustomerService;
IWeSensitiveService weSensitiveService;
@Autowired
private WeCustomerMessageTimeTaskMapper customerMessageTimeTaskMapper;
IWeSensitiveActHitService weSensitiveActHitService;
@Autowired
private IWeCustomerMessageService weCustomerMessageService;
IWeCorpAccountService iWeCorpAccountService;
@Value("${wecome.chatKey}")
private String chartKey;
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
}
/**
* 会话数据拉取
* @throws IOException
*/
public void FinanceTask(){
List<WeCorpAccount> weCorpAccounts = iWeCorpAccountService.list(
new LambdaQueryWrapper<WeCorpAccount>()
.eq(WeCorpAccount::getDelFlag, 1)
.eq(WeCorpAccount::getStatus, 1)
);
if(CollectionUtil.isNotEmpty(weCorpAccounts)){
weCorpAccounts.stream().forEach(k->{
try {
if(StrUtil.isNotBlank(k.getCorpId()) && StrUtil.isNotBlank(k.getSecret())){
findFinance(k.getCorpId(),k.getSecret(),"ChartKey_"+k.getCorpId(),k.getFinancePrivateKey());
}
} catch (IOException e) {
e.printStackTrace();
}
});
public void ryParams(String params) {
System.out.println("执行有参方法:" + params);
}
}
public void ryNoParams() {
System.out.println("执行无参方法");
}
public void FinanceTask(String corpId, String secret) throws IOException {
private void findFinance(String corpId, String secret,String chartKey,String financePrivateKey) throws IOException {
log.info("执行有参方法: params:{},{}", corpId, secret);
//创建索引
elasticSearch.createIndex2(chartKey, elasticSearch.getFinanceMapping());
//从缓存中获取消息标识
Object seqObject = Optional.ofNullable(redisCache.getCacheObject(WeConstans.CONTACT_SEQ_KEY)).orElse(0L);
Object seqObject = Optional.ofNullable(redisCache.getCacheObject(chartKey+"::"+WeConstans.CONTACT_SEQ_KEY)).orElse(0L);
Long seqLong = Long.valueOf(String.valueOf(seqObject));
AtomicLong index = new AtomicLong(seqLong);
if (index.get() == 0) {
setRedisCacheSeqValue(index);
setRedisCacheSeqValue(index,chartKey);
}
log.info(">>>>>>>seq:{}", index.get());
FinanceUtils.initSDK(corpId, secret);
List<JSONObject> chatDataList = FinanceUtils.getChatData(index.get(),
"",
"", redisCache);
"", redisCache,financePrivateKey);
if (CollectionUtil.isNotEmpty(chatDataList)) {
try {
List<JSONObject> elasticSearchEntities = weChatContactMappingService.saveWeChatContactMapping(chatDataList);
......@@ -100,24 +108,13 @@ public class RyTask {
e.printStackTrace();
}
}
}
public void WeCustomers() {
// //查询系统所有客户
// List<WeCustomer> cacheList = redisCache.getCacheList(WeConstans.WECUSTOMERS_KEY);
// if (CollectionUtils.isEmpty(cacheList)) {
// List<WeCustomer> customers = weCustomerService.selectWeCustomerList(null);
// redisCache.setCacheList(WeConstans.WECUSTOMERS_KEY, customers);
// } else {
// List<WeCustomer> customers = weCustomerService.selectWeCustomerList(null);
// List<WeCustomer> weCustomers = redisCache.getCacheList(WeConstans.WECUSTOMERS_KEY);
// if (CollectionUtils.isNotEmpty(weCustomers) && weCustomers.size() < customers.size()) {
// redisCache.setCacheList(WeConstans.WECUSTOMERS_KEY, customers);
// }
// }
}
private void setRedisCacheSeqValue(AtomicLong index) {
private void setRedisCacheSeqValue(AtomicLong index,String chartKey) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SortBuilder<?> sortBuilderPrice = SortBuilders.fieldSort(WeConstans.CONTACT_SEQ_KEY).order(SortOrder.DESC);
searchSourceBuilder.sort(sortBuilderPrice);
......@@ -128,49 +125,4 @@ public class RyTask {
});
redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY, index);
}
/**
* @param corpId 企业id
* @param secret 会话密钥
*/
public void getPermitUserList(String corpId, String secret) {
log.info("执行有参方法: params:{},{}", corpId, secret);
}
/**
* 扫描群发消息定时任务
*/
public void messageTask() {
//获的当前时间的毫秒数
long currentTime = System.currentTimeMillis();
//customerMessageTimeTaskMapper
List<WeCustomerMessageTimeTask> weCustomerMessageTimeTasks = customerMessageTimeTaskMapper.selectWeCustomerMessageTimeTaskGteSettingTime(currentTime);
final Semaphore semaphore = new Semaphore(5);
if (CollectionUtils.isNotEmpty(weCustomerMessageTimeTasks)) {
weCustomerMessageTimeTasks.forEach(
s -> {
try {
semaphore.acquire();
if (s.getMessageInfo() != null && s.getMessageId() != null || (s.getMessageInfo().getPushType().equals(WeConstans.SEND_MESSAGE_CUSTOMER)
&& CollectionUtils.isNotEmpty(s.getCustomersInfo())) || (s.getMessageInfo().getPushType().equals(WeConstans.SEND_MESSAGE_GROUP)
&& CollectionUtils.isNotEmpty(s.getGroupsInfo()))) {
weCustomerMessageService.sendMessgae(s.getMessageInfo(), s.getMessageId(), s.getCustomersInfo(), s.getGroupsInfo());
//更新消息处理状态
customerMessageTimeTaskMapper.updateTaskSolvedById(s.getTaskId());
}
semaphore.release();
} catch (JsonProcessingException | InterruptedException e) {
log.error("定时群发消息处理异常:ex:{}", e);
e.printStackTrace();
}
}
);
}
}
}
package com.linkwechat.quartz.task;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.utils.bean.BeanUtils;
import com.linkwechat.wecom.client.WeCustomerClient;
import com.linkwechat.wecom.domain.WeGroup;
import com.linkwechat.wecom.domain.WeGroupStatistic;
import com.linkwechat.wecom.domain.dto.GroupChatStatisticDto;
import com.linkwechat.wecom.domain.query.GroupChatStatisticQuery;
import com.linkwechat.wecom.service.IWeGroupService;
import com.linkwechat.wecom.service.IWeGroupStatisticService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @author sxw
* @description 群聊数据统计
* @date 2021/2/24 0:42
**/
@Slf4j
@Component("GroupChatStatisticTask")
public class GroupChatStatisticTask {
@Autowired
private WeCustomerClient weCustomerClient;
@Autowired
private IWeGroupService weGroupService;
@Autowired
private IWeGroupStatisticService weGroupStatisticService;
public void getGroupChatData() {
log.info("群聊数据统计>>>>>>>>>>>>>>>>>>>启动");
List<WeGroup> weGroupList = weGroupService.list(new LambdaQueryWrapper<WeGroup>().eq(WeGroup::getStatus, 0));
log.info("群聊数据统计>>>>>>>>>>>>>>>>>>>weGroupList:{}",weGroupList.size());
if (CollectionUtil.isNotEmpty(weGroupList)){
List<WeGroupStatistic> weGroupStatisticList = new ArrayList<>();
GroupChatStatisticQuery query = new GroupChatStatisticQuery();
//前一天的数据
Long startTime = strToDate(-1, 0);
Long endTime = strToDate(-1, 1);
query.setDay_begin_time(startTime);
query.setDay_end_time(endTime);
weGroupList.forEach(weGroup -> {
GroupChatStatisticQuery.OwnerFilter ownerFilter = new GroupChatStatisticQuery.OwnerFilter();
List<String> idList = new ArrayList<>();
idList.add(weGroup.getOwner());
ownerFilter.setUserid_list(idList);
query.setOwnerFilter(ownerFilter);
try {
GroupChatStatisticDto groupChatStatistic = weCustomerClient.getGroupChatStatisticGroupByDay(query);
List<GroupChatStatisticDto.GroupchatStatisticData> items = groupChatStatistic.getItems();
if(CollectionUtil.isNotEmpty(items)){
items.forEach(groupchatStatisticData -> {
WeGroupStatistic weGroupStatistic = new WeGroupStatistic();
GroupChatStatisticDto.StatisticData data = groupchatStatisticData.getData();
BeanUtils.copyPropertiesignoreOther(data, weGroupStatistic);
weGroupStatistic.setChatId(weGroup.getChatId());
weGroupStatistic.setStatTime(groupchatStatisticData.getStatTime());
weGroupStatisticList.add(weGroupStatistic);
});
}
} catch (Exception e) {
e.printStackTrace();
log.error("群聊数据拉取失败: ownerFilter:【{}】,ex:【{}】", JSONObject.toJSONString(ownerFilter),e.getStackTrace());
}
});
weGroupStatisticService.saveBatch(weGroupStatisticList);
}
}
private Long strToDate(int days, Integer type) {
Long time = null;
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
Calendar cale = Calendar.getInstance();
cale.add(Calendar.DATE, days);
String tarday = new SimpleDateFormat("yyyy-MM-dd").format(cale.getTime());
if (type.equals(0)) {
tarday += " 00:00:00";
} else {
tarday += " 23:59:59";
}
// String转Date
try {
date = format2.parse(tarday);
System.out.println(date.getTime());
time = date.getTime() / 1000;
System.out.println(time.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
}
package com.linkwechat.quartz.task;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
import com.linkwechat.wecom.mapper.WeCustomerMessageTimeTaskMapper;
import com.linkwechat.wecom.service.IWeCustomerMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.Semaphore;
/**
* @description: 群发任务推送
* @author: HaoN
* @create: 2021-05-31 10:43
**/
@Slf4j
@Component("GroupPushTask")
public class GroupPushTask {
@Autowired
private WeCustomerMessageTimeTaskMapper customerMessageTimeTaskMapper;
@Autowired
private IWeCustomerMessageService weCustomerMessageService;
/**
* 扫描群发消息定时任务
*/
public void messageTask() {
//获的当前时间的毫秒数
long currentTime = System.currentTimeMillis();
//customerMessageTimeTaskMapper
List<WeCustomerMessageTimeTask> weCustomerMessageTimeTasks = customerMessageTimeTaskMapper.selectWeCustomerMessageTimeTaskGteSettingTime(currentTime);
final Semaphore semaphore = new Semaphore(5);
if (CollectionUtils.isNotEmpty(weCustomerMessageTimeTasks)) {
weCustomerMessageTimeTasks.forEach(
s -> {
try {
semaphore.acquire();
if (s.getMessageInfo() != null && s.getMessageId() != null || (s.getMessageInfo().getPushType().equals(WeConstans.SEND_MESSAGE_CUSTOMER)
&& CollectionUtils.isNotEmpty(s.getCustomersInfo())) || (s.getMessageInfo().getPushType().equals(WeConstans.SEND_MESSAGE_GROUP)
&& CollectionUtils.isNotEmpty(s.getGroupsInfo()))) {
weCustomerMessageService.sendMessgae(s.getMessageInfo(), s.getMessageId(), s.getCustomersInfo(), s.getGroupsInfo());
//更新消息处理状态
customerMessageTimeTaskMapper.updateTaskSolvedById(s.getTaskId());
}
semaphore.release();
} catch (JsonProcessingException | InterruptedException e) {
log.error("定时群发消息处理异常:ex:{}", e);
e.printStackTrace();
}
}
);
}
}
}
package com.linkwechat.quartz.task;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.dto.WePageCountDto;
import com.linkwechat.wecom.domain.dto.WePageStaticDataDto;
import com.linkwechat.wecom.domain.query.WePageStateQuery;
import com.linkwechat.wecom.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author danmo
* @description 首页数据统计
* @date 2021/2/23 23:51
**/
@Slf4j
@Component("PageHomeDataTask")
public class PageHomeDataTask {
@Autowired
private IWeCorpAccountService weCorpAccountService;
@Autowired
private IWeUserService weUserService;
@Autowired
private IWeCustomerService weCustomerService;
@Autowired
private IWeGroupService weGroupService;
@Autowired
private IWeGroupMemberService weGroupMemberService;
@Autowired
private IWeUserBehaviorDataService weUserBehaviorDataService;
@Autowired
private IWeGroupStatisticService weGroupStatisticService;
@Autowired
private RedisCache redisCache;
public void getPageHomeDataData(){
getCorpBasicData();
getCorpRealTimeData();
}
public void getCorpBasicData(){
//查询当前使用企业
//WeCorpAccount weCorpAccount = weCorpAccountService.findValidWeCorpAccount();
//String corpId = weCorpAccount.getCorpId();
Map<String,Object> totalMap = new HashMap<>(16);
//企业成员总数
int userCount = weUserService.count(new LambdaQueryWrapper<WeUser>().eq(WeUser::getIsActivate, WeConstans.WE_USER_IS_ACTIVATE));
//客户总人数
int customerCount = weCustomerService.count();
//客户群总数
int groupCount = weGroupService.count();
//群成员总数
int groupMemberCount = weGroupMemberService.count();
totalMap.put("userCount",userCount);
totalMap.put("customerCount",customerCount);
totalMap.put("groupCount",groupCount);
totalMap.put("groupMemberCount",groupMemberCount);
redisCache.setCacheMap("getCorpBasicData",totalMap);
}
public void getCorpRealTimeData(){
WePageStaticDataDto wePageStaticDataDto = new WePageStaticDataDto();
//今天
wePageStaticDataDto.setToday(getTodayData());
wePageStaticDataDto.setWeek(getWeekData());
wePageStaticDataDto.setMonth(getMonthData());
wePageStaticDataDto.setUpdateTime(DateUtil.now());
redisCache.setCacheObject("getCorpRealTimeData",wePageStaticDataDto);
}
private WePageStaticDataDto.PageStaticData getTodayData(){
/**
* 今日
*/
String today = DateUtil.today();
String yesterday = DateUtil.yesterday().toDateStr();
//客户统计
WePageCountDto nowData = weUserBehaviorDataService.getCountDataByDay(today,"day");
//客户群统计
WePageCountDto nowTimeGroupChatData = weGroupStatisticService.getCountDataByDay(today, "day");
if(nowTimeGroupChatData != null){
nowData.setChatCnt(nowTimeGroupChatData.getChatCnt());
nowData.setChatTotal(nowTimeGroupChatData.getChatTotal());
nowData.setChatHasMsg(nowTimeGroupChatData.getChatHasMsg());
nowData.setNewChatCnt(nowTimeGroupChatData.getNewChatCnt());
nowData.setNewMemberCnt(nowTimeGroupChatData.getNewMemberCnt());
nowData.setMemberTotal(nowTimeGroupChatData.getMemberTotal());
nowData.setMsgTotal(nowTimeGroupChatData.getMsgTotal());
}
/**
* 昨日
*/
//客户统计
WePageCountDto lastTime = weUserBehaviorDataService.getCountDataByDay(yesterday,"day");
//客户群统计
WePageCountDto lastGroupChatTime = weGroupStatisticService.getCountDataByDay(yesterday,"day");
if(lastGroupChatTime != null){
lastTime.setChatCnt(lastGroupChatTime.getChatCnt());
lastTime.setChatTotal(lastGroupChatTime.getChatTotal());
lastTime.setChatHasMsg(lastGroupChatTime.getChatHasMsg());
lastTime.setNewChatCnt(lastGroupChatTime.getNewChatCnt());
lastTime.setNewMemberCnt(lastGroupChatTime.getNewMemberCnt());
lastTime.setMemberTotal(lastGroupChatTime.getMemberTotal());
lastTime.setMsgTotal(lastGroupChatTime.getMsgTotal());
}
WePageStaticDataDto.PageStaticData pageStaticData = setPageStaticData(nowData, lastTime);
WePageStateQuery wePageStateQuery = new WePageStateQuery();
//获取15天前的时间
wePageStateQuery.setStartTime(DateUtil.offsetDay(new Date(), -15).toDateStr());
wePageStateQuery.setEndTime(today);
wePageStateQuery.setFew(14);
List<WePageCountDto> dayCountData = weUserBehaviorDataService.getDayCountData(wePageStateQuery);
List<WePageCountDto> dayGroupChatCountData = weGroupStatisticService.getDayCountData(wePageStateQuery);
for (WePageCountDto dayData : dayCountData) {
for (WePageCountDto dayGroupChatData : dayGroupChatCountData) {
if (dayData.getXTime().equals(dayGroupChatData.getXTime())){
dayData.setChatCnt(dayGroupChatData.getChatCnt());
dayData.setChatTotal(dayGroupChatData.getChatTotal());
dayData.setChatHasMsg(dayGroupChatData.getChatHasMsg());
dayData.setNewChatCnt(dayGroupChatData.getNewChatCnt());
dayData.setNewMemberCnt(dayGroupChatData.getNewMemberCnt());
dayData.setMemberTotal(dayGroupChatData.getMemberTotal());
dayData.setMsgTotal(dayGroupChatData.getMsgTotal());
break;
}
}
}
pageStaticData.setDataList(dayCountData);
return pageStaticData;
}
private WePageStaticDataDto.PageStaticData getWeekData(){
/**
* 本周
*/
//客户统计
WePageCountDto newTime = weUserBehaviorDataService.getCountDataByDay(DateUtil.today(),"week");
WePageCountDto nowTimeGroupChatData = weGroupStatisticService.getCountDataByDay(DateUtil.today(),"week");
if(nowTimeGroupChatData != null){
newTime.setChatCnt(nowTimeGroupChatData.getChatCnt());
newTime.setChatTotal(nowTimeGroupChatData.getChatTotal());
newTime.setChatHasMsg(nowTimeGroupChatData.getChatHasMsg());
newTime.setNewChatCnt(nowTimeGroupChatData.getNewChatCnt());
newTime.setNewMemberCnt(nowTimeGroupChatData.getNewMemberCnt());
newTime.setMemberTotal(nowTimeGroupChatData.getMemberTotal());
newTime.setMsgTotal(nowTimeGroupChatData.getMsgTotal());
}
/**
* 上周
*/
//客户统计
WePageCountDto lastTime = weUserBehaviorDataService.getCountDataByDay(DateUtil.lastWeek().toDateStr(),"week");
WePageCountDto lastTimeGroupChatData = weGroupStatisticService.getCountDataByDay(DateUtil.lastWeek().toDateStr(),"week");
if(lastTimeGroupChatData != null){
lastTime.setChatCnt(lastTimeGroupChatData.getChatCnt());
lastTime.setChatTotal(lastTimeGroupChatData.getChatTotal());
lastTime.setChatHasMsg(lastTimeGroupChatData.getChatHasMsg());
lastTime.setNewChatCnt(lastTimeGroupChatData.getNewChatCnt());
lastTime.setNewMemberCnt(lastTimeGroupChatData.getNewMemberCnt());
lastTime.setMemberTotal(lastTimeGroupChatData.getMemberTotal());
lastTime.setMsgTotal(lastTimeGroupChatData.getMsgTotal());
}
WePageStaticDataDto.PageStaticData pageStaticData = setPageStaticData(newTime, lastTime);
WePageStateQuery wePageStateQuery = new WePageStateQuery();
wePageStateQuery.setFew(5);
List<WePageCountDto> weekCountData = weUserBehaviorDataService.getWeekCountData(wePageStateQuery);
List<WePageCountDto> weekGroupChatCountData = weGroupStatisticService.getWeekCountData(wePageStateQuery);
for (WePageCountDto weekData : weekCountData) {
for (WePageCountDto weekGroupChatData : weekGroupChatCountData) {
if (weekData.getXTime().equals(weekGroupChatData.getXTime())){
weekData.setChatCnt(weekGroupChatData.getChatCnt());
weekData.setChatTotal(weekGroupChatData.getChatTotal());
weekData.setChatHasMsg(weekGroupChatData.getChatHasMsg());
weekData.setNewChatCnt(weekGroupChatData.getNewChatCnt());
weekData.setNewMemberCnt(weekGroupChatData.getNewMemberCnt());
weekData.setMemberTotal(weekGroupChatData.getMemberTotal());
weekData.setMsgTotal(weekGroupChatData.getMsgTotal());
break;
}
}
}
pageStaticData.setDataList(weekCountData);
return pageStaticData;
}
private WePageStaticDataDto.PageStaticData getMonthData(){
/**
* 本月
*/
//客户统计
WePageCountDto newTime = weUserBehaviorDataService.getCountDataByDay(DateUtil.today(),"month");
WePageCountDto nowTimeGroupChatData = weGroupStatisticService.getCountDataByDay(DateUtil.today(),"month");
if(nowTimeGroupChatData != null){
newTime.setChatCnt(nowTimeGroupChatData.getChatCnt());
newTime.setChatTotal(nowTimeGroupChatData.getChatTotal());
newTime.setChatHasMsg(nowTimeGroupChatData.getChatHasMsg());
newTime.setNewChatCnt(nowTimeGroupChatData.getNewChatCnt());
newTime.setNewMemberCnt(nowTimeGroupChatData.getNewMemberCnt());
newTime.setMemberTotal(nowTimeGroupChatData.getMemberTotal());
newTime.setMsgTotal(nowTimeGroupChatData.getMsgTotal());
}
/**
* 上月
*/
//客户统计
WePageCountDto lastTime = weUserBehaviorDataService.getCountDataByDay(DateUtil.lastMonth().toDateStr(),"month");
WePageCountDto lastTimeGroupChatData = weGroupStatisticService.getCountDataByDay(DateUtil.lastMonth().toDateStr(),"month");
if(lastTimeGroupChatData != null){
lastTime.setChatCnt(lastTimeGroupChatData.getChatCnt());
lastTime.setChatTotal(lastTimeGroupChatData.getChatTotal());
lastTime.setChatHasMsg(lastTimeGroupChatData.getChatHasMsg());
lastTime.setNewChatCnt(lastTimeGroupChatData.getNewChatCnt());
lastTime.setNewMemberCnt(lastTimeGroupChatData.getNewMemberCnt());
lastTime.setMemberTotal(lastTimeGroupChatData.getMemberTotal());
lastTime.setMsgTotal(lastTimeGroupChatData.getMsgTotal());
}
WePageStaticDataDto.PageStaticData pageStaticData = setPageStaticData(newTime, lastTime);
WePageStateQuery wePageStateQuery = new WePageStateQuery();
wePageStateQuery.setFew(5);
List<WePageCountDto> monthCountData = weUserBehaviorDataService.getMonthCountData(wePageStateQuery);
List<WePageCountDto> monthGroupChatCountData = weGroupStatisticService.getMonthCountData(wePageStateQuery);
for (WePageCountDto monthData : monthCountData) {
for (WePageCountDto monthGroupChatData : monthGroupChatCountData) {
if (monthData.getXTime().equals(monthGroupChatData.getXTime())){
monthData.setChatCnt(monthGroupChatData.getChatCnt());
monthData.setChatTotal(monthGroupChatData.getChatTotal());
monthData.setChatHasMsg(monthGroupChatData.getChatHasMsg());
monthData.setNewChatCnt(monthGroupChatData.getNewChatCnt());
monthData.setNewMemberCnt(monthGroupChatData.getNewMemberCnt());
monthData.setMemberTotal(monthGroupChatData.getMemberTotal());
monthData.setMsgTotal(monthGroupChatData.getMsgTotal());
break;
}
}
}
pageStaticData.setDataList(monthCountData);
return pageStaticData;
}
private WePageStaticDataDto.PageStaticData setPageStaticData(WePageCountDto nowTime,WePageCountDto lastTime){
WePageStaticDataDto.PageStaticData pageStaticData = new WePageStaticDataDto.PageStaticData();
pageStaticData.setNewApplyCnt(nowTime.getNewApplyCnt());
pageStaticData.setNewApplyCntDiff(nowTime.getNewApplyCnt() - lastTime.getNewApplyCnt());
pageStaticData.setNegativeFeedbackCnt(nowTime.getNegativeFeedbackCnt());
pageStaticData.setNegativeFeedbackCntDiff(nowTime.getNegativeFeedbackCnt() - lastTime.getNegativeFeedbackCnt());
pageStaticData.setNewContactCnt(nowTime.getNewContactCnt());
pageStaticData.setNewContactCntDiff(nowTime.getNewContactCnt() - lastTime.getNewContactCnt());
pageStaticData.setNewMemberCnt(nowTime.getNewMemberCnt());
pageStaticData.setNewMemberCntDiff(nowTime.getNewMemberCnt() - lastTime.getNewMemberCnt());
return pageStaticData;
}
}
package com.linkwechat.quartz.task;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.utils.bean.BeanUtils;
import com.linkwechat.wecom.client.WeCustomerClient;
import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.WeUserBehaviorData;
import com.linkwechat.wecom.domain.dto.UserBehaviorDataDto;
import com.linkwechat.wecom.domain.query.UserBehaviorDataQuery;
import com.linkwechat.wecom.service.IWeUserBehaviorDataService;
import com.linkwechat.wecom.service.IWeUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @author sxw
* @description 联系客户统计
* @date 2021/2/24 0:41
**/
@Slf4j
@Component("UserBehaviorDataTak")
public class UserBehaviorDataTak {
@Autowired
private WeCustomerClient weCustomerClient;
@Autowired
private IWeUserService weUserService;
@Autowired
private IWeUserBehaviorDataService weUserBehaviorDataService;
private final int offset = 500;
public void getUserBehaviorData() {
log.info("联系客户统计>>>>>>>>>>>>>>>>>>>启动");
LambdaQueryWrapper<WeUser> wrapper = new LambdaQueryWrapper<WeUser>().eq(WeUser::getIsActivate, WeConstans.WE_USER_IS_ACTIVATE);
int userCount = weUserService.count(wrapper);
log.info("联系客户统计>>>>>>>>>>>>>>>>>>>userCount:{}",userCount);
double num = 1;
if (userCount > offset) {
num = Math.ceil((double) userCount / offset);
}
int temp = 0;
for (int i = 0; i < num; i++) {
wrapper.last("limit " + temp + "," + offset);
List<WeUser> list = weUserService.list(wrapper);
if (CollectionUtil.isNotEmpty(list)) {
List<WeUserBehaviorData> dataList = new ArrayList<>();
UserBehaviorDataQuery query = new UserBehaviorDataQuery();
//前一天的数据
Long startTime = strToDate(-1, 0);
Long endTime = strToDate(-1, 1);
query.setStart_time(startTime);
query.setEnd_time(endTime);
list.forEach(weUser -> {
List<String> idList = new ArrayList<>();
idList.add(weUser.getUserId());
query.setUserid(idList);
try {
UserBehaviorDataDto userBehaviorData = weCustomerClient.getUserBehaviorData(query);
List<UserBehaviorDataDto.BehaviorData> behaviorDataList = userBehaviorData.getBehaviorData();
for (UserBehaviorDataDto.BehaviorData data : behaviorDataList) {
WeUserBehaviorData weUserBehaviorData = new WeUserBehaviorData();
BeanUtils.copyPropertiesignoreOther(data, weUserBehaviorData);
weUserBehaviorData.setUserId(weUser.getUserId());
dataList.add(weUserBehaviorData);
}
} catch (ForestRuntimeException e) {
e.printStackTrace();
log.error("员工数据拉取失败: userId:【{}】,ex:【{}】",weUser.getUserId(),e.getStackTrace());
}
});
weUserBehaviorDataService.saveBatch(dataList);
}
temp += offset;
}
}
private Long strToDate(int days, Integer type) {
Long time = null;
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
Calendar cale = Calendar.getInstance();
cale.add(Calendar.DATE, days);
String tarday = new SimpleDateFormat("yyyy-MM-dd").format(cale.getTime());
if (type.equals(0)) {
tarday += " 00:00:00";
} else {
tarday += " 23:59:59";
}
// String转Date
try {
date = format2.parse(tarday);
System.out.println(date.getTime());
time = date.getTime() / 1000;
System.out.println(time.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册