mirror of https://gitee.com/stylefeng/roses
【7.6.0】【dict】更新接口:更新整个字典树结构,用来更新上下级结构和顺序
parent
d03406ffa7
commit
e760bd0acd
|
@ -132,4 +132,16 @@ public class DictController {
|
||||||
return new SuccessResponseData<>(this.dictService.findList(dictRequest));
|
return new SuccessResponseData<>(this.dictService.findList(dictRequest));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新整个字典树结构,用来更新上下级结构和顺序
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/6/27 18:23
|
||||||
|
*/
|
||||||
|
@GetResource(name = "更新整个字典树结构,用来更新上下级结构和顺序", path = "/dict/updateDictTree")
|
||||||
|
public ResponseData<List<SysDict>> updateDictTree(DictRequest dictRequest) {
|
||||||
|
this.dictService.updateDictTree(dictRequest);
|
||||||
|
return new SuccessResponseData<>();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,10 @@
|
||||||
*/
|
*/
|
||||||
package cn.stylefeng.roses.kernel.dict.modular.entity;
|
package cn.stylefeng.roses.kernel.dict.modular.entity;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseBusinessEntity;
|
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseBusinessEntity;
|
||||||
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
|
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
|
||||||
|
import cn.stylefeng.roses.kernel.rule.tree.buildpids.BasePidBuildModel;
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
@ -34,6 +36,7 @@ import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典实体
|
* 字典实体
|
||||||
|
@ -44,7 +47,7 @@ import java.math.BigDecimal;
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@TableName(value = "sys_dict", autoResultMap = true)
|
@TableName(value = "sys_dict", autoResultMap = true)
|
||||||
@Data
|
@Data
|
||||||
public class SysDict extends BaseBusinessEntity {
|
public class SysDict extends BaseBusinessEntity implements BasePidBuildModel {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ -146,4 +149,32 @@ public class SysDict extends BaseBusinessEntity {
|
||||||
@ChineseDescription("字典上级的名称(字典有上下级,字典类型没有上下级)")
|
@ChineseDescription("字典上级的名称(字典有上下级,字典类型没有上下级)")
|
||||||
private String parentName;
|
private String parentName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字典的下级结构
|
||||||
|
*/
|
||||||
|
@TableField(exist = false)
|
||||||
|
@ChineseDescription("字典的下级结构")
|
||||||
|
private List<SysDict> children;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String pidBuildNodeId() {
|
||||||
|
if (ObjectUtil.isEmpty(dictId)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return dictId.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String pidBuildParentId() {
|
||||||
|
if (ObjectUtil.isEmpty(dictParentId)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return dictParentId.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setPidBuildPidStructure(String pids) {
|
||||||
|
this.dictPids = pids;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,102 @@
|
||||||
|
package cn.stylefeng.roses.kernel.dict.modular.factory;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字典构建工厂
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/6/27 18:32
|
||||||
|
*/
|
||||||
|
public class DictFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新字典的排序
|
||||||
|
*
|
||||||
|
* @param tree 被更新的字典树
|
||||||
|
* @param level 当前被更新的字典的层级(level从1开始)
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/6/27 18:32
|
||||||
|
*/
|
||||||
|
public static void updateSort(List<SysDict> tree, Integer level) {
|
||||||
|
|
||||||
|
// 初始的排序值
|
||||||
|
int i = 1;
|
||||||
|
|
||||||
|
// 倍数,第1层级从100开始排列
|
||||||
|
// 第2层是1000开始
|
||||||
|
int beishu = 10;
|
||||||
|
|
||||||
|
for (int integer = 0; integer < level; integer++) {
|
||||||
|
beishu = beishu * 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第1层级是110,120,130
|
||||||
|
// 第2层级是1010,1020,1030
|
||||||
|
// 第3层级是10010,10020,10030
|
||||||
|
for (SysDict sysDict : tree) {
|
||||||
|
BigDecimal bigDecimal = new BigDecimal(1);
|
||||||
|
bigDecimal = bigDecimal.multiply(new BigDecimal(beishu));
|
||||||
|
bigDecimal = bigDecimal.add(new BigDecimal(i * 10));
|
||||||
|
sysDict.setDictSort(bigDecimal);
|
||||||
|
i++;
|
||||||
|
|
||||||
|
// 递归修改子树
|
||||||
|
List<SysDict> children = sysDict.getChildren();
|
||||||
|
if (children != null && children.size() > 0) {
|
||||||
|
updateSort(children, level + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 填充节点的父级id【递归】
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/6/27 18:35
|
||||||
|
*/
|
||||||
|
public static void fillParentId(Long parentDictId, List<SysDict> treeList) {
|
||||||
|
|
||||||
|
if (ObjectUtil.isEmpty(treeList)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (SysDict sysMenu : treeList) {
|
||||||
|
|
||||||
|
sysMenu.setDictParentId(parentDictId);
|
||||||
|
|
||||||
|
if (ObjectUtil.isNotEmpty(sysMenu.getChildren())) {
|
||||||
|
fillParentId(sysMenu.getDictId(), sysMenu.getChildren());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将指定的树形结构,平行展开,添加到指定的参数totalDictList
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/6/27 18:36
|
||||||
|
*/
|
||||||
|
public static void collectTreeTasks(List<SysDict> dictTree, List<SysDict> totalDictList) {
|
||||||
|
|
||||||
|
if (ObjectUtil.isEmpty(dictTree)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (SysDict sysDict : dictTree) {
|
||||||
|
|
||||||
|
totalDictList.add(sysDict);
|
||||||
|
|
||||||
|
if (ObjectUtil.isNotEmpty(sysDict.getChildren())) {
|
||||||
|
collectTreeTasks(sysDict.getChildren(), totalDictList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -24,6 +24,7 @@
|
||||||
*/
|
*/
|
||||||
package cn.stylefeng.roses.kernel.dict.modular.pojo.request;
|
package cn.stylefeng.roses.kernel.dict.modular.pojo.request;
|
||||||
|
|
||||||
|
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
|
||||||
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
|
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
|
||||||
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
|
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
|
||||||
import cn.stylefeng.roses.kernel.validator.api.validators.status.StatusValue;
|
import cn.stylefeng.roses.kernel.validator.api.validators.status.StatusValue;
|
||||||
|
@ -33,6 +34,7 @@ import lombok.EqualsAndHashCode;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典请求参数封装
|
* 字典请求参数封装
|
||||||
|
@ -134,6 +136,14 @@ public class DictRequest extends BaseRequest {
|
||||||
@ChineseDescription("搜索条件:字典类型编码")
|
@ChineseDescription("搜索条件:字典类型编码")
|
||||||
private String dictTypeCode;
|
private String dictTypeCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字典树的整个结构
|
||||||
|
* <p>
|
||||||
|
* 一般用在更新字典树接口中作为参数
|
||||||
|
*/
|
||||||
|
@ChineseDescription("字典树的整个结构,一般用在更新字典树接口中作为参数")
|
||||||
|
private List<SysDict> totalDictStructure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取树形列表
|
* 获取树形列表
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -103,4 +103,12 @@ public interface DictService extends IService<SysDict>, DictApi {
|
||||||
*/
|
*/
|
||||||
void removeByDictTypeId(Long dictTypeId);
|
void removeByDictTypeId(Long dictTypeId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新整个字典树结构,用来更新上下级结构和顺序
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @since 2023/6/27 18:24
|
||||||
|
*/
|
||||||
|
void updateDictTree(DictRequest dictRequest);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ import cn.hutool.core.util.StrUtil;
|
||||||
import cn.stylefeng.roses.kernel.dict.api.exception.DictException;
|
import cn.stylefeng.roses.kernel.dict.api.exception.DictException;
|
||||||
import cn.stylefeng.roses.kernel.dict.api.exception.enums.DictExceptionEnum;
|
import cn.stylefeng.roses.kernel.dict.api.exception.enums.DictExceptionEnum;
|
||||||
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
|
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
|
||||||
|
import cn.stylefeng.roses.kernel.dict.modular.factory.DictFactory;
|
||||||
import cn.stylefeng.roses.kernel.dict.modular.mapper.DictMapper;
|
import cn.stylefeng.roses.kernel.dict.modular.mapper.DictMapper;
|
||||||
import cn.stylefeng.roses.kernel.dict.modular.pojo.TreeDictInfo;
|
import cn.stylefeng.roses.kernel.dict.modular.pojo.TreeDictInfo;
|
||||||
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictRequest;
|
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictRequest;
|
||||||
|
@ -39,6 +40,7 @@ import cn.stylefeng.roses.kernel.pinyin.api.PinYinApi;
|
||||||
import cn.stylefeng.roses.kernel.rule.constants.SymbolConstant;
|
import cn.stylefeng.roses.kernel.rule.constants.SymbolConstant;
|
||||||
import cn.stylefeng.roses.kernel.rule.constants.TreeConstants;
|
import cn.stylefeng.roses.kernel.rule.constants.TreeConstants;
|
||||||
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
|
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
|
||||||
|
import cn.stylefeng.roses.kernel.rule.tree.buildpids.PidStructureBuildUtil;
|
||||||
import cn.stylefeng.roses.kernel.rule.tree.factory.DefaultTreeBuildFactory;
|
import cn.stylefeng.roses.kernel.rule.tree.factory.DefaultTreeBuildFactory;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
@ -165,6 +167,30 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
|
||||||
this.remove(sysDictLambdaQueryWrapper);
|
this.remove(sysDictLambdaQueryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void updateDictTree(DictRequest dictRequest) {
|
||||||
|
|
||||||
|
// 获取字典树的结构
|
||||||
|
List<SysDict> totalDictStructure = dictRequest.getTotalDictStructure();
|
||||||
|
|
||||||
|
// 调整字典的顺序
|
||||||
|
DictFactory.updateSort(totalDictStructure, 1);
|
||||||
|
|
||||||
|
// 填充树节点的parentId字段
|
||||||
|
DictFactory.fillParentId(-1L, totalDictStructure);
|
||||||
|
|
||||||
|
// 平行展开树形结构,准备从新整理pids
|
||||||
|
List<SysDict> totalDictList = new ArrayList<>();
|
||||||
|
DictFactory.collectTreeTasks(totalDictStructure, totalDictList);
|
||||||
|
|
||||||
|
// 从新整理上下级结构,整理id和pid关系
|
||||||
|
PidStructureBuildUtil.createPidStructure(totalDictList);
|
||||||
|
|
||||||
|
// 更新菜单的sort字段、pid字段和pids字段这3个字段
|
||||||
|
this.updateBatchById(totalDictList);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getDictName(String dictTypeCode, String dictCode) {
|
public String getDictName(String dictTypeCode, String dictCode) {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue