kaiyu
2021-12-30 bd9a7f2d97af968d43ccd5516644f6933b42f5b2
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
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.Menu;
import com.moral.api.entity.MonitorPoint;
import com.moral.api.entity.Organization;
import com.moral.api.entity.SysArea;
import com.moral.api.mapper.MonitorPointMapper;
import com.moral.api.mapper.SysAreaMapper;
import com.moral.api.service.OrganizationService;
import com.moral.api.service.SysAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 系统区域表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-04-07
 */
@Service
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea> implements SysAreaService {
 
    @Autowired
    private SysAreaMapper sysAreaMapper;
 
    @Autowired
    private MonitorPointMapper monitorPointMapper;
 
    @Autowired
    private OrganizationService organizationService;
 
    @Override
    public List<Map<String, Object>> getMapPath(Integer orgId) {
        //获取组织下所有子组织
        List<Organization> organizations = organizationService.getChildrenOrganizationsById(orgId);
        List<Integer> orgIds = organizations.stream().map(Organization::getId).collect(Collectors.toList());
        orgIds.add(orgId);
        QueryWrapper<MonitorPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("province_code", "city_code", "area_code")
                .eq("is_delete", Constants.NOT_DELETE)
                .in("organization_id", orgIds);
 
        //根据组织id查询所有站点
        List<MonitorPoint> monitorPoints = monitorPointMapper.selectList(queryWrapper);
        Set<Integer> cityCodes = new HashSet<>();
        for (MonitorPoint monitorPoint : monitorPoints) {
            Integer provinceCode = monitorPoint.getProvinceCode();
            Integer cityCode = monitorPoint.getCityCode();
            Integer areaCode = monitorPoint.getAreaCode();
            if (provinceCode != null) {
                cityCodes.add(provinceCode);
            }
            if (cityCode != null) {
                cityCodes.add(cityCode);
            }
            if (areaCode != null) {
                cityCodes.add(areaCode);
            }
        }
 
        QueryWrapper<SysArea> sysAreaQueryWrapper = new QueryWrapper<>();
        sysAreaQueryWrapper.select("area_code", "area_name", "parent_code").in("area_code", cityCodes);
        //获取当前用户拥有权限的地图列表
        List<SysArea> allAreas = sysAreaMapper.selectList(sysAreaQueryWrapper);
 
        //第一级城市
        List<SysArea> oneSysArea = allAreas.stream()
                .filter(o -> o.getParentCode().equals(0))
                .sorted(Comparator.comparing(SysArea::getAreaCode))
                .collect(Collectors.toList());
 
        List<Map<String, Object>> newList = new ArrayList<>();
        //遍历一级菜单
        oneSysArea.forEach(o -> {
            Map<String, Object> sysAreaMap = new LinkedHashMap<>();
            sysAreaMap.put("provinceCode", o.getAreaCode());
            sysAreaMap.put("provinceName", o.getAreaName());
            sysAreaMap.put("cities", getAreasByRecursion(o, allAreas));
            newList.add(sysAreaMap);
        });
        return newList;
    }
 
    @Override
    public List<SysArea> getChildren(Integer regionCode) {
        QueryWrapper<SysArea> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code",regionCode);
        return sysAreaMapper.selectList(wrapper);
    }
 
    //获取层级城市列表
    private List<Map<String, Object>> getAreasByRecursion(SysArea sysArea, List<SysArea> sysAreas) {
        SysArea newSysArea = new SysArea();
        newSysArea.setParentCode(sysArea.getAreaCode());
        //筛选出下一级城市信息
        List<SysArea> nextLevel = sysAreas.stream()
                .filter(o -> o.getParentCode().equals(sysArea.getAreaCode()))
                .collect(Collectors.toList());
        List<Map<String, Object>> list = new ArrayList<>();
 
        if (nextLevel.size() > 0) {
            nextLevel.forEach(o -> {
                Map<String, Object> sysMap = new LinkedHashMap<>();
                if (o.getAreaCode().toString().endsWith("00")) {
                    sysMap.put("cityCode", o.getAreaCode());
                    sysMap.put("cityName", o.getAreaName());
                    sysMap.put("provinceCode", o.getParentCode());
                    sysMap.put("areas", getAreasByRecursion(o, sysAreas));
                } else {
                    sysMap.put("areaCode", o.getAreaCode());
                    sysMap.put("areaName", o.getAreaName());
                    sysMap.put("cityCode", o.getParentCode());
                }
                list.add(sysMap);
            });
        }
        return list;
    }
}