部门树数据导入解析
根据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);
}
}
}
}
}
}