天天看點

根據Excel導入樹形部門行政區域

部門樹資料導入解析

根據excel提供的内容進行行政區域的導入

根據Excel導入樹形部門行政區域
//通過excel工具類擷取workbook
        Workbook workbook = ExcelUtil.load(file);
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        List<OrgDeptPo> areaList = new ArrayList<>();
        for(int rowNum = 1;rowNum <= lastRowNum; rowNum++){;
            Row row = sheet.getRow(rowNum);
            if(ExcelUtil.getCellValueStr(row,1).length() <= 0){
                break;
            }
            //解析擷取部門資料
            String orgCode = ExcelUtil.getCellValueStr(row,1);
            String deptName = ExcelUtil.getCellValueStr(row,2);
            String pathName = ExcelUtil.getCellValueStr(row,3);
            Integer sortIndex = Integer.parseInt(ExcelUtil.getCellValueStr(row,4));
            areaList.add(new OrgDeptPo().setOrgCode(orgCode).setDeptName(deptName).setPathName(pathName).
                    setShortName(deptName).setFirstLetter(PinyinUtil.getPinYinHeadChar(deptName)).setSortIndex(sortIndex));
        }
        //根據orgCode的長度對orgDept進行分組排序排序(排序是為了保證區域的id在前面按照順序導入,這樣之後的查詢結果也是按照順序查詢出來的與Excel中的内容保持一緻)
        TreeMap<Integer, List<OrgDeptPo>> orgMap = areaList.parallelStream()
                .collect(Collectors.groupingBy(OrgDeptPo :: gainOrgCodeLength,
                          TreeMap :: new , Collectors.collectingAndThen(Collectors.toCollection(
                                () -> new TreeSet<>(Comparator.comparing(OrgDeptPo::getSortIndex))), ArrayList::new)));
//        areaMap.entrySet().stream().sorted()

        if(orgMap.get(4) != null && orgMap.get(4).size() > 0){
            //過濾部門OrgCoded等于6的部門剩餘的為區域部門 并根據sortIndex排序
            TreeMap<String, List<OrgDeptPo>> areaMap = areaList.parallelStream().filter(x -> x.getOrgCode().length() == 6)
                    .collect(Collectors.groupingBy(x -> x.getOrgCode().substring(0, 4),
                            TreeMap :: new , Collectors.collectingAndThen(Collectors.toCollection(
                                    () -> new TreeSet<>(Comparator.comparing(OrgDeptPo::getSortIndex))), ArrayList::new)));

            //過濾部門OrgCode小于7的部門剩餘的為街道部門
            TreeMap<String, List<OrgDeptPo>> townMap = areaList.parallelStream().filter(x -> x.getOrgCode().length() > 6)
                    .collect(Collectors.groupingBy(x -> x.getOrgCode().substring(0, 6),
                            TreeMap :: new , Collectors.collectingAndThen(Collectors.toCollection(
                                    () -> new TreeSet<>(Comparator.comparing(OrgDeptPo::getSortIndex))), ArrayList::new)));
            for (OrgDeptPo city : orgMap.get(4)) {
                String oldCityCode = city.getOrgCode();
                StringBuilder stringBuilder = new StringBuilder(city.getOrgCode());
                //市級節點插入2中生成為330000.000000.001這樣符合預設資料庫規範的節點
                stringBuilder.insert(2,"0000.000000.0");
                city.setOrgCode(stringBuilder.toString());
                city.setDeptType((byte) 11);
                city.setParentId(1);
                orgDeptDao.insert(city);
                if(areaMap.get(oldCityCode) != null) {
                    for (OrgDeptPo area : areaMap.get(oldCityCode)) {
                        String areaCode = area.getOrgCode();
                        StringBuilder areaBuilder = new StringBuilder(area.getOrgCode());
                  //區級節點插入4,2節點中生成為330000.000000.001.001這樣符合預設資料庫規範的節點
                        areaBuilder.insert(4, ".0");
                        areaBuilder.insert(2,"0000.000000.0");
                        area.setOrgCode(areaBuilder.toString());
                        area.setDeptType((byte) 12);
                        area.setParentId(city.getId());
                        orgDeptDao.insert(area);
                        List<OrgDeptPo> townList = townMap.get(areaCode);
                        if(townList != null) {
                            townList.parallelStream().forEach(x -> {
                                StringBuilder townBuilder = new StringBuilder(x.getOrgCode());
                                townBuilder.insert(6, ".");
                                townBuilder.insert(4, ".0");
                                townBuilder.insert(2, "0000.000000.0");
                                x.setOrgCode(townBuilder.toString());
                                x.setDeptType((byte) 13);
                                x.setParentId(area.getId());
                            });
                            if (townList.size() > 0) {
                                orgDeptDao.insertBatch(townList);
                            }
                        }
                    }
                }

            }
        }