数据库
DROP TABLE IF EXISTS `xg_materail`;
CREATE TABLE `xg_materail` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`pid` int(10) DEFAULT NULL COMMENT '父id',
`material_name` varchar(36) DEFAULT NULL COMMENT '材料名称',
`material_unit` varchar(10) DEFAULT NULL COMMENT '材料单位',
`material_num` int(10) DEFAULT NULL COMMENT '材料数量',
`supply_price` double(10,2) DEFAULT NULL COMMENT '供应单价',
`install_price` double(10,2) DEFAULT NULL COMMENT '安装单价',
`synthesis_price` double(10,2) DEFAULT NULL COMMENT '综合单价',
`synthesis_prices` double(10,2) DEFAULT NULL COMMENT '综合总价',
`level` int(10) DEFAULT NULL COMMENT '层级',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1145860133 DEFAULT CHARSET=utf8;
代码实现
查询
public ComResult selectTree( QueryVo queryVo) {
List<XgMaterail> xgMaterailList=baseMapper.selectTree(queryVo);
List<XgMaterail> testTrees = xgMaterailList.stream().filter(testTree -> 0 == testTree.getPid())
.peek(testTree -> testTree.setChildren(createChildList(testTree, xgMaterailList)))
.collect(Collectors.toList());
return new ComResult(ComCodeMsg.SUCCESS,ComCodeMsg.SUCCESS_MSG,testTrees);
}
新增以及节点上下级新增
public ComResult addNode(AddNodeQuery addNodeQuery) {
XgMaterail newXg=null;
Date date=new Date();
XgMaterail oldXg=baseMapper.selectById(addNodeQuery.getSelectedId());
switch(addNodeQuery.getOperation()){
case 1: //向上创建子级
if(addNodeQuery.getMaterialName()!=null && !"".equals(addNodeQuery.getMaterialName())){
newXg=dozerBeanMapper.map(addNodeQuery,XgMaterail.class);
//设置当前数据排序为选择数据的等级之前
newXg.setLevel(addNodeQuery.getNowLevel()-1);
//将所有数据依次向后排序
baseMapper.updateLevel(addNodeQuery.getNowLevel(), oldXg.getPid());
newXg.setPid(oldXg.getPid());
newXg.setCreateTime(date);
baseMapper.insert(newXg);
}
break;
case 2://向下创建子级
if(addNodeQuery.getMaterialName()!=null && !"".equals(addNodeQuery.getMaterialName())){
newXg=dozerBeanMapper.map(addNodeQuery,XgMaterail.class);
//设置当前数据排序为选择数据的等级之后
newXg.setLevel(addNodeQuery.getNowLevel()+1);
//将所有数据依次向后排序
baseMapper.updateLevel(addNodeQuery.getNowLevel()+1, oldXg.getPid());
newXg.setPid(oldXg.getPid());
newXg.setCreateTime(date);
baseMapper.insert(newXg);
}
break;
case 3: //创建父级
if(addNodeQuery.getMaterialName()!=null && !"".equals(addNodeQuery.getMaterialName())){
newXg=dozerBeanMapper.map(addNodeQuery,XgMaterail.class);
//设置当前数据排序为子级的排序
newXg.setLevel(addNodeQuery.getNowLevel());
newXg.setPid(oldXg.getPid());
newXg.setCreateTime(date);
baseMapper.insert(newXg);
oldXg.setPid(newXg.getId());
oldXg.setLevel(0);//初始
baseMapper.updateById(oldXg);
}
break;
case 4: //创建子级
if(addNodeQuery.getMaterialName()!=null && !"".equals(addNodeQuery.getMaterialName())){
newXg=dozerBeanMapper.map(addNodeQuery,XgMaterail.class);
//设置当前数据排序为选择数据的等级之后
newXg.setLevel(0);
newXg.setPid(oldXg.getId());
newXg.setCreateTime(date);
baseMapper.insert(newXg);
}
break;
}
return new ComResult(ComCodeMsg.SUCCESS,ComCodeMsg.SUCCESS_MSG,null);
}
复制节点
public ComResult copyNode(CopyNodeQuery copyNodeQuery) {
if(copyNodeQuery.getSelectedId()!=null && copyNodeQuery.getList()!=null){ //全部复制
for (AddNodeQuery anq: copyNodeQuery.getList()) {
XgMaterail xm=dozerBeanMapper.map(anq,XgMaterail.class);
xm.setPid(copyNodeQuery.getSelectedId());
xm.setCreateTime(new Date());
baseMapper.insert(xm);
if(anq.getChildren()!=null){ //如果存在子级,同样粘贴 ,只需修改父id
for (XgMaterail xmChild: anq.getChildren()) {
xmChild.setId(null);
xmChild.setPid(xm.getId());
xmChild.setCreateTime(new Date());
baseMapper.insert(xmChild);
}
}
}
}
return new ComResult(ComCodeMsg.SUCCESS,ComCodeMsg.SUCCESS_MSG,null);
}
删除
public ComResult deleteNode(DeleteNode deleteNode) {
//不管选中什么节点,直接删除,子节点提升为父节点。保留父节点属性
if(deleteNode.getDeleteList()!=null && deleteNode.getDeleteList().size()>=0){
Iterator it=deleteNode.getDeleteList().iterator();
while (it.hasNext()){
Integer id=(Integer) it.next();
XgMaterail materail= baseMapper.selectById(id);
if(materail!=null){
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.eq("pid",materail.getId());
List<XgMaterail> materails=baseMapper.selectList(queryWrapper);
List<XgMaterail> xms=null;
if(!materails.isEmpty() && materails.size()>0){
xms=new LinkedList<>();
for (XgMaterail xm: materails) {
xm.setLevel(materail.getLevel());
xm.setPid(materail.getPid());
xms.add(xm);
}
this.saveOrUpdateBatch(xms);
}
}
this.removeById(id);
}
}
return new ComResult(ComCodeMsg.SUCCESS,ComCodeMsg.SUCCESS_MSG,null);
}
/**
* @param testTree 父级
* @param testTreeList 对应的list
* @return
*/
private static List<XgMaterail> createChildList(XgMaterail testTree, List<XgMaterail> testTreeList) {
return testTreeList.stream().filter(model -> testTree.getId().equals(model.getPid()))
.peek(model -> model.setChildren(createChildList(model, testTreeList)))
.collect(Collectors.toList());
}