5 天以前 c62fa713ee37db1ff7520d2ca6545f569395bb40
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
package com.ruoyi.device.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.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.device.dto.DeviceAreaTreeDto;
import com.ruoyi.device.mapper.DeviceAreaMapper;
import com.ruoyi.device.mapper.DeviceLedgerMapper;
import com.ruoyi.device.pojo.DeviceArea;
import com.ruoyi.device.pojo.DeviceLedger;
import com.ruoyi.device.service.IDeviceAreaService;
import com.ruoyi.framework.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class DeviceAreaServiceImpl extends ServiceImpl<DeviceAreaMapper, DeviceArea> implements IDeviceAreaService {
 
    @Autowired
    private DeviceAreaMapper deviceAreaMapper;
 
    @Autowired
    private DeviceLedgerMapper deviceLedgerMapper;
 
    @Override
    public IPage<DeviceArea> queryPage(Page page, DeviceArea deviceArea) {
        return deviceAreaMapper.queryPage(page, deviceArea);
    }
 
    @Override
    public List<DeviceAreaTreeDto> listTree() {
        return buildTree(Collections.emptyMap());
    }
 
    @Override
    public List<DeviceAreaTreeDto> listTreeWithDevices() {
        LambdaQueryWrapper<DeviceLedger> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(DeviceLedger::getAreaId);
        queryWrapper.orderByAsc(DeviceLedger::getId);
        List<DeviceLedger> deviceLedgers = deviceLedgerMapper.selectList(queryWrapper);
 
        Map<Long, List<DeviceLedger>> deviceMap = new HashMap<>();
        for (DeviceLedger deviceLedger : deviceLedgers) {
            deviceMap.computeIfAbsent(deviceLedger.getAreaId(), key -> new ArrayList<>()).add(deviceLedger);
        }
        return buildTree(deviceMap);
    }
 
    @Override
    public AjaxResult saveDeviceArea(DeviceArea deviceArea) {
        if (deviceArea == null || !StringUtils.hasText(deviceArea.getAreaName())) {
            return AjaxResult.error("区域名称不能为空");
        }
        normalizeDeviceArea(deviceArea);
        if (existsSameName(deviceArea.getParentId(), deviceArea.getAreaName(), null)) {
            return AjaxResult.error("同级区域名称已存在");
        }
        if (deviceArea.getParentId() != null && deviceAreaMapper.selectById(deviceArea.getParentId()) == null) {
            return AjaxResult.error("父级区域不存在");
        }
        return this.save(deviceArea) ? AjaxResult.success() : AjaxResult.error();
    }
 
    @Override
    public AjaxResult updateDeviceArea(DeviceArea deviceArea) {
        if (deviceArea == null || deviceArea.getId() == null) {
            return AjaxResult.error("区域ID不能为空");
        }
        if (!StringUtils.hasText(deviceArea.getAreaName())) {
            return AjaxResult.error("区域名称不能为空");
        }
        normalizeDeviceArea(deviceArea);
        DeviceArea current = deviceAreaMapper.selectById(deviceArea.getId());
        if (current == null) {
            return AjaxResult.error("区域不存在");
        }
        if (deviceArea.getParentId() != null && deviceArea.getId().equals(deviceArea.getParentId())) {
            return AjaxResult.error("父级区域不能选择自身");
        }
        if (isDescendant(deviceArea.getId(), deviceArea.getParentId())) {
            return AjaxResult.error("父级区域不能选择子节点");
        }
        if (deviceArea.getParentId() != null && deviceAreaMapper.selectById(deviceArea.getParentId()) == null) {
            return AjaxResult.error("父级区域不存在");
        }
        if (existsSameName(deviceArea.getParentId(), deviceArea.getAreaName(), deviceArea.getId())) {
            return AjaxResult.error("同级区域名称已存在");
        }
        return this.updateById(deviceArea) ? AjaxResult.success() : AjaxResult.error();
    }
 
    @Override
    public AjaxResult removeDeviceAreas(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return AjaxResult.error("请选择要删除的区域");
        }
        LambdaQueryWrapper<DeviceArea> childWrapper = new LambdaQueryWrapper<>();
        childWrapper.in(DeviceArea::getParentId, ids);
        if (deviceAreaMapper.selectCount(childWrapper) > 0) {
            return AjaxResult.error("存在子区域,不能直接删除");
        }
        return this.removeBatchByIds(ids) ? AjaxResult.success() : AjaxResult.error();
    }
 
    private List<DeviceAreaTreeDto> buildTree(Map<Long, List<DeviceLedger>> deviceMap) {
        LambdaQueryWrapper<DeviceArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.isNull(DeviceArea::getParentId).or().eq(DeviceArea::getParentId, 0L));
        queryWrapper.orderByAsc(DeviceArea::getSort).orderByAsc(DeviceArea::getId);
        List<DeviceArea> rootList = deviceAreaMapper.selectList(queryWrapper);
 
        List<DeviceAreaTreeDto> tree = new ArrayList<>();
        for (DeviceArea deviceArea : rootList) {
            DeviceAreaTreeDto node = toTreeDto(deviceArea, deviceMap);
            node.setChildren(buildChildren(deviceArea.getId(), deviceMap));
            tree.add(node);
        }
        return tree;
    }
 
    private void normalizeDeviceArea(DeviceArea deviceArea) {
        if (deviceArea.getParentId() != null && deviceArea.getParentId() == 0L) {
            deviceArea.setParentId(null);
        }
        if (deviceArea.getSort() == null) {
            deviceArea.setSort(0L);
        }
    }
 
    private boolean existsSameName(Long parentId, String areaName, Long excludeId) {
        LambdaQueryWrapper<DeviceArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceArea::getAreaName, areaName);
        if (parentId == null) {
            queryWrapper.and(wrapper -> wrapper.isNull(DeviceArea::getParentId).or().eq(DeviceArea::getParentId, 0L));
        } else {
            queryWrapper.eq(DeviceArea::getParentId, parentId);
        }
        if (excludeId != null) {
            queryWrapper.ne(DeviceArea::getId, excludeId);
        }
        return deviceAreaMapper.selectCount(queryWrapper) > 0;
    }
 
    private boolean isDescendant(Long currentId, Long parentId) {
        if (currentId == null || parentId == null) {
            return false;
        }
        Long cursor = parentId;
        while (cursor != null && cursor != 0L) {
            if (currentId.equals(cursor)) {
                return true;
            }
            DeviceArea parent = deviceAreaMapper.selectById(cursor);
            if (parent == null) {
                return false;
            }
            cursor = parent.getParentId();
        }
        return false;
    }
 
    private List<DeviceAreaTreeDto> buildChildren(Long parentId, Map<Long, List<DeviceLedger>> deviceMap) {
        LambdaQueryWrapper<DeviceArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceArea::getParentId, parentId);
        queryWrapper.orderByAsc(DeviceArea::getSort).orderByAsc(DeviceArea::getId);
        List<DeviceArea> children = deviceAreaMapper.selectList(queryWrapper);
 
        List<DeviceAreaTreeDto> result = new ArrayList<>();
        for (DeviceArea child : children) {
            DeviceAreaTreeDto node = toTreeDto(child, deviceMap);
            node.setChildren(buildChildren(child.getId(), deviceMap));
            result.add(node);
        }
        return result;
    }
 
    private DeviceAreaTreeDto toTreeDto(DeviceArea deviceArea, Map<Long, List<DeviceLedger>> deviceMap) {
        DeviceAreaTreeDto dto = new DeviceAreaTreeDto();
        BeanUtils.copyProperties(deviceArea, dto);
        dto.setLabel(deviceArea.getAreaName());
        dto.setDeviceList(new ArrayList<>(deviceMap.getOrDefault(deviceArea.getId(), Collections.emptyList())));
        dto.setChildren(new ArrayList<>());
        return dto;
    }
}