All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.aizuda.snailjob.server.web.service.impl.NotifyConfigServiceImpl Maven / Gradle / Ivy
package com.aizuda.snailjob.server.web.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.common.core.util.StreamUtils;
import com.aizuda.snailjob.server.common.enums.SyetemTaskTypeEnum;
import com.aizuda.snailjob.server.common.exception.SnailJobServerException;
import com.aizuda.snailjob.server.web.model.base.PageResult;
import com.aizuda.snailjob.server.web.model.request.NotifyConfigQueryVO;
import com.aizuda.snailjob.server.web.model.request.NotifyConfigRequestVO;
import com.aizuda.snailjob.server.web.model.request.UserSessionVO;
import com.aizuda.snailjob.server.web.model.response.NotifyConfigResponseVO;
import com.aizuda.snailjob.server.web.service.NotifyConfigService;
import com.aizuda.snailjob.server.web.service.convert.NotifyConfigConverter;
import com.aizuda.snailjob.server.web.service.convert.NotifyConfigResponseVOConverter;
import com.aizuda.snailjob.server.web.service.handler.SyncConfigHandler;
import com.aizuda.snailjob.server.web.util.UserSessionUtils;
import com.aizuda.snailjob.template.datasource.access.AccessTemplate;
import com.aizuda.snailjob.template.datasource.access.ConfigAccess;
import com.aizuda.snailjob.template.datasource.persistence.mapper.JobMapper;
import com.aizuda.snailjob.template.datasource.persistence.mapper.NotifyRecipientMapper;
import com.aizuda.snailjob.template.datasource.persistence.mapper.WorkflowMapper;
import com.aizuda.snailjob.template.datasource.persistence.po.Job;
import com.aizuda.snailjob.template.datasource.persistence.po.NotifyConfig;
import com.aizuda.snailjob.template.datasource.persistence.po.NotifyRecipient;
import com.aizuda.snailjob.template.datasource.persistence.po.Workflow;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author: opensnail
* @date : 2022-03-03 11:17
*/
@Service
@RequiredArgsConstructor
public class NotifyConfigServiceImpl implements NotifyConfigService {
private final AccessTemplate accessTemplate;
private final NotifyRecipientMapper notifyRecipientMapper;
private final JobMapper jobMapper;
private final WorkflowMapper workflowMapper;
@Override
public PageResult> getNotifyConfigList(NotifyConfigQueryVO queryVO) {
PageDTO pageDTO = new PageDTO<>();
UserSessionVO userSessionVO = UserSessionUtils.currentUserSession();
List notifyConfigs = accessTemplate.getNotifyConfigAccess().listPage(pageDTO,
new LambdaQueryWrapper()
.eq(NotifyConfig::getNamespaceId, userSessionVO.getNamespaceId())
.in(userSessionVO.isUser(), NotifyConfig::getGroupName, userSessionVO.getGroupNames())
.eq(StrUtil.isNotBlank(queryVO.getGroupName()), NotifyConfig::getGroupName, queryVO.getGroupName())
.eq(StrUtil.isNotBlank(queryVO.getSceneName()), NotifyConfig::getBusinessId, queryVO.getSceneName())
.orderByDesc(NotifyConfig::getId))
.getRecords();
if (CollUtil.isEmpty(notifyConfigs)) {
return new PageResult<>(pageDTO, Lists.newArrayList());
}
List notifyConfigResponseVOS = NotifyConfigResponseVOConverter.INSTANCE.convertList(
notifyConfigs);
Map recipientNameMap = getRecipientNameMap(notifyConfigResponseVOS);
Map jobNameMap = getJobNameMap(notifyConfigResponseVOS);
Map workflowNameMap = getWorkflowNameMap(notifyConfigResponseVOS);
for (final NotifyConfigResponseVO notifyConfigResponseVO : notifyConfigResponseVOS) {
notifyConfigResponseVO.setRecipientNames(StreamUtils.toSet(notifyConfigResponseVO.getRecipientIds(),
recipientId -> recipientNameMap.getOrDefault(recipientId, StrUtil.EMPTY)));
if (Objects.equals(notifyConfigResponseVO.getSystemTaskType(), SyetemTaskTypeEnum.RETRY.getType()) ||
Objects.equals(notifyConfigResponseVO.getSystemTaskType(), SyetemTaskTypeEnum.CALLBACK.getType())) {
notifyConfigResponseVO.setBusinessName(notifyConfigResponseVO.getBusinessId());
} else if (Objects.equals(notifyConfigResponseVO.getSystemTaskType(), SyetemTaskTypeEnum.JOB.getType())) {
notifyConfigResponseVO.setBusinessName(
jobNameMap.get(Long.parseLong(notifyConfigResponseVO.getBusinessId())));
} else if (Objects.equals(notifyConfigResponseVO.getSystemTaskType(),
SyetemTaskTypeEnum.WORKFLOW.getType())) {
notifyConfigResponseVO.setBusinessName(
workflowNameMap.get(Long.parseLong(notifyConfigResponseVO.getBusinessId())));
}
}
return new PageResult<>(pageDTO, notifyConfigResponseVOS);
}
private Map getWorkflowNameMap(final List notifyConfigResponseVOS) {
Set workflowIds = notifyConfigResponseVOS.stream().filter(responseVO ->
responseVO.getSystemTaskType().equals(SyetemTaskTypeEnum.WORKFLOW.getType()))
.map(responseVO -> Long.parseLong(responseVO.getBusinessId()))
.collect(Collectors.toSet());
if (CollUtil.isNotEmpty(workflowIds)) {
List workflows = workflowMapper.selectBatchIds(workflowIds);
return StreamUtils.toMap(workflows, Workflow::getId, Workflow::getWorkflowName);
}
return new HashMap<>();
}
private Map getJobNameMap(final List notifyConfigResponseVOS) {
Set jobIds = notifyConfigResponseVOS.stream().filter(responseVO ->
responseVO.getSystemTaskType().equals(SyetemTaskTypeEnum.JOB.getType()))
.map(responseVO -> Long.parseLong(responseVO.getBusinessId()))
.collect(Collectors.toSet());
if (CollUtil.isNotEmpty(jobIds)) {
List jobs = jobMapper.selectBatchIds(jobIds);
return StreamUtils.toMap(jobs, Job::getId, Job::getJobName);
}
return new HashMap<>();
}
@NotNull
private Map getRecipientNameMap(final List notifyConfigResponseVOS) {
Set recipientIds = StreamUtils.toSetByFlatMap(notifyConfigResponseVOS,
NotifyConfigResponseVO::getRecipientIds, Collection::stream);
if (CollUtil.isEmpty(recipientIds)) {
return Maps.newHashMap();
}
List notifyRecipients = notifyRecipientMapper.selectBatchIds(recipientIds);
return StreamUtils.toMap(notifyRecipients, NotifyRecipient::getId, NotifyRecipient::getRecipientName);
}
@Override
public Boolean saveNotify(NotifyConfigRequestVO requestVO) {
NotifyConfig notifyConfig = NotifyConfigConverter.INSTANCE.convert(requestVO);
notifyConfig.setCreateDt(LocalDateTime.now());
notifyConfig.setRecipientIds(JsonUtil.toJsonString(requestVO.getRecipientIds()));
notifyConfig.setNamespaceId(UserSessionUtils.currentUserSession().getNamespaceId());
ConfigAccess notifyConfigAccess = accessTemplate.getNotifyConfigAccess();
Assert.isTrue(1 == notifyConfigAccess.insert(notifyConfig),
() -> new SnailJobServerException("failed to insert notify. sceneConfig:[{}]",
JsonUtil.toJsonString(notifyConfig)));
return Boolean.TRUE;
}
@Override
public Boolean updateNotify(NotifyConfigRequestVO requestVO) {
Assert.notNull(requestVO.getId(), () -> new SnailJobServerException("参数异常"));
NotifyConfig notifyConfig = NotifyConfigConverter.INSTANCE.convert(requestVO);
notifyConfig.setRecipientIds(JsonUtil.toJsonString(requestVO.getRecipientIds()));
// 防止被覆盖
notifyConfig.setNamespaceId(null);
Assert.isTrue(1 == accessTemplate.getNotifyConfigAccess().updateById(notifyConfig),
() -> new SnailJobServerException("failed to update notify. sceneConfig:[{}]",
JsonUtil.toJsonString(notifyConfig)));
return Boolean.TRUE;
}
@Override
public NotifyConfigResponseVO getNotifyConfigDetail(Long id) {
NotifyConfig notifyConfig = accessTemplate.getNotifyConfigAccess().one(new LambdaQueryWrapper()
.eq(NotifyConfig::getId, id));
return NotifyConfigResponseVOConverter.INSTANCE.convert(notifyConfig);
}
@Override
public Boolean updateStatus(final Long id, final Integer status) {
String namespaceId = UserSessionUtils.currentUserSession().getNamespaceId();
NotifyConfig notifyConfig = accessTemplate.getNotifyConfigAccess().one(
new LambdaQueryWrapper()
.eq(NotifyConfig::getId, id)
.eq(NotifyConfig::getNamespaceId, namespaceId)
);
Assert.notNull(notifyConfig, () -> new SnailJobServerException("通知配置不存在"));
// 同步配置到客户端
SyncConfigHandler.addSyncTask(notifyConfig.getGroupName(), namespaceId);
NotifyConfig config = new NotifyConfig();
config.setNotifyStatus(status);
config.setUpdateDt(LocalDateTime.now());
int update = accessTemplate.getNotifyConfigAccess()
.update(config, new LambdaUpdateWrapper()
.eq(NotifyConfig::getNamespaceId, namespaceId)
.eq(NotifyConfig::getId, id)
);
return 1 == update;
}
@Override
public Boolean batchDeleteNotify(final Set ids) {
return ids.size() == accessTemplate.getNotifyConfigAccess()
.delete(new LambdaQueryWrapper().in(NotifyConfig::getId, ids));
}
}