|
|
|
@ -3,6 +3,8 @@ package com.ruoyi.quartz.task;
|
|
|
|
|
import cn.hutool.extra.template.Template;
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.google.gson.internal.LinkedTreeMap;
|
|
|
|
|
import com.lark.oapi.service.bitable.v1.model.AppTableRecord;
|
|
|
|
|
import com.lark.oapi.service.bitable.v1.model.ListAppTableRecordRespBody;
|
|
|
|
|
import com.ruoyi.common.enums.*;
|
|
|
|
|
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
|
|
|
|
@ -51,127 +53,137 @@ public class ScheduledRemindersTask {
|
|
|
|
|
@Autowired
|
|
|
|
|
private EventLogMapper eventLogMapper;
|
|
|
|
|
|
|
|
|
|
/**查询分页大小*/
|
|
|
|
|
/**
|
|
|
|
|
* 查询分页大小
|
|
|
|
|
*/
|
|
|
|
|
private static final Integer PAGE_SIZE = 20;
|
|
|
|
|
/**查询分页大小*/
|
|
|
|
|
/**
|
|
|
|
|
* 查询分页大小
|
|
|
|
|
*/
|
|
|
|
|
private static final String RANGE_KEY = "日期";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void scheduledReminders() {
|
|
|
|
|
log.info("scheduledRemindersTask start");
|
|
|
|
|
List<LarkCompanyTableInfo> larkList = this.getLarkList();
|
|
|
|
|
if (CollectionUtils.isEmpty(larkList)){
|
|
|
|
|
if (CollectionUtils.isEmpty(larkList)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
List<EventLog> logList = new ArrayList<>();
|
|
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
// LocalDateTime now = LocalDateTime.of(2023, 4, 22, 10, 0);
|
|
|
|
|
for (LarkCompanyTableInfo larkCompanyTableInfo : larkList) {
|
|
|
|
|
try {
|
|
|
|
|
List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo);
|
|
|
|
|
for (LarkTableConfiguration larkTableConfiguration : larkConfiguration) {
|
|
|
|
|
ConfigurationSubTypeEnum subType = ConfigurationSubTypeEnum.getByCode(larkTableConfiguration.getSubConfigType());
|
|
|
|
|
if (subType == null){
|
|
|
|
|
throw new RuntimeException(String.format("tableId:%s 配置子类:%s 无对应策略处理逻辑",larkTableConfiguration.getLarkTableId(),larkTableConfiguration.getSubConfigType()));
|
|
|
|
|
if (subType == null) {
|
|
|
|
|
throw new RuntimeException(String.format("tableId:%s 配置子类:%s 无对应策略处理逻辑", larkTableConfiguration.getLarkTableId(), larkTableConfiguration.getSubConfigType()));
|
|
|
|
|
}
|
|
|
|
|
if (ConfigurationSubTypeEnum.REMINDER_AROUND.equals(subType)){
|
|
|
|
|
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration,ConfigurationSubTypeEnum.REMINDER_BEFORE,RANGE_KEY,LocalDateTime.now());
|
|
|
|
|
this.sendMessage(script,larkCompanyTableInfo,logList);
|
|
|
|
|
script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration,ConfigurationSubTypeEnum.REMINDER_AFTER,RANGE_KEY,LocalDateTime.now());
|
|
|
|
|
this.sendMessage(script,larkCompanyTableInfo,logList);
|
|
|
|
|
}else {
|
|
|
|
|
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration,subType,RANGE_KEY,LocalDateTime.now());
|
|
|
|
|
this.sendMessage(script,larkCompanyTableInfo,logList);
|
|
|
|
|
if (ConfigurationSubTypeEnum.REMINDER_AROUND.equals(subType)) {
|
|
|
|
|
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_BEFORE, RANGE_KEY, now);
|
|
|
|
|
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration);
|
|
|
|
|
script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_AFTER, RANGE_KEY, now);
|
|
|
|
|
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration);
|
|
|
|
|
} else {
|
|
|
|
|
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, subType, RANGE_KEY, now);
|
|
|
|
|
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
log.error("ScheduledRemindersTask#scheduledReminders error request:{}",JSONObject.toJSONString(larkCompanyTableInfo),e);
|
|
|
|
|
new EventLog(larkCompanyTableInfo.getId(),EventOperateType.SCHEDULE_REMINDER.getCode(), JSONObject.toJSONString(larkCompanyTableInfo),null,e.getMessage());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("ScheduledRemindersTask#scheduledReminders error request:{}", JSONObject.toJSONString(larkCompanyTableInfo), e);
|
|
|
|
|
logList.add(new EventLog(larkCompanyTableInfo.getId(), EventOperateType.SCHEDULE_REMINDER.getCode(), JSONObject.toJSONString(larkCompanyTableInfo), null, e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (CollectionUtils.isNotEmpty(logList)){
|
|
|
|
|
if (CollectionUtils.isNotEmpty(logList)) {
|
|
|
|
|
eventLogMapper.insertBatchEventLog(logList);
|
|
|
|
|
}
|
|
|
|
|
log.info("scheduledRemindersTask end");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendMessage(String script,LarkCompanyTableInfo larkCompanyTableInfo,List<EventLog> logList){
|
|
|
|
|
private void sendMessage(String script, LarkCompanyTableInfo larkCompanyTableInfo, List<EventLog> logList,LarkTableConfiguration larkTableConfiguration) {
|
|
|
|
|
String pageToken = null;
|
|
|
|
|
LarkTableRequest request = this.buildRequest(larkCompanyTableInfo, script);
|
|
|
|
|
LarkTemplate template = this.getTemplate(larkCompanyTableInfo);
|
|
|
|
|
LarkTemplate template = this.getTemplate(larkCompanyTableInfo,larkTableConfiguration.getId());
|
|
|
|
|
do {
|
|
|
|
|
JSONObject msg = new JSONObject();
|
|
|
|
|
try {
|
|
|
|
|
ListAppTableRecordRespBody respBody = larkTableHelper.listTableRecord(request);
|
|
|
|
|
ListAppTableRecordRespBody respBody = larkTableHelper.listTableRecord(request);
|
|
|
|
|
if (respBody.getPageToken().equals(pageToken)){
|
|
|
|
|
return;
|
|
|
|
|
}else {
|
|
|
|
|
pageToken = respBody.getPageToken();
|
|
|
|
|
Arrays.stream(respBody.getItems()).forEach(r -> {
|
|
|
|
|
Map<String, Object> fields = r.getFields();
|
|
|
|
|
JSONObject msg = this.buildMsg(larkCompanyTableInfo,fields, template);
|
|
|
|
|
OkHttpHelper.post(larkCompanyTableInfo.getToAppToken(),msg.toString());
|
|
|
|
|
});
|
|
|
|
|
new EventLog(larkCompanyTableInfo.getId(),EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString());
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
log.error("ScheduledRemindersTask#scheduledReminders error request tableId:{} msg:{}",JSONObject.toJSONString(larkCompanyTableInfo),msg.toString(),e);
|
|
|
|
|
new EventLog(larkCompanyTableInfo.getId(),EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString(),null,e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}while (StringUtils.isNotBlank(pageToken));
|
|
|
|
|
for (AppTableRecord item : respBody.getItems()) {
|
|
|
|
|
try {
|
|
|
|
|
Map<String, Object> fields = item.getFields();
|
|
|
|
|
msg = this.buildMsg(larkCompanyTableInfo, fields, template);
|
|
|
|
|
OkHttpHelper.post(larkCompanyTableInfo.getToAppToken(), msg.toString());
|
|
|
|
|
logList.add(new EventLog(larkCompanyTableInfo.getId(), EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString()));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("ScheduledRemindersTask#scheduledReminders error request tableId:{} msg:{}", JSONObject.toJSONString(larkCompanyTableInfo), msg.toString(), e);
|
|
|
|
|
logList.add(new EventLog(larkCompanyTableInfo.getId(), EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString(), null, e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (StringUtils.isNotBlank(pageToken));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LarkTemplate getTemplate(LarkCompanyTableInfo larkCompanyTableInfo){
|
|
|
|
|
private LarkTemplate getTemplate(LarkCompanyTableInfo larkCompanyTableInfo,Long configurationId) {
|
|
|
|
|
LarkTemplate larkTemplate = new LarkTemplate();
|
|
|
|
|
larkTemplate.setTemplateType(TemplateTypeEnum.SCHEDULED_REMINDER.getCode());
|
|
|
|
|
larkTemplate.setFlag(FlagStatus.OK.getCode());
|
|
|
|
|
larkTemplate.setLarkTableId(larkCompanyTableInfo.getId());
|
|
|
|
|
larkTemplate.setRelationId(configurationId);
|
|
|
|
|
List<LarkTemplate> larkTemplates = larkTemplateMapper.selectLarkTemplateList(larkTemplate);
|
|
|
|
|
if (CollectionUtils.isEmpty(larkTemplates)){
|
|
|
|
|
throw new RuntimeException(String.format("当前tableId:%s %s类型的模板配置不存在",larkCompanyTableInfo.getId(),larkTemplate.getTemplateType()));
|
|
|
|
|
if (CollectionUtils.isEmpty(larkTemplates)) {
|
|
|
|
|
throw new RuntimeException(String.format("当前tableId:%s %s类型的模板配置不存在", larkCompanyTableInfo.getId(), larkTemplate.getTemplateType()));
|
|
|
|
|
}
|
|
|
|
|
if (larkTemplates.size() > 1){
|
|
|
|
|
throw new RuntimeException(String.format("当前tableId:%s %s类型存在多个有效的模板配置",larkCompanyTableInfo.getId(),larkTemplate.getTemplateType()));
|
|
|
|
|
if (larkTemplates.size() > 1) {
|
|
|
|
|
throw new RuntimeException(String.format("当前tableId:%s %s类型存在多个有效的模板配置", larkCompanyTableInfo.getId(), larkTemplate.getTemplateType()));
|
|
|
|
|
}
|
|
|
|
|
return larkTemplates.get(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<LarkTableConfiguration> getLarkConfiguration(LarkCompanyTableInfo larkCompanyTableInfo){
|
|
|
|
|
private List<LarkTableConfiguration> getLarkConfiguration(LarkCompanyTableInfo larkCompanyTableInfo) {
|
|
|
|
|
List<LarkTableConfiguration> configurationList = new ArrayList<>();
|
|
|
|
|
LarkTableConfiguration query = new LarkTableConfiguration();
|
|
|
|
|
query.setLarkTableId(larkCompanyTableInfo.getId());
|
|
|
|
|
query.setFlag(FlagStatus.OK.getCode());
|
|
|
|
|
query.setConfigType(ConfigurationTypeEnum.SCHEDULE_REMINDER.getCode());
|
|
|
|
|
configurationList = larkTableConfigurationMapper.selectLarkTableConfigurationList(query);
|
|
|
|
|
if (CollectionUtils.isEmpty(configurationList)){
|
|
|
|
|
throw new RuntimeException(String.format("当前tableId:%s 无定时提醒配置",larkCompanyTableInfo.getId()));
|
|
|
|
|
if (CollectionUtils.isEmpty(configurationList)) {
|
|
|
|
|
throw new RuntimeException(String.format("当前tableId:%s 无定时提醒配置", larkCompanyTableInfo.getId()));
|
|
|
|
|
}
|
|
|
|
|
return configurationList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JSONObject buildMsg(LarkCompanyTableInfo larkCompanyTableInfo,Map<String, Object> fields, LarkTemplate template){
|
|
|
|
|
JSONObject msg = new JSONObject();
|
|
|
|
|
TemplateSubTypeEnum templateSubTypeEnum = TemplateSubTypeEnum.getByCode(template.getSubTemplateType());
|
|
|
|
|
if (templateSubTypeEnum == null){
|
|
|
|
|
throw new RuntimeException(String.format("当前tableId:%s 模板类型%s对应策略不存在",larkCompanyTableInfo.getId(),template.getSubTemplateType()));
|
|
|
|
|
}
|
|
|
|
|
switch (templateSubTypeEnum){
|
|
|
|
|
case TEXT:
|
|
|
|
|
msg.put("msg_type",templateSubTypeEnum.getCode());
|
|
|
|
|
String templateContent = template.getTemplateContent();
|
|
|
|
|
JSONArray people = JSONObject.parseArray(String.valueOf(fields.get("人员")));
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
for (int i = 0; i < people.size(); i++) {
|
|
|
|
|
JSONObject jsonObject = people.getJSONObject(i);
|
|
|
|
|
sb.append(String.format("<at user_id = \"%s\">%s</at>",jsonObject.getString("id"),jsonObject.getString("name")));
|
|
|
|
|
}
|
|
|
|
|
templateContent.replaceAll("@name",sb.toString()).replaceAll("#name",String.valueOf(fields.get("学生姓名")));
|
|
|
|
|
JSONObject content = new JSONObject();
|
|
|
|
|
content.put("text",sb.append(templateContent).toString());
|
|
|
|
|
msg.put("content",content);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
private JSONObject buildMsg(LarkCompanyTableInfo larkCompanyTableInfo, Map<String, Object> fields, LarkTemplate template) {
|
|
|
|
|
JSONObject msg = new JSONObject();
|
|
|
|
|
TemplateSubTypeEnum templateSubTypeEnum = TemplateSubTypeEnum.getByCode(template.getSubTemplateType());
|
|
|
|
|
if (templateSubTypeEnum == null) {
|
|
|
|
|
throw new RuntimeException(String.format("当前tableId:%s 模板类型%s对应策略不存在", larkCompanyTableInfo.getId(), template.getSubTemplateType()));
|
|
|
|
|
}
|
|
|
|
|
switch (templateSubTypeEnum) {
|
|
|
|
|
case TEXT:
|
|
|
|
|
msg.put("msg_type", templateSubTypeEnum.getCode());
|
|
|
|
|
String templateContent = template.getTemplateContent();
|
|
|
|
|
List<LinkedTreeMap<String,String>> people = (List<LinkedTreeMap<String,String>>)fields.get("人员");
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
for (int i = 0; i < people.size(); i++) {
|
|
|
|
|
LinkedTreeMap<String,String> detail = people.get(i);
|
|
|
|
|
sb.append(String.format("<at user_id = \"%s\">%s</at>", detail.get("id"), detail.get("name")));
|
|
|
|
|
}
|
|
|
|
|
templateContent = templateContent.replaceAll("#name", String.valueOf(fields.get("学生姓名")));
|
|
|
|
|
JSONObject content = new JSONObject();
|
|
|
|
|
content.put("text", sb.append(templateContent).toString());
|
|
|
|
|
msg.put("content", content);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<LarkCompanyTableInfo> getLarkList() {
|
|
|
|
|
List<LarkCompanyTableInfo> larkCompanyTableInfos = larkTableRelationMapper.queryListJoinLarkCompany(FlagStatus.OK.getCode(), AppType.SCHEDULED_REMINDERS.getCode(), TableRelationTypeEnum.SYNC_EMAIL.getCode());
|
|
|
|
|
List<LarkCompanyTableInfo> larkCompanyTableInfos = larkTableRelationMapper.queryListJoinLarkCompany(FlagStatus.OK.getCode(), AppType.SCHEDULED_REMINDERS.getCode(), TableRelationTypeEnum.SCHEDULED_REMINDERS.getCode());
|
|
|
|
|
return larkCompanyTableInfos.stream()
|
|
|
|
|
.filter(r -> {
|
|
|
|
|
return r.getId() != null
|
|
|
|
@ -184,11 +196,23 @@ public class ScheduledRemindersTask {
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LarkTableRequest buildRequest(LarkCompanyTableInfo larkCompanyTableInfo,String script){
|
|
|
|
|
LarkTableRequest request = new LarkTableRequest(larkCompanyTableInfo.getAppId(),larkCompanyTableInfo.getAppSecret(),larkCompanyTableInfo.getFromAppToken(), larkCompanyTableInfo.getFromTableId());
|
|
|
|
|
private LarkTableRequest buildRequest(LarkCompanyTableInfo larkCompanyTableInfo, String script) {
|
|
|
|
|
LarkTableRequest request = new LarkTableRequest(larkCompanyTableInfo.getAppId(), larkCompanyTableInfo.getAppSecret(), larkCompanyTableInfo.getFromAppToken(), larkCompanyTableInfo.getFromTableId());
|
|
|
|
|
request.setFilter(script);
|
|
|
|
|
request.setPageSize(PAGE_SIZE);
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String str = "[{avatar_url:https://s1-imfile.feishucdn.com/static-resource/v1/v2_5462d76b-2c9c-405f-8753-149c36bf097g~?image_size:72x72&cut_type:default-face&quality:&format:jpeg&sticker_format:.webp, email:, en_name:于相涌, id:ou_cd09481727e91fce1f12b7b0ed1500d2, name:于相涌}]";
|
|
|
|
|
str = str.replace("https:","https")
|
|
|
|
|
.replaceAll("\\{","{\"")
|
|
|
|
|
.replaceAll(":","\":\"")
|
|
|
|
|
.replaceAll(",","\",\"")
|
|
|
|
|
.replaceAll("}","}")
|
|
|
|
|
.replace("https","https:");
|
|
|
|
|
JSONArray objects = JSONArray.parseArray(str);
|
|
|
|
|
int i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|