package com.zzg.system.service.system.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.zzg.common.core.domain.entity.system.SysUser;
|
import com.zzg.common.utils.ConstantUtils;
|
import com.zzg.common.utils.SecurityUtils;
|
import com.zzg.system.domain.SysCity;
|
import com.zzg.system.mapper.system.SysCityMapper;
|
import com.zzg.system.service.system.ISysCityService;
|
import org.springframework.stereotype.Service;
|
|
import java.util.Arrays;
|
import java.util.Comparator;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
@Service
|
public class SysCityServiceImpl extends ServiceImpl<SysCityMapper, SysCity> implements ISysCityService {
|
|
private static final Integer DEFAULT_LEVEL = 1;
|
|
@Override
|
public List<SysCity> getListTree() {
|
//原数据
|
List<SysCity> sysCityList = this.list(new LambdaQueryWrapper<SysCity>()
|
.orderByAsc(SysCity::getRanking));
|
|
return streamToTree(sysCityList, ConstantUtils.PID);
|
}
|
|
public List<SysCity> streamToTree(List<SysCity> treeList, String pid) {
|
List<SysCity> list = treeList.stream()
|
// 过滤父节点
|
.filter(parent -> parent.getPid().equals(pid))
|
// 把父节点children递归赋值成为子节点
|
.map(child -> {
|
if (!child.getLevel().equals(SysCity.GROUPS_LEVEL)) {
|
child.setChildList(streamToTree(treeList, child.getId()));
|
return child;
|
}
|
child.setChildList(null);
|
return child;
|
}).collect(Collectors.toList());
|
list.sort(Comparator.comparingInt(SysCity::getRanking));
|
return list;
|
}
|
|
@Override
|
public SysCity getUpwardById(String id) {
|
SysCity SysCity = this.getById(id);
|
|
List<SysCity> sysCityList = this.list(new LambdaQueryWrapper<SysCity>());
|
if (SysCity.getLevel().equals(SysCity.TOWN_LEVEL)) {
|
SysCity.setTown(SysCity.getName());
|
} else {
|
childToParent(sysCityList, SysCity);
|
}
|
|
return SysCity;
|
}
|
|
@Override
|
public SysCity getUpDataById(String id, Integer level) {
|
if (level == null) {
|
level = DEFAULT_LEVEL;
|
}
|
|
SysCity SysCity = this.getById(id);
|
List<SysCity> sysCityList = this.list(new LambdaQueryWrapper<>());
|
if (SysCity.getLevel().equals(level)) {
|
return SysCity;
|
} else {
|
return getUpData(SysCity.getPid(), sysCityList, level);
|
}
|
}
|
|
public SysCity getUpData(String pid, List<SysCity> list, Integer level) {
|
SysCity group = list.stream().filter(t -> t.getId().equals(pid)).collect(Collectors.toList()).stream().findFirst().get();
|
if (group.getLevel().equals(level)) {
|
return group;
|
} else {
|
return getUpData(group.getPid(), list, level);
|
}
|
}
|
|
@Override
|
public List<SysCity> getTowns() {
|
LambdaQueryWrapper<SysCity> wrapper = new LambdaQueryWrapper<>();
|
wrapper.eq(SysCity::getLevel, SysCity.TOWN_LEVEL);
|
SysUser user = SecurityUtils.getLoginUser().getUser();
|
if (!user.isAdmin()) {
|
SysCity city = this.getById(user.getCityId());
|
if (!city.getLevel().equals(SysCity.CITY_LEVEL)) {
|
return Arrays.asList(city);
|
}
|
}
|
|
return this.list(wrapper);
|
}
|
|
@Override
|
public List<SysCity> getNextTree(String pid) {
|
List<SysCity> sysCityList = this.list(new LambdaQueryWrapper<>());
|
return streamToTree(sysCityList, pid);
|
}
|
|
@Override
|
public List<SysCity> selectByTownIds(List<String> townIds) {
|
List<SysCity> townList = this.list(new LambdaQueryWrapper<SysCity>()
|
.in(SysCity::getId, townIds));
|
return townList;
|
}
|
|
@Override
|
public SysCity getLinksByGroup(String groupsId) {
|
SysCity tvg = this.getById(groupsId);
|
List<SysCity> sysCityList = this.list(new LambdaQueryWrapper<>());
|
|
SysCity village = sysCityList.stream().filter(e -> e.getId().equals(tvg.getPid())).collect(Collectors.toList()).get(0);
|
village.setChildList(Arrays.asList(tvg));
|
|
SysCity town = sysCityList.stream().filter(e -> e.getId().equals(village.getPid())).collect(Collectors.toList()).get(0);
|
town.setChildList(Arrays.asList(village));
|
return town;
|
}
|
|
|
public SysCity childToParent(List<SysCity> list, SysCity child) {
|
SysCity parent = list.stream().filter(
|
p -> p.getId().equals(child.getPid())).collect(Collectors.toList()).stream().findFirst().get();
|
if (parent.getLevel() == SysCity.TOWN_LEVEL) {
|
child.setTown(parent.getName());
|
child.setVillage(child.getName());
|
} else if (parent.getLevel() == SysCity.VILLAGE_LEVEL) {
|
SysCity top = list.stream().filter(
|
p -> p.getId().equals(parent.getPid())).collect(Collectors.toList()).stream().findFirst().get();
|
child.setTown(top.getName());
|
child.setVillage(parent.getName());
|
child.setGroups(child.getName());
|
}
|
|
return child;
|
}
|
|
@Override
|
public List<SysCity> getByTownName(String name) {
|
List<SysCity> townList = this.list(new LambdaQueryWrapper<SysCity>()
|
.in(SysCity::getName, name));
|
return townList;
|
}
|
|
/**
|
* 如果是admin,则返回空对象(admin不作为系统操作人员)
|
*
|
* @return
|
*/
|
@Override
|
public SysCity getByLoginUser() {
|
SysUser user = SecurityUtils.getLoginUser().getUser();
|
if (!user.isAdmin()) {
|
String cityId = user.getCityId();
|
SysCity city = this.getById(cityId);
|
return city;
|
}
|
return null;
|
}
|
|
}
|