gunshi-project-ss/src/main/java/com/gunshi/project/xyt/service/StAddvcdDService.java

116 lines
5.4 KiB
Java

package com.gunshi.project.xyt.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gunshi.project.xyt.entity.vo.StAddvcdTreeVo;
import com.gunshi.project.xyt.mapper.StAddvcdDMapper;
import com.gunshi.project.xyt.model.StAddvcdD;
import com.gunshi.project.xyt.util.MyBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
* 描述: 行政区划表
* author: xusan
* date: 2024-07-08 17:30:38
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class StAddvcdDService extends ServiceImpl<StAddvcdDMapper, StAddvcdD>
{
public List<StAddvcdTreeVo> tree(String level) {
List<StAddvcdTreeVo> treeList = new ArrayList<>();
//根据adcd模糊查询得到所有的行政区划
List<StAddvcdD> list = this.baseMapper.selectList(new LambdaQueryWrapper<>());
List<StAddvcdTreeVo> totalList = MyBeanUtil.collectionCopy(list, StAddvcdTreeVo.class);
//根据adcd查询行政区划信息
StAddvcdD stAddvcdD = this.baseMapper.selectByAdcd("421181000000000");
StAddvcdTreeVo stAddvcdTreeVo = new StAddvcdTreeVo();
BeanUtils.copyProperties(stAddvcdD, stAddvcdTreeVo);
if ("1".equals(level)) {
treeList.add(stAddvcdTreeVo);
//当下拉深度为2时
} else if ("2".equals(level)) {
List<StAddvcdTreeVo> towns = new ArrayList<>();
//遍历所有的行政区划
for (StAddvcdTreeVo total : totalList) {
//后6位都是0并且6到9位不是0时为乡镇
if ("000000".equals(total.getAdcd().substring(9)) && !"000".equals(total.getAdcd().substring(6, 9))) {
towns.add(total);
}
}
stAddvcdTreeVo.setChildren(towns);
treeList.add(stAddvcdTreeVo);
//当下拉深度为3时
}else if("3".equals(level)){
List<StAddvcdTreeVo> towns = new ArrayList<>();
//遍历所有的行政区划
for (StAddvcdTreeVo total : totalList) {
//后6位都是0并且6到9位不是0时为乡镇
if ("000000".equals(total.getAdcd().substring(9)) && !"000".equals(total.getAdcd().substring(6, 9))) {
towns.add(total);
}
}
//遍历镇
for (StAddvcdTreeVo town : towns) {
List<StAddvcdTreeVo> administrativeVillageList = new ArrayList<>();
//遍历所有的行政区划
for (StAddvcdTreeVo total : totalList) {
//当行政区划前9位相同并且后3位都为0并且9到12不为0时为同一乡镇下的行政村
if (town.getAdcd().substring(0, 9).equals(total.getAdcd().substring(0, 9)) && "000".equals(total.getAdcd().substring(12)) && !"000".equals(total.getAdcd().substring(9, 12))) {
administrativeVillageList.add(total);
}
}
town.setChildren(administrativeVillageList);
}
stAddvcdTreeVo.setChildren(towns);
treeList.add(stAddvcdTreeVo);
}else{
List<StAddvcdTreeVo> towns = new ArrayList<>();
//遍历所有的行政区划
for (StAddvcdTreeVo total : totalList) {
//后6位都是0并且6到9位不是0时为乡镇
if ("000000".equals(total.getAdcd().substring(9)) && !"000".equals(total.getAdcd().substring(6, 9))) {
towns.add(total);
}
}
//遍历镇
for (StAddvcdTreeVo town : towns) {
List<StAddvcdTreeVo> administrativeVillageList = new ArrayList<>();
//遍历所有的行政区划
for (StAddvcdTreeVo total : totalList) {
//当行政区划前9位相同并且后3位都为0并且9到12不为0时为同一乡镇下的行政村
if (town.getAdcd().substring(0, 9).equals(total.getAdcd().substring(0, 9)) && "000".equals(total.getAdcd().substring(12)) && !"000".equals(total.getAdcd().substring(9, 12))) {
administrativeVillageList.add(total);
}
}
//遍历行政村
for (StAddvcdTreeVo administrativeVillage : administrativeVillageList) {
List<StAddvcdTreeVo> naturalVillages = new ArrayList<>();
//遍历所有的行政区划
for (StAddvcdTreeVo total : totalList) {
//行政区划前12为相同并且后3位不为0时为同一行政村下的自然组
if (total.getAdcd().substring(0, 12).equals(administrativeVillage.getAdcd().substring(0, 12)) && !"000".equals(total.getAdcd().substring(12))) {
naturalVillages.add(total);
}
}
administrativeVillage.setChildren(naturalVillages);
}
town.setChildren(administrativeVillageList);
}
stAddvcdTreeVo.setChildren(towns);
treeList.add(stAddvcdTreeVo);
}
return treeList;
}
}