提交 72096f5d 编写于 作者: sq3536's avatar sq3536

init

上级 a6bfca56
......@@ -7,7 +7,7 @@
<artifactId>ibizlab-util</artifactId>
<name>ibizlab-util</name>
<description>ibizlab-util</description>
<version>0.0.1</version>
<version>0.0.2</version>
<!-- Spring Boot -->
<parent>
......
package cn.ibizlab.util.aspect;
import lombok.SneakyThrows;
import cn.ibizlab.util.annotation.Audit;
import cn.ibizlab.util.domain.EntityBase;
import cn.ibizlab.util.helper.DEFieldCacheMap;
import cn.ibizlab.util.service.IBZDataAuditService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* 实体数据审计切面类
*/
@Aspect
@Component
public class AuditAspect
{
private final ExpressionParser parser = new SpelExpressionParser();
@Autowired
IBZDataAuditService dataAuditService;
/**
* 实体数据建立切面,在成功创建数据后将新增数据内容记录审计日志内(审计明细【AuditInfo】中只记录审计属性变化情况,审计属性在平台属性中配置)
* @param point
*/
@AfterReturning(value = "execution(* cn.ibizlab.core.*.service.*.create(..))")
@SneakyThrows
public void create(JoinPoint point) {
HttpServletRequest request = null;
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(requestAttributes!=null) {
request = ((ServletRequestAttributes)requestAttributes).getRequest();
}
Object [] args = point.getArgs();
if(ObjectUtils.isEmpty(args) || args.length==0) {
return;
}
Object serviceParam = args[0];
if(serviceParam instanceof EntityBase) {
EntityBase entity = (EntityBase)serviceParam;
Map<String, Audit> auditFields = DEFieldCacheMap.getAuditFields(entity.getClass());
//是否有审计属性
if(auditFields.size()==0) {
return;
}
String idField = DEFieldCacheMap.getDEKeyField(entity.getClass());
Object idValue = "";
if(!StringUtils.isEmpty(idField)) {
idValue=entity.get(idField);
}
//记录审计日志
dataAuditService.createAudit(request, entity, idValue, auditFields);
}
}
/**
* 实体数据更新切面,在成功更新数据后将新增数据内容记录审计日志内(审计明细【AuditInfo】中只记录审计属性变化情况,审计属性在平台属性中配置)
* 使用环切【@Around】获取到更新前后的实体数据并进行差异比较,并将差异内容记入审计日志内
* @param point
*/
@Around("execution(* cn.ibizlab.core.*.service.*.update(..))")
public Object update(ProceedingJoinPoint point) throws Throwable {
HttpServletRequest request = null;
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(requestAttributes!=null) {
request=((ServletRequestAttributes)requestAttributes).getRequest();
}
Object serviceObj = point.getTarget();
Object args[] = point.getArgs();
if(ObjectUtils.isEmpty(args) || args.length==0) {
return point.proceed();
}
Object arg = args[0];
if(arg instanceof EntityBase) {
EntityBase entity = (EntityBase) arg;
Map<String, Audit> auditFields = DEFieldCacheMap.getAuditFields(entity.getClass());
//是否有审计属性
if(auditFields.size()==0) {
return point.proceed();
}
String idField = DEFieldCacheMap.getDEKeyField(entity.getClass());
Object idValue = "";
if(!StringUtils.isEmpty(idField)){
idValue = entity.get(idField);
}
if(ObjectUtils.isEmpty(idValue)) {
return point.proceed();
}
//获取更新前实体
EntityBase beforeEntity = getEntity(serviceObj, idValue);
//执行更新操作
point.proceed();
//记录审计日志
dataAuditService.updateAudit(request, beforeEntity, serviceObj, idValue, auditFields);
return true;
}
return point.proceed();
}
/**
* 实体数据更新切面,在成功更新数据后将新增数据内容记录审计日志内(审计明细【AuditInfo】中只记录审计属性变化情况,审计属性在平台属性中配置)
* 使用环切【@Around】获取要删除的完整数据,并将审计属性相关信息记录到审计日志中
* @param point
* @return
* @throws Throwable
*/
@Around("execution(* cn.ibizlab.core.*.service.*.remove(..))")
public Object remove(ProceedingJoinPoint point) throws Throwable {
HttpServletRequest request = null;
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(requestAttributes!= null) {
request = ((ServletRequestAttributes)requestAttributes).getRequest();
}
Object serviceObj = point.getTarget();
Object args[] = point.getArgs();
if(ObjectUtils.isEmpty(args) || args.length==0) {
return point.proceed();
}
Object idValue = args[0];
EntityBase entity = getEntity(serviceObj, idValue);
Map<String, Audit> auditFields = DEFieldCacheMap.getAuditFields(entity.getClass());
if(auditFields.size()==0) {
return point.proceed();
}
else{
//执行删除操作
point.proceed();
//记录审计日志
dataAuditService.removeAudit(request, entity, idValue, auditFields);
return true;
}
}
/**
* 获取实体
* @param service
* @param id
* @return
*/
@SneakyThrows
private EntityBase getEntity(Object service, Object id) {
EntityBase entity = null;
if(!ObjectUtils.isEmpty(service)) {
EvaluationContext oldContext = new StandardEvaluationContext();
oldContext.setVariable("service", service);
oldContext.setVariable("id", id);
Expression oldExp = parser.parseExpression("#service.get(#id)");
return oldExp.getValue(oldContext, EntityBase.class);
}
return entity;
}
}
\ No newline at end of file
package cn.ibizlab.util.aspect;
import lombok.SneakyThrows;
import cn.ibizlab.util.annotation.VersionCheck;
import cn.ibizlab.util.domain.EntityBase;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.RuleUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
/**
* 数据库版本检查
*/
@Aspect
@Order(50)
@Component
public class VersionCheckAspect
{
private final ExpressionParser parser = new SpelExpressionParser();
private final String IgnoreField = "ignoreversioncheck";
@SneakyThrows
@Before("execution(* cn.ibizlab.*.rest.*.update(..)) && @annotation(versionCheck)")
public void BeforeUpdate(JoinPoint point, VersionCheck versionCheck) {
Object[] args = point.getArgs();
Object id = args[0];
Object dto = args[1];
if(ObjectUtils.isEmpty(id) || ObjectUtils.isEmpty(dto)) {
return;
}
String versionField = versionCheck.versionfield();
if(StringUtils.isEmpty(versionField)) {
return;
}
versionCheck(versionCheck,point.getTarget(), dto, id);
}
@SneakyThrows
@Before("execution(* cn.ibizlab.*.rest.*.updateBy*(..)) && @annotation(versionCheck)")
public void BeforeUpdateBy(JoinPoint point, VersionCheck versionCheck) {
Object[] args = point.getArgs();
if(args.length>=2) {
Object id = args[args.length-2];
Object dto = args[args.length-1];
if(ObjectUtils.isEmpty(id) || ObjectUtils.isEmpty(dto)) {
return;
}
String versionField = versionCheck.versionfield();
if(StringUtils.isEmpty(versionField)) {
return;
}
versionCheck(versionCheck, point.getTarget(), dto, id);
}
}
private void versionCheck(VersionCheck versionCheck, Object resource, Object dto, Object id) {
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("dto", dto);
//忽略版本检查
Expression dtoParamsExp = parser.parseExpression("#dto.extensionparams");
Map dtoParam = dtoParamsExp.getValue(context, Map.class);
if(!ObjectUtils.isEmpty(dtoParam) && !ObjectUtils.isEmpty(dtoParam.get(IgnoreField)) && dtoParam.get(IgnoreField).equals(1)) {
return;
}
Expression newExp = parser.parseExpression(String.format("#dto.%s", versionCheck.versionfield()));
Object newVersion = newExp.getValue(context);
if(ObjectUtils.isEmpty(newVersion)) {
return;
}
//进行版本检查
Object oldVersion = getDBVersion(versionCheck,getService(resource, versionCheck.entity()), id);
if(!ObjectUtils.isEmpty(oldVersion)) {
if(RuleUtils.gt(newVersion, oldVersion)) {
throw new BadRequestAlertException("数据已变更,可能后台数据已被修改,请重新加载数据", "VersionCheckAspect", "versionCheck");
}
}
}
/**
* 获取实体服务对象
* @param resource
* @param entity
* @return
*/
@SneakyThrows
private Object getService(Object resource, String entity) {
Object service = null;
Field[] fields = resource.getClass().getDeclaredFields();
for(Field field : fields) {
if(field.getModifiers()==1 && field.getName().equalsIgnoreCase(String.format("%sService",entity))) {
service = field.get(resource);
break;
}
}
return service;
}
/**
* 获取数据库版本
* @param versionCheck
* @param service
* @param id
* @return
*/
@SneakyThrows
private Object getDBVersion(VersionCheck versionCheck, Object service, Object id) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp dbVersion = null;
String versionField = versionCheck.versionfield();
if(!ObjectUtils.isEmpty(service)) {
EvaluationContext oldContext = new StandardEvaluationContext();
oldContext.setVariable("service", service);
oldContext.setVariable("id", id);
Expression oldExp = parser.parseExpression("#service.get(#id)");
EntityBase oldEntity = oldExp.getValue(oldContext, EntityBase.class);
Object oldDate = oldEntity.get(versionField);
if(oldDate!=null && oldDate instanceof Timestamp) {
Timestamp db_time = (Timestamp) oldDate;
Date db_date = sdf.parse(sdf.format(db_time));
dbVersion = new Timestamp(db_date.getTime());
}
}
return dbVersion;
}
}
package cn.ibizlab.util.client;
import cn.ibizlab.util.dict.Catalog;
import cn.ibizlab.util.dict.CodeList;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Component;
@Component
public class IBZDictFallback implements IBZDictFeignClient {
@Override
public Boolean syncRuntimeDict(JSONArray catalogs) {
return null;
}
@Override
public CodeList getCodeList(String code) {
return null;
}
@Override
public Catalog getCatalog(String code) {
return null;
}
}
\ No newline at end of file
package cn.ibizlab.util.client;
import cn.ibizlab.util.dict.Catalog;
import cn.ibizlab.util.dict.CodeList;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
import java.util.Map;
@FeignClient(value = "${r'${ibiz.ref.service.dict:ibzdict-api}'}",contextId = "dict",fallback = IBZDictFallback.class)
public interface IBZDictFeignClient
{
@RequestMapping(method = RequestMethod.POST, value = "/dictionarys/catalogs/sync")
Boolean syncRuntimeDict(@RequestBody JSONArray catalogs);
@Cacheable( value="dictcatalog",key = "'codelist:'+#p0")
@RequestMapping(method = RequestMethod.GET, value = "/dictionarys/codelist/{code}")
CodeList getCodeList(@PathVariable("code") String code);
@Cacheable( value="dictcatalog",key = "'dict:'+#p0")
@RequestMapping(method = RequestMethod.GET, value = "/dictionarys/catalogs/{code}")
Catalog getCatalog(@PathVariable("code") String code);
}
\ No newline at end of file
package cn.ibizlab.util.dict;
import cn.ibizlab.util.helper.DataObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Catalog
{
private String code;
private String name;
@JSONField(name = "items")
@JsonProperty("items")
private List<Option> options = new ArrayList<>();
public Option findCodeItem(Object value)
{
return findCodeItem(value,this.options);
}
public Option findCodeItem(Object value, List<Option> options){
for(Option codeItem : options){
if(value != null && codeItem.getValue() != null && value.equals(codeItem.getValue()))
return codeItem;
else if (!ObjectUtils.isEmpty(codeItem.getChildren()))
{
Option rt=findCodeItem(value,codeItem.getChildren());
if(rt!=null)
return rt;
}
}
return null;
}
private Map<String, Object> advancedSettings;
public Catalog putAdvancedSettings(String settings)
{
try
{
if(!StringUtils.isEmpty(settings))
advancedSettings= JSON.parseObject(settings, Map.class);
}catch (Exception ex){}
return this;
}
@JsonIgnore
@JSONField(serialize = false)
public String getUrl() {
return advancedSettings!=null&&advancedSettings.get("url")!=null?advancedSettings.get("url").toString():null;
}
@JsonIgnore
@JSONField(serialize = false)
public String getRequestMethod() {
return advancedSettings!=null&&advancedSettings.get("requestMethod")!=null?advancedSettings.get("requestMethod").toString():"GET";
}
@JsonIgnore
@JSONField(serialize = false)
public String getRequestBody() {
return advancedSettings!=null&&advancedSettings.get("requestBody")!=null?advancedSettings.get("requestBody").toString():"{}";
}
@JsonIgnore
@JSONField(serialize = false)
public String getNodeProp() {
return advancedSettings!=null&&advancedSettings.get("nodeProp")!=null?advancedSettings.get("nodeProp").toString():"";
}
@JsonIgnore
@JSONField(serialize = false)
public String getValueProp() {
return advancedSettings!=null&&advancedSettings.get("valueProp")!=null?advancedSettings.get("valueProp").toString():"value";
}
@JsonIgnore
@JSONField(serialize = false)
public String getLabelProp() {
return advancedSettings!=null&&advancedSettings.get("labelProp")!=null?advancedSettings.get("labelProp").toString():"label";
}
@JsonIgnore
@JSONField(serialize = false)
public String getParentProp() {
return advancedSettings!=null&&advancedSettings.get("parentProp")!=null?advancedSettings.get("parentProp").toString():"parent";
}
@JsonIgnore
@JSONField(serialize = false)
public String getChildrenProp() {
return advancedSettings!=null&&advancedSettings.get("childrenProp")!=null?advancedSettings.get("childrenProp").toString():"children";
}
@JsonIgnore
@JSONField(serialize = false)
public String getDisabledProp() {
return advancedSettings!=null&&advancedSettings.get("disabledProp")!=null?advancedSettings.get("disabledProp").toString():"disabled";
}
@JsonIgnore
@JSONField(serialize = false)
public String getFilterProp() {
return advancedSettings!=null&&advancedSettings.get("filterProp")!=null?advancedSettings.get("filterProp").toString():"filter";
}
@JsonIgnore
@JSONField(serialize = false)
public String getExpiredProp() {
return advancedSettings!=null&&advancedSettings.get("expiredProp")!=null?advancedSettings.get("expiredProp").toString():"expired";
}
@JsonIgnore
@JSONField(serialize = false)
public String getIconClassProp() {
return advancedSettings!=null&&advancedSettings.get("iconClassProp")!=null?advancedSettings.get("iconClassProp").toString():"iconClass";
}
public Catalog setNodes(List<Map> nodes)
{
return setNodes(nodes,null,null);
}
public Catalog setNodes(List<Map> nodes, String filter, String subCode)
{
Map<String, List<Option>> map = new LinkedHashMap<>();
Option subOption=null;
for(Map item:unfoldList(nodes,null))
{
Map<String, Object> extension = new HashMap<>();
if(!StringUtils.isEmpty(item.get("extension")))
extension = JSONObject.parseObject(item.get("extension").toString(), Map.class);
String pid= DataObject.getStringValue(item.get(this.getParentProp()), DataObject.getStringValue(item.get("pvalue"),"_root"));
if(StringUtils.isEmpty(pid))pid="_root";
List<Option> list=null;
if(!map.containsKey(pid))
{
list=new ArrayList<>();
map.put(pid,list);
}
else
list=map.get(pid);
Option option=new Option().setValue(DataObject.getStringValue(item.get(this.getValueProp()), DataObject.getStringValue(item.get("id"),null)))
.setId(DataObject.getStringValue(item.get(this.getValueProp()), DataObject.getStringValue(item.get("id"),null)))
.setDisabled(DataObject.getBooleanValue(item.get(this.getDisabledProp()),false)|| DataObject.getBooleanValue(item.get(this.getExpiredProp()),false))
.setFilter(DataObject.getStringValue(item.get(this.getFilterProp()),null)).setIconClass(DataObject.getStringValue(item.get(this.getIconClassProp()),null))
.setLabel(DataObject.getStringValue(item.get(this.getLabelProp()), DataObject.getStringValue(item.get("text"),"")))
.setParent(DataObject.getStringValue(item.get(this.getParentProp()), DataObject.getStringValue(item.get("pvalue"),""))).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("");
this.getOptions().add(subOption.setParent(null));
}
else
this.setOptions(codeItemTreeList);
return this;
}
public List<Map> unfoldList(List<Map> nodes, String parent)
{
List<Map> unfoldList = new ArrayList<>();
for(Map item:nodes)
{
if((!StringUtils.isEmpty(parent))&&StringUtils.isEmpty(DataObject.getStringValue(item.get(this.getParentProp()), DataObject.getStringValue(item.get("pvalue"),null))))
item.put(this.getParentProp(),parent);
String value = DataObject.getStringValue(item.get(this.getValueProp()), DataObject.getStringValue(item.get("id"),null));
unfoldList.add(item);
Object child=item.get(this.getChildrenProp());
if(!ObjectUtils.isEmpty(child))
{
if(child instanceof List && ((List) child).get(0) instanceof Map)
unfoldList.addAll(unfoldList((List)child,value));
item.remove(this.getChildrenProp());
}
}
return unfoldList;
}
public List<Option> loop(Map<String, List<Option>> listCodeItem, Object parentValue) {
List<Option> trees = new ArrayList<Option>();
if(listCodeItem.containsKey(parentValue)) {
for (Option codeItem : listCodeItem.get(parentValue)) {
List<Option> childCodeItem = loop(listCodeItem, codeItem.getValue());
if (childCodeItem.size() > 0)
codeItem.setChildren(childCodeItem);
trees.add(codeItem);
}
}
return trees;
}
}
package cn.ibizlab.util.dict;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import liquibase.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CodeItem
{
private String id;
private Object value;
private String label;
@JsonIgnore
@JSONField(serialize = false)
private List<CodeItem> children;
private String iconClass;
private String filter;
private Boolean disabled;
@JSONField(name = "pvalue")
@JsonProperty("pvalue")
private String parent;
private Map<String, Object> extension;
@JSONField(name = "text")
@JsonProperty("text")
public String getText()
{
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;
}
}
package cn.ibizlab.util.dict;
import cn.ibizlab.util.helper.DataObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CodeList
{
@JSONField(name = "srfkey")
@JsonProperty("srfkey")
private String code;
private String name;
@JSONField(name = "emptytext")
@JsonProperty("emptytext")
public String getEmptytext()
{
return "";
}
@JSONField(name = "items")
@JsonProperty("items")
private List<CodeItem> options = new ArrayList<>();
public CodeItem findCodeItem(Object value){
for(CodeItem codeItem : options){
if(value != null && codeItem.getValue() != null && value.equals(codeItem.getValue()))
return codeItem;
}
return null;
}
public CodeItem findChildren(Object value){
CodeItem resultCodeItem = this.findCodeItem(value);
List<CodeItem> children = new ArrayList<>();
for(CodeItem codeItem : options){
if(value != null && resultCodeItem.getValue() != null && codeItem.getParent() != null && codeItem.getParent().equals(resultCodeItem.getValue()))
children.add(codeItem);
}
resultCodeItem.setChildren(children);
return resultCodeItem;
}
private Map<String, Object> advancedSettings;
public CodeList putAdvancedSettings(String settings)
{
try
{
if(!StringUtils.isEmpty(settings))
advancedSettings= JSON.parseObject(settings, Map.class);
}catch (Exception ex){}
return this;
}
@JsonIgnore
@JSONField(serialize = false)
public String getUrl() {
return advancedSettings!=null&&advancedSettings.get("url")!=null?advancedSettings.get("url").toString():null;
}
@JsonIgnore
@JSONField(serialize = false)
public String getRequestMethod() {
return advancedSettings!=null&&advancedSettings.get("requestMethod")!=null?advancedSettings.get("requestMethod").toString():"GET";
}
@JsonIgnore
@JSONField(serialize = false)
public String getRequestBody() {
return advancedSettings!=null&&advancedSettings.get("requestBody")!=null?advancedSettings.get("requestBody").toString():"{}";
}
@JsonIgnore
@JSONField(serialize = false)
public String getNodeProp() {
return advancedSettings!=null&&advancedSettings.get("nodeProp")!=null?advancedSettings.get("nodeProp").toString():"";
}
@JsonIgnore
@JSONField(serialize = false)
public String getValueProp() {
return advancedSettings!=null&&advancedSettings.get("valueProp")!=null?advancedSettings.get("valueProp").toString():"value";
}
@JsonIgnore
@JSONField(serialize = false)
public String getLabelProp() {
return advancedSettings!=null&&advancedSettings.get("labelProp")!=null?advancedSettings.get("labelProp").toString():"label";
}
@JsonIgnore
@JSONField(serialize = false)
public String getParentProp() {
return advancedSettings!=null&&advancedSettings.get("parentProp")!=null?advancedSettings.get("parentProp").toString():"parent";
}
@JsonIgnore
@JSONField(serialize = false)
public String getChildrenProp() {
return advancedSettings!=null&&advancedSettings.get("childrenProp")!=null?advancedSettings.get("childrenProp").toString():"children";
}
@JsonIgnore
@JSONField(serialize = false)
public String getDisabledProp() {
return advancedSettings!=null&&advancedSettings.get("disabledProp")!=null?advancedSettings.get("disabledProp").toString():"disabled";
}
@JsonIgnore
@JSONField(serialize = false)
public String getFilterProp() {
return advancedSettings!=null&&advancedSettings.get("filterProp")!=null?advancedSettings.get("filterProp").toString():"filter";
}
@JsonIgnore
@JSONField(serialize = false)
public String getExpiredProp() {
return advancedSettings!=null&&advancedSettings.get("expiredProp")!=null?advancedSettings.get("expiredProp").toString():"expired";
}
@JsonIgnore
@JSONField(serialize = false)
public String getIconClassProp() {
return advancedSettings!=null&&advancedSettings.get("iconClassProp")!=null?advancedSettings.get("iconClassProp").toString():"iconClass";
}
public CodeList setNodes(List<Map> nodes, String filter, String subCode)
{
Map<String, List<CodeItem>> map = new LinkedHashMap<>();
List<CodeItem> alllist = new ArrayList<>();
CodeItem subOption=null;
for(Map item:unfoldList(nodes,null))
{
Map<String, Object> extension = new HashMap<>();
if(!StringUtils.isEmpty(item.get("extension")))
extension = JSONObject.parseObject(item.get("extension").toString(), Map.class);
String pid= DataObject.getStringValue(item.get(this.getParentProp()), DataObject.getStringValue(item.get("pvalue"),"_root"));
if(StringUtils.isEmpty(pid))pid="_root";
List<CodeItem> list=null;
if(!map.containsKey(pid))
{
list=new ArrayList<>();
map.put(pid,list);
}
else
list=map.get(pid);
CodeItem option=new CodeItem().setValue(DataObject.getStringValue(item.get(this.getValueProp()), DataObject.getStringValue(item.get("id"),null)))
.setId(DataObject.getStringValue(item.get(this.getValueProp()), DataObject.getStringValue(item.get("id"),null)))
.setDisabled(DataObject.getBooleanValue(item.get(this.getDisabledProp()),false)|| DataObject.getBooleanValue(item.get(this.getExpiredProp()),false))
.setFilter(DataObject.getStringValue(item.get(this.getFilterProp()),null)).setIconClass(DataObject.getStringValue(item.get(this.getIconClassProp()),null))
.setLabel(DataObject.getStringValue(item.get(this.getLabelProp()), DataObject.getStringValue(item.get("text"),"")))
.setParent(DataObject.getStringValue(item.get(this.getParentProp()), DataObject.getStringValue(item.get("pvalue"),""))).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 = loop(map, (StringUtils.isEmpty(subCode))?"_root":subCode);
codeItemTreeList.add(0,subOption.setParent(null));
this.setOptions(codeItemTreeList);
}
else
this.setOptions(alllist);
return this;
}
public List<Map> unfoldList(List<Map> nodes, String parent)
{
List<Map> unfoldList = new ArrayList<>();
for(Map item:nodes)
{
if((!StringUtils.isEmpty(parent))&&StringUtils.isEmpty(DataObject.getStringValue(item.get(this.getParentProp()), DataObject.getStringValue(item.get("pvalue"),null))))
item.put(this.getParentProp(),parent);
String value = DataObject.getStringValue(item.get(this.getValueProp()), DataObject.getStringValue(item.get("id"),null));
unfoldList.add(item);
Object child=item.get(this.getChildrenProp());
if(!ObjectUtils.isEmpty(child))
{
if(child instanceof List && ((List) child).get(0) instanceof Map)
unfoldList.addAll(unfoldList((List)child,value));
item.remove(this.getChildrenProp());
}
}
return unfoldList;
}
public List<CodeItem> loop(Map<String, List<CodeItem>> listCodeItem, Object parentValue) {
List<CodeItem> trees = new ArrayList<CodeItem>();
if(listCodeItem.containsKey(parentValue)) {
for (CodeItem codeItem : listCodeItem.get(parentValue)) {
trees.add(codeItem);
List<CodeItem> childCodeItem = loop(listCodeItem, codeItem.getValue());
if (childCodeItem.size() > 0)
trees.addAll(childCodeItem);
}
}
return trees;
}
}
package cn.ibizlab.util.dict;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import liquibase.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Option
{
private String id;
private Object value;
private String label;
private List<Option> children;
private String iconClass;
private String filter;
private Boolean disabled;
@JsonIgnore
@JSONField(serialize = false)
private String parent;
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 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册