系统管理之组织机构树形化结构

Posted 在奋斗的大道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了系统管理之组织机构树形化结构相关的知识,希望对你有一定的参考价值。

/*
 Navicat mysql Data Transfer

 Source Server         : 192.168.1.73
 Source Server Type    : MySQL
 Source Server Version : 80015
 Source Host           : 192.168.1.73:3306
 Source Schema         : ucas_zh_08

 Target Server Type    : MySQL
 Target Server Version : 80015
 File Encoding         : 65001

 Date: 15/10/2021 15:50:59
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for auth_organization
-- ----------------------------
DROP TABLE IF EXISTS `auth_organization`;
CREATE TABLE `auth_organization`  (
  `sid` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主键',
  `psid` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '父Id',
  `organization_code` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '机构编码',
  `organization_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '机构名称',
  `created_by` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建人',
  `created_dt` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `version` int(9) NULL DEFAULT 1 COMMENT '版本号',
  `updated_by` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新人',
  `updated_dt` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  `zone_org_code` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '区域机构',
  `state` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '2' COMMENT '状态(1:禁用,2:启用)',
  `value1` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备用字段1',
  `value2` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备用字段2',
  `value3` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备用字段3',
  `depth` int(10) NULL DEFAULT NULL COMMENT '组织深度',
  `sort_field` int(9) NULL DEFAULT NULL COMMENT '排序字段',
  `organization_path` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '机构路径',
  `is_business` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否绑定业务: 1绑定、2:未绑定',
  `unique_sid` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '操作日志记录uuid',
  PRIMARY KEY (`sid`) USING BTREE,
  UNIQUE INDEX `organization_code`(`organization_code`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '机构表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of auth_organization
-- ----------------------------
INSERT INTO `auth_organization` VALUES ('1', '', 'office', '珠海市局', NULL, '2019-08-13 00:00:00', 9, NULL, '2019-10-16 12:03:15', NULL, '2', NULL, NULL, NULL, 1, 1, '/office', '2', NULL);
INSERT INTO `auth_organization` VALUES ('10', '1', '202', '住房保障科', NULL, '2019-10-16 10:44:09', 5, NULL, '2019-10-29 19:22:47', NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('11', '1', '203', '规委秘书处', NULL, '2019-10-16 10:44:45', 2, NULL, '2019-10-16 10:46:01', NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('12', '1', '201', '住房发展改革科', NULL, '2019-09-19 10:34:21', 5, NULL, '2019-10-16 10:46:20', NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('13', '1', '205', '建筑工程规划管理科', NULL, '2019-10-16 10:45:38', 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('14', '1', '204', '城市更新项目统筹科、城市更新项目监管科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('15', '1', '206', '验收备案科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('16', '1', '207', '住房发展与房地产市场监管科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('17', '1', '208', '规划编制管理科', NULL, NULL, 2, NULL, '2019-10-22 09:54:32', NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('18', '1', '209', '建设用地规划管理科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('19', '1', '210', '环境艺术与城市设计管理科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('20', '1', '211', '交通与市政工程规划管理科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('21', '1', '212', '建筑市场监管科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('22', '1', '213', '建设工程质量安全监管科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('23', '1', '214', '建设工程招标投标管理科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('24', '1', '215', '村镇规划建设科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('25', '1', '216', '行政审批服务科', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('26', '1', '217', '执法监察支队', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('27', '3', '218', '香洲区规划分局', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('28', '3', '219', '保税区规划分局', NULL, NULL, 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 2, NULL, NULL, '1', NULL);
INSERT INTO `auth_organization` VALUES ('3', '', 'record-office', '珠海市档案分局', NULL, '2019-08-29 16:53:46', 2, NULL, '2019-10-16 12:03:40', NULL, '2', NULL, NULL, NULL, 1, 2, '/record-office', '2', NULL);
INSERT INTO `auth_organization` VALUES ('636140421965283328', '11', 'ceshi001', '测试', NULL, '2019-10-22 09:54:55', 2, NULL, '2019-10-22 09:56:50', NULL, '2', NULL, NULL, NULL, 3, NULL, NULL, NULL, NULL);
INSERT INTO `auth_organization` VALUES ('638820190129356800', '10', 'test-001', '测试部', NULL, '2019-10-29 19:23:22', 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, 3, NULL, NULL, NULL, NULL);
INSERT INTO `auth_organization` VALUES ('709440331082498048', '', 'sz_20201', '深圳市城建档案局1', NULL, '2020-05-11 16:22:37', 3, NULL, '2020-05-11 17:18:06', NULL, '2', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'd6d1c0f7362b45818f1996acd8834cde');
INSERT INTO `auth_organization` VALUES ('815959806274830336', '', 'gz_0001', '广州市城建档案局', NULL, '2021-03-01 14:51:47', 1, NULL, NULL, NULL, '2', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

SET FOREIGN_KEY_CHECKS = 1;

 组织机构实体对象

import java.util.Date;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import com.***.common.model.BaseModel;



public class UcasAuthOrganizationInfo extends BaseModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String sid;
	
	private String psid;

	@NotBlank(message="统一社会信用代码不能为空")
	private String organizationCode;
	
	@NotBlank(message="机构名称不能为空")
   	@Length(min = 1, max = 32, message="机构名称超长")
	private String organizationName;

	private String createdBy;

	private Date createdDt;

	private Integer version;

	private String updatedBy;

	private Date updatedDt;

	private String zoneOrgCode;

	private String state;

	private String value1;

	private String value2;

	private String value3;

	private Integer depth;

	private Integer sortField;
	
	private String organizationPath;
	
	// 补充字段:是否涉及业务
	private String isBusiness;
	
	private String uniqueSid;
	
	public String getUniqueSid() {
		return uniqueSid;
	}

	public void setUniqueSid(String uniqueSid) {
		this.uniqueSid = uniqueSid;
	}

	public String getOrganizationPath() {
		return organizationPath;
	}

	public void setOrganizationPath(String organizationPath) {
		this.organizationPath = organizationPath;
	}

	public Integer getSortField() {
		return sortField;
	}

	public void setSortField(Integer sortField) {
		this.sortField = sortField;
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getPsid() {
		return psid;
	}

	public void setPsid(String psid) {
		this.psid = psid;
	}

	public String getOrganizationCode() {
		return organizationCode;
	}

	public void setOrganizationCode(String organizationCode) {
		this.organizationCode = organizationCode == null ? null : organizationCode.trim();
	}

	public String getOrganizationName() {
		return organizationName;
	}

	public void setOrganizationName(String organizationName) {
		this.organizationName = organizationName == null ? null : organizationName.trim();
	}

	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy == null ? null : createdBy.trim();
	}

	public Date getCreatedDt() {
		return createdDt;
	}

	public void setCreatedDt(Date createdDt) {
		this.createdDt = createdDt;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getUpdatedBy() {
		return updatedBy;
	}

	public void setUpdatedBy(String updatedBy) {
		this.updatedBy = updatedBy == null ? null : updatedBy.trim();
	}

	public Date getUpdatedDt() {
		return updatedDt;
	}

	public void setUpdatedDt(Date updatedDt) {
		this.updatedDt = updatedDt;
	}

	public String getZoneOrgCode() {
		return zoneOrgCode;
	}

	public void setZoneOrgCode(String zoneOrgCode) {
		this.zoneOrgCode = zoneOrgCode == null ? null : zoneOrgCode.trim();
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state == null ? null : state.trim();
	}

	public String getValue1() {
		return value1;
	}

	public void setValue1(String value1) {
		this.value1 = value1 == null ? null : value1.trim();
	}

	public String getValue2() {
		return value2;
	}

	public void setValue2(String value2) {
		this.value2 = value2 == null ? null : value2.trim();
	}

	public String getValue3() {
		return value3;
	}

	public void setValue3(String value3) {
		this.value3 = value3 == null ? null : value3.trim();
	}

	public Integer getDepth() {
		return depth;
	}

	public void setDepth(Integer depth) {
		this.depth = depth;
	}

	public String getIsBusiness() {
		return isBusiness;
	}

	public void setIsBusiness(String isBusiness) {
		this.isBusiness = isBusiness;
	}
	
	@Override
	public String toString() {
		return "UcasAuthOrganizationInfo [sid=" + sid + ", psid=" + psid + ", organizationCode=" + organizationCode
				+ ", organizationName=" + organizationName + ", createdBy=" + createdBy + ", createdDt=" + createdDt
				+ ", version=" + version + ", updatedBy=" + updatedBy + ", updatedDt=" + updatedDt + ", zoneOrgCode="
				+ zoneOrgCode + ", state=" + state + ", value1=" + value1 + ", value2=" + value2 + ", value3=" + value3
				+ ", depth=" + depth + ", sortField=" + sortField + ", organizationPath=" + organizationPath
				+ ", isBusiness=" + isBusiness + "]";
	}
}

树形结构实体对象

import java.util.List;

import lombok.Data;

/**
 * 组织机构树形结构实体对象
 * @author zzg
 *
 */
