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

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

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

See merge request !101
parents fe0d68d5 b1f1b7c4
......@@ -7,6 +7,7 @@ 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 com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -96,6 +97,9 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
// 游标值
private String cursorValue = "";
// 分组数
private Integer groupSize = 1000;
@PostConstruct
private void init() {
// 公众号
......@@ -143,35 +147,40 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
WechatUserListResponse wechatUserListResponse = JSONObject.parseObject(response, WechatUserListResponse.class);
if (wechatUserListResponse.getErrcode() == null && wechatUserListResponse.getCount() > 0) {
nextOpenId = wechatUserListResponse.getNext_openid();
List<String> opendIdList = wechatUserListResponse.getData().get("openid");
if (!opendIdList.isEmpty()) {
String[] openIds = opendIdList.toArray(new String[0]);
String openIdStrs = String.join("\',\'", openIds);
openIdStrs = "\'" + openIdStrs + "\'";
// 查询微信信息
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);
List<String> opendIdAllList = wechatUserListResponse.getData().get("openid");
if (!opendIdAllList.isEmpty()) {
List<List<String>> openIdLists = Lists.partition(opendIdAllList, groupSize);
for (List<String> opendIdList : openIdLists) {
String[] openIds = opendIdList.toArray(new String[0]);
String openIdStrs = String.join("\',\'", openIds);
openIdStrs = "\'" + openIdStrs + "\'";
// 查询微信信息
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);
System.out.println(wxSql);
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));
}
// 添加公众号数据
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);
}
return JsonResult.buildSuccessResult("", opendIdList);
return JsonResult.buildSuccessResult("", opendIdAllList);
} else {
return null;
}
......
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