|
|
|
@ -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,9 +53,13 @@ public class ScheduledRemindersTask {
|
|
|
|
|
@Autowired
|
|
|
|
|
private EventLogMapper eventLogMapper;
|
|
|
|
|
|
|
|
|
|
/**查询分页大小*/
|
|
|
|
|
/**
|
|
|
|
|
* 查询分页大小
|
|
|
|
|
*/
|
|
|
|
|
private static final Integer PAGE_SIZE = 20;
|
|
|
|
|
/**查询分页大小*/
|
|
|
|
|
/**
|
|
|
|
|
* 查询分页大小
|
|
|
|
|
*/
|
|
|
|
|
private static final String RANGE_KEY = "日期";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -64,6 +70,8 @@ public class ScheduledRemindersTask {
|
|
|
|
|
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);
|
|
|
|
@ -73,18 +81,18 @@ public class ScheduledRemindersTask {
|
|
|
|
|
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);
|
|
|
|
|
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,LocalDateTime.now());
|
|
|
|
|
this.sendMessage(script,larkCompanyTableInfo,logList);
|
|
|
|
|
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());
|
|
|
|
|
logList.add(new EventLog(larkCompanyTableInfo.getId(), EventOperateType.SCHEDULE_REMINDER.getCode(), JSONObject.toJSONString(larkCompanyTableInfo), null, e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (CollectionUtils.isNotEmpty(logList)) {
|
|
|
|
@ -93,33 +101,37 @@ public class ScheduledRemindersTask {
|
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
for (AppTableRecord item : respBody.getItems()) {
|
|
|
|
|
try {
|
|
|
|
|
Map<String, Object> fields = item.getFields();
|
|
|
|
|
msg = this.buildMsg(larkCompanyTableInfo, fields, template);
|
|
|
|
|
OkHttpHelper.post(larkCompanyTableInfo.getToAppToken(), msg.toString());
|
|
|
|
|
});
|
|
|
|
|
new EventLog(larkCompanyTableInfo.getId(),EventOperateType.SCHEDULE_REMINDER.getCode(), 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);
|
|
|
|
|
new EventLog(larkCompanyTableInfo.getId(),EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString(),null,e.getMessage());
|
|
|
|
|
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()));
|
|
|
|
@ -153,13 +165,13 @@ public class ScheduledRemindersTask {
|
|
|
|
|
case TEXT:
|
|
|
|
|
msg.put("msg_type", templateSubTypeEnum.getCode());
|
|
|
|
|
String templateContent = template.getTemplateContent();
|
|
|
|
|
JSONArray people = JSONObject.parseArray(String.valueOf(fields.get("人员")));
|
|
|
|
|
List<LinkedTreeMap<String,String>> people = (List<LinkedTreeMap<String,String>>)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")));
|
|
|
|
|
LinkedTreeMap<String,String> detail = people.get(i);
|
|
|
|
|
sb.append(String.format("<at user_id = \"%s\">%s</at>", detail.get("id"), detail.get("name")));
|
|
|
|
|
}
|
|
|
|
|
templateContent.replaceAll("@name",sb.toString()).replaceAll("#name",String.valueOf(fields.get("学生姓名")));
|
|
|
|
|
templateContent = templateContent.replaceAll("#name", String.valueOf(fields.get("学生姓名")));
|
|
|
|
|
JSONObject content = new JSONObject();
|
|
|
|
|
content.put("text", sb.append(templateContent).toString());
|
|
|
|
|
msg.put("content", content);
|
|
|
|
@ -171,7 +183,7 @@ public class ScheduledRemindersTask {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
@ -191,4 +203,16 @@ public class ScheduledRemindersTask {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|