@SuppressWarnings("serial")
@Data
public class TreeBo implements java.io.Serializable {
	private String id;
	private String name;
	private String code;
	private String pid;
	private List<TreeBo> children;
	
	public TreeBo() {
		super();
	}
	
	public TreeBo(String id, String code, String name, String pid, List<TreeBo> children) {
		super();
		this.id = id;
		this.code = code;
		this.name = name;
		this.pid = pid;
		this.children = children;
	}
	
	public TreeBo(String id, String code, String name, String pid) {
		super();
		this.id = id;
		this.code = code;
		this.name = name;
		this.pid = pid;
	}

}

 树形实体对象转换工具类

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.***.vo.TreeBo;

public class TreeUtil {
	private List<TreeBo> rootList; // 根节点对象存放到这里
	private List<TreeBo> bodyList; // 其他节点存放到这里,可以包含根节点

	public TreeUtil(List<TreeBo> rootList, List<TreeBo> bodyList) {
		this.rootList = rootList;
		this.bodyList = bodyList;
	}

	public List<TreeBo> getTree() { // 调用的方法入口
		if (bodyList != null && !bodyList.isEmpty()) {
			// 声明一个map,用来过滤已操作过的数据
			Map<String, String> map = new HashMap<String, String>(bodyList.size());
			rootList.forEach(beanTree -> getChild(beanTree, map));
			return rootList;
		}
		return null;
	}

