1.表设计
CREATE TABLE `party_examine_bank_type` ( `id` bigint(20) NOT NULL COMMENT '类型id', `ancestors` text COMMENT '祖级列表', `parent_id` bigint(20) DEFAULT '0' COMMENT '父id', `name` varchar(200) DEFAULT '' COMMENT '名称', `sort` int(4) DEFAULT '0' COMMENT '显示顺序', `status` char(1) DEFAULT '0' COMMENT '状态(0正常 1停用)', `is_delete` char(1) DEFAULT '0' COMMENT '删除标志(0代表存在 1代表删除)', `create_time` bigint(20) DEFAULT '0' COMMENT '创建时间', `update_time` bigint(20) DEFAULT '0' COMMENT '更新时间', `supplier_id` bigint(20) DEFAULT NULL COMMENT '商户编号', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='类型表';
2.插入操作 祖级列表直接取上级的祖级列表数据拼接当前的上级就可以了
3.主要看更新操作,支持所有节点随意移动,树的下级也跟着移动,与上一篇 左右值算法实现的无限极异曲同工之效
public int updatePartyExamineBankType(PartyExamineBankType partyExamineBankType) {
//查询新上级的数据 PartyExamineBankType newParentBank = partyExamineBankTypeMapper.selectById(partyExamineBankType.getParentId());
//查询当前id的旧数据 PartyExamineBankType oldBank = partyExamineBankTypeMapper.selectPartyExamineBankTypeById(partyExamineBankType.getId()); String newAncestors =""; if (StringUtils.isNotNull(newParentBank) && StringUtils.isNotNull(oldBank)) { String oldAncestors = oldBank.getAncestors(); Long id=partyExamineBankType.getId(); Long pId=newParentBank.getId();
//这里判断下,如果涉及上下级或者间接关系走这里 if(newParentBank.getAncestors().contains(Convert.toStr(id))){ String s=newParentBank.getAncestors(); String param=s.substring(s.lastIndexOf(Convert.toStr(id))); newAncestors=oldAncestors + "," + pId; newParentBank.setAncestors(oldAncestors); newParentBank.setParentId(oldBank.getParentId());
//先更新新上级的数据 partyExamineBankTypeMapper.updatePartyExamineBankType(newParentBank); partyExamineBankType.setAncestors(newAncestors);
//更新子节点信息 updateBankRelationChildren(id, newAncestors, oldAncestors,pId,param); }else {//非关系的走这里
newAncestors = newParentBank.getAncestors() + "," + pId; partyExamineBankType.setAncestors(newAncestors);
//更新子节点信息 updateBankChildren(id, newAncestors, oldAncestors); } } int result = partyExamineBankTypeMapper.updatePartyExamineBankType(partyExamineBankType); if (UserConstants.DEPT_NORMAL.equals(partyExamineBankType.getStatus())) { // 如果该节点是启用状态,则启用该节点的所有上级 updateParentTypeStatus(partyExamineBankType); } return result; }
/** * 修改子元素关系 * * @param typeId 被修改的类型ID * @param newAncestors 新的父ID集合 * @param oldAncestors 旧的父ID集合 * */ public void updateBankChildren(Long typeId, String newAncestors, String oldAncestors) { List<PartyExamineBankType> children = partyExamineBankTypeMapper.selectChildrenTypeById(typeId); for (PartyExamineBankType child : children) { child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors)); } if (children.size() > 0) { partyExamineBankTypeMapper.updateTypeChildren(children); } }
/** * 修改子元素关系(上下级互换用) * * @param typeId 被修改的类型ID * @param newAncestors 新的父ID集合 * @param oldAncestors 旧的父ID集合 * @param param 新的父ID与旧的父ID之间间隔的级别 */ public void updateBankRelationChildren(Long typeId, String newAncestors, String oldAncestors,Long newParentId,String param) { List<PartyExamineBankType> children = partyExamineBankTypeMapper.selectChildrenTypeById(typeId); String p[]=param.split(","); String pId=Convert.toStr(newParentId); for (PartyExamineBankType child : children) { if(child.getAncestors().contains(pId)){ for(int i=0;i<p.length;i++){ child.setAncestors(child.getAncestors().replace(","+p[i],"")); } continue; } child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors)); } if (children.size() > 0) { partyExamineBankTypeMapper.updateTypeChildren(children); } }