|
|
@ -17,14 +17,21 @@ |
|
|
|
|
|
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
import com.epmet.commons.mybatis.service.impl.BaseServiceImpl; |
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
import com.epmet.commons.tools.page.PageData; |
|
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
|
import com.epmet.commons.tools.constant.FieldConstant; |
|
|
|
import com.epmet.constant.AreaCodeConstant; |
|
|
|
import com.epmet.dao.AreaCodeChildDao; |
|
|
|
import com.epmet.dao.AreaCodeDao; |
|
|
|
import com.epmet.dto.AreaCodeChildDTO; |
|
|
|
import com.epmet.dto.AreaCodeDTO; |
|
|
|
import com.epmet.dto.form.AreaCodeDictFormDTO; |
|
|
|
import com.epmet.dto.result.AreaCodeDictResultDTO; |
|
|
|
import com.epmet.entity.AreaCodeEntity; |
|
|
|
import com.epmet.redis.AreaCodeRedis; |
|
|
|
import com.epmet.service.AreaCodeService; |
|
|
@ -32,10 +39,11 @@ import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* 大陆省市区地区码 |
|
|
@ -48,6 +56,8 @@ public class AreaCodeServiceImpl extends BaseServiceImpl<AreaCodeDao, AreaCodeEn |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private AreaCodeRedis areaCodeRedis; |
|
|
|
@Autowired |
|
|
|
private AreaCodeChildDao childDao; |
|
|
|
|
|
|
|
@Override |
|
|
|
public PageData<AreaCodeDTO> page(Map<String, Object> params) { |
|
|
@ -101,4 +111,156 @@ public class AreaCodeServiceImpl extends BaseServiceImpl<AreaCodeDao, AreaCodeEn |
|
|
|
baseDao.deleteBatchIds(Arrays.asList(ids)); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 行政地区编码查询 |
|
|
|
* @Param formDTO |
|
|
|
* @author zxc |
|
|
|
* @date 2021/1/7 下午1:41 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public List<AreaCodeDictResultDTO> areaCodeDictTree(AreaCodeDictFormDTO formDTO) { |
|
|
|
List<AreaCodeDictResultDTO> tree = new ArrayList<>(); |
|
|
|
String s = areaCodeRedis.get(AreaCodeConstant.SCREEN_TREE); |
|
|
|
if (StringUtils.isNotBlank(s)){ |
|
|
|
tree = JSON.parseArray(s,AreaCodeDictResultDTO.class); |
|
|
|
}else { |
|
|
|
List<AreaCodeDictResultDTO> areaCodeDictResultDTOS = disposeTree(); |
|
|
|
areaCodeRedis.set(AreaCodeConstant.SCREEN_TREE,areaCodeDictResultDTOS); |
|
|
|
tree = areaCodeDictResultDTOS; |
|
|
|
} |
|
|
|
if (StringUtils.isBlank(formDTO.getRootAreaCode())){ |
|
|
|
return tree; |
|
|
|
} |
|
|
|
List<AreaCodeDictResultDTO> treeByRootAreaCode = getTreeByRootAreaCode(tree, formDTO.getRootAreaCode()); |
|
|
|
return treeByRootAreaCode; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 行政区树查询 |
|
|
|
* @Param |
|
|
|
* @author zxc |
|
|
|
* @date 2021/1/8 上午8:57 |
|
|
|
*/ |
|
|
|
public List<AreaCodeDictResultDTO> disposeTree(){ |
|
|
|
List<AreaCodeDTO> areaCodeDTOS = baseDao.selectAllArea(); |
|
|
|
List<AreaCodeChildDTO> areaCodeChildDTOS = childDao.selectAllChild(); |
|
|
|
List<AreaCodeDictResultDTO> result = new ArrayList<>(); |
|
|
|
List<AreaCodeDictResultDTO> fourResult = new ArrayList<>(); |
|
|
|
List<AreaCodeDictResultDTO> cityResult = new ArrayList<>(); |
|
|
|
|
|
|
|
// 街道-社区
|
|
|
|
Map<Integer, List<AreaCodeChildDTO>> groupByLevel = areaCodeChildDTOS.stream().collect(Collectors.groupingBy(AreaCodeChildDTO::getLevel)); |
|
|
|
List<AreaCodeChildDTO> levelFive = groupByLevel.get(NumConstant.FIVE); |
|
|
|
Map<String, List<AreaCodeChildDTO>> groupByPCode = levelFive.stream().collect(Collectors.groupingBy(AreaCodeChildDTO::getPCode)); |
|
|
|
List<AreaCodeChildDTO> levelFour = groupByLevel.get(NumConstant.FOUR); |
|
|
|
levelFour.forEach(four -> { |
|
|
|
AreaCodeDictResultDTO dto = new AreaCodeDictResultDTO(); |
|
|
|
dto.setCode(four.getCode()); |
|
|
|
dto.setName(four.getName()); |
|
|
|
dto.setParentCode(four.getPCode()); |
|
|
|
groupByPCode.forEach((pCode,v) -> { |
|
|
|
if (dto.getCode().equals(pCode)){ |
|
|
|
List<AreaCodeDictResultDTO> fiveResult = new ArrayList<>(); |
|
|
|
v.forEach(five -> { |
|
|
|
AreaCodeDictResultDTO fiveDto = new AreaCodeDictResultDTO(); |
|
|
|
fiveDto.setCode(five.getCode()); |
|
|
|
fiveDto.setName(five.getName()); |
|
|
|
fiveResult.add(fiveDto); |
|
|
|
}); |
|
|
|
List<AreaCodeDictResultDTO> fiveSort = fiveResult.stream().sorted(Comparator.comparing(AreaCodeDictResultDTO::getCode)).collect(Collectors.toList()); |
|
|
|
dto.setChildren(fiveSort); |
|
|
|
} |
|
|
|
}); |
|
|
|
fourResult.add(dto); |
|
|
|
}); |
|
|
|
List<AreaCodeDictResultDTO> fourSort = new ArrayList<>(); |
|
|
|
if (!CollectionUtils.isEmpty(fourResult)){ |
|
|
|
fourSort = fourResult.stream().sorted(Comparator.comparing(AreaCodeDictResultDTO::getCode)).collect(Collectors.toList()); |
|
|
|
} |
|
|
|
|
|
|
|
// 省市区
|
|
|
|
Map<String, List<AreaCodeDTO>> groupByCity = areaCodeDTOS.stream().collect(Collectors.groupingBy(AreaCodeDTO::getCityCode)); |
|
|
|
Map<String, List<AreaCodeDTO>> groupByProvince = areaCodeDTOS.stream().collect(Collectors.groupingBy(AreaCodeDTO::getProvinceCode)); |
|
|
|
List<AreaCodeDictResultDTO> finalFourSort = fourSort; |
|
|
|
groupByCity.forEach((c, v) -> { |
|
|
|
AreaCodeDictResultDTO cityDto = new AreaCodeDictResultDTO(); |
|
|
|
cityDto.setName(v.get(NumConstant.ZERO).getCityName()); |
|
|
|
cityDto.setCode(v.get(NumConstant.ZERO).getCityCode()); |
|
|
|
cityDto.setParentCode(v.get(NumConstant.ZERO).getProvinceCode()); |
|
|
|
List<AreaCodeDictResultDTO> threeResult = new ArrayList<>(); |
|
|
|
v.forEach(three -> { |
|
|
|
AreaCodeDictResultDTO dto = new AreaCodeDictResultDTO(); |
|
|
|
dto.setCode(three.getCountyCode()); |
|
|
|
dto.setName(three.getCountyName()); |
|
|
|
List<AreaCodeDictResultDTO> fourOnceResult = new ArrayList<>(); |
|
|
|
finalFourSort.forEach(four -> { |
|
|
|
if (dto.getCode().equals(four.getParentCode())){ |
|
|
|
fourOnceResult.add(four); |
|
|
|
} |
|
|
|
}); |
|
|
|
dto.setChildren(fourOnceResult); |
|
|
|
threeResult.add(dto); |
|
|
|
}); |
|
|
|
List<AreaCodeDictResultDTO> threeSort = threeResult.stream().sorted(Comparator.comparing(AreaCodeDictResultDTO::getCode)).collect(Collectors.toList()); |
|
|
|
cityDto.setChildren(threeSort); |
|
|
|
cityResult.add(cityDto); |
|
|
|
}); |
|
|
|
groupByProvince.forEach((p,v) -> { |
|
|
|
AreaCodeDictResultDTO dto = new AreaCodeDictResultDTO(); |
|
|
|
dto.setCode(p); |
|
|
|
dto.setName(v.get(NumConstant.ZERO).getProvinceName()); |
|
|
|
List<AreaCodeDictResultDTO> cityOnceResult = new ArrayList<>(); |
|
|
|
cityResult.forEach(c -> { |
|
|
|
if (dto.getCode().equals(c.getParentCode())){ |
|
|
|
cityOnceResult.add(c); |
|
|
|
} |
|
|
|
}); |
|
|
|
List<AreaCodeDictResultDTO> citySort = cityOnceResult.stream().sorted(Comparator.comparing(AreaCodeDictResultDTO::getCode)).collect(Collectors.toList()); |
|
|
|
dto.setChildren(citySort); |
|
|
|
result.add(dto); |
|
|
|
}); |
|
|
|
List<AreaCodeDictResultDTO> collect = result.stream().sorted(Comparator.comparing(AreaCodeDictResultDTO::getCode)).collect(Collectors.toList()); |
|
|
|
return collect; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 根据节点获取组织区域树【遍历】,传参只能是 省,市,区级别 |
|
|
|
* @Param tree |
|
|
|
* @Param rootAreaCode |
|
|
|
* @author zxc |
|
|
|
* @date 2021/1/8 上午10:02 |
|
|
|
*/ |
|
|
|
public List<AreaCodeDictResultDTO> getTreeByRootAreaCode(List<AreaCodeDictResultDTO> tree , String rootAreaCode){ |
|
|
|
List<AreaCodeDictResultDTO> result = new ArrayList<>(); |
|
|
|
AtomicReference<Boolean> flag = new AtomicReference<>(false); |
|
|
|
if (!CollectionUtils.isEmpty(tree)){ |
|
|
|
// 省级获取
|
|
|
|
tree.forEach(t -> { |
|
|
|
if (rootAreaCode.equals(t.getCode())){ |
|
|
|
result.add(t); |
|
|
|
flag.set(true); |
|
|
|
} |
|
|
|
// 市级获取
|
|
|
|
if (!CollectionUtils.isEmpty(t.getChildren()) && flag.get() == false){ |
|
|
|
t.getChildren().forEach(c -> { |
|
|
|
if (rootAreaCode.equals(c.getCode())){ |
|
|
|
result.add(c); |
|
|
|
flag.set(true); |
|
|
|
} |
|
|
|
// 区级获取
|
|
|
|
if (!CollectionUtils.isEmpty(c.getChildren()) && flag.get() == false){ |
|
|
|
c.getChildren().forEach(three -> { |
|
|
|
if (rootAreaCode.equals(three.getCode()) && flag.get() == false){ |
|
|
|
result.add(three); |
|
|
|
flag.set(true); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
} |