Commit fe0d68d5 authored by 李健华's avatar 李健华

Merge branch 'hotfix/wechat-job-sql-optimize-20221024' into 'master'

优化微信定时任务SQL慢查询

See merge request !100
parents aedb4c4d f2ad7a5d
......@@ -2,24 +2,11 @@ package cn.quantgroup.xyqb.service.wechat.impl;
import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.model.webchat.AccessTokenResponse;
import cn.quantgroup.xyqb.model.webchat.CustomerListResponse;
import cn.quantgroup.xyqb.model.webchat.DepartmentInfoResponse;
import cn.quantgroup.xyqb.model.webchat.DepartmentListResponse;
import cn.quantgroup.xyqb.model.webchat.WechatUserListResponse;
import cn.quantgroup.xyqb.model.webchat.*;
import cn.quantgroup.xyqb.repository.IWeChatInfoRelationRepository;
import cn.quantgroup.xyqb.service.http.IHttpService;
import cn.quantgroup.xyqb.service.wechat.IWechatFollowService;
import com.alibaba.fastjson.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -32,6 +19,13 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* Created by Miraculous on 2017/1/19.
*/
......@@ -89,9 +83,13 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
// 公众号
private final Integer WECHAT_ACCOUNT = 1;
private final String WECHAT_ACCOUNT_STR = "1";
//企业微信
private final Integer ENTERPRISE_WECHAT = 2;
private final String ENTERPRISE_WECHAT_STR = "2";
// 公众号类型
private final String WECHAT_XYQB = "xyqb";
......@@ -150,9 +148,25 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
String[] openIds = opendIdList.toArray(new String[0]);
String openIdStrs = String.join("\',\'", openIds);
openIdStrs = "\'" + openIdStrs + "\'";
// 添加公众号数据
String sql = String.format("INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `task_period`) SELECT `user_id`, `open_id`, `union_id`, '%s', %s from wechat_userinfo where app_name='%s' and user_id is not null and `open_id` IN (%s)", WECHAT_ACCOUNT, period, WECHAT_XYQB, openIdStrs);
jdbcTemplate.update(sql);
// 查询微信信息
String wxSql = String.format("SELECT `user_id`, `open_id`, `union_id` from wechat_userinfo where app_name='%s' and user_id is not null and `open_id` IN (%s)", WECHAT_XYQB, openIdStrs);
List<Map<String, Object>> wxInfoList = jdbcTemplate.queryForList(wxSql);
ArrayList<Object[]> objectArrayList = new ArrayList<>();
if (!wxInfoList.isEmpty()) {
for (Map<String, Object> wxInfo : wxInfoList) {
String userId = String.valueOf(wxInfo.get("user_id"));
String openId = String.valueOf(wxInfo.get("open_id"));
String unionId = String.valueOf(wxInfo.get("union_id"));
Object[] objects = new Object[]{userId, openId, unionId, WECHAT_ACCOUNT_STR, period};
objectArrayList.add(objects);
}
// 添加公众号数据
String sql = "INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `task_period`) values (?,?,?,?,?)";
long stime = System.currentTimeMillis();
jdbcTemplate.batchUpdate(sql, objectArrayList);
long etime = System.currentTimeMillis();
System.out.printf("执行时间:%d 毫秒", (etime - stime));
}
}
if (nextOpenId != "") {
executeWechatFollowStatus(nextOpenId, period);
......@@ -206,7 +220,6 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
DepartmentListResponse departmentListResponse = JSONObject.parseObject(departmentRep, DepartmentListResponse.class);
if (!departmentListResponse.getUserlist().isEmpty()) {
List<String> userIdList = departmentListResponse.getUserlist().stream().map(DepartmentInfoResponse::getUserid).collect(Collectors.toList());
System.out.println(userIdList);
if (!userIdList.isEmpty()) {
// 批量查询客户详情
batchQueryCustomerDetailInfo2(token, userIdList, "", period);
......@@ -275,9 +288,25 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
String[] unionIds = unionIdAll.toArray(new String[0]);
String unionIdStrs = String.join("\',\'", unionIds);
unionIdStrs = "\'" + unionIdStrs + "\'";
// String sql = String.format("INSERT INTO wechat_enterprise(`unionid`) values ('%s')", String.join(",", unionIdAll));
String sql = String.format("INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `task_period`) SELECT `user_id`, `open_id`, `union_id`, '%s', %s from wechat_userinfo where user_id is not null and `union_id` IN (%s)", ENTERPRISE_WECHAT, period, unionIdStrs);
jdbcTemplate.update(sql);
// 查询微信信息
String wxSql = String.format(" SELECT `user_id`, `open_id`, `union_id` from wechat_userinfo where user_id is not null and `union_id` IN (%s)", unionIdStrs);
List<Map<String, Object>> wxInfoList = jdbcTemplate.queryForList(wxSql);
ArrayList<Object[]> objectArrayList = new ArrayList<>();
if (!wxInfoList.isEmpty()) {
for (Map<String, Object> wxInfo : wxInfoList) {
String userId = String.valueOf(wxInfo.get("user_id"));
String openId = String.valueOf(wxInfo.get("open_id"));
String unionId = String.valueOf(wxInfo.get("union_id"));
Object[] objects = new Object[]{userId, openId, unionId, ENTERPRISE_WECHAT_STR, period};
objectArrayList.add(objects);
}
// 添加公众号数据
String sql = "INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `task_period`) values (?,?,?,?,?)";
long stime = System.currentTimeMillis();
jdbcTemplate.batchUpdate(sql, objectArrayList);
long etime = System.currentTimeMillis();
System.out.printf("执行时间:%d 毫秒", (etime - stime));
}
}
// 根据unionid 添加现有指定信息
......@@ -315,9 +344,25 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
String[] unionIds = unionIdAll.toArray(new String[0]);
String unionIdStrs = String.join("\',\'", unionIds);
unionIdStrs = "\'" + unionIdStrs + "\'";
// String sql = String.format("INSERT INTO wechat_enterprise(`unionid`) values ('%s')", String.join(",", unionIdAll));
String sql = String.format("INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `task_period`) SELECT `user_id`, `open_id`, `union_id`, '%s', %s from wechat_userinfo where user_id is not null and `union_id` IN (%s)", ENTERPRISE_WECHAT, period, unionIdStrs);
jdbcTemplate.update(sql);
// 查询微信信息
String wxSql = String.format(" SELECT `user_id`, `open_id`, `union_id` from wechat_userinfo where user_id is not null and `union_id` IN (%s)", unionIdStrs);
List<Map<String, Object>> wxInfoList = jdbcTemplate.queryForList(wxSql);
ArrayList<Object[]> objectArrayList = new ArrayList<>();
if (!wxInfoList.isEmpty()) {
for (Map<String, Object> wxInfo : wxInfoList) {
String userId = String.valueOf(wxInfo.get("user_id"));
String openId = String.valueOf(wxInfo.get("open_id"));
String unionId = String.valueOf(wxInfo.get("union_id"));
Object[] objects = new Object[]{userId, openId, unionId, ENTERPRISE_WECHAT_STR, period};
objectArrayList.add(objects);
}
// 添加公众号数据
String sql = "INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `task_period`) values (?,?,?,?,?)";
long stime = System.currentTimeMillis();
jdbcTemplate.batchUpdate(sql, objectArrayList);
long etime = System.currentTimeMillis();
System.out.printf("执行时间:%d 毫秒", (etime - stime));
}
}
// 根据unionid 添加现有指定信息
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment