|
@@ -6,14 +6,14 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.dingtalk.api.response.OapiMediaUploadResponse;
|
|
|
-import com.google.gson.Gson;
|
|
|
import com.nokia.dingtalk_api.common.R;
|
|
|
import com.nokia.dingtalk_api.common.exception.BizException;
|
|
|
import com.nokia.dingtalk_api.common.exception.MyRuntimeException;
|
|
|
-import com.nokia.dingtalk_api.dao.IAlertConfigService;
|
|
|
import com.nokia.dingtalk_api.dao.IAppTaskLogService;
|
|
|
import com.nokia.dingtalk_api.dao.IAppTaskService;
|
|
|
import com.nokia.dingtalk_api.dao.IRetryAppTaskService;
|
|
|
+import com.nokia.dingtalk_api.dao.ISendMessageLogService;
|
|
|
+import com.nokia.dingtalk_api.mapper.AlertConfigMapper;
|
|
|
import com.nokia.dingtalk_api.mapper.AppTaskMapper;
|
|
|
import com.nokia.dingtalk_api.mapper.RetryAppTaskMapper;
|
|
|
import com.nokia.dingtalk_api.pojos.bo.AppTaskBo;
|
|
@@ -30,11 +30,12 @@ import com.nokia.dingtalk_api.pojos.enums.FileMethodEnum;
|
|
|
import com.nokia.dingtalk_api.pojos.enums.SortEnum;
|
|
|
import com.nokia.dingtalk_api.pojos.enums.SubdirectoryMethodEnum;
|
|
|
import com.nokia.dingtalk_api.pojos.enums.TaskAlertTypeEnum;
|
|
|
-import com.nokia.dingtalk_api.pojos.po.AlertConfigPo;
|
|
|
import com.nokia.dingtalk_api.pojos.po.AppTaskLogPo;
|
|
|
import com.nokia.dingtalk_api.pojos.po.AppTaskPo;
|
|
|
import com.nokia.dingtalk_api.pojos.po.RetryAppTaskPo;
|
|
|
+import com.nokia.dingtalk_api.pojos.po.SendMessageLogPo;
|
|
|
import com.nokia.dingtalk_api.pojos.vo.PageVo;
|
|
|
+import com.nokia.dingtalk_api.pojos.vo.cms.GetAlertConfigVo;
|
|
|
import com.nokia.dingtalk_api.pojos.vo.cms.ListAppTaskVo;
|
|
|
import com.nokia.dingtalk_api.util.DingTalkApiUtil;
|
|
|
import com.taobao.api.FileItem;
|
|
@@ -103,9 +104,10 @@ public class AppTaskService {
|
|
|
private final OpenService openService;
|
|
|
private final AesService aesService;
|
|
|
private final IAppTaskService iAppTaskService;
|
|
|
- private final IAlertConfigService iAlertConfigService;
|
|
|
private final IRetryAppTaskService iRetryAppTaskService;
|
|
|
private final RetryAppTaskMapper retryAppTaskMapper;
|
|
|
+ private final AlertConfigMapper alertConfigMapper;
|
|
|
+ private final ISendMessageLogService iSendMessageLogService;
|
|
|
|
|
|
/**
|
|
|
* 应用定时任务初始化
|
|
@@ -161,7 +163,6 @@ public class AppTaskService {
|
|
|
MDC.put("traceId", t.getTaskId());
|
|
|
Set<String> fileList = new HashSet<>();
|
|
|
Set<String> processQueryKeys = new HashSet<>();
|
|
|
- Gson gson = new Gson();
|
|
|
try {
|
|
|
Runnable runnable = () -> {
|
|
|
MDC.put("traceId", t.getTaskId());
|
|
@@ -197,24 +198,34 @@ public class AppTaskService {
|
|
|
findFile(t, template, dirPath, fileList, processQueryKeys);
|
|
|
} else if (SubdirectoryMethodEnum.MONTH.name().equals(t.getSubdirectoryMethod())) {
|
|
|
// 按月匹配文件夹
|
|
|
- LocalDate localDate = LocalDate.now().plusMonths(t.getDirTimeDelay());
|
|
|
+ LocalDate localDate;
|
|
|
+ if (StringUtils.hasText(t.getDateString())) {
|
|
|
+ localDate = dateStringParse(t);
|
|
|
+ } else {
|
|
|
+ localDate = LocalDate.now().plusMonths(t.getDirTimeDelay());
|
|
|
+ }
|
|
|
String month = localDate.format(DateTimeFormatter.ofPattern("yyyyMM"));
|
|
|
String dirPath = t.getMasterDir() + (t.getMasterDir().endsWith("/") ? "" : "/") + month;
|
|
|
findFile(t, template, dirPath, fileList, processQueryKeys);
|
|
|
} else if (SubdirectoryMethodEnum.DAY.name().equals(t.getSubdirectoryMethod())) {
|
|
|
// 按天匹配文件夹
|
|
|
- LocalDate localDate = LocalDate.now().plusDays(t.getDirTimeDelay());
|
|
|
- String month = localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
|
|
- String dirPath = t.getMasterDir() + (t.getMasterDir().endsWith("/") ? "" : "/") + month;
|
|
|
+ LocalDate localDate;
|
|
|
+ if (StringUtils.hasText(t.getDateString())) {
|
|
|
+ localDate = dateStringParse(t);
|
|
|
+ } else {
|
|
|
+ localDate = LocalDate.now().plusDays(t.getDirTimeDelay());
|
|
|
+ }
|
|
|
+ String day = localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
|
|
+ String dirPath = t.getMasterDir() + (t.getMasterDir().endsWith("/") ? "" : "/") + day;
|
|
|
findFile(t, template, dirPath, fileList, processQueryKeys);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
// 执行完删除重试任务
|
|
|
iRetryAppTaskService.removeById(t.getTaskId());
|
|
|
RETRY_TASKS.remove(t.getTaskId());
|
|
|
log.info("结束应用任务: {}", t);
|
|
|
- String content = t.getAppName() + " " + t.getTaskName() + " 执行成功" + " : "
|
|
|
- + gson.toJson(processQueryKeys);
|
|
|
+ String content = t.getAppName() + " " + t.getTaskName() + " 执行成功,消息id:" + processQueryKeys;
|
|
|
sendMessage(t, content);
|
|
|
};
|
|
|
if (t.getTaskTimeout() > 0) {
|
|
@@ -247,12 +258,22 @@ public class AppTaskService {
|
|
|
retry(t);
|
|
|
} finally {
|
|
|
// 记录日志
|
|
|
- appTaskLogPo.setFiles(gson.toJson(fileList));
|
|
|
- appTaskLogPo.setProcessQueryKeys(gson.toJson(processQueryKeys));
|
|
|
+ appTaskLogPo.setFiles(fileList.toString());
|
|
|
+ appTaskLogPo.setProcessQueryKeys(processQueryKeys.toString());
|
|
|
iAppTaskLogService.save(appTaskLogPo);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static LocalDate dateStringParse(AppTaskBo t) {
|
|
|
+ LocalDate localDate;
|
|
|
+ try {
|
|
|
+ localDate = LocalDate.parse(t.getDateString(), DateTimeFormatter.ofPattern("yyyyMMdd"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BizException("日期参数格式错误");
|
|
|
+ }
|
|
|
+ return localDate;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 重试
|
|
|
* @param t 任务信息
|
|
@@ -298,22 +319,25 @@ public class AppTaskService {
|
|
|
String openConversationId = t.getOpenConversationId();
|
|
|
String phones = t.getPhones();
|
|
|
String userIds = t.getUserIds();
|
|
|
+ String alertRobotCode = t.getRobotCode();
|
|
|
if (TaskAlertTypeEnum.DEFAULT.name().equals(t.getAlertType())) {
|
|
|
- AlertConfigPo alertConfigPo = iAlertConfigService.getById(1);
|
|
|
- if (alertConfigPo == null) {
|
|
|
+ GetAlertConfigVo alertConfig = alertConfigMapper.getConfig();
|
|
|
+ if (alertConfig == null) {
|
|
|
return;
|
|
|
}
|
|
|
- robotCode = alertConfigPo.getRobotCode();
|
|
|
- robotSecret = alertConfigPo.getRobotSecret();
|
|
|
- openConversationId = alertConfigPo.getOpenConversationId();
|
|
|
- phones = alertConfigPo.getPhones();
|
|
|
- userIds = alertConfigPo.getUserIds();
|
|
|
+ robotCode = alertConfig.getRobotCode();
|
|
|
+ robotSecret = alertConfig.getRobotSecret();
|
|
|
+ openConversationId = alertConfig.getOpenConversationId();
|
|
|
+ phones = alertConfig.getPhones();
|
|
|
+ userIds = alertConfig.getUserIds();
|
|
|
+ alertRobotCode = alertConfig.getRobotCode();
|
|
|
} else if (TaskAlertTypeEnum.CUSTOM.name().equals(t.getAlertType())) {
|
|
|
robotCode = t.getAlertRobotCode();
|
|
|
robotSecret = t.getAlertRobotSecret();
|
|
|
openConversationId = t.getAlertOpenConversationId();
|
|
|
phones = t.getAlertPhones();
|
|
|
userIds = t.getAlertUserIds();
|
|
|
+ alertRobotCode = t.getAlertRobotCode();
|
|
|
}
|
|
|
if (!StringUtils.hasText(robotCode)
|
|
|
|| !StringUtils.hasText(robotSecret)
|
|
@@ -324,13 +348,18 @@ public class AppTaskService {
|
|
|
}
|
|
|
String accessToken = openService.getAccessToken(robotCode, robotSecret);
|
|
|
if (StringUtils.hasText(openConversationId)) {
|
|
|
- DingTalkApiUtil.groupSendSampleText(content, accessToken, openConversationId,
|
|
|
- robotCode);
|
|
|
+ OrgGroupSendResponse response = DingTalkApiUtil.groupSendSampleText(content, accessToken,
|
|
|
+ openConversationId, robotCode);
|
|
|
+ iSendMessageLogService.save(new SendMessageLogPo(robotCode, alertRobotCode, openConversationId,
|
|
|
+ content, response));
|
|
|
}
|
|
|
if (StringUtils.hasText(phones) || StringUtils.hasText(userIds)) {
|
|
|
List<String> userIdList = getUserIdList(phones, userIds,
|
|
|
accessToken);
|
|
|
- DingTalkApiUtil.batchSendOtoSampleText(content, accessToken, robotCode, userIdList);
|
|
|
+ BatchSendOTOResponse response = DingTalkApiUtil.batchSendOtoSampleText(content, accessToken,
|
|
|
+ robotCode, userIdList);
|
|
|
+ iSendMessageLogService.save(new SendMessageLogPo(robotCode, alertRobotCode, userIdList,
|
|
|
+ content, response));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.error("发送通知失败: {}", e.getMessage(), e);
|
|
@@ -374,13 +403,23 @@ public class AppTaskService {
|
|
|
sendFile(t, dirEntry.getFilename(), dirPath, template, fileList, processQueryKeys);
|
|
|
} else if (FileMethodEnum.MONTH.name().equals(t.getFileMethod())) {
|
|
|
// 按月匹配文件
|
|
|
- LocalDate localDate = LocalDate.now().plusMonths(t.getFileTimeDelay());
|
|
|
+ LocalDate localDate;
|
|
|
+ if (StringUtils.hasText(t.getDateString())) {
|
|
|
+ localDate = dateStringParse(t);
|
|
|
+ } else {
|
|
|
+ localDate = LocalDate.now().plusMonths(t.getFileTimeDelay());
|
|
|
+ }
|
|
|
String month = localDate.format(DateTimeFormatter.ofPattern("yyyyMM"));
|
|
|
String filename = t.getFilePrefix() + month + "." + t.getFileExtension();
|
|
|
sendFile(t, filename, dirPath, template, fileList, processQueryKeys);
|
|
|
} else if (FileMethodEnum.DAY.name().equals(t.getFileMethod())) {
|
|
|
// 按日匹配文件
|
|
|
- LocalDate localDate = LocalDate.now().plusDays(t.getFileTimeDelay());
|
|
|
+ LocalDate localDate;
|
|
|
+ if (StringUtils.hasText(t.getDateString())) {
|
|
|
+ localDate = dateStringParse(t);
|
|
|
+ } else {
|
|
|
+ localDate = LocalDate.now().plusDays(t.getFileTimeDelay());
|
|
|
+ }
|
|
|
String day = localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
|
|
String filename = t.getFilePrefix() + day + "." + t.getFileExtension();
|
|
|
sendFile(t, filename, dirPath, template, fileList, processQueryKeys);
|
|
@@ -420,6 +459,12 @@ public class AppTaskService {
|
|
|
String robotCode = t.getRobotCode();
|
|
|
String openConversationId = t.getOpenConversationId();
|
|
|
String accessToken = openService.getAccessToken(robotCode, t.getRobotSecret());
|
|
|
+ String alertRobotCode = null;
|
|
|
+ if (TaskAlertTypeEnum.TASK.name().equals(t.getAlertType())) {
|
|
|
+ alertRobotCode = robotCode;
|
|
|
+ } else if (TaskAlertTypeEnum.CUSTOM.name().equals(t.getAlertType())) {
|
|
|
+ alertRobotCode = t.getAlertRobotCode();
|
|
|
+ }
|
|
|
// 发送markdown消息
|
|
|
if (t.getFileToText().equals(1) && ("txt".equalsIgnoreCase(fileExtension) || "md".equalsIgnoreCase(fileExtension))) {
|
|
|
String content = new String(bytes, StandardCharsets.UTF_8);
|
|
@@ -430,12 +475,15 @@ public class AppTaskService {
|
|
|
org.apache.commons.lang3.StringUtils.substring(content, 0, 30), content,
|
|
|
accessToken, robotCode, userIdList);
|
|
|
processQueryKeys.add(r.body.processQueryKey);
|
|
|
+ iSendMessageLogService.save(new SendMessageLogPo(robotCode, alertRobotCode, userIdList, content, r));
|
|
|
} else {
|
|
|
// 发送群聊消息
|
|
|
OrgGroupSendResponse r = DingTalkApiUtil.groupSendSampleMarkdown(
|
|
|
org.apache.commons.lang3.StringUtils.substring(content, 0, 30), content,
|
|
|
accessToken, openConversationId, robotCode);
|
|
|
processQueryKeys.add(r.body.processQueryKey);
|
|
|
+ iSendMessageLogService.save(new SendMessageLogPo(robotCode, alertRobotCode, openConversationId,
|
|
|
+ content, r));
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
@@ -451,11 +499,13 @@ public class AppTaskService {
|
|
|
BatchSendOTOResponse r = DingTalkApiUtil.batchSendOtoSampleImageMsg(mediaId, accessToken, robotCode,
|
|
|
userIdList);
|
|
|
processQueryKeys.add(r.body.processQueryKey);
|
|
|
+ iSendMessageLogService.save(new SendMessageLogPo(robotCode, alertRobotCode, userIdList, mediaId, r));
|
|
|
} else {
|
|
|
// 发送文件
|
|
|
- BatchSendOTOResponse r = DingTalkApiUtil.batchSendOtoSampleFile(mediaId, filename, fileExtension, accessToken, robotCode,
|
|
|
- userIdList);
|
|
|
+ BatchSendOTOResponse r = DingTalkApiUtil.batchSendOtoSampleFile(mediaId, filename, fileExtension,
|
|
|
+ accessToken, robotCode, userIdList);
|
|
|
processQueryKeys.add(r.body.processQueryKey);
|
|
|
+ iSendMessageLogService.save(new SendMessageLogPo(robotCode, alertRobotCode, userIdList, filename, r));
|
|
|
}
|
|
|
} else {
|
|
|
// 发送群聊消息
|
|
@@ -464,11 +514,15 @@ public class AppTaskService {
|
|
|
OrgGroupSendResponse r = DingTalkApiUtil.groupSendSampleImageMsg(mediaId, accessToken,
|
|
|
openConversationId, robotCode);
|
|
|
processQueryKeys.add(r.body.processQueryKey);
|
|
|
+ iSendMessageLogService.save(new SendMessageLogPo(robotCode, alertRobotCode, openConversationId,
|
|
|
+ mediaId, r));
|
|
|
} else {
|
|
|
// 发送文件
|
|
|
OrgGroupSendResponse r = DingTalkApiUtil.groupSendSampleFile(mediaId, filename, fileExtension,
|
|
|
accessToken, openConversationId, robotCode);
|
|
|
processQueryKeys.add(r.body.processQueryKey);
|
|
|
+ iSendMessageLogService.save(new SendMessageLogPo(robotCode, alertRobotCode, openConversationId,
|
|
|
+ filename, r));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -566,7 +620,7 @@ public class AppTaskService {
|
|
|
}
|
|
|
if (TaskAlertTypeEnum.CUSTOM.equals(dto.getAlertType())) {
|
|
|
if (StringUtils.hasText(dto.getAlertPhones())) {
|
|
|
- Matcher matcher = PHONE_LIST_PATTERN.matcher(dto.getPhones());
|
|
|
+ Matcher matcher = PHONE_LIST_PATTERN.matcher(dto.getAlertPhones());
|
|
|
if (!matcher.matches()) {
|
|
|
throw new BizException("alertPhones格式不正确");
|
|
|
}
|
|
@@ -574,7 +628,7 @@ public class AppTaskService {
|
|
|
dto.setAlertPhones("");
|
|
|
}
|
|
|
if (StringUtils.hasText(dto.getAlertUserIds())) {
|
|
|
- Matcher matcher = USER_ID_LIST_PATTERN.matcher(dto.getUserIds());
|
|
|
+ Matcher matcher = USER_ID_LIST_PATTERN.matcher(dto.getAlertUserIds());
|
|
|
if (!matcher.matches()) {
|
|
|
throw new BizException("alertUserIds格式不正确");
|
|
|
}
|
|
@@ -624,7 +678,9 @@ public class AppTaskService {
|
|
|
AppTaskPo po = new AppTaskPo(dto);
|
|
|
iAppTaskService.save(po);
|
|
|
AppTaskBo bo = appTaskMapper.getAppTaskByTaskId(po.getTaskId());
|
|
|
- addTask(bo);
|
|
|
+ if (StringUtils.hasText(bo.getCron())) {
|
|
|
+ addTask(bo);
|
|
|
+ }
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
@@ -643,7 +699,7 @@ public class AppTaskService {
|
|
|
AppTaskPo po = new AppTaskPo(dto);
|
|
|
iAppTaskService.updateById(po);
|
|
|
AppTaskBo bo = appTaskMapper.getAppTaskByTaskId(po.getTaskId());
|
|
|
- if (AppTaskStatusEnum.RUNNING.value.equals(bo.getStatus())) {
|
|
|
+ if (AppTaskStatusEnum.RUNNING.value.equals(bo.getStatus()) && StringUtils.hasText(bo.getCron())) {
|
|
|
addTask(bo);
|
|
|
}
|
|
|
return R.ok();
|
|
@@ -656,6 +712,9 @@ public class AppTaskService {
|
|
|
po.setStatus(AppTaskStatusEnum.RUNNING.value);
|
|
|
iAppTaskService.updateById(po);
|
|
|
AppTaskBo bo = appTaskMapper.getAppTaskByTaskId(po.getTaskId());
|
|
|
+ if (!StringUtils.hasText(bo.getCron())) {
|
|
|
+ throw new BizException("cron表达式未设置无法启用");
|
|
|
+ }
|
|
|
addTask(bo);
|
|
|
return R.ok();
|
|
|
}
|
|
@@ -682,6 +741,7 @@ public class AppTaskService {
|
|
|
|
|
|
public R<Object> runAppTask(RunAppTaskDto dto) {
|
|
|
AppTaskBo bo = appTaskMapper.getAppTaskByTaskId(dto.getTaskId());
|
|
|
+ bo.setDateString(dto.getDateString());
|
|
|
runTask(bo);
|
|
|
return R.ok();
|
|
|
}
|