吉安感知网项目-后端
rain
2026-02-09 66bfbdb9251c22ffa2b5f05595a76a3547a2f5eb
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.sxkj.fw.area.service.impl;
 
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.sxkj.common.utils.HeaderUtils;
import org.sxkj.fw.area.entity.FwAreaDivideEntity;
import org.sxkj.fw.area.entity.FwDefenseSceneEntity;
import org.sxkj.fw.area.vo.FwDefenseSceneVO;
import org.sxkj.fw.area.excel.FwDefenseSceneExcel;
import org.sxkj.fw.area.mapper.FwDefenseSceneMapper;
import org.sxkj.fw.area.service.IFwAreaDivideService;
import org.sxkj.fw.area.service.IFwDefenseSceneService;
import org.sxkj.fw.device.entity.FwDeviceEntity;
import org.sxkj.fw.device.service.IFwDeviceService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.sxkj.system.cache.SysCache;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Date;
import java.util.stream.Collectors;
 
/**
 * 防控场景表 服务实现类
 *
 * @author lw
 * @since 2026-01-08
 */
@Service
public class FwDefenseSceneServiceImpl extends BaseServiceImpl<FwDefenseSceneMapper, FwDefenseSceneEntity> implements IFwDefenseSceneService {
 
    private static final BigDecimal AREA_SIZE_FACTOR = new BigDecimal("1000000");
 
    @Autowired
    private IFwAreaDivideService fwAreaDivideService;
 
    @Autowired
    private IFwDeviceService fwDeviceService;
 
    @Override
    public IPage<FwDefenseSceneVO> selectFwDefenseScenePage(IPage<FwDefenseSceneVO> page, FwDefenseSceneVO fwDefenseScene) {
        if (!AuthUtil.isAdministrator()) {
            List<Long> deptList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
            fwDefenseScene.setDeptList(deptList);
        }
        IPage<FwDefenseSceneVO> result = page.setRecords(baseMapper.selectFwDefenseScenePage(page, fwDefenseScene));
        List<FwDefenseSceneVO> records = result.getRecords();
        if (records == null || records.isEmpty()) {
            return result;
        }
 
        Set<Long> areaIdSet = new HashSet<>();
        for (FwDefenseSceneVO record : records) {
            areaIdSet.addAll(parseIdList(record.getAreaDivideIds()));
        }
 
        Map<Long, BigDecimal> areaSizeMap = new HashMap<>();
        Map<Long, List<Long>> areaDeviceMap = new HashMap<>();
        Set<Long> deviceIdSet = new HashSet<>();
        if (!areaIdSet.isEmpty()) {
            List<FwAreaDivideEntity> areaList = fwAreaDivideService.listByIds(areaIdSet);
            for (FwAreaDivideEntity area : areaList) {
                if (area == null || area.getId() == null) {
                    continue;
                }
//                areaSizeMap.put(area.getId(), area.getAreaSize());
                List<Long> areaDeviceIds = parseIdList(area.getDeviceIds());
                areaDeviceMap.put(area.getId(), areaDeviceIds);
                deviceIdSet.addAll(areaDeviceIds);
            }
        }
 
        Map<Long, String> deviceTypeMap = new HashMap<>();
        if (!deviceIdSet.isEmpty()) {
            List<FwDeviceEntity> deviceList = fwDeviceService.listByIds(deviceIdSet);
            for (FwDeviceEntity device : deviceList) {
                if (device == null || device.getId() == null) {
                    continue;
                }
                deviceTypeMap.put(device.getId(), device.getDeviceType());
            }
        }
 
        for (FwDefenseSceneVO record : records) {
            List<Long> areaIds = parseIdList(record.getAreaDivideIds());
            record.setAreaCount(areaIds.size());
 
            Set<Long> deviceIds = new HashSet<>();
            for (Long areaId : areaIds) {
                List<Long> areaDeviceIds = areaDeviceMap.get(areaId);
                if (areaDeviceIds != null) {
                    deviceIds.addAll(areaDeviceIds);
                }
            }
            int detectCount = 0;
            int counterCount = 0;
            for (Long deviceId : deviceIds) {
                String deviceType = deviceTypeMap.get(deviceId);
                if ("1".equals(deviceType)) {
                    detectCount++;
                    counterCount++;
                } else if ("2".equals(deviceType)) {
                    detectCount++;
                } else if ("3".equals(deviceType)) {
                    counterCount++;
                }
            }
            record.setDetectDeviceCount(detectCount);
            record.setCounterDeviceCount(counterCount);
        }
 
        return result;
    }
 
    @Override
    public List<FwDefenseSceneVO> selectFwDefenseSceneList(Integer filterSelected, Long zoneId, Long areaId, Integer unbound, Long sceneManageId) {
        List<Long> deptList = new ArrayList<>();
        if (!AuthUtil.isAdministrator()) {
            deptList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        }
        return baseMapper.selectFwDefenseSceneList(filterSelected, zoneId, areaId, unbound, sceneManageId, deptList);
    }
 
    private List<Long> parseIdList(String ids) {
        if (StringUtil.isBlank(ids)) {
            return Collections.emptyList();
        }
        String[] idArray = ids.split(",");
        List<Long> idList = new ArrayList<>(idArray.length);
        for (String item : idArray) {
            if (item == null) {
                continue;
            }
            String value = item.trim();
            if (StringUtil.isBlank(value)) {
                continue;
            }
            try {
                idList.add(Long.valueOf(value));
            } catch (NumberFormatException ignored) {
            }
        }
        return idList;
    }
 
    @Override
    public List<FwDefenseSceneExcel> exportFwDefenseScene(Wrapper<FwDefenseSceneEntity> queryWrapper) {
        List<FwDefenseSceneExcel> fwDefenseSceneList = baseMapper.exportFwDefenseScene(queryWrapper);
        return fwDefenseSceneList;
    }
 
    @Override
    public boolean save(FwDefenseSceneEntity fwDefenseScene) {
        fillCreateFields(fwDefenseScene);
        return super.save(fwDefenseScene);
    }
 
    @Override
    public boolean updateById(FwDefenseSceneEntity fwDefenseScene) {
        fillUpdateFields(fwDefenseScene);
        return super.updateById(fwDefenseScene);
    }
 
    @Override
    public boolean saveOrUpdate(FwDefenseSceneEntity fwDefenseScene) {
        if (fwDefenseScene.getId() == null) {
            fillCreateFields(fwDefenseScene);
        } else {
            fillUpdateFields(fwDefenseScene);
        }
        String areaDivideIds = fwDefenseScene.getAreaDivideIds();
 
        // 处理区域划分的新增和删除
        if (fwDefenseScene.getId() != null) {
            // 更新操作,需要比较旧的areaDivideIds
            FwDefenseSceneEntity oldEntity = baseMapper.selectById(fwDefenseScene.getId());
            if (oldEntity != null) {
                String oldAreaDivideIds = oldEntity.getAreaDivideIds();
 
                // 解析新旧区域划分ID列表
                Set<Long> oldAreaIds = parseAreaDivideIds(oldAreaDivideIds);
                Set<Long> newAreaIds = parseAreaDivideIds(areaDivideIds);
 
                // 找出新增的区域划分ID
                Set<Long> addedAreaIds = new HashSet<>();
                for (Long id : newAreaIds) {
                    if (!oldAreaIds.contains(id)) {
                        addedAreaIds.add(id);
                    }
                }
 
                // 找出删除的区域划分ID
                Set<Long> removedAreaIds = new HashSet<>();
                for (Long id : oldAreaIds) {
                    if (!newAreaIds.contains(id)) {
                        removedAreaIds.add(id);
                    }
                }
 
                // 处理删除的区域划分对应的设备(禁用)
                if (!removedAreaIds.isEmpty()) {
                    List<FwAreaDivideEntity> removedAreas = fwAreaDivideService.listByIds(new ArrayList<>(removedAreaIds));
                    List<Long> removedDeviceIds = removedAreas.stream()
                        .map(item -> Func.toLongList(item.getDeviceIds()))
                        .flatMap(List::stream)
                        .collect(Collectors.toList());
                    if (!removedDeviceIds.isEmpty()) {
                        // 禁用设备
                        disableDevices(removedDeviceIds);
                    }
                }
 
                // 处理新增的区域划分对应的设备(启用)
                if (!addedAreaIds.isEmpty()) {
                    List<FwAreaDivideEntity> addedAreas = fwAreaDivideService.listByIds(new ArrayList<>(addedAreaIds));
                    List<Long> addedDeviceIds = addedAreas.stream()
                        .map(item -> Func.toLongList(item.getDeviceIds()))
                        .flatMap(List::stream)
                        .collect(Collectors.toList());
                    if (!addedDeviceIds.isEmpty()) {
                        // 启用设备
                        enableDevices(addedDeviceIds);
                    }
                }
            }
        } else {
            // 新增操作,所有区域划分都是新增的,启用对应设备
            List<FwAreaDivideEntity> fwAreaDivideEntities = fwAreaDivideService.listByIds(Func.toLongList(areaDivideIds));
            List<Long> addedDeviceIds = fwAreaDivideEntities.stream()
                .map(item -> Func.toLongList(item.getDeviceIds()))
                .flatMap(List::stream)
                .collect(Collectors.toList());
            if (!addedDeviceIds.isEmpty()) {
                // 启用设备
                enableDevices(addedDeviceIds);
            }
        }
 
        return super.saveOrUpdate(fwDefenseScene);
    }
 
    /**
     * 解析区域划分ID字符串为集合
     * @param areaDivideIds 区域划分ID字符串,逗号分隔
     * @return 区域划分ID集合
     */
    private Set<Long> parseAreaDivideIds(String areaDivideIds) {
        Set<Long> areaIdSet = new HashSet<>();
        if (StringUtil.isNotBlank(areaDivideIds)) {
            List<Long> ids = Func.toLongList(areaDivideIds);
            areaIdSet.addAll(ids);
        }
        return areaIdSet;
    }
 
    /**
     * 启用设备
     * @param deviceIds 设备ID列表
     */
    private void enableDevices(List<Long> deviceIds) {
        if (deviceIds != null && !deviceIds.isEmpty()) {
            // 实现设备启用逻辑
            // 这里假设FwDeviceEntity有enabled字段,1表示启用,0表示禁用
            for (Long deviceId : deviceIds) {
                FwDeviceEntity device = new FwDeviceEntity();
                device.setId(deviceId);
                device.setIsEnabled(true);
                fwDeviceService.updateById(device);
            }
        }
    }
 
    /**
     * 禁用设备
     * @param deviceIds 设备ID列表
     */
    private void disableDevices(List<Long> deviceIds) {
        if (deviceIds != null && !deviceIds.isEmpty()) {
            // 实现设备禁用逻辑
            // 这里假设FwDeviceEntity有enabled字段,1表示启用,0表示禁用
            for (Long deviceId : deviceIds) {
                FwDeviceEntity device = new FwDeviceEntity();
                device.setId(deviceId);
                device.setIsEnabled(false);
                fwDeviceService.updateById(device);
            }
        }
    }
 
    /**
     * 逻辑删除防控场景,并处理关联设备的禁用
     * @param ids 防控场景ID列表
     * @return 是否删除成功
     */
    @Override
    public boolean deleteLogic(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
 
        // 先获取要删除的防控场景
        List<FwDefenseSceneEntity> scenes = listByIds(ids);
        if (scenes == null || scenes.isEmpty()) {
            return false;
        }
 
        // 收集所有相关的区域划分ID
        Set<Long> areaDivideIds = new HashSet<>();
        for (FwDefenseSceneEntity scene : scenes) {
            if (scene != null && StringUtil.isNotBlank(scene.getAreaDivideIds())) {
                areaDivideIds.addAll(Func.toLongList(scene.getAreaDivideIds()));
            }
        }
 
        // 收集所有相关的设备ID
        List<Long> deviceIds = new ArrayList<>();
        if (!areaDivideIds.isEmpty()) {
            List<FwAreaDivideEntity> areas = fwAreaDivideService.listByIds(new ArrayList<>(areaDivideIds));
            deviceIds = areas.stream()
                .map(item -> Func.toLongList(item.getDeviceIds()))
                .flatMap(List::stream)
                .collect(Collectors.toList());
        }
 
        // 禁用相关设备
        if (!deviceIds.isEmpty()) {
            disableDevices(deviceIds);
        }
 
        // 执行逻辑删除
        return super.deleteLogic(ids);
    }
 
    private void fillCreateFields(FwDefenseSceneEntity fwDefenseScene) {
        Long userId = AuthUtil.getUserId();
        String deptIdStr = AuthUtil.getDeptId();
        Long deptId = null;
        if (StringUtil.isNotBlank(deptIdStr)) {
            try {
                deptId = Long.valueOf(deptIdStr);
            } catch (NumberFormatException ignored) {
                deptId = null;
            }
        }
//        String areaCode = HeaderUtils.getAreaCode();
//        if (StringUtil.isBlank(areaCode)) {
//            areaCode = fwDefenseScene.getAreaCode();
//        }
        Date now = new Date();
        fwDefenseScene.setCreateUser(userId);
        fwDefenseScene.setUpdateUser(userId);
        if (deptId != null) {
            fwDefenseScene.setCreateDept(deptId);
        }
//        if (StringUtil.isNotBlank(areaCode)) {
//            fwDefenseScene.setAreaCode(areaCode);
//        }
        fwDefenseScene.setCreateTime(now);
        fwDefenseScene.setUpdateTime(now);
    }
 
    private void fillUpdateFields(FwDefenseSceneEntity fwDefenseScene) {
        Long userId = AuthUtil.getUserId();
        if (userId != null) {
            fwDefenseScene.setUpdateUser(userId);
        }
        fwDefenseScene.setUpdateTime(new Date());
    }
 
}