字典数据加载
parent
9d1f8beac9
commit
0a648d7b24
|
|
@ -8,10 +8,7 @@ import lombok.Setter;
|
|||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Getter
|
||||
|
|
@ -21,9 +18,15 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
@JSONType(deserializer = DictObjectDeserializer.class)
|
||||
public class DictItem implements DictStr {
|
||||
private static final Map<String, List<DictItem>> cache = new ConcurrentHashMap<>();
|
||||
|
||||
private String key;
|
||||
private String val;
|
||||
private String txt;
|
||||
private static DictItemLoader loader;
|
||||
|
||||
public static void setLoader(DictItemLoader loader) {
|
||||
DictItem.loader = loader;
|
||||
}
|
||||
|
||||
public static void put(String key, DictItem dictItem) {
|
||||
cache.compute(key, (k, v) -> {
|
||||
|
|
@ -40,14 +43,25 @@ public class DictItem implements DictStr {
|
|||
public static DictItem get(String key, String val) {
|
||||
DictItem dictItem = new DictItem();
|
||||
cache.compute(key, (k, v) -> {
|
||||
if (CollUtil.isNotEmpty(v)) {
|
||||
Optional<DictItem> first = v.stream().filter(it -> it.val.equals(val)).findFirst();
|
||||
if (first.isPresent()) {
|
||||
DictItem data = first.get();
|
||||
dictItem.key = data.key;
|
||||
dictItem.val = data.val;
|
||||
dictItem.txt = data.txt;
|
||||
Optional<DictItem> data = Optional.empty();
|
||||
if (CollUtil.isEmpty(v)) {
|
||||
if (loader != null) {
|
||||
List<DictItem> list = loader.load(k);
|
||||
if (CollUtil.isNotEmpty(list)) {
|
||||
if (v == null) v = new LinkedList<>();
|
||||
v.addAll(list);
|
||||
data = v.stream().filter(it -> Objects.equals(it.val, val)).findFirst();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
data = v.stream().filter(it -> Objects.equals(it.val, val)).findFirst();
|
||||
}
|
||||
|
||||
if (data.isPresent()) {
|
||||
DictItem item = data.get();
|
||||
dictItem.key = item.key;
|
||||
dictItem.val = item.val;
|
||||
dictItem.txt = item.txt;
|
||||
}
|
||||
return v;
|
||||
});
|
||||
|
|
@ -61,7 +75,7 @@ public class DictItem implements DictStr {
|
|||
|
||||
public static void remove(String key, String val) {
|
||||
cache.compute(key, (k, v) -> {
|
||||
if (CollUtil.isNotEmpty(v)) v.removeIf(it -> it.val.equals(val));
|
||||
if (CollUtil.isNotEmpty(v)) v.removeIf(it -> Objects.equals(it.val, val));
|
||||
return v;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
package com.njzscloud.common.core.ienum;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface DictItemLoader {
|
||||
List<DictItem> load(String key);
|
||||
}
|
||||
|
|
@ -15,13 +15,6 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
|||
*/
|
||||
@SuppressWarnings({"ConstantConditions"})
|
||||
public class DictHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
{
|
||||
DictItem dictItem = new DictItem()
|
||||
.setKey("key")
|
||||
.setVal("val")
|
||||
.setTxt("txt");
|
||||
DictItem.put("key", dictItem);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支持 {@link DictInt} 和 {@link DictStr} 类型的返回值
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
package com.njzscloud.dispose.config;
|
||||
|
||||
import com.njzscloud.common.core.ienum.DictItem;
|
||||
import com.njzscloud.common.security.util.SecurityUtil;
|
||||
import com.njzscloud.dispose.sys.dict.service.DictService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
|
@ -10,10 +13,13 @@ import java.util.List;
|
|||
import static com.njzscloud.dispose.event.SysMittEvent.COERCE_LOGOUT;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SpringReady {
|
||||
private final DictService dictService;
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void onApplicationReady() {
|
||||
COERCE_LOGOUT.on((List<Long> ids) -> ids.forEach(SecurityUtil::removeToken));
|
||||
DictItem.setLoader(dictService);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package com.njzscloud.dispose.sys.dict.controller;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.njzscloud.common.core.ienum.DictItem;
|
||||
import com.njzscloud.common.core.ienum.DictKey;
|
||||
import com.njzscloud.common.core.utils.R;
|
||||
import com.njzscloud.common.mp.support.PageParam;
|
||||
import com.njzscloud.common.mp.support.PageResult;
|
||||
|
|
@ -31,6 +31,7 @@ public class DictController {
|
|||
@PostMapping("/add")
|
||||
public R<?> add(@RequestBody DictEntity dictEntity) {
|
||||
dictService.add(dictEntity);
|
||||
DictItem.remove(dictEntity.getDictKey());
|
||||
return R.success();
|
||||
}
|
||||
|
||||
|
|
@ -40,6 +41,7 @@ public class DictController {
|
|||
@PostMapping("/modify")
|
||||
public R<?> modify(@RequestBody DictEntity dictEntity) {
|
||||
dictService.modify(dictEntity);
|
||||
DictItem.remove(dictEntity.getDictKey());
|
||||
return R.success();
|
||||
}
|
||||
|
||||
|
|
@ -48,7 +50,12 @@ public class DictController {
|
|||
*/
|
||||
@PostMapping("/del")
|
||||
public R<?> del(@RequestBody List<Long> ids) {
|
||||
dictService.del(ids);
|
||||
List<String> keys = dictService.del(ids);
|
||||
if (CollUtil.isNotEmpty(keys)) {
|
||||
for (String key : keys) {
|
||||
DictItem.remove(key);
|
||||
}
|
||||
}
|
||||
return R.success();
|
||||
}
|
||||
|
||||
|
|
@ -78,23 +85,4 @@ public class DictController {
|
|||
return R.success(dictService.obtainDictData(dictKey, txt));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取字典数据
|
||||
*/
|
||||
@GetMapping("/a")
|
||||
public R<?> a(@DictKey("key") @RequestParam("dictItem") DictItem dictItem) {
|
||||
return R.success(dictItem);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取字典数据
|
||||
*/
|
||||
@PostMapping("/b")
|
||||
public R<?> b(@RequestBody Data data) {
|
||||
return R.success(data);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.njzscloud.common.core.ex.Exceptions;
|
||||
import com.njzscloud.common.core.ienum.DictItem;
|
||||
import com.njzscloud.common.core.ienum.DictItemLoader;
|
||||
import com.njzscloud.common.mp.support.PageParam;
|
||||
import com.njzscloud.common.mp.support.PageResult;
|
||||
import com.njzscloud.dispose.sys.dict.mapper.DictMapper;
|
||||
|
|
@ -27,7 +29,7 @@ import java.util.stream.Collectors;
|
|||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class DictService extends ServiceImpl<DictMapper, DictEntity> implements IService<DictEntity> {
|
||||
public class DictService extends ServiceImpl<DictMapper, DictEntity> implements DictItemLoader, IService<DictEntity> {
|
||||
private final DictItemService dictItemService;
|
||||
|
||||
/**
|
||||
|
|
@ -51,9 +53,12 @@ public class DictService extends ServiceImpl<DictMapper, DictEntity> implements
|
|||
* 删除
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void del(List<Long> ids) {
|
||||
public List<String> del(List<Long> ids) {
|
||||
List<DictEntity> dictEntities = listByIds(ids);
|
||||
List<String> list = dictEntities.stream().map(DictEntity::getDictKey).toList();
|
||||
this.removeBatchByIds(ids);
|
||||
dictItemService.remove(Wrappers.<DictItemEntity>lambdaQuery().in(DictItemEntity::getDictId, ids));
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -90,4 +95,16 @@ public class DictService extends ServiceImpl<DictMapper, DictEntity> implements
|
|||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DictItem> load(String key) {
|
||||
try {
|
||||
return dictItemService.list(Wrappers.<DictItemEntity>lambdaQuery().eq(DictItemEntity::getDictKey, key)
|
||||
.orderByAsc(DictItemEntity::getSort, DictItemEntity::getVal, DictItemEntity::getId)
|
||||
)
|
||||
.stream().map(it -> new DictItem().setKey(key).setVal(it.getVal()).setTxt(it.getTxt()))
|
||||
.collect(Collectors.toList());
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue