wenminjie
2026-07-24 1679e681200f426ffa10309652c0e8dc988f1ee2
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.ruoyi.system.service.impl;
 
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysVisitArea;
import com.ruoyi.system.dto.SysVisitAreaAddRequest;
import com.ruoyi.system.dto.SysVisitAreaDeleteRequest;
import com.ruoyi.system.dto.SysVisitAreaDetailRequest;
import com.ruoyi.system.dto.SysVisitAreaEditRequest;
import com.ruoyi.system.dto.SysVisitAreaTreeRequest;
import com.ruoyi.system.mapper.SysVisitAreaMapper;
import com.ruoyi.system.service.ISysVisitAreaService;
import com.ruoyi.system.vo.SysVisitAreaVO;
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.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 访问区域服务实现
 *
 * @author ruoyi
 */
@Service
public class SysVisitAreaServiceImpl implements ISysVisitAreaService
{
    @Autowired
    private SysVisitAreaMapper visitAreaMapper;
 
    @Override
    public List<SysVisitAreaVO> tree(SysVisitAreaTreeRequest request)
    {
        SysVisitAreaTreeRequest query = request == null ? new SysVisitAreaTreeRequest() : request;
        query.setAreaName(StringUtils.trim(query.getAreaName()));
        return buildTree(visitAreaMapper.selectAreaTreeList(query));
    }
 
    @Override
    public SysVisitAreaVO detail(SysVisitAreaDetailRequest request)
    {
        Long areaId = getRequiredAreaId(request == null ? null : request.getAreaId());
        SysVisitAreaVO detail = visitAreaMapper.selectVoById(areaId);
        if (detail == null)
        {
            throw new ServiceException("访问区域不存在");
        }
        return detail;
    }
 
    @Override
    public int add(SysVisitAreaAddRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("访问区域内容不能为空");
        }
        Long parentId = getParentId(request.getParentId());
        SysVisitArea parent = getParentArea(parentId);
        String areaName = getRequiredAreaName(request.getAreaName());
        String areaCode = trimToNull(request.getAreaCode());
        checkUnique(areaName, areaCode, parentId, null);
 
        SysVisitArea area = new SysVisitArea();
        area.setParentId(parentId);
        area.setAncestors(getAncestors(parent));
        area.setAreaCode(areaCode);
        area.setAreaName(areaName);
        area.setAreaDesc(StringUtils.trim(request.getAreaDesc()));
        area.setOrderNum(request.getOrderNum() == null ? 0 : request.getOrderNum());
        area.setCreateBy(username);
        return visitAreaMapper.insertArea(area);
    }
 
    @Override
    public int edit(SysVisitAreaEditRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("访问区域内容不能为空");
        }
        Long areaId = getRequiredAreaId(request.getAreaId());
        SysVisitArea oldArea = visitAreaMapper.selectById(areaId);
        if (oldArea == null)
        {
            throw new ServiceException("访问区域不存在");
        }
 
        Long parentId = getParentId(request.getParentId());
        if (Objects.equals(areaId, parentId))
        {
            throw new ServiceException("上级区域不能选择自身");
        }
        SysVisitArea parent = getParentArea(parentId);
        if (parent != null && containsAncestor(parent.getAncestors(), areaId))
        {
            throw new ServiceException("上级区域不能选择当前区域的下级");
        }
 
        String areaName = getRequiredAreaName(request.getAreaName());
        String areaCode = trimToNull(request.getAreaCode());
        checkUnique(areaName, areaCode, parentId, areaId);
 
        String newAncestors = getAncestors(parent);
        SysVisitArea area = new SysVisitArea();
        area.setAreaId(areaId);
        area.setParentId(parentId);
        area.setAncestors(newAncestors);
        area.setAreaCode(areaCode);
        area.setAreaName(areaName);
        area.setAreaDesc(StringUtils.trim(request.getAreaDesc()));
        area.setOrderNum(request.getOrderNum() == null ? 0 : request.getOrderNum());
        area.setUpdateBy(username);
        int rows = visitAreaMapper.updateArea(area);
 
        String oldChildAncestors = appendAncestor(oldArea.getAncestors(), areaId);
        String newChildAncestors = appendAncestor(newAncestors, areaId);
        if (!Objects.equals(oldChildAncestors, newChildAncestors))
        {
            visitAreaMapper.updateChildrenAncestors(areaId, oldChildAncestors, newChildAncestors);
        }
        return rows;
    }
 
    @Override
    public int delete(SysVisitAreaDeleteRequest request, String username)
    {
        if (request == null || CollectionUtils.isEmpty(request.getAreaIds()))
        {
            throw new ServiceException("区域主键集合不能为空");
        }
        for (Long areaId : request.getAreaIds())
        {
            getRequiredAreaId(areaId);
        }
        if (visitAreaMapper.countChildrenByParentIds(request.getAreaIds()) > 0)
        {
            throw new ServiceException("存在下级区域");
        }
        return visitAreaMapper.deleteByIds(request.getAreaIds(), username);
    }
 
    /**
     * 构建前端所需区域树。
     */
    private List<SysVisitAreaVO> buildTree(List<SysVisitAreaVO> areas)
    {
        List<SysVisitAreaVO> returnList = new ArrayList<SysVisitAreaVO>();
        List<Long> tempList = areas.stream().map(SysVisitAreaVO::getAreaId).collect(Collectors.toList());
        for (SysVisitAreaVO area : areas)
        {
            if (area.getParentId() == null || area.getParentId() == 0 || !tempList.contains(area.getParentId()))
            {
                recursionFn(areas, area);
                returnList.add(area);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = areas;
        }
        return returnList;
    }
 
    private void recursionFn(List<SysVisitAreaVO> list, SysVisitAreaVO area)
    {
        List<SysVisitAreaVO> childList = getChildList(list, area);
        area.setChildren(childList);
        for (SysVisitAreaVO child : childList)
        {
            if (!getChildList(list, child).isEmpty())
            {
                recursionFn(list, child);
            }
        }
    }
 
    private List<SysVisitAreaVO> getChildList(List<SysVisitAreaVO> list, SysVisitAreaVO area)
    {
        List<SysVisitAreaVO> result = new ArrayList<SysVisitAreaVO>();
        Iterator<SysVisitAreaVO> it = list.iterator();
        while (it.hasNext())
        {
            SysVisitAreaVO current = it.next();
            if (current.getParentId() != null && current.getParentId().longValue() == area.getAreaId().longValue())
            {
                result.add(current);
            }
        }
        return result;
    }
 
    private Long getRequiredAreaId(Long areaId)
    {
        if (areaId == null || areaId <= 0)
        {
            throw new ServiceException("区域主键不能为空");
        }
        return areaId;
    }
 
    private Long getParentId(Long parentId)
    {
        if (parentId == null)
        {
            return 0L;
        }
        if (parentId < 0)
        {
            throw new ServiceException("上级区域不正确");
        }
        return parentId;
    }
 
    private SysVisitArea getParentArea(Long parentId)
    {
        if (parentId == 0)
        {
            return null;
        }
        SysVisitArea parent = visitAreaMapper.selectById(parentId);
        if (parent == null)
        {
            throw new ServiceException("上级区域不存在");
        }
        return parent;
    }
 
    private String getRequiredAreaName(String areaName)
    {
        String value = StringUtils.trim(areaName);
        if (StringUtils.isEmpty(value))
        {
            throw new ServiceException("区域名称不能为空");
        }
        return value;
    }
 
    private String trimToNull(String value)
    {
        String result = StringUtils.trim(value);
        return StringUtils.isEmpty(result) ? null : result;
    }
 
    private void checkUnique(String areaName, String areaCode, Long parentId, Long excludeAreaId)
    {
        if (visitAreaMapper.checkAreaNameUnique(areaName, parentId, excludeAreaId) != null)
        {
            throw new ServiceException("同级区域名称已存在");
        }
        if (StringUtils.isNotEmpty(areaCode) && visitAreaMapper.checkAreaCodeUnique(areaCode, excludeAreaId) != null)
        {
            throw new ServiceException("区域编码已存在");
        }
    }
 
    private String getAncestors(SysVisitArea parent)
    {
        if (parent == null)
        {
            return "0";
        }
        return appendAncestor(parent.getAncestors(), parent.getAreaId());
    }
 
    private String appendAncestor(String ancestors, Long areaId)
    {
        String prefix = StringUtils.isEmpty(ancestors) ? "0" : ancestors;
        return prefix + "," + areaId;
    }
 
    private boolean containsAncestor(String ancestors, Long areaId)
    {
        if (StringUtils.isEmpty(ancestors) || areaId == null)
        {
            return false;
        }
        String target = String.valueOf(areaId);
        for (String ancestor : ancestors.split(","))
        {
            if (target.equals(ancestor))
            {
                return true;
            }
        }
        return false;
    }
}