	private void getChild(TreeBo beanTree, Map<String, String> map) {
		List<TreeBo> childList = new ArrayList<TreeBo>();
		bodyList.stream().filter(c -> !map.containsKey(c.getId())).filter(c -> c.getPid().equals(beanTree.getId()))
				.forEach(c -> {
					map.put(c.getId(), c.getPid());
					getChild(c, map);
					childList.add(c);
				});
		beanTree.setChildren(childList);
	}

}

 组织机构Service 接口定义

import java.util.List;
import com.***.vo.TreeBo;

public interface UcasAuthOrganizationInfoService extends BaseService<UcasAuthOrganizationInfo> {
	List<TreeBo> listTree();

}

 组织机构Service 实现定义



import java.util.ArrayList;
import java.util.List;

import com.***.util.TreeUtil;
import com.***.vo.TreeBo;
import com.***.vo.UcasAuthOrganizationInfoWrapper;


@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
public class UcasAuthOrganizationInfoServiceImpl extends AbstractService<UcasAuthOrganizationInfo> implements UcasAuthOrganizationInfoService {
	 	@Autowired
	    private UcasAuthOrganizationMapper mapper;
		
		@Override
		public List<TreeBo> listTree() {
			// TODO Auto-generated method stub
			List<UcasAuthOrganizationInfo> all = mapper.select(null);
			List<TreeBo> rootList = new ArrayList<>();
		    List<TreeBo> bodyList = new ArrayList<>();
		    for (UcasAuthOrganizationInfo company : all) {
		        if (StringUtils.isEmpty(company.getPsid())) {
		            /** 根结点列表*/
		            rootList.add(new TreeBo(company.getSid(), company.getOrganizationCode(), company.getOrganizationName(), company.getPsid()));
		        }else {
		            /** 其他结点列表(可以包含根结点)*/
		            bodyList.add(new TreeBo(company.getSid(), company.getOrganizationCode(), company.getOrganizationName(), company.getPsid()));
		        }
		    }
		    TreeUtil treeUtil = new TreeUtil(rootList, bodyList);
		    return treeUtil.getTree();
		}
}

 组织机构Controller 定义

	@ApiOperation(httpMethod = "POST", value = "部门信息查询")
	@RequestMapping(value = "/tree", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Result tree(@RequestBody Map<String, Object> params) {
		List<TreeBo> list = organizationInfoService.listTree();
		return Result.ok().setDatas("tree", list);
	}

postman效果截图:

{
    "code": "0",
    "message": "success",
    "type": null,
    "datas": {
        "tree": [
            {
                "id": "1",
                "name": "珠海市局",
                "code": "office",
                "pid": "",
                "children": [
                    {
                        "id": "10",
                        "name": "住房保障科",
                        "code": "202",
                        "pid": "1",
                        "children": [
                            {
                                "id": "638820190129356800",
                                "name": "测试部",
                                "code": "test-001",
                                "pid": "10",
                                "children": []
                            }
                        ]
                    },
                    {
                        "id": "11",
                        "name": "规委秘书处",
                        "code": "203",
                        "pid": "1",
                        "children": [
                            {
                                "id": "636140421965283328",
                                "name": "测试",
                                "code": "ceshi001",
                                "pid": "11",
                                "children": []
                            }
                        ]
                    },
                    {
                        "id": "12",
                        "name": "住房发展改革科",
                        "code": "201",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "13",
                        "name": "建筑工程规划管理科",
                        "code": "205",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "14",
                        "name": "城市更新项目统筹科、城市更新项目监管科",
                        "code": "204",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "15",
                        "name": "验收备案科",
                        "code": "206",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "16",
                        "name": "住房发展与房地产市场监管科",
                        "code": "207",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "17",
                        "name": "规划编制管理科",
                        "code": "208",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "18",
                        "name": "建设用地规划管理科",
                        "code": "209",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "19",
                        "name": "环境艺术与城市设计管理科",
                        "code": "210",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "20",
                        "name": "交通与市政工程规划管理科",
                        "code": "211",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "21",
                        "name": "建筑市场监管科",
                        "code": "212",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "22",
                        "name": "建设工程质量安全监管科",
                        "code": "213",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "23",
                        "name": "建设工程招标投标管理科",
                        "code": "214",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "24",
                        "name": "村镇规划建设科",
                        "code": "215",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "25",
                        "name": "行政审批服务科",
                        "code": "216",
                        "pid": "1",
                        "children": []
                    },
                    {
                        "id": "26",
                        "name": "执法监察支队",
                        "code": "217",
                        "pid": "1",
                        "children": []
                    }
                ]
            },
            {
                "id": "3",
                "name": "珠海市档案分局",
                "code": "record-office",
                "pid": "",
                "children": [
                    {
                        "id": "27",
                        "name": "香洲区规划分局",
                        "code": "218",
                        "pid": "3",
                        "children": []
                    },
                    {
                        "id": "28",
                        "name": "保税区规划分局",
                        "code": "219",
                        "pid": "3",
                        "children": []
                    }
                ]
            },
            {
                "id": "709440331082498048",
                "name": "深圳市城建档案局1",
                "code": "sz_20201",
                "pid": "",
                "children": []
            },
            {
                "id": "815959806274830336",
                "name": "广州市城建档案局",
                "code": "gz_0001",
                "pid": "",
                "children": []
            }
        ]
    }
}

以上是关于系统管理之组织机构树形化结构的主要内容,如果未能解决你的问题,请参考以下文章

在windows操作系统中,文件的组织形式是采用?

Django中树形结构

java设计模式之组合模式

Django中使用Bootstrap展示树形结构

组织机构列表转树形结构Hutool工具类TreeUtil

组织机构列表转树形结构Hutool工具类TreeUtil