147 lines
4.2 KiB
Java
147 lines
4.2 KiB
Java
package com.whdc.service.impl;
|
||
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||
import com.whdc.mapper.AdinfoMapper;
|
||
import com.whdc.model.entity.Adinfo;
|
||
import com.whdc.model.vo.AdcdTree;
|
||
import com.whdc.service.IAdinfoService;
|
||
import org.apache.commons.collections4.CollectionUtils;
|
||
import org.apache.commons.lang3.StringUtils;
|
||
import org.springframework.stereotype.Service;
|
||
|
||
import java.util.*;
|
||
import java.util.stream.Collectors;
|
||
|
||
/**
|
||
* <p>
|
||
* 服务实现类
|
||
* </p>
|
||
* @author xusan
|
||
* @date 2024-05-11
|
||
*/
|
||
@Service
|
||
public class AdinfoServiceImpl extends ServiceImpl<AdinfoMapper, Adinfo> implements IAdinfoService {
|
||
|
||
|
||
@Override
|
||
public IPage<Adinfo> page(Adinfo dto) {
|
||
return baseMapper.page(new Page<>(), dto);
|
||
}
|
||
|
||
|
||
@Override
|
||
public List<Adinfo> find(Adinfo dto) {
|
||
return baseMapper.find(dto);
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 行政区划树型查询
|
||
* 420116,001,002,000
|
||
* 6位区县,9位街道,12位组
|
||
* 省 市 县
|
||
* 42 28 22 100 011 100
|
||
*
|
||
* @param adcd
|
||
* @return
|
||
*/
|
||
// @Cacheable(cacheNames = {REDIS_KEY}, key = "getMethodName()", condition = "#adcd", unless = "false", cacheResolver = "redisExpireCacheResolver")
|
||
public List<AdcdTree> tree(String adcd, String adnm) {
|
||
|
||
List<Adinfo> list = treeList(adcd, adnm);
|
||
|
||
if (CollectionUtils.isNotEmpty(list)){
|
||
list = list.stream().sorted(Comparator.comparing(Adinfo::getAdcd)
|
||
.reversed()
|
||
).collect(Collectors.toList());
|
||
}
|
||
|
||
// 市 4205 00000000000
|
||
Map<String, List<AdcdTree>> city = new HashMap<>();
|
||
|
||
// 县 421122 000000000
|
||
Map<String, List<AdcdTree>> county = new HashMap<>();
|
||
|
||
List<AdcdTree> tree = new ArrayList<>();
|
||
|
||
for (Adinfo adinfo : list) {
|
||
String ad = adinfo.getAdcd();
|
||
String nm = adinfo.getAdnm();
|
||
AdcdTree adcdTree = new AdcdTree().setAdcd(ad).setAdnm(nm).setAdcdChildren(new ArrayList<>());
|
||
|
||
// 判断行政区划编码
|
||
String provinceStr = ad.substring(0, 2);
|
||
String cityStr = ad.substring(0, 4);
|
||
|
||
if ("00000000000".equals(ad.substring(4))) { // 市
|
||
adcdTree.setAdlevel(1);
|
||
adcdTree.setAdcdChildren(county.get(cityStr));
|
||
addTree(city, provinceStr, adcdTree);
|
||
|
||
} else if ("000000000".equals(ad.substring(6))) { // 县
|
||
adcdTree.setAdlevel(2);
|
||
addTree(county, cityStr, adcdTree);
|
||
}
|
||
|
||
}
|
||
|
||
// 排序
|
||
|
||
return sorted(tree);
|
||
}
|
||
|
||
|
||
public List<Adinfo> treeList(String adcd, String adnm) {
|
||
|
||
if(StringUtils.isNotBlank(adcd) && adcd.endsWith("0000000000000")){
|
||
adcd = adcd.substring(0,2);
|
||
}
|
||
if (StringUtils.isNotBlank(adcd) && StringUtils.isNotBlank(adnm)) {
|
||
|
||
// 需要查出层级数据
|
||
return baseMapper.selectByCdOrNm(adcd, adnm);
|
||
|
||
} else {
|
||
LambdaQueryWrapper<Adinfo> queryWrapper = new LambdaQueryWrapper<>();
|
||
if (StringUtils.isNotBlank(adcd)) {
|
||
queryWrapper.like(Adinfo::getAdcd, adcd);
|
||
}
|
||
List<Adinfo> adinfos = baseMapper.selectList(queryWrapper);
|
||
|
||
return adinfos; // 查所有
|
||
}
|
||
|
||
}
|
||
|
||
private List<AdcdTree> sorted( List<AdcdTree> tree) {
|
||
|
||
List<AdcdTree> sorteds = null;
|
||
|
||
if (CollectionUtils.isNotEmpty(tree)){
|
||
sorteds = tree.stream().sorted(Comparator.comparing(AdcdTree::getAdcd)
|
||
).collect(Collectors.toList());
|
||
sorteds.forEach(o->{
|
||
o.setAdcdChildren(sorted(o.getAdcdChildren()));
|
||
});
|
||
}
|
||
|
||
return sorteds;
|
||
}
|
||
|
||
private void addTree(Map<String, List<AdcdTree>> data, String key, AdcdTree tree) {
|
||
List<AdcdTree> trees = data.get(key);
|
||
if (CollectionUtils.isEmpty(trees)) {
|
||
trees = new ArrayList<>();
|
||
}
|
||
trees.add(tree);
|
||
|
||
data.put(key, trees);
|
||
}
|
||
|
||
}
|