yanghb
2024-12-17 1287337fd0b0c156ec79712f9a600ebeffefe3a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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;
    }
 
}