|
@@ -3,27 +3,28 @@ package cn.iocoder.yudao.module.system.service.notify;
|
|
|
import cn.hutool.core.util.ReUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
-import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.template.NotifyTemplateCreateReqVO;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.template.NotifyTemplatePageReqVO;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.template.NotifyTemplateUpdateReqVO;
|
|
|
import cn.iocoder.yudao.module.system.convert.notify.NotifyTemplateConvert;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyTemplateMapper;
|
|
|
-import cn.iocoder.yudao.module.system.mq.producer.notify.NotifyProducer;
|
|
|
+import cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants;
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.cache.annotation.CacheEvict;
|
|
|
+import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
-import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTIFY_TEMPLATE_CODE_DUPLICATE;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTIFY_TEMPLATE_NOT_EXISTS;
|
|
|
|
|
|
/**
|
|
|
* 站内信模版 Service 实现类
|
|
@@ -43,36 +44,6 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
|
|
|
@Resource
|
|
|
private NotifyTemplateMapper notifyTemplateMapper;
|
|
|
|
|
|
- @Resource
|
|
|
- private NotifyProducer notifyProducer;
|
|
|
-
|
|
|
- /**
|
|
|
- * 站内信模板缓存
|
|
|
- * key:站内信模板编码 {@link NotifyTemplateDO#getCode()}
|
|
|
- * <p>
|
|
|
- * 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向
|
|
|
- */
|
|
|
- private volatile Map<String, NotifyTemplateDO> notifyTemplateCache;
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始化站内信模板的本地缓存
|
|
|
- */
|
|
|
- @Override
|
|
|
- @PostConstruct
|
|
|
- public void initLocalCache() {
|
|
|
- // 第一步:查询数据
|
|
|
- List<NotifyTemplateDO> templates = notifyTemplateMapper.selectList();
|
|
|
- log.info("[initLocalCache][缓存站内信模版,数量为:{}]", templates.size());
|
|
|
-
|
|
|
- // 第二步:构建缓存
|
|
|
- notifyTemplateCache = CollectionUtils.convertMap(templates, NotifyTemplateDO::getCode);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public NotifyTemplateDO getNotifyTemplateByCodeFromCache(String code) {
|
|
|
- return notifyTemplateCache.get(code);
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public Long createNotifyTemplate(NotifyTemplateCreateReqVO createReqVO) {
|
|
|
// 校验站内信编码是否重复
|
|
@@ -82,13 +53,12 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
|
|
|
NotifyTemplateDO notifyTemplate = NotifyTemplateConvert.INSTANCE.convert(createReqVO);
|
|
|
notifyTemplate.setParams(parseTemplateContentParams(notifyTemplate.getContent()));
|
|
|
notifyTemplateMapper.insert(notifyTemplate);
|
|
|
-
|
|
|
- // 发送刷新消息
|
|
|
- notifyProducer.sendNotifyTemplateRefreshMessage();
|
|
|
return notifyTemplate.getId();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @CacheEvict(cacheNames = RedisKeyConstants.NOTIFY_TEMPLATE,
|
|
|
+ allEntries = true) // allEntries 清空所有缓存,因为可能修改到 code 字段,不好清理
|
|
|
public void updateNotifyTemplate(NotifyTemplateUpdateReqVO updateReqVO) {
|
|
|
// 校验存在
|
|
|
validateNotifyTemplateExists(updateReqVO.getId());
|
|
@@ -99,9 +69,6 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
|
|
|
NotifyTemplateDO updateObj = NotifyTemplateConvert.INSTANCE.convert(updateReqVO);
|
|
|
updateObj.setParams(parseTemplateContentParams(updateObj.getContent()));
|
|
|
notifyTemplateMapper.updateById(updateObj);
|
|
|
-
|
|
|
- // 发送刷新消息
|
|
|
- notifyProducer.sendNotifyTemplateRefreshMessage();
|
|
|
}
|
|
|
|
|
|
@VisibleForTesting
|
|
@@ -110,13 +77,13 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @CacheEvict(cacheNames = RedisKeyConstants.NOTIFY_TEMPLATE,
|
|
|
+ allEntries = true) // allEntries 清空所有缓存,因为 id 不是直接的缓存 code,不好清理
|
|
|
public void deleteNotifyTemplate(Long id) {
|
|
|
// 校验存在
|
|
|
validateNotifyTemplateExists(id);
|
|
|
// 删除
|
|
|
notifyTemplateMapper.deleteById(id);
|
|
|
- // 发送刷新消息
|
|
|
- notifyProducer.sendNotifyTemplateRefreshMessage();
|
|
|
}
|
|
|
|
|
|
private void validateNotifyTemplateExists(Long id) {
|
|
@@ -130,13 +97,20 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
|
|
|
return notifyTemplateMapper.selectById(id);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Cacheable(cacheNames = RedisKeyConstants.NOTIFY_TEMPLATE, key = "#code",
|
|
|
+ unless = "#result == null")
|
|
|
+ public NotifyTemplateDO getNotifyTemplateByCodeFromCache(String code) {
|
|
|
+ return notifyTemplateMapper.selectByCode(code);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public PageResult<NotifyTemplateDO> getNotifyTemplatePage(NotifyTemplatePageReqVO pageReqVO) {
|
|
|
return notifyTemplateMapper.selectPage(pageReqVO);
|
|
|
}
|
|
|
|
|
|
@VisibleForTesting
|
|
|
- public void validateNotifyTemplateCodeDuplicate(Long id, String code) {
|
|
|
+ void validateNotifyTemplateCodeDuplicate(Long id, String code) {
|
|
|
NotifyTemplateDO template = notifyTemplateMapper.selectByCode(code);
|
|
|
if (template == null) {
|
|
|
return;
|