fix: 修复偶发性报错 zip file closed

fix #I9OHXP
This commit is contained in:
2024-07-03 21:36:34 +08:00
parent 52f3be8ee3
commit b587cb82aa
3 changed files with 53 additions and 42 deletions

View File

@@ -16,19 +16,30 @@
package top.continew.admin.system.service.impl;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import com.alicp.jetcache.anno.Cached;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.system.mapper.DictItemMapper;
import top.continew.admin.system.model.entity.DictItemDO;
import top.continew.admin.system.model.query.DictItemQuery;
import top.continew.admin.system.model.req.DictItemReq;
import top.continew.admin.system.model.resp.DictItemResp;
import top.continew.admin.system.service.DictItemService;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.core.autoconfigure.project.ProjectProperties;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.util.validate.CheckUtils;
import top.continew.starter.data.mybatis.plus.base.IBaseEnum;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;
import java.util.List;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* 字典项业务实现
@@ -40,26 +51,34 @@ import java.util.List;
@RequiredArgsConstructor
public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictItemDO, DictItemResp, DictItemResp, DictItemQuery, DictItemReq> implements DictItemService {
private final ProjectProperties projectProperties;
private final Map<String, List<LabelValueResp>> enumDictCache = new ConcurrentHashMap<>();
@Override
protected void beforeAdd(DictItemReq req) {
String value = req.getValue();
CheckUtils.throwIf(this.isValueExists(value, null, req.getDictId()), "新增失败,字典值 [{}] 已存在", value);
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK);
}
@Override
protected void beforeUpdate(DictItemReq req, Long id) {
String value = req.getValue();
CheckUtils.throwIf(this.isValueExists(value, id, req.getDictId()), "修改失败,字典值 [{}] 已存在", value);
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK);
}
@Override
@Cached(key = "#dictCode", name = CacheConstants.DICT_KEY_PREFIX)
public List<LabelValueResp> listByDictCode(String dictCode) {
return baseMapper.listByDictCode(dictCode);
return Optional.ofNullable(enumDictCache.get(dictCode.toLowerCase()))
.orElseGet(() -> baseMapper.listByDictCode(dictCode));
}
@Override
public void deleteByDictIds(List<Long> dictIds) {
baseMapper.lambdaUpdate().in(DictItemDO::getDictId, dictIds).remove();
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK);
}
/**
@@ -77,4 +96,29 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
.ne(null != id, DictItemDO::getId, id)
.exists();
}
/**
* 将枚举转换为枚举字典
*
* @param enumClass 枚举类型
* @return 枚举字典
*/
private List<LabelValueResp> toEnumDict(Class<?> enumClass) {
Object[] enumConstants = enumClass.getEnumConstants();
return Arrays.stream(enumConstants).map(e -> {
IBaseEnum baseEnum = (IBaseEnum)e;
return new LabelValueResp(baseEnum.getDescription(), baseEnum.getValue(), baseEnum.getColor());
}).toList();
}
/**
* 缓存枚举字典
*/
@PostConstruct
public void init() {
Set<Class<?>> classSet = ClassUtil.scanPackageBySuper(projectProperties.getBasePackage(), IBaseEnum.class);
enumDictCache.putAll(classSet.stream()
.collect(Collectors.toMap(cls -> StrUtil.toUnderlineCase(cls.getSimpleName())
.toLowerCase(), this::toEnumDict)));
}
}