5 changed files with 141 additions and 42 deletions
@ -0,0 +1,32 @@ |
|||||||
|
package com.biutag.lan.domain; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType; |
||||||
|
import com.baomidou.mybatisplus.annotation.TableField; |
||||||
|
import com.baomidou.mybatisplus.annotation.TableId; |
||||||
|
import lombok.Getter; |
||||||
|
import lombok.Setter; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author wxc |
||||||
|
* @date 2025/8/18 |
||||||
|
*/ |
||||||
|
@Setter |
||||||
|
@Getter |
||||||
|
public class CountyStreetDept { |
||||||
|
|
||||||
|
@TableId(type = IdType.AUTO) |
||||||
|
private Integer id; |
||||||
|
|
||||||
|
private String deptId; |
||||||
|
|
||||||
|
private String pid; |
||||||
|
|
||||||
|
@TableField("\"level\"") |
||||||
|
private Integer level; |
||||||
|
|
||||||
|
private String name; |
||||||
|
|
||||||
|
// 地图数据
|
||||||
|
private String geometry; |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,11 @@ |
|||||||
|
package com.biutag.lan.mapper; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||||
|
import com.biutag.lan.domain.CountyStreetDept; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author wxc |
||||||
|
* @date 2025/8/18 |
||||||
|
*/ |
||||||
|
public interface CountyStreetDeptMapper extends BaseMapper<CountyStreetDept> { |
||||||
|
} |
||||||
@ -1,59 +1,105 @@ |
|||||||
package com.biutag.lan; |
package com.biutag.lan; |
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil; |
||||||
|
import com.alibaba.excel.EasyExcel; |
||||||
|
import com.alibaba.excel.ExcelReader; |
||||||
|
import com.alibaba.excel.context.AnalysisContext; |
||||||
|
import com.alibaba.excel.read.listener.ReadListener; |
||||||
|
import com.alibaba.excel.read.metadata.ReadSheet; |
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
import com.biutag.lan.domain.CountyStreetDept; |
||||||
import com.biutag.constants.AppConstants; |
import com.biutag.lan.domain.bo.DutyImport; |
||||||
import com.biutag.entity.system.Dept; |
import com.biutag.lan.mapper.CountyStreetDeptMapper; |
||||||
import com.biutag.entity.vo.DeptTree; |
|
||||||
import com.biutag.mapper.system.DeptMapper; |
|
||||||
import org.junit.jupiter.api.Test; |
import org.junit.jupiter.api.Test; |
||||||
import org.springframework.beans.BeanUtils; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
import org.springframework.beans.factory.annotation.Autowired; |
||||||
import org.springframework.boot.test.context.SpringBootTest; |
import org.springframework.boot.test.context.SpringBootTest; |
||||||
|
|
||||||
import java.util.ArrayList; |
import java.io.*; |
||||||
import java.util.HashMap; |
import java.util.*; |
||||||
import java.util.List; |
import java.util.zip.ZipFile; |
||||||
import java.util.Map; |
|
||||||
|
|
||||||
@SpringBootTest |
@SpringBootTest |
||||||
public class SpringTestCase { |
public class SpringTestCase { |
||||||
|
|
||||||
@Autowired |
@Autowired |
||||||
private DeptMapper deptMapper; |
private CountyStreetDeptMapper countyStreetDeptMapper; |
||||||
|
|
||||||
|
|
||||||
@Test |
@Test |
||||||
public void testMailCategory() { |
public void map() { |
||||||
LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<Dept>().eq(Dept::getIsDelete, 0).orderByAsc(Dept::getSort); |
List<File> files = FileUtil.loopFiles("D:\\Workspace\\创客\\地图数据\\湖南\\长沙"); |
||||||
List<Dept> depts = deptMapper.selectList(queryWrapper); |
for (File file : files) { |
||||||
|
System.out.println(file.getName()); |
||||||
Map<String, DeptTree> childMap = new HashMap<>(); |
String name = file.getName().substring(0, file.getName().indexOf("边界")); |
||||||
depts.forEach(dept -> { |
int firstIndex = file.getName().indexOf("_"); |
||||||
DeptTree node = new DeptTree(); |
String deptId = file.getName().substring(firstIndex + 1, file.getName().indexOf("_", firstIndex + 1)) + "000000"; |
||||||
BeanUtils.copyProperties(dept, node); |
try { |
||||||
childMap.put(dept.getId(), node); |
ZipFile zipFile = new ZipFile(file); |
||||||
}); |
zipFile.stream().forEach(entry -> { |
||||||
List<DeptTree> tree = new ArrayList<>(); |
try (InputStream is = zipFile.getInputStream(entry)) { |
||||||
for (Dept dept : depts) { |
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
||||||
// 根节点
|
StringBuilder content = new StringBuilder(); |
||||||
if (AppConstants.ROOT_DEPT_ID.equals(dept.getId())) { |
String line; |
||||||
tree.add(childMap.get(dept.getId())); |
|
||||||
} else { |
// 逐行读取并拼接
|
||||||
DeptTree parent = childMap.get(dept.getPid()); |
while ((line = reader.readLine()) != null) { |
||||||
if (parent != null) { |
content.append(line); // 保留换行符
|
||||||
parent.getChildren().add(childMap.get(dept.getId())); // 添加到父节点的子列表
|
} |
||||||
} |
String geo = content.toString(); |
||||||
|
CountyStreetDept countryStreetDept = countyStreetDeptMapper.selectOne(new LambdaQueryWrapper<CountyStreetDept>().eq(CountyStreetDept::getName, name)); |
||||||
|
if (Objects.isNull(countryStreetDept)) { |
||||||
|
countryStreetDept = new CountyStreetDept(); |
||||||
|
countryStreetDept.setDeptId(deptId); |
||||||
|
countryStreetDept.setName(name); |
||||||
|
countryStreetDept.setLevel(2); |
||||||
|
countryStreetDept.setGeometry(geo); |
||||||
|
countyStreetDeptMapper.insert(countryStreetDept); |
||||||
|
} else { |
||||||
|
countryStreetDept.setDeptId(deptId); |
||||||
|
countryStreetDept.setGeometry(geo); |
||||||
|
countyStreetDeptMapper.updateById(countryStreetDept); |
||||||
|
} |
||||||
|
|
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
}); |
||||||
|
} catch (IOException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
} |
} |
||||||
|
|
||||||
|
// String geo = FileUtil.readString(unzip, "UTF-8");
|
||||||
|
//
|
||||||
|
// System.out.println(geo);
|
||||||
} |
} |
||||||
update(tree, 0); |
|
||||||
} |
} |
||||||
|
|
||||||
public void update(List<DeptTree> tree, int level) { |
|
||||||
for (DeptTree deptTree : tree) { |
@Test |
||||||
LambdaUpdateWrapper<Dept> updateWrapper = new LambdaUpdateWrapper<Dept>().eq(Dept::getId, deptTree.getId()).set(Dept::getLevel, level); |
public void saveCountyStreetDept() { |
||||||
deptMapper.update(updateWrapper); |
ExcelReader excelReader = EasyExcel.read(new File("D:\\Workspace\\创客\\地图数据\\湖南省乡镇区行政区划编码.xlsx"), new ReadListener<LinkedHashMap>() { |
||||||
update(deptTree.getChildren(), level + 1); |
|
||||||
} |
@Override |
||||||
|
public void invoke(LinkedHashMap data, AnalysisContext analysisContext) { |
||||||
|
System.out.println(data); |
||||||
|
String pid = data.get(5).toString(); |
||||||
|
String name = data.get(6).toString(); |
||||||
|
String deptId = data.get(7).toString(); |
||||||
|
CountyStreetDept countyStreetDept = new CountyStreetDept(); |
||||||
|
countyStreetDept.setPid(pid); |
||||||
|
countyStreetDept.setName(name); |
||||||
|
countyStreetDept.setDeptId(deptId); |
||||||
|
countyStreetDept.setLevel(3); |
||||||
|
countyStreetDeptMapper.insert(countyStreetDept); |
||||||
|
} |
||||||
|
@Override |
||||||
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
||||||
|
} |
||||||
|
}).build(); |
||||||
|
|
||||||
|
ReadSheet sheet = EasyExcel.readSheet(0).build(); |
||||||
|
excelReader.read(sheet); |
||||||
|
excelReader.close(); |
||||||
|
|
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue