提交 0dfa4433 编写于 作者: sq3536's avatar sq3536

字典支持filter和指定subNode

上级 86286777
...@@ -22,10 +22,7 @@ import org.springframework.stereotype.Service; ...@@ -22,10 +22,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j @Slf4j
@Service @Service
...@@ -70,7 +67,7 @@ public class DictCoreService ...@@ -70,7 +67,7 @@ public class DictCoreService
wrapper.likeRight(DictCatalog::getGroup,context.getN_vfilter_leftlike()); wrapper.likeRight(DictCatalog::getGroup,context.getN_vfilter_leftlike());
if(!StringUtils.isEmpty(context.getN_vfilter_eq())) if(!StringUtils.isEmpty(context.getN_vfilter_eq()))
wrapper.eq(DictCatalog::getGroup,context.getN_vfilter_eq()); wrapper.eq(DictCatalog::getGroup,context.getN_vfilter_eq());
wrapper.orderByAsc(DictCatalog::getGroup,DictCatalog::getCode); wrapper.orderByAsc(DictCatalog::getGroup, DictCatalog::getCode);
dictCatalogService.list(wrapper).forEach(item->{ dictCatalogService.list(wrapper).forEach(item->{
list.add(new Option().setValue(item.getCode()).setId(item.getCode()) list.add(new Option().setValue(item.getCode()).setId(item.getCode())
...@@ -82,23 +79,57 @@ public class DictCoreService ...@@ -82,23 +79,57 @@ public class DictCoreService
} }
else else
{ {
DictCatalog dictCatalog = dictCatalogService.getOne(Wrappers.<DictCatalog>query().eq("ccode",code)); String codeName=code;
catalog.setCode(dictCatalog.getCode()).setName(dictCatalog.getName()); String filter="";
String subCode="";
if(codeName.indexOf(".")>0)
{
String[] arg= codeName.split("[.]");
codeName=arg[0];
subCode=arg[1];
}
if(codeName.indexOf("-")>0)
{
String[] arg= codeName.split("-");
codeName=arg[0];
filter=arg[1];
}
DictCatalog dictCatalog = dictCatalogService.getOne(Wrappers.<DictCatalog>query().eq("ccode",codeName));
catalog.setCode(code).setName(dictCatalog.getName());
List<Option> list = new ArrayList<>(); Map<String,List<Option>> map = new LinkedHashMap<>();
optionService.list(context.getSelectCond().eq("cid",dictCatalog.getId()).orderByAsc("showorder")).forEach(item->{ Option subOption=null;
for(DictOption item:optionService.list(context.getSelectCond().eq("cid",dictCatalog.getId()).orderByAsc("showorder")))
{
Map<String,Object> extension = new HashMap<>(); Map<String,Object> extension = new HashMap<>();
if(!StringUtils.isEmpty(item.getExtension())) if(!StringUtils.isEmpty(item.getExtension()))
extension = JSONObject.parseObject(item.getExtension(),Map.class); extension = JSONObject.parseObject(item.getExtension(),Map.class);
list.add(new Option().setValue(item.getValue()).setId(item.getValue()) String pid=StringUtils.isEmpty(item.getParent())?"_root":item.getParent();
.setDisabled(((item.getDisabled()!=null && item.getDisabled()==1)||(item.getExpired()!=null && item.getExpired()==1))?true:false) List<Option> list=null;
.setFilter(item.getFilter()).setIconClass(item.getIconClass()).setLabel(item.getLabel()).setParent(item.getParent()).setExtension(extension) if(!map.containsKey(pid))
); {
}); list=new ArrayList<>();
map.put(pid,list);
}
else
list=map.get(pid);
List<Option> codeItemTreeList = new ArrayList<Option>(); Option option=new Option().setValue(item.getValue()).setId(item.getValue())
codeItemTreeList = loop(list, ""); .setDisabled(((item.getDisabled()!=null && item.getDisabled()==1)||(item.getExpired()!=null && item.getExpired()==1))?true:false)
.setFilter(item.getFilter()).setIconClass(item.getIconClass()).setLabel(item.getLabel()).setParent(item.getParent()).setExtension(extension);
if(option.getValue().equals(subCode))
subOption=option;
if(StringUtils.isEmpty(filter)||option.getFilterSet().contains(filter))
list.add(option);
}
List<Option> codeItemTreeList = loop(map, (StringUtils.isEmpty(subCode))?"_root":subCode);
if(subOption!=null)
{
subOption.setChildren(codeItemTreeList).setParent("");
catalog.getOptions().add(subOption);
}
else
catalog.setOptions(codeItemTreeList); catalog.setOptions(codeItemTreeList);
} }
return catalog; return catalog;
...@@ -143,7 +174,7 @@ public class DictCoreService ...@@ -143,7 +174,7 @@ public class DictCoreService
wrapper.likeRight(DictCatalog::getGroup,context.getN_vfilter_leftlike()); wrapper.likeRight(DictCatalog::getGroup,context.getN_vfilter_leftlike());
if(!StringUtils.isEmpty(context.getN_vfilter_eq())) if(!StringUtils.isEmpty(context.getN_vfilter_eq()))
wrapper.eq(DictCatalog::getGroup,context.getN_vfilter_eq()); wrapper.eq(DictCatalog::getGroup,context.getN_vfilter_eq());
wrapper.orderByAsc(DictCatalog::getGroup,DictCatalog::getCode); wrapper.orderByAsc(DictCatalog::getGroup, DictCatalog::getCode);
dictCatalogService.list(wrapper).forEach(item->{ dictCatalogService.list(wrapper).forEach(item->{
list.add(new CodeItem().setValue(item.getCode()).setId(item.getCode()) list.add(new CodeItem().setValue(item.getCode()).setId(item.getCode())
...@@ -154,25 +185,67 @@ public class DictCoreService ...@@ -154,25 +185,67 @@ public class DictCoreService
catalog.setOptions(list); catalog.setOptions(list);
} }
else { else {
DictCatalog dictCatalog = dictCatalogService.getOne(Wrappers.<DictCatalog>query().eq("ccode",code));
catalog.setCode(dictCatalog.getCode()).setName(dictCatalog.getName());
String codeName=code;
String filter="";
String subCode="";
if(codeName.indexOf(".")>0)
{
String[] arg= codeName.split("[.]");
codeName=arg[0];
subCode=arg[1];
}
if(codeName.indexOf("-")>0)
{
String[] arg= codeName.split("-");
codeName=arg[0];
filter=arg[1];
}
List<CodeItem> list = new ArrayList<>(); DictCatalog dictCatalog = dictCatalogService.getOne(Wrappers.<DictCatalog>query().eq("ccode",codeName));
optionService.list(context.getSelectCond().eq("cid",dictCatalog.getId()).orderByAsc("showorder")).forEach(item->{ catalog.setCode(code).setName(dictCatalog.getName());
Map<String,List<CodeItem>> map = new LinkedHashMap<>();
List<CodeItem> alllist = new ArrayList<>();
CodeItem subOption=null;
for(DictOption item:optionService.list(context.getSelectCond().eq("cid",dictCatalog.getId()).orderByAsc("showorder"))){
Map<String,Object> extension = new HashMap<>(); Map<String,Object> extension = new HashMap<>();
if(!StringUtils.isEmpty(item.getExtension())) if(!StringUtils.isEmpty(item.getExtension()))
extension = JSONObject.parseObject(item.getExtension(),Map.class); extension = JSONObject.parseObject(item.getExtension(),Map.class);
list.add(new CodeItem().setValue(item.getValue()).setId(item.getValue()) String pid=StringUtils.isEmpty(item.getParent())?"_root":item.getParent();
.setDisabled(((item.getDisabled()!=null && item.getDisabled()==1)||(item.getExpired()!=null && item.getExpired()==1))?true:false) List<CodeItem> list=null;
.setFilter(item.getFilter()).setIconClass(item.getIconClass()).setLabel(item.getLabel()).setParent(item.getParent()).setExtension(extension) if(!map.containsKey(pid))
); {
}); list=new ArrayList<>();
catalog.setOptions(list); map.put(pid,list);
} }
else
list=map.get(pid);
CodeItem option=new CodeItem().setValue(item.getValue()).setId(item.getValue())
.setDisabled(((item.getDisabled()!=null && item.getDisabled()==1)||(item.getExpired()!=null && item.getExpired()==1))?true:false)
.setFilter(item.getFilter()).setIconClass(item.getIconClass()).setLabel(item.getLabel()).setParent(item.getParent()).setExtension(extension);
if(option.getValue().equals(subCode))
subOption=option;
if(StringUtils.isEmpty(filter)||option.getFilterSet().contains(filter))
{
alllist.add(option);
list.add(option);
}
}
if(subOption!=null)
{
List<CodeItem> codeItemTreeList = loopItem(map, subCode);
codeItemTreeList.add(0,subOption);
catalog.setOptions(codeItemTreeList);
}
else
catalog.setOptions(alllist);
}
return catalog; return catalog;
} }
...@@ -183,14 +256,10 @@ public class DictCoreService ...@@ -183,14 +256,10 @@ public class DictCoreService
} }
public List<Option> loop(List<Option> listCodeItem, Object parentValue) { public List<Option> loop(Map<String,List<Option>> listCodeItem, Object parentValue) {
List<Option> trees = new ArrayList<Option>(); List<Option> trees = new ArrayList<Option>();
for (Option codeItem : listCodeItem) { if(listCodeItem.containsKey(parentValue)) {
String codeItemParentValue = codeItem.getParent(); for (Option codeItem : listCodeItem.get(parentValue)) {
if (StringUtils.isEmpty(codeItemParentValue)) {
codeItemParentValue = "";
}
if (parentValue.equals(codeItemParentValue)) {
List<Option> childCodeItem = loop(listCodeItem, codeItem.getValue()); List<Option> childCodeItem = loop(listCodeItem, codeItem.getValue());
if (childCodeItem.size() > 0) { if (childCodeItem.size() > 0) {
codeItem.setChildren(childCodeItem); codeItem.setChildren(childCodeItem);
...@@ -201,19 +270,15 @@ public class DictCoreService ...@@ -201,19 +270,15 @@ public class DictCoreService
return trees; return trees;
} }
public List<CodeItem> loopCodeItem(List<CodeItem> listCodeItem, Object parentValue) { public List<CodeItem> loopItem(Map<String,List<CodeItem>> listCodeItem, Object parentValue) {
List<CodeItem> trees = new ArrayList<CodeItem>(); List<CodeItem> trees = new ArrayList<CodeItem>();
for (CodeItem codeItem : listCodeItem) { if(listCodeItem.containsKey(parentValue)) {
String codeItemParentValue = codeItem.getParent(); for (CodeItem codeItem : listCodeItem.get(parentValue)) {
if (StringUtils.isEmpty(codeItemParentValue)) { trees.add(codeItem);
codeItemParentValue = ""; List<CodeItem> childCodeItem = loopItem(listCodeItem, codeItem.getValue());
}
if (parentValue.equals(codeItemParentValue)) {
List<CodeItem> childCodeItem = loopCodeItem(listCodeItem, codeItem.getValue());
if (childCodeItem.size() > 0) { if (childCodeItem.size() > 0) {
codeItem.setChildren(childCodeItem); trees.addAll(childCodeItem);
} }
trees.add(codeItem);
} }
} }
return trees; return trees;
......
...@@ -4,14 +4,17 @@ package cn.ibizlab.util.dict; ...@@ -4,14 +4,17 @@ package cn.ibizlab.util.dict;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import liquibase.util.StringUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
@Getter @Getter
@Setter @Setter
...@@ -40,4 +43,15 @@ public class CodeItem ...@@ -40,4 +43,15 @@ public class CodeItem
{ {
return label; return label;
} }
@JsonIgnore
@JSONField(serialize = false)
public Set<String> getFilterSet()
{
Set<String> set=new HashSet<>();
if(!StringUtils.isEmpty(filter))
for(String str:filter.split(";|,"))
set.add(str);
return set;
}
} }
...@@ -3,14 +3,17 @@ package cn.ibizlab.util.dict; ...@@ -3,14 +3,17 @@ package cn.ibizlab.util.dict;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import liquibase.util.StringUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
@Getter @Getter
@Setter @Setter
...@@ -31,4 +34,14 @@ public class Option ...@@ -31,4 +34,14 @@ public class Option
private String parent; private String parent;
private Map<String,Object> extension; private Map<String,Object> extension;
@JsonIgnore
@JSONField(serialize = false)
public Set<String> getFilterSet()
{
Set<String> set=new HashSet<>();
if(!StringUtils.isEmpty(filter))
for(String str:filter.split(";|,"))
set.add(str);
return set;
}
} }
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册