Commit 9c49ccf5 authored by 钱坤's avatar 钱坤

早报发送的长链改为短链

新增后门接口强制重发某个机构某日的早报,
parent e0a56e9f
package com.tanpu.feo.feojob.controller; package com.tanpu.feo.feojob.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.tanpu.common.api.CommonResp; import com.tanpu.common.api.CommonResp;
import com.tanpu.feo.feojob.dao.user.entity.DaySubjectEntity;
import com.tanpu.feo.feojob.dto.OrgSyncDto; import com.tanpu.feo.feojob.dto.OrgSyncDto;
import com.tanpu.feo.feojob.jobs.DaySubJob; import com.tanpu.feo.feojob.jobs.DaySubJob;
import com.tanpu.feo.feojob.jobs.OrgSyncByWxcpJob; import com.tanpu.feo.feojob.jobs.OrgSyncByWxcpJob;
...@@ -9,13 +11,13 @@ import com.tanpu.feo.feojob.service.OrgSyncByJyzyService; ...@@ -9,13 +11,13 @@ import com.tanpu.feo.feojob.service.OrgSyncByJyzyService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@RestController @RestController
@Api(tags = "手动执行 早报推送") @Api(tags = "手动执行 早报推送")
...@@ -33,4 +35,15 @@ public class DaySubjectController { ...@@ -33,4 +35,15 @@ public class DaySubjectController {
return CommonResp.success("执行完成"); return CommonResp.success("执行完成");
} }
@RequestMapping(value = "/sendOneOrgDaySubject", method = RequestMethod.GET)
@ApiOperation(value = "单机构早报推送")
public CommonResp<String> sendOneOrgDaySubject(@RequestParam String orgId, @RequestParam String date) {
List<DaySubjectEntity> daySubjectList = daySubJob.getDaySubject(date, orgId);
List<DaySubjectEntity> list = daySubjectList.stream().filter(p -> StringUtils.equals(p.getDsPt(), orgId))
.collect(Collectors.toList());
String oneMorningTitle = daySubJob.getOneMorningTitle();
daySubJob.sendDaySubject(orgId, list, oneMorningTitle);
return CommonResp.success("执行完成");
}
} }
package com.tanpu.feo.feojob.feign.shorter;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
@FeignClient(value = "shorturl", url = "${tanpu.feo-shorter.svc:}", path = "/")
public interface ShorterFeign {
@PostMapping("/shorter/get")
ShorterResp getShorter(String url);
}
package com.tanpu.feo.feojob.feign.shorter;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class ShorterResp<T> {
private int code;
private String msg;
private T result;
public ShorterResp(int code, String msg) {
this.code = code;
this.msg = msg;
}
public boolean isSuccess() {
return code == 200;
}
public boolean isNotSuccess() {
return !isSuccess();
}
}
...@@ -13,6 +13,8 @@ import com.tanpu.common.constant.BizStatus; ...@@ -13,6 +13,8 @@ import com.tanpu.common.constant.BizStatus;
import com.tanpu.feo.feojob.config.MorningPaperProperties; import com.tanpu.feo.feojob.config.MorningPaperProperties;
import com.tanpu.feo.feojob.dao.user.entity.*; import com.tanpu.feo.feojob.dao.user.entity.*;
import com.tanpu.feo.feojob.dao.user.mapper.*; import com.tanpu.feo.feojob.dao.user.mapper.*;
import com.tanpu.feo.feojob.feign.shorter.ShorterFeign;
import com.tanpu.feo.feojob.feign.shorter.ShorterResp;
import com.tanpu.feo.feojob.feign.wxcp.FeignClientForWxCp; import com.tanpu.feo.feojob.feign.wxcp.FeignClientForWxCp;
import com.tanpu.feo.feojob.feign.wxmp.FeignClientForWxMp; import com.tanpu.feo.feojob.feign.wxmp.FeignClientForWxMp;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -60,6 +62,8 @@ public class DaySubJob { ...@@ -60,6 +62,8 @@ public class DaySubJob {
private DaySelectionMapper daySelectionMapper; private DaySelectionMapper daySelectionMapper;
@Resource @Resource
private OrgMapper orgMapper; private OrgMapper orgMapper;
@Resource
private ShorterFeign shorterFeign;
@Scheduled(cron = "0 30 7 * * ? ") // @Scheduled(cron = "0 30 7 * * ? ") //
...@@ -82,78 +86,122 @@ public class DaySubJob { ...@@ -82,78 +86,122 @@ public class DaySubJob {
Map<String, List<DaySubjectEntity>> stringListMap = daySubjectList.stream().collect(Collectors.groupingBy(DaySubjectEntity::getDsPt)); Map<String, List<DaySubjectEntity>> stringListMap = daySubjectList.stream().collect(Collectors.groupingBy(DaySubjectEntity::getDsPt));
int size = morningPaperProperties.getMainTitles().size(); int size = morningPaperProperties.getMainTitles().size();
String mainTitle = morningPaperProperties.getMainTitles().get(RandomUtil.randomInt(0, size)); String mainTitle = morningPaperProperties.getMainTitles().get(RandomUtil.randomInt(0, size));
stringListMap.keySet().forEach(orgId -> { for (String orgId : stringListMap.keySet()) {
log.info("开始推送机构(orgId={})的早报", orgId);
// 查询 机构配置信息 //查询orgCode
OrgExtEntity orgExtEntity = orgExtMapper.selectOne(new LambdaQueryWrapper<OrgExtEntity>() OrgEntity orgEntity = orgMapper.selectOne(new LambdaQueryWrapper<OrgEntity>().eq(OrgEntity::getId, orgId).last("limit 1"));
.eq(OrgExtEntity::getOrgId, orgId) if (ObjectUtil.isNull(orgEntity)) {
.eq(OrgExtEntity::getDeleteTag, BizStatus.DeleteTag.tag_init_str) log.error("找不到机构(orgId={})的信息", orgId);
.last("limit 1")); return;
String jsonKey = ObjectUtil.isNotNull(orgExtEntity) ? orgExtEntity.getJsonWxcpKey() : null;
if (StrUtil.isNotBlank(jsonKey)) {
//在orgExt 表中有查到 jsonKey 执行消息推送任务
JSONObject jsonKeyInfo = JSONUtil.parseObj(jsonKey);
// 查询该机构下 用户
LambdaQueryWrapper<UserInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfoEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
.eq(UserInfoEntity::getOrgId, orgId)
.eq(UserInfoEntity::getLevel, "2");
List<UserInfoEntity> userInfoEntities = userInfoMapper.selectList(queryWrapper);
//查询orgCode
OrgEntity orgEntity = orgMapper.selectOne(new LambdaQueryWrapper<OrgEntity>().eq(OrgEntity::getId, orgId).last("limit 1"));
if (ObjectUtil.isNotNull(orgEntity)) {
userInfoEntities.stream().forEach(userInfoEntity -> {
List<DaySubjectEntity> daySubjectEntities = stringListMap.get(orgId);
daySubjectEntities.forEach(daySubjectEntity -> {
HashMap<String, String> parmMap = new HashMap<>();
parmMap.put("type", "text");
String wxOpenId = userInfoEntity.getUiOpenid();
String wxcpUId = userInfoEntity.getWxcpUid();
if (StringUtils.isNotBlank(wxcpUId)) {
// 1 企业微信 推送
String agentId = jsonKeyInfo.getStr("agentId");
String corpId = jsonKeyInfo.getStr("corpId");
// ww=corpId&orgCode=orgCode
StringBuilder sb = new StringBuilder();
sb.append(mainTitle).append("\n\n")
.append(morningPaperProperties.getSubTitle()).append("\n\n")
.append("<a href=\"")
.append(String.format(daySubjectUrl, corpId, orgEntity.getOrgCode(), daySubjectEntity.getId())).append("\">")
.append("快去转发获客吧")
.append("</a>\n\n");
parmMap.put("content", sb.toString());
log.info("===企业微信 推送===userID:{} agentId:{} corpId:{} wxcpUId:{} parmMap:{}",userInfoEntity.getId() , agentId, corpId, wxcpUId, JSONUtil.toJsonStr(parmMap));
if (StrUtil.isNotBlank(agentId) && StrUtil.isNotBlank(corpId) && StrUtil.isNotBlank(wxcpUId) && StrUtil.isNotBlank(JSONUtil.toJsonStr(parmMap))) {
feignClientForWxCp.sendMessage(agentId, corpId, wxcpUId, JSONUtil.toJsonStr(parmMap));
}
}
if (StringUtils.isNotBlank(wxOpenId)) {
// 2 公众号推送
String wxAppID = jsonKeyInfo.getStr("sendMessageAppId");
// ww=wxAppID&orgCode=orgCode
StringBuilder sb = new StringBuilder();
sb.append(mainTitle).append("\n\n")
.append(morningPaperProperties.getSubTitle()).append("\n\n")
.append("<a href=\"")
.append(String.format(daySubjectUrl, wxAppID, orgEntity.getOrgCode(), daySubjectEntity.getId())).append("\">")
.append("快去转发获客吧")
.append("</a>\n\n");
parmMap.put("content", sb.toString());
log.info("===公众号 推送=== userID:{} wxAppID:{} wxOpenId:{} parmMap:{}",userInfoEntity.getId() , wxAppID, wxOpenId, JSONUtil.toJsonStr(parmMap));
if (StrUtil.isNotBlank(wxAppID) && StrUtil.isNotBlank(wxOpenId) && StrUtil.isNotBlank(JSONUtil.toJsonStr(parmMap))) {
feignClientForWxMp.sendMessage(wxAppID, wxOpenId, JSONUtil.toJsonStr(parmMap));
}
} else {
log.error("======用户ID:{} 没有微信或企业微信id======", userInfoEntity.getId());
}
});
});
}
} }
}); List<DaySubjectEntity> daySubjectEntities = stringListMap.get(orgId);
if (CollectionUtils.isEmpty(daySubjectEntities)) {
log.warn("当前机构(orgCode={})没有要推送的早报", orgEntity.getOrgCode());
return;
}
sendDaySubject(orgId, daySubjectEntities, mainTitle);
}
}
public void sendDaySubject(String orgId, List<DaySubjectEntity> daySubjectList,String mainTitle){
if (CollectionUtils.isEmpty(daySubjectList) || StringUtils.isBlank(orgId) || StringUtils.isBlank(mainTitle)) {
return;
}
OrgEntity orgEntity = orgMapper.selectOne(new LambdaQueryWrapper<OrgEntity>().eq(OrgEntity::getId, orgId).last("limit 1"));
if (orgEntity == null) {
return;
}
// 查询 机构配置信息
OrgExtEntity orgExtEntity = orgExtMapper.selectOne(new LambdaQueryWrapper<OrgExtEntity>()
.eq(OrgExtEntity::getOrgId, orgId)
.eq(OrgExtEntity::getDeleteTag, BizStatus.DeleteTag.tag_init_str)
.last("limit 1"));
String jsonKey = ObjectUtil.isNotNull(orgExtEntity) ? orgExtEntity.getJsonWxcpKey() : null;
if (StringUtils.isBlank(jsonKey)) {
return;
}
//在orgExt 表中有查到 jsonKey 执行消息推送任务
JSONObject jsonKeyInfo = JSONUtil.parseObj(jsonKey);
// 查询该机构下 用户
LambdaQueryWrapper<UserInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserInfoEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
.eq(UserInfoEntity::getOrgId, orgId)
.eq(UserInfoEntity::getLevel, "2");
List<UserInfoEntity> userInfoEntities = userInfoMapper.selectList(queryWrapper);
if (CollectionUtils.isEmpty(userInfoEntities)) {
log.info("机构(orgCode={})没有需要推送早报的人员", orgEntity.getOrgCode());
return;
}
log.info("机构(orgCode={})本次推送{}人,早报文章{}篇", orgEntity.getOrgCode(), userInfoEntities.size(), daySubjectList.size());
for (UserInfoEntity userInfoEntity : userInfoEntities) {
for (DaySubjectEntity daySubjectEntity : daySubjectList) {
HashMap<String, String> parmMap = new HashMap<>();
parmMap.put("type", "text");
String wxOpenId = userInfoEntity.getUiOpenid();
String wxcpUId = userInfoEntity.getWxcpUid();
if (StringUtils.isNotBlank(wxcpUId)) {
// 1 企业微信 推送
String agentId = jsonKeyInfo.getStr("agentId");
String corpId = jsonKeyInfo.getStr("corpId");
// ww=corpId&orgCode=orgCode
String link = String.format(daySubjectUrl, corpId, orgEntity.getOrgCode(), daySubjectEntity.getId());
String url = getShortUrl(link);
if (StringUtils.isBlank(url)) {
log.error("早报的短链生成失败, orgCode: {}, link: {}", orgEntity.getOrgCode(), link);
continue;
}
log.info("早报推送使用短链, short-url: {}, origin-url: {}", url, link);
StringBuilder sb = new StringBuilder();
sb.append(mainTitle).append("\n\n")
.append(morningPaperProperties.getSubTitle()).append("\n\n")
.append("<a href=\"")
.append(url).append("\">")
.append("快去转发获客吧")
.append("</a>\n\n");
parmMap.put("content", sb.toString());
log.info("===企业微信 推送===userID:{} agentId:{} corpId:{} wxcpUId:{} parmMap:{}", userInfoEntity.getId(), agentId, corpId, wxcpUId, JSONUtil.toJsonStr(parmMap));
if (StrUtil.isNotBlank(agentId) && StrUtil.isNotBlank(corpId) && StrUtil.isNotBlank(wxcpUId) && StrUtil.isNotBlank(JSONUtil.toJsonStr(parmMap))) {
feignClientForWxCp.sendMessage(agentId, corpId, wxcpUId, JSONUtil.toJsonStr(parmMap));
}
}
if (StringUtils.isNotBlank(wxOpenId)) {
// 2 公众号推送
String wxAppID = jsonKeyInfo.getStr("sendMessageAppId");
// ww=wxAppID&orgCode=orgCode
String link = String.format(daySubjectUrl, wxAppID, orgEntity.getOrgCode(), daySubjectEntity.getId());
String url = getShortUrl(link);
if (StringUtils.isBlank(url)) {
log.error("早报的短链生成失败, orgCode: {}, link: {}", orgEntity.getOrgCode(), link);
continue;
}
log.info("早报推送使用短链, short-url: {}, origin-url: {}", url, link);
StringBuilder sb = new StringBuilder();
sb.append(mainTitle).append("\n\n")
.append(morningPaperProperties.getSubTitle()).append("\n\n")
.append("<a href=\"")
.append(url).append("\">")
.append("快去转发获客吧")
.append("</a>\n\n");
parmMap.put("content", sb.toString());
log.info("===公众号 推送=== userID:{} wxAppID:{} wxOpenId:{} parmMap:{}", userInfoEntity.getId(), wxAppID, wxOpenId, JSONUtil.toJsonStr(parmMap));
if (StrUtil.isNotBlank(wxAppID) && StrUtil.isNotBlank(wxOpenId) && StrUtil.isNotBlank(JSONUtil.toJsonStr(parmMap))) {
feignClientForWxMp.sendMessage(wxAppID, wxOpenId, JSONUtil.toJsonStr(parmMap));
}
} else {
if (userInfoEntity.getUiTelphone() != null && userInfoEntity.getUiTelphone().startsWith("1700000")) {
log.info("内部测试账号,跳过, uid: {}, telphone: {}", userInfoEntity.getId(), userInfoEntity.getUiTelphone());
continue;
}
log.error("======用户ID:{} 没有微信或企业微信id======", userInfoEntity.getId());
}
}
}
} }
private List<DaySubjectEntity> getDaySubList(String today) {
public List<DaySubjectEntity> getDaySubList(String today) {
//查询今日早报 已上架 //查询今日早报 已上架
LambdaQueryWrapper<DaySubjectEntity> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<DaySubjectEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DaySubjectEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str) queryWrapper.eq(DaySubjectEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
...@@ -171,6 +219,28 @@ public class DaySubJob { ...@@ -171,6 +219,28 @@ public class DaySubJob {
return daySubjectEntityList; return daySubjectEntityList;
} }
public List<DaySubjectEntity> getDaySubject(String date, String orgId) {
LambdaQueryWrapper<DaySubjectEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DaySubjectEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
.eq(DaySubjectEntity::getDsDate, DateUtil.parseDate(date))
.eq(DaySubjectEntity::getDsPt, orgId)
.eq(DaySubjectEntity::getDsStatus, "1");
return daySubjectMapper.selectList(queryWrapper);
}
public String getOneMorningTitle() {
int size = morningPaperProperties.getMainTitles().size();
return morningPaperProperties.getMainTitles().get(RandomUtil.randomInt(0, size));
}
private String getShortUrl(String url) {
ShorterResp resp = shorterFeign.getShorter(url);
if (resp.isNotSuccess()) {
return null;
}
return String.valueOf(resp.getResult());
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
log.info("========================="); log.info("=========================");
......